串口不可靠

时间:2018-11-30 15:29:48

标签: c serial-port

我以C语言对连接到串行端口的设备进行读/写操作。初始化步骤成功完成,但是尝试写入它的时间超过一半,我得到errno = 5,“输入/输出错误”。 我的设置有问题吗?

Set to  : cflag=0x4be, lflag=0x0, iflag=0x4, oflag=0x4
c_cc: INTR=3 QUIT=28 ERASE=127 KILL=21 EOF=1 TIME=1 START=17 STOP=19 SUSP=26 REPRINT=18 DISCARD=15 WERASE=23 LNEXT=22 
c_iflag: IGNPAR 
c_oflag: ONLCR NLDLY=NL0 CRDLY=CR0 TABDLY=TAB0 BSDLY=BS0 VTDLY=VT0 FFDLY=FF0 
c_cflag: CBAUD=B19200 CSIZE=CS8 CREAD HUPCL 
c_lflag:

简化代码(出于可读性考虑,已删除错误测试):

int fd=open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY );
struct termios options;
fcntl(fd, F_SETFL, 0 ));
tcgetattr(fd, &options))
cfsetospeed(&options, Mult==2 ? B19200 : B9600);
cfsetispeed(&options, Mult==2 ? B19200 : B9600);

options.c_lflag &= ~ICANON; 
options.c_lflag &= ~(ECHO | ECHOCTL | ECHONL);
options.c_cflag |= HUPCL;

options.c_oflag |= ONLCR;
options.c_iflag &= ~ICRNL;

options.c_cc[VMIN]  = 0;
options.c_cc[VTIME] = 1;

options.c_iflag &= ~(IXON | IXOFF | IXANY);

options.c_cflag &= ~(PARENB | CSTOPB | CSIZE | CRTSCTS);
options.c_cflag |= CS8;

tcsetattr(fd, TCSANOW, &options));

R=write(fd, Msg, strlen(Msg));

->失败,错误码为errno = 5,输入/输出错误的时间约为一半

0 个答案:

没有答案