- 论坛徽章:
- 0
|
10可用积分
linux 串口读取数据被截断,怎样一次全部接收?
打开串口后,用read读取串口数据,串口发来的
数据为20,而用read读取到的数据却是分多次得到的?
有时只读了8个数据便被截断了,这是为什么??
怎样一次全部接收?? 谢谢
附代码:- #include <stdio.h>
- #include <unistd.h> /*Unix标准函数定义*/
- #include <sys/types.h> /**/
- #include <sys/stat.h> /**/
- #include <fcntl.h> /*文件控制定义*/
- #include <termios.h> /*PPSIX终端控制定义*/
- #include <errno.h> /*错误号定义*/
- #include <stdlib.h>
- #include <string.h>
- int main()
- {
- int fd,readnum;
- char *dev="/dev/ttyS0",recv[100];
- struct termios opt,oldopt;
-
- fd = open( dev, O_RDWR );
-
- tcgetattr( fd,&oldopt);
- tcgetattr( fd,&opt);
- cfsetispeed(&opt, B9600);//9600
- cfsetospeed(&opt, B9600);
- opt.c_cflag &= ~PARENB; //N
- opt.c_cflag &= ~INPCK;
- opt.c_cflag &= ~CSTOPB;//1
- opt.c_cflag &= ~CSIZE;
- opt.c_cflag |= CS8; //8
- opt.c_iflag &= ~(IXON | IXOFF | IXANY);
- opt.c_cc[VTIME] = 0;
- opt.c_cc[VMIN] = 0;
- opt.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); /*Input*/
- opt.c_oflag &= ~OPOST; /*Output*/
- tcflush(fd, TCIOFLUSH);
- tcsetattr(fd, TCSANOW, &opt);
- while(1)
- {
- bzero(recv,100);
- while((readnum = read(fd,recv,100))>0)
- {
- printf("%s\n",recv);
- }
- }
-
- tcflush(fd, TCIOFLUSH);
- tcsetattr(fd, TCSANOW, &oldopt);
- close(fd);
- printf("exit.");
- }
复制代码 |
|