当前位置: 技术问答>linux和unix
如何在linux下用C/C++实现RS232 串口通讯的程序?
来源: 互联网 发布时间:2015-03-23
本文导语: 在linux下用C/C++实现RS232 串口通讯的程序 | int fd,readbyte=0; unsigned char cmd[6]; struct termios newio,oldio; fd = open("/dev/ttyS1", O_RDWR | O_NOCTTY|O_NDELAY); newio.c_cflag= C...
在linux下用C/C++实现RS232 串口通讯的程序
|
int fd,readbyte=0;
unsigned char cmd[6];
struct termios newio,oldio;
fd = open("/dev/ttyS1", O_RDWR | O_NOCTTY|O_NDELAY);
newio.c_cflag= CS8|B9600|CLOCAL|CREAD;
newio.c_iflag = 0;
newio.c_oflag =0;
newio.c_lflag =0 ;
tcgetattr(fd,&oldio);
tcsetattr(fd,TCSANOW,&newio);
write(fd,cmd,6);
readbyte=read(fd,cmd,6);
if(readbyte==6)
printf("read file ok");
close(fd);
tcsetattr(fd,TCSANOW,&oldio);
unsigned char cmd[6];
struct termios newio,oldio;
fd = open("/dev/ttyS1", O_RDWR | O_NOCTTY|O_NDELAY);
newio.c_cflag= CS8|B9600|CLOCAL|CREAD;
newio.c_iflag = 0;
newio.c_oflag =0;
newio.c_lflag =0 ;
tcgetattr(fd,&oldio);
tcsetattr(fd,TCSANOW,&newio);
write(fd,cmd,6);
readbyte=read(fd,cmd,6);
if(readbyte==6)
printf("read file ok");
close(fd);
tcsetattr(fd,TCSANOW,&oldio);
|
http://www.fanqiang.com/a4/b7/20010502/110712_b.html
|
termios,tcgetattr,tcsetattr都可以用man命令找到定义啊。
|
上网找找,网上有很多源码,我也刚写完串口程序.
|
mark