社区
C语言
帖子详情
怎样在unix下用c编写串口通信?
ricsson
2003-02-25 09:13:43
谁有这方面的资料??
...全文
39
2
打赏
收藏
怎样在unix下用c编写串口通信?
谁有这方面的资料??
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
2 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
z_sky
2003-02-25
打赏
举报
回复
网上可以找到很多,这是我以前程序的部分代码,仅供参考。
static int fdmodem=-1;
void dummyalrm(int dummy)
{
dummy = 0;
longjmp(jmpbuf, 1);
}
int com_open(int n)
{
char s[255];
int modem;
struct stat status;
/* Create the full name of the device */
sprintf(s, "/dev/ttyS%d", n);
/* Opening the device */
if (setjmp(jmpbuf) == 0)
{
modem = -1;
signal(SIGALRM, dummyalrm);
alarm(2);
if ((modem = open(s, O_RDWR|O_NOCTTY|O_NONBLOCK)) == -1) return EOPEN;
}
alarm(0);
signal(SIGALRM, SIG_IGN);
/*Get device's status */
if (fstat(modem, &status) == -1)
{
close(modem);
return ESTAT;
}
/* Checking if device is charachter device */
if (!S_ISCHR(status.st_mode))
{
close(modem);
return ECHAR;
}
/* Get device's flags using termios */
if (tcgetattr(modem, &term) == -1)
{
close(modem);
return EGETA;
}
/* Save the flags */
memcpy(&saveterm, &term, sizeof(struct termios));
/* Enter raw mode */
term.c_iflag &= ~(IGNBRK | IGNCR | INLCR | ICRNL | IUCLC | IXANY |
IXON | IXOFF | INPCK | ISTRIP);
term.c_iflag |= BRKINT | IGNPAR;
term.c_oflag &= ~OPOST;
term.c_lflag = ~(ICANON | ISIG | ECHO | ECHONL | ECHOE | ECHOK);
term.c_cflag |= CS8 | CREAD | HUPCL | CRTSCTS;
/* Set the baud */
cfsetospeed(&term, B9600);
cfsetispeed(&term, B9600);
/* Set the attributes */
tcsetattr(modem, TCSANOW, &term);
return modem;
}
int com_close()
{
close(fdmodem);
fdmodem = -1;
return 0;
}
/* A function that checks if there is any input from the modem */
int com_charwaiting()
{
fd_set set;
struct timeval timeout;
timeout.tv_sec = 0;
timeout.tv_usec = MSWAIT;
FD_ZERO(&set);
FD_SET(fdmodem, &set);
if (select(fdmodem+1, &set, NULL, NULL, &timeout) > 0)
{
if (FD_ISSET(fdmodem, &set)) return TRUE;
}
return FALSE;
}
/* This function checks if a write can be done */
int com_canwrite()
{
fd_set set;
struct timeval timeout;
timeout.tv_sec = 0;
timeout.tv_usec = MSWAIT;
FD_ZERO(&set);
FD_SET(fdmodem, &set);
if (select(fdmodem+1, NULL, &set, NULL, &timeout) > 0)
{
if (FD_ISSET(fdmodem, &set)) return TRUE;
}
return FALSE;
}
/* Get a char from the modem */
char com_getchar()
{
char ch;
read(fdmodem, &ch, 1);
return(ch);
}
/* Send a char to the modem */
void com_sendchar(char ch)
{
write(fdmodem, &ch, 1);
}
int com_write(char *buff, int nmax)
{
int n;
n = strlen(buff);
if (nmax>0&&n>nmax) n = nmax;
return write(fdmodem,buff,n);
}
int com_read(char *buff, int nmax)
{
return read(fdmodem,buff,nmax);
}
luxc1972
2003-02-25
打赏
举报
回复
很简单,给你一段源代码,使用举例: ngComFd = InitCom("/dev/tty1a", O_RDONLY);
读写时调用read(), write()就可以了。
int InitCom(strTtyName, nOpenFlag)
char *strTtyName;
int nOpenFlag;
{
int nFd;
char temp[50];
struct termio termNewtty;
if ((nFd = open(strTtyName, nOpenFlag)) == -1)
{
sprintf(temp, "Open %s error !\n", strTtyName);
perror(temp);
return (-1);
}
if (ioctl(nFd, TCGETA, &termNewtty) == -1)
{
sprintf(temp, "%s TCGETA is error !\n", strTtyName);
perror(temp);
close(nFd);
return (-1);
}
termNewtty.c_iflag = IGNBRK;
termNewtty.c_oflag = 0;
switch (atoi(strsBaudRate))
{
case 1200:
termNewtty.c_cflag=CLOCAL|HUPCL|CREAD|CS8|B1200;
break;
case 2400:
termNewtty.c_cflag=CLOCAL|HUPCL|CREAD|CS8|B2400;
break;
case 4800:
termNewtty.c_cflag=CLOCAL|HUPCL|CREAD|CS8|B4800;
break;
case 9600:
termNewtty.c_cflag=CLOCAL|HUPCL|CREAD|CS8|B9600;
break;
case 19200:
termNewtty.c_cflag=CLOCAL|HUPCL|CREAD|CS8|B19200;
break;
case 38400:
termNewtty.c_cflag=CLOCAL|HUPCL|CREAD|CS8|B38400;
break;
case 115200:
termNewtty.c_cflag=CLOCAL|HUPCL|CREAD|CS8|B115200;
break;
default:
termNewtty.c_cflag=CLOCAL|HUPCL|CREAD|CS8|B2400;
break;
}
termNewtty.c_lflag = 0;
termNewtty.c_cc[VMIN] = 1;
if (ioctl(nFd, TCSETA, &termNewtty) == -1)
{
sprintf(temp, "%s TCSETA is error !\n", strTtyName);
perror(temp);
close(nFd);
return (-1);
}
return(nFd);
}
232chuanxing.zip_Linux/
Unix
编程_
Unix
_Linux_
这是一个在linux环境
下用
rs232串口实现的双机单工通信,使用C语言
编写
,下载后在linux下运行,效果较好
C# vs2008 WCF开发实例
WCF 概括地说,WCF具有如下的优势: 1、统一性 前面已经叙述,WCF是对于ASMX,.Net Remoting,Enterprise ...应用程序可以运行在Windows操作系统下,也可以运行在其他的操作系统,如Sun Solaris,HP
Unix
,Linux等等
C语言基础
C语言是目前世界上流行、使用最广泛的高级程序设计语言 6、很多编译器,几乎所有操作系统Windows,Linux,
Unix
的大部分代码都是C,C在背后做了很多东西的,也许开发游戏用C++,安卓用XX更为合适,图形界面的用其他...
vc源代码合集2244.rar
2012-06-11 22:38 11,438,631 VC++
串口通信
20个经典源码合集.rar 2012-06-11 22:37 11,944,668 VC++
串口通信
技术与实践.pdf 2012-06-11 22:30 183,893 VC++环境下WinSock编程及实例分析.rar 2012-06-11 22:38 18,985...
RED HAT LINUX 6大全
12.5 在/etc/passwd文件中使用NISisms 232 12.6 使用网络组 232 12.7 解决问题的一些技巧 233 12.8 小结 233 第13章 网络文件系统 235 13.1 NFS安装 235 13.2 启动和停止NFS守护程序 236 13.3 NFS状态 236 13.4 配置...
C语言
69,382
社区成员
243,073
社区内容
发帖
与我相关
我的任务
C语言
C语言相关问题讨论
复制链接
扫一扫
分享
社区描述
C语言相关问题讨论
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章