错误序列的libserial接收数据 [英] wrong sequence of libserial received data
问题描述
我现在正在使用libserial在Ubuntu下进行串口通信的项目。我们修改了libserial源代码以允许MARK / SPACE奇偶校验。但是当进行数据接收测试时,有时(约5%的机会)输入数据序列是错误的。第一个字节可以跳转到传入缓冲区的中间或甚至结束。
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.
问题在于原来的 SerialPort :: SerialPortImpl: :HandlePosixSignal(int singnalNumber)f
unction,当错误发生时,它不能正确读取一些字节,错误描述是资源暂时不可用。但是,由于它已经使用ioctl获得 num_of_bytes_available
,在测试期间可能会失败?
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?
之间的MARK / SPACE奇偶校验。是否会导致问题?
during my test, I toggle between MARK/SPACE parity frequently. will that cause the problem?
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;
推荐答案
好的,我终于知道是什么原因。 libserial
SerialPort
类不是线程安全的。 POSIX信号可能与升压线程冲突。我能够通过切换到 SerialStream
类来解决这个问题。
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.
这篇关于错误序列的libserial接收数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!