- 论坛徽章:
- 0
|
测了几天没什么进展!!不知道为什么!我把串口部分的等待时间和最小接收字符参数设置成零后,我的串口就只能收到一大堆零!!
修改的串口初始化代码如下:请各位高手帮忙看看啥原因!- int set_speed(int fd, int speed)
- {
- int i;
- int status;
- struct termios Opt;
-
- tcgetattr(fd, &Opt);
- for ( i= 0; i < sizeof(speed_arr) / sizeof(int); i++)
- {
- if (speed == name_arr[i])
- {
- tcflush(fd, TCIOFLUSH);
- cfsetispeed(&Opt, speed_arr[i]);
- cfsetospeed(&Opt, speed_arr[i]);
- status = tcsetattr(fd, TCSANOW, &Opt);
- if (status != 0)
- {
- perror("tcsetattr fd");
- return -1;
- }
- tcflush(fd,TCIOFLUSH);
- return 0;
- }
- }
- return -1;
- }
- int set_other_attribute(int fd, int nbits, char bevent, int nstop)
- {
- struct termios newtio,oldtio;
- if (tcgetattr(fd, &oldtio) != 0)
- {
- perror("SetupSerial 1");
- return -1;
- }
- bzero(&newtio,sizeof(newtio));
- //设置字符大小
- newtio.c_cflag |= CLOCAL | CREAD;
- newtio.c_cflag &= CSIZE;
- //设置数据位
- switch(nbits)
- {
- case 7:
- newtio.c_cflag |= CS7;
- break;
- case 8:
- newtio.c_cflag |= CS8;
- }
- //设置奇偶校验位
- switch(bevent)
- {
- case 'O'://奇数
- newtio.c_cflag |= PARENB;
- newtio.c_cflag |= PARODD;
- newtio.c_iflag |= (INPCK | ISTRIP);
- break;
- case 'E'://偶数
- newtio.c_iflag |= (INPCK | ISTRIP);
- newtio.c_cflag |= PARENB;
- newtio.c_cflag &= ~PARODD;
- break;
- case 'N'://无奇偶校验
- newtio.c_cflag &= ~PARENB;
- break;
- }
- //设置停止位
- if(nstop == 1)
- newtio.c_cflag &= ~CSTOPB;
- else if(nstop == 2)
- newtio.c_cflag |= CSTOPB;
- //设置等待时间和最小接收字符
- newtio.c_cc[VTIME] = 0;
- newtio.c_cc[VMIN] = 0;
- //处理未接收字符
- tcflush(fd,TCIFLUSH);
- //激活配置
- if((tcsetattr(fd,TCSANOW,&newtio)) != 0)
- {
- perror("Com set error");
- return -1;
- }
- printf("set done!\n");
- return 0;
- }
复制代码 |
|