I'm now working on a project which uses libserial for serial port communication under Ubuntu. we modified the libserial source code to allow MARK/SPACE parity. but when doing data receiving test, sometimes (about 5% chance) the incoming data sequence is wrong. 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". But since it already get num_of_bytes_available
using ioctl, how is it possible to fail?
during my test, I toggle between MARK/SPACE parity frequently. will that cause the problem?
the modified part in SetParity
function
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. A POSIX signal probably conflicted with the boost thread. I was able to solve the problem by switching to the SerialStream
class.
精彩评论