请教关于linux系统下 用read读取串口数据出现的丢失问题
as1_2 2017-07-19 09:37:01 在Linux串口下读取数据,总是丢失前面的一两个数据,每一帧数据都是这样,这是主要的源码:
int set_Parity(int fd, int databits, int stopbits, int parity) {
struct termios options;
if (tcgetattr(fd, &options) != 0) {
perror("SetupSerial 1");
//return (FALSE);
return 0;
}
options.c_cflag &= ~CSIZE;
switch (databits) /*设置数据位数*/
{
case 7:
options.c_cflag |= CS7;
break;
case 8:
options.c_cflag |= CS8;
break;
default:
// fprintf(stderr, "Unsupported data size\n");
return 0;
}
switch (parity) {
case 'n':
case 'N':
options.c_cflag &= ~PARENB; /* Clear parity enable */
options.c_iflag &= ~INPCK; /* Enable parity checking */
break;
case 'o':
case 'O':
options.c_cflag |= (PARODD | PARENB); /* 设置为奇效验*/
options.c_iflag |= INPCK; /* Disnable parity checking */
break;
case 'e':
case 'E':
options.c_cflag |= PARENB; /* Enable parity */
options.c_cflag &= ~PARODD; /* 转换为偶效验*/
options.c_iflag |= INPCK; /* Disnable parity checking */
break;
case 'S':
case 's': /*as no parity*/
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
break;
default:
//fprintf(stderr, "Unsupported parity\n");
return 0;
}
/* 设置停止位*/
switch (stopbits) {
case 1:
options.c_cflag &= ~CSTOPB;
break;
case 2:
options.c_cflag |= CSTOPB;
break;
default:
//fprintf(stderr, "Unsupported stop bits\n");
return 0;
}
/* Set input parity option */
if (parity != 'n')
options.c_iflag |= INPCK;
//修改输出模式,原始数据输出,这里可能需要去掉,跟原来的好像不一样
tcflush(fd, TCIFLUSH);
options.c_cc[VTIME] = 10; /* 设置超时15 seconds*/
options.c_cc[VMIN] = 8; /* Update the options and do it NOW */
if (tcsetattr(fd, TCSANOW, &options) != 0) {
perror("SetupSerial 3");
return 0;
}
return 1;
}