我正在实现一个串行接口,以使用 /dev/ttyS0 与外部硬件进行通信。我已经为 RAW 输入和输出进行了配置,因为数据流采用数据包格式并封装有消息开头、长度字节和二进制数据。传入数据流中的任何 DC1 或 DC3 字符 0x11 或 0x13 都会被 Linux OS(最新的 Ubuntu)吃掉。
我有一台 Windows PC 和 Realterm 监控通信,并在监视器上看到 0x11 和 0x13 字符。据我所知,没有其他丢失的字符。
这是我的串行设置代码(注意 IXON、IXOFF、IXANY 已禁用):
void Ser_Open_Port( unsigned char *port )
{
tcgetattr(ser_handle, &ser_old_settings);
tcgetattr(ser_handle, &ser_new_settings);
// Set Baud Rate
cfsetospeed(&ser_new_settings, baud);
// ~BRKINT - No Flush on Break
// ~PARMRK, ~IGNPAR - Parity Error result in \0 char
// ~ISTRIP - No 8th bit Strip
// ~INLCR - No NL to CR Translate
// ~IGNCR No Ignore CR
// ~ICRNL No CR to NL Translate
// ~IXON - No Software Flow Control on Output
// ~IXOFF - No Software Flow Control on Output
// ~IXANY - No Software Flow Control on Output
// ~INPCK - Ignore Parity Checking
ser_new_settings.c_iflag &= ~(BRKINT|PARMRK|IGNPAR|ISTRIP|INLCR|IGNCR|ICRNL|IXON|IXOFF|IXANY|INPCK);
// IGNBRK - Ignore Break
ser_new_settings.c_iflag |= (IGNBRK);
// ~OPOST - Implementation Defined Output Processing Off - Raw Output
// All Other c_oflag bits are ignored
ser_new_settings.c_oflag &= ~(OPOST);
// ~ICANON - Not Canonical Input Mode
// ~ECHO - No Echo
// ~ECHOE - No Echo Erase
// ~ECHONL - No Echo NL
// ~ISIG - No Signals
// ~IEXTEN - Implementation Defined Input Processing Off - Raw Input
ser_new_settings.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHONL|ISIG|IEXTEN);
// ~PARENB - No Parity
// ~CSTOPB - 1 stop bit
// ~CSIZE - Clear Character Size Bits
ser_new_settings.c_cflag &= ~(PARENB|CSTOPB|CSIZE);
// CS8 - Character Size 8 bits
// CLOCAL - Ignore Modem Control Lines
ser_new_settings.c_cflag |= (CS8|CLOCAL);
// apply the settings
tcsetattr(ser_handle, TCSANOW, &ser_new_settings);
tcflush(ser_handle, TCOFLUSH);
// Open Serial Port for Read/Write, and Non Blocking
ser_handle = open(port, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK);
}
这是我从串行端口缓冲区读取的位置:
ser_charin_count = read( ser_handle, ser_charin, 30);
if( ser_charin_count > 0 && ser_charin_count != 0xFFFFFFFF)
{
// Code to Process Message Bytes Here
}
谢谢您的帮助。