简体   繁体   English

自由接收数据的顺序错误

[英]wrong sequence of libserial received data

I'm now working on a project which uses libserial for serial port communication under Ubuntu. 我现在正在一个使用libserial在Ubuntu下进行串行端口通信的项目。 we modified the libserial source code to allow MARK/SPACE parity. 我们修改了libserial源代码以允许MARK / SPACE奇偶校验。 but when doing data receiving test, sometimes (about 5% chance) the incoming data sequence is wrong. 但是在进行数据接收测试时,有时(大约5%的机会)输入的数据顺序是错误的。 The first byte can jump to the middle of the incoming buffer or even to the end. 第一个字节可以跳到传入缓冲区的中间,甚至跳到结尾。

The problem is inside the original SerialPort::SerialPortImpl::HandlePosixSignal( int singnalNumber) f unction, when the error happens it can not read some byte correctly and the error description is "Resource temporarily unavailable". 问题出在原始SerialPort::SerialPortImpl::HandlePosixSignal( int singnalNumber) f ,当错误发生时它无法正确读取某些字节,并且错误描述为“资源暂时不可用”。 But since it already get num_of_bytes_available using ioctl, how is it possible to fail? 但是,既然已经使用ioctl获得了num_of_bytes_available ,那么怎么可能失败呢?

during my test, I toggle between MARK/SPACE parity frequently. 在测试期间,我经常在MARK / SPACE奇偶校验之间切换。 will that cause the problem? 那会引起问题吗?

the modified part in SetParity function SetParity函数中的修改部分

for **PARITY_SPACE**

port_settings.c_cflag |= CMSPAR | PARENB;

port_settings.c_cflag &= ~PARODD;

port_settings.c_iflag = 0;

for **PARITY_MARK**

port_settings.c_cflag |= CMSPAR | PARENB;

port_settings.c_iflag = 0;

Okay, I finally figured out what was causing the problem. 好的,我终于弄清楚是什么原因引起的。 The libserial SerialPort class is not thread-safe. libserial SerialPort类不是线程安全的。 A POSIX signal probably conflicted with the boost thread. POSIX信号可能与增强线程冲突。 I was able to solve the problem by switching to the SerialStream class. 我可以通过切换到SerialStream类来解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM