我正在尝试从使用标记奇偶校验作为地址字节和空间奇偶校验作为消息正文的设备接收消息。该设备是多点串行总线的“主机”。根据 termios 手册页,我正在使用 CMSPAR、PARENB、~PARODD、INPCK、~IGNPAR 和 PARMRK。我希望在每个地址字节上获得一个 3 字节序列: '\377' '\0' 。它不会发生......我总是得到地址字节(和正文字节),但没有前导 '\377' '\0' 字符。
我试图让 PARMRK 使用奇偶校验设置,以防不支持 CMSPAR。数据流中仍然没有 3 字节序列。我正在使用 Ubuntu 12.04 LTS。
n_tty.c:n_tty_receive_parity_error() 具有实现 PARMRK 的逻辑。8250_core.c 具有标记奇偶校验错误的逻辑。dmesg | grep ttyS0 显示 serail8250: ... 是 16550A。嗯...随后的消息显示 00:0a: ... 是 16550A。也许 8250 驱动程序实际上并没有处理 ttyS0?
有任何想法吗?即使你没有看到我做错了什么但让 PARMAR 工作,关于你的情况的评论可能对我有帮助。
更新:我的 Linux 在 VMware VM 中运行,所以我尝试了非 VM 配置,现在它可以工作了!如果有人知道,我仍然想知道为什么在 VM 中未检测到奇偶校验错误。
这是我的配置代码:
struct termios tio;
bzero(&tio, sizeof(tio));
tcgetattr(fd, &tio);
// Frame bus runs at 38,400 BAUD
const int BAUD_Rate = B38400;
cfsetispeed(&tio, BAUD_Rate);
cfsetospeed(&tio, BAUD_Rate);
// Initialize to raw mode. PARMRK and PARENB will be over-ridden before calling tcsetattr()
cfmakeraw(&tio);
// Ignore modem lines and enable receiver
tio.c_cflag |= (CLOCAL | CREAD);
// No flow control
tio.c_cflag &= ~CRTSCTS; // No HW flow control
tio.c_iflag &= ~(IXON | IXOFF); // Set the input flags to disable in-band flow control
// Set bits per byte
tio.c_cflag &= ~CSIZE;
tio.c_cflag |= CS8;
// Use space parity to get 3-byte sequence (0xff 0x00 <address>) on address byte
tio.c_cflag |= CMSPAR; // Set "stick" parity (either mark or space)
tio.c_cflag &= ~PARODD; // Select space parity so that only address byte causes error
// NOTE: The following block overrides PARMRK and PARENB bits cleared by cfmakeraw.
tio.c_cflag |= PARENB; // Enable parity generation
tio.c_iflag |= INPCK; // Enable parity checking
tio.c_iflag |= PARMRK; // Enable in-band marking
tio.c_iflag &= ~IGNPAR; // Make sure input parity errors are not ignored
// Set it up now
if (tcsetattr(fd, TCSANOW, &tio) == -1)
{
cout << "Failed to setup the port: " << errno << endl;
return -1;
}