社区
C语言
帖子详情
怎样在unix下用c编写串口通信?
ricsson
2003-02-25 09:13:43
谁有这方面的资料??
...全文
45
2
打赏
收藏
怎样在unix下用c编写串口通信?
谁有这方面的资料??
复制链接
扫一扫
分享
转发到动态
举报
AI
作业
写回复
配置赞助广告
用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);
}
C++
串口通信
RS232 代码
C++
串口通信
RS232 希望对大家有所帮助
C++
串口通信
与工程应用实践
关于C++ 与
串口通信
的开发,里面都是关于串口的工程应用实践代码,从简单的到复杂的都有……
串口程序C++
编写
这是用C++写的一个
串口通信
类,很有效果,希望给大家一个参考...
串口通信
库
c语言开发的串口库,由于跨平台,封装性号,使用很方便
一个最简单的PC
串口通信
程序
一个用C/C++实现的最简单的PC
串口通信
程序
C语言
70,022
社区成员
243,263
社区内容
发帖
与我相关
我的任务
C语言
C语言相关问题讨论
复制链接
扫一扫
分享
社区描述
C语言相关问题讨论
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章