linux串口通信的接收与发送:实现高效的数据传输

Linux串口通信接收与发送:接收:

Linux串口通信接收与发送:

接收:

1. 打开串口:int fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);

2. 设置串口参数:struct termios options;tcgetattr(fd, &options);cfsetispeed(&options, B9600);cfsetospeed(&options, B9600);options.c_cflag |= (CLOCAL | CREAD);options.c_cflag &= ~CSIZE;options.c_cflag |= CS8;options.c_cflag &= ~PARENB;options.c_cflag &= ~CSTOPB;options.c_cflag &= ~CRTSCTS;options.c_iflag |= IGNPAR;options.c_iflag &= ~(IXON | IXOFF | IXANY);options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);options.c_oflag &= ~OPOST;tcsetattr(fd, TCSANOW, &options);

3. 读取串口数据:char buf[128];int len = read(fd, buf, sizeof(buf));

发送:

1. 打开串口:int fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);

2. 设置串口参数:struct termios options;tcgetattr(fd, &options);cfsetispeed(&options, B9600);cfsetospeed(&options, B9600);options.c_cflag |= (CLOCAL | CREAD);options.c_cflag &= ~CSIZE;options.c_cflag |= CS8;options.c_cflag &= ~PARENB;options.c_cflag &= ~CSTOPB;options.c_cflag &= ~CRTSCTS;options.c_iflag |= IGNPAR;options.c_iflag &= ~(IXON | IXOFF | IXANY);options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);options.c_oflag &= ~OPOST;tcsetattr(fd, TCSANOW, &options);

3. 写入串口数据:char buf[128] = "hello world";int len = write(fd, buf, strlen(buf));

本站系公益性非盈利分享网址,本文来自用户投稿,不代表边看边学立场,如若转载,请注明出处

(6)
matlab linux版:如何使用Matlab在Linux系统上进行数据分析
上一篇
java linkedlist用法:使用Java LinkedList实现数据结构的动态存储
下一篇

相关推荐

发表评论

登录 后才能评论

评论列表(85条)