|
最近写了一个LINUX串口程序,出现8位数据位的时候无法加奇偶校验,一加上就部分输出乱码,7位数据位就可以加,我想请问一下各位高手是怎么回事,谢谢,以下是我的串口设置:
int Serial_set(int fd,int nSpeed,int nBits,char nParity,int nStop)
{
bzero( &newtio, sizeof( newtio ) );
cfmakeraw(&newtio);
tcflush(fd,TCIOFLUSH);
newtio.c_cflag &= ~CRTSCTS; // no stream ctrl
newtio.c_cflag |= CLOCAL | CREAD; /* | CRTSCTS //stream strl*/
newtio.c_cflag &= ~CSIZE;
switch( nBits )
{
case 7:
newtio.c_cflag |= CS7;
break;
case 8:
newtio.c_cflag |= CS8;
break;
default:
perror("unsupport data bits");
return -1;
}
switch( nParity )
{
case 'o':
case 'O':
newtio.c_cflag |= PARENB;
newtio.c_cflag |= PARODD;
newtio.c_iflag |= (INPCK | ISTRIP);
break;
case 'e':
case 'E':
newtio.c_cflag |= PARENB;
newtio.c_cflag &= ~PARODD;
newtio.c_iflag |= (INPCK | ISTRIP);
break;
case 'n':
case 'N':
newtio.c_cflag &= ~PARENB;
newtio.c_iflag &= ~(INPCK |ISTRIP);
break;
default:
perror("setting parity error\n");
return -1;
}
switch( nSpeed )
{
case 1200:
cfsetispeed(&newtio, B1200);
cfsetospeed(&newtio, B1200);
break;
case 2400:
cfsetispeed(&newtio, B2400);
cfsetospeed(&newtio, B2400);
break;
case 4800:
cfsetispeed(&newtio, B4800);
cfsetospeed(&newtio, B4800);
break;
case 9600:
cfsetispeed(&newtio, B9600);
cfsetospeed(&newtio, B9600);
break;
case 115200:
cfsetispeed(&newtio, B115200);
cfsetospeed(&newtio, B115200);
break;
case 460800:
cfsetispeed(&newtio, B460800);
cfsetospeed(&newtio, B460800);
break;
default:
cfsetispeed(&newtio, B9600);
cfsetospeed(&newtio, B9600);
break;
}
if( nStop == 1 )
newtio.c_cflag &= ~CSTOPB;
else if ( nStop == 2 )
newtio.c_cflag |= CSTOPB;
newtio.c_oflag = 0;
newtio.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
newtio.c_iflag &= ~(BRKINT | ICRNL | IXON);
newtio.c_oflag &= ~OPOST;
newtio.c_cc[VTIME] = 5; /* unit: 1/10 second. */
newtio.c_cc[VMIN] = 5; /* minimal characters for reading */
if((tcsetattr(fd,TCSANOW,&newtio))!=0)
{
perror("com set error");
return -1;
}
//printf("set done!\n");
return 0;
} |
|