我为串口 ttyS4 编写了一个程序,它使用了 termios 函数。该程序使用非规范模式。我可以发送十六进制数据,也可以读取十六进制数据。我的问题是当我读取接收到多个 0xFF 字节的数据时,我的串行端口缓冲区以垃圾形式填充了额外的 0xFF 字节。例如我的传输数据是
01 23 73 00 03 7b 29 00 03 86 9c 00 03 61 73 00 00 03 5b 00 00 03 63 00 00 02 d6 00 00 00 00 00 00 c3 70 00 00 03 e8 00 00 03 e8 00 00 03 e8 00 00 03 e8 00 00 02 2c ff ff ff fc 00 00 02 2c 00 00 72 89 00 00 00 00 00 00 00 93 00 00 72 9d 0b 35 33 0d 06 11 00 05 00 08 00 00 2a d9 00 00 28 22 00 16 00 01 00 05 00 ab 82 40 00 ab a9 50 01 01 01 00 00 00 1f 9b 71
但实际我收到的数据如下。
01 23 73 00 03 7b 29 00 03 86 9c 00 03 61 73 00 00 03 5b 00 00 03 63 00 00 02 d6 00 00 00 00 00 00 c3 70 00 00 03 e8 00 00 03 e8 00 00 03 e8 00 00 03 e8 00 00 02 2c ff ff ff ff ff ff fc 00 00 02 2c 00 00 72 89 00 00 00 00 00 00 00 93 00 00 72 9d 0b 35 33 0d 06 11 00 05 00 08 00 00 2a d9 00 00 28 22 00 16 00 01 00 05 00 ab 82 40 00 ab a9 50 01 01 01 00 00 00 1f 9b 71
额外收到 3 个 0xFF(垃圾)。收到多个字节时会出现此问题0xFF
。
以下配置用于配置端口。
///////////////////////////////////////////////////////
fd = open("/dev/ttyS4",O_RDWR | O_NOCTTY| O_SYNC);
struct termios SerialPortSettings;
cfsetispeed(&SerialPortSettings,B9600); // 9600 baud rate
SerialPortSettings.c_cflag &= ~PARENB; // No Parity}
SerialPortSettings.c_cflag &= ~CSTOPB; //Stop bits = 1
SerialPortSettings.c_cflag &= ~CSIZE; /* Clears the Mask*/
SerialPortSettings.c_cflag |= CS8; /* Set the data bits = 8 */
SerialPortSettings.c_cflag &= ~CRTSCTS; //Turn off hardware based flow control
SerialPortSettings.c_cflag |= CREAD | CLOCAL;//Turn on the receiver of the serial port (CREAD)
SerialPortSettings.c_iflag &= ~IGNBRK;
SerialPortSettings.c_iflag |= BRKINT;
SerialPortSettings.c_iflag &= ~ISTRIP;
SerialPortSettings.c_iflag &= ~(IXON | IXOFF | IXANY); //Turn off software based flow control (XON/XOFF).
SerialPortSettings.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG );//Setting the mode of operation,the default mode of operation of serial port in Linux is the Non - Cannonical mode
SerialPortSettings.c_iflag &= ~(INLCR | ICRNL );
SerialPortSettings.c_cc[VMIN] = 0;
SerialPortSettings.c_cc[VTIME] = 100;
tcflush(fd, TCIOFLUSH);
tcsetattr(fd,TCSANOW,&SerialPortSettings);
///////////////////////////////////////////////////////////////
请任何人建议如何解决此问题。