【问题标题】:How do I prevent libserial to eat up characters like 0x0A, 0x0D, or 0x0F coming from my serial device?如何防止 libserial 吞噬来自串行设备的 0x0A、0x0D 或 0x0F 等字符?
【发布时间】:2020-06-07 16:13:11
【问题描述】:

当使用libserial'sSerialStream 对象与串行 USB 设备(用于获取道路交通警报的 GNS FM9 接收器)通信时,我发现并非来自设备的所有字符实际上都被读取.这是我一直得到的服务:

3F 00 56 08 37 3F

当使用与RDS Surveyor app(用Java 编写并使用com.fazecast.jSerialComm.SerialPort)相同的设备时,相同的响应如下所示:

3F 00 00 56 08 0A 37 0C 0D 3F

经过一番研究,我偶然发现了这个帖子:Wrong newline character over the serial port (CR instead of LF)。发帖人没有使用libserial(而是一些现成的串口通讯app),但他的问题和我的一模一样。

这是相关端口的系统端配置:

pi@autoradio:~ $ stty -a
speed 38400 baud; rows 40; columns 80; line = 0; intr = ^C; quit = ^\; erase = ^?; kill = ^U;
eof = ^D; eol = <undef>; eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z;
rprnt = ^R; werase = ^W; lnext = ^V; discard = ^O; min = 1; time = 0; -parenb -parodd -cmspar
cs8 -hupcl -cstopb cread -clocal -crtscts -ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr
-igncr icrnl ixon ixoff -iuclc -ixany -imaxbel iutf8 opost -olcuc -ocrnl onlcr -onocr -onlret
-ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0 isig icanon iexten echo echoe echok -echonl -noflsh
-xcase -tostop -echoprt echoctl echoke -flusho -extproc

系统是运行最新生产版本的 Raspbian 的 Raspbery Pi 3B。在系统端,设备如下所示:

pi@autoradio:~ $ ls -al /dev/ttyUSB0
crw-rw---- 1 root dialout 188, 0 Jun  7 17:11 /dev/ttyUSB0

现在,我的问题是:如何让我的 SerialStream 读取所有字符而不进行任何转换,就像它们来自端口一样?我真的不想在这种情况下使用stty,因为在移除和重新连接设备时它的设置可能会被覆盖。谢谢。

【问题讨论】:

  • 您可能需要为该语言添加标签。
  • 我刚刚做了,但此时,语言本身似乎还没有相关性。

标签: c++ newline libserial


【解决方案1】:

您需要确保端口处于“原始”模式。

快速浏览一下 libserial 文档,似乎它没有设置原始模式的参数。一些选项:

  • 修改 libserial 以设置原始模式。
  • 不要使用 libserial,而使用其他东西。
  • 更改操作系统的默认端口。

libserial 的实现看起来很基础;你可能最好使用 Boost.asio 之类的东西。

【讨论】:

  • 首先,我得到的信息表明问题与LibSerial 无关。请参阅下面的答案。其次,由于目标系统是一个 RPi,所以 Boost 会有点矫枉过正。对不起!
【解决方案2】:

好的,我 opened an issuelibserial 的维护人员得到了答案,即 C++ std::iostream(甚至 LibSerial::SerialStream!)默认会踢出空白字符。 CR, LF, FF 均视为此类。

他们向我提出了这个解决方案:

SerialStream tmc_receiver_handler ("/dev/ttyUSB0", ios::in | ios::out);
char c = 0x00;

tmc_receiver_handler.SetBaudRate      (SerialStreamBuf::BAUD_38400);
tmc_receiver_handler.SetCharSize      (SerialStreamBuf::CHAR_SIZE_8);
tmc_receiver_handler.SetFlowControl   (SerialStreamBuf::FLOW_CONTROL_NONE);
tmc_receiver_handler.SetParity        (SerialStreamBuf::PARITY_NONE);
tmc_receiver_handler.SetNumOfStopBits (SerialPort::STOP_BITS_1);

tmc_receiver_handler >> noskipws >> c;

noskipws 关闭空格抑制。应用后,一切正常。

【讨论】:

    猜你喜欢
    • 2021-05-20
    • 1970-01-01
    • 2016-01-17
    • 1970-01-01
    • 2011-04-15
    • 1970-01-01
    • 2021-10-08
    • 1970-01-01
    • 2014-06-10
    相关资源
    最近更新 更多