简体   繁体   English

Linux:通过一个进程从串行端口读取数据,然后通过另一个进程写入数据

[英]Linux: Read data from serial port with one process and write to it with another

I've ecountered a problem using a serial GPS/GNSS device on a Raspberry Pi. 我在Raspberry Pi上使用串行GPS / GNSS设备遇到了问题。 The device in question is a u-blox GNSS receiver symlinked to /dev/gps . 有问题的设备是链接到/dev/gps的u-blox GNSS接收器。

I try to achieve logging the output data from this device and simultaneously sending correction data to it. 我试图实现记录该设备的输出数据并同时向其发送校正数据。

To be more specific, I use RTKLIBs ( http://www.rtklib.com/ ) str2str tool for sending NTRIP/RTCM correction data to the GNSS receiver in order to get better position estimations using DGNSS/RTK. 更具体地说,我使用RTKLIB( http://www.rtklib.com/str2str工具将NTRIP / RTCM校正数据发送到GNSS接收器,以便使用DGNSS / RTK获得更好的位置估计。 The receiver's output data will be logged by a python script which is based on the GPS deamon (gpsd). 接收器的输出数据将由基于GPS守护进程(gpsd)的python脚本记录。

However, I guess the main issue is related to the serial port control. 但是,我想主要问题与串行端口控制有关。 When I run the writing process (str2str) first and afterwards any reading process (my python script/gpsd frontends (eg cgps) /cat) at the same time, the reading process will output data for a few seconds and freeze then. 当我先运行写入过程(str2str),然后同时运行任何读取过程(我的python脚本/ gpsd前端(例如cgps)/ cat)时,读取过程将输出数据几秒钟,然后冻结。 It doesn't matter which tool I use for reading the data. 我使用哪种工具读取数据都没有关系。

I found this question: https://superuser.com/questions/488908/sharing-a-serial-port-between-two-processes . 我发现了这个问题: https : //superuser.com/questions/488908/sharing-a-serial-port-between-two-processes Therefore I made sure that the processes got rw access to the device and even tried running them as superuser. 因此,我确保进程可以读写访问该设备,甚至尝试以超级用户身份运行它们。 Furthermore I stumbled upon socat and virtual serial ports, but didn't find any use for it. 此外,我偶然发现了socat和虚拟串行端口,但没有发现任何用处。 ( Virtual Serial Port for Linux ) 适用于Linux的虚拟串行端口

Is there any way to read data from a serial port with one process and write to it with another? 有什么方法可以通过一个进程从串行端口读取数据,然后通过另一个进程写入数据吗? The only solution I know of right now might be to rewrite the read and write process in python using pySerial. 我现在知道的唯一解决方案可能是使用pySerial在python中重写读写过程。 This would allow to only have one process accessing the serial device, but would mean plenty of work. 这将只允许一个进程访问串行设备,但是这意味着大量的工作。

Finally I found a soultion using a construction somehow similar to this: https://serverfault.com/questions/453032/socat-to-share-a-serial-link-between-multiple-processes 最终,我发现了一个使用类似于此的构造的灵魂: https : //serverfault.com/questions/453032/socat-to-share-a-serial-link-between-multiple-processes

A first socat instance (A) gets GNSS correction data from a TCP connection, which is piped to socat B. Socat B manages the connection to the serial device and pipes output data to another socat instance C, which allows other processes such as gpsd to connect and get the receiver's output from TCP port. 第一个socat实例(A)从TCP连接获取GNSS校正数据,该连接通过管道传输到socatB。SocatB管理与串行设备的连接,并将输出数据传输到另一个socat实例C,这允许其他进程(例如gpsd)执行以下操作:连接并从TCP端口获取接收器的输出。

In total, this looks like: 总的来说,这看起来像:

socat -d -d -d -u -lpA   TCP4:127.0.0.1:10030 - 2>>log.txt | 
socat -d -d -d -t3 -lpB  - /dev/gps,raw  2>>log.txt|
socat -d -d -d -u -lpC   - TCP4-LISTEN:10031,forever,reuseaddr,fork 2>>log.txt

With only one process managing the serial connection, it doesn't block anymore. 只需要一个进程来管理串行连接,它就不再阻塞。

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

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