2

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.

  • This is an old one and there are already many answers. See if you find [this one](https://stackoverflow.com/questions/58909815/how-do-you-open-ftdi-serial-ports-ttyusbx-such-that-we-can-run-2-applications-on) useful. I don't think you will be able to do it with python out of the box or easily. – Marcos G. Apr 05 '21 at 12:49
  • @markos-g I tried running the command `sudo socat -d -d pty,link=/dev/ttyS0,raw,echo=0 pty,raw,echo=0,link=/home/panos/ttyPort1` but even though the virtual post is created, my script does not even connect to it as it reports `Permission denied /home/panos/ttyPort1` – Panagiotis Dimitrakis Apr 05 '21 at 14:35
  • You probably need to run as su or add your user to the dialout group – Marcos G. Apr 05 '21 at 14:53
  • @markos-g I rebooted and stopped the process listening to the actual serial port before running the same socat command and then changed the privileges as the ports where now in "root" group instead of dialout. Now the new process runs but does not get any data from the virtual serial port (even with the main process terminated). – Panagiotis Dimitrakis Apr 05 '21 at 20:17
  • sorry I don't get what is wrong, maybe you can edit your question to include what you did with socat and what is wrong? Try to reproduce the steps on the answer I linked above and I'll take a look. – Marcos G. Apr 06 '21 at 09:08
  • @markos-g As a first try I tried to just forward the actual serial port to a new virtual one but the virrual one never send out any data when I connected to it. While looking into it, I came across a mention of ttybus that has a use case to do exactly what I was trying. Thanks a lot, you really helped me on this one. – Panagiotis Dimitrakis Apr 07 '21 at 19:55

1 Answers1

5

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.