简体   繁体   中英

Two processes listening to same serial port on linux

I have a serial port /dev/ttyS0 that keeps transmitting data.

I already have a running process P1 that listens to the serial port and processes the data. That process cannot be customized to listen to another serial port.

I am building a python script to only listen to the same data and store the output. If I directly connect to the serial port then P1 dies not get any data as my script has already read it.

How can I sniff the data without interrupting the transmission to P1?

Someone mentioned that o could use named pipes but I am not sure how.

Thanks to the link Marcos G. provided I looked into socat command and ended up with using https://github.com/danielinux/ttybus as descried in use case 1. It seems to be intended to resolve certain scenarios that can be resolved with socat but in a more straightforward way.

Use case 1

Multiplexing serial input only or output only device attached to /dev/ttyS0, for use with multiple applications.

1. Create a new tty_bus called /tmp/ttyS0mux:

tty_bus -d -s /tmp/ttyS0mux

2. Connect the real device to the bus using tty_attach:

tty_attach -d -s /tmp/ttyS0mux /dev/ttyS0

3. Create two fake /dev/ttyS0 devices, attached to the bus:

tty_fake -d -s /tmp/ttyS0mux /dev/ttyS0fake0

tty_fake -d -s /tmp/ttyS0mux /dev/ttyS0fake1

4. Start your application and force it to use the new serial device for input or output

/bin/foo /dev/ttyS0fake0 &

/bin/bar /dev/ttyS0fake1 &

Both application will read (or write) from the same serial device.

CAUTION: All data written on each of the two fake devices will be echoed on the other one too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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