4

I am trying to use socat to impersonate an external serial device. If I do this in one terminal:

sudo socat -ddd -ddd PTY,raw,link=/dev/ttyS32,echo=0 READLINE

and run this simplified version of my Python serial interface client in another:

import serial
s = serial.Serial('/dev/ttyS32',baudrate=9600,timeout=1)
while True:
   s.write("query\n")
   resp = s.read()
   if resp: print(resp)

It works as expected: the READLINE socat terminal prints a recieved 'query' every second, and anything I type there is sent to and and printed by the client.

Now I have a Python program which emulates the serial device:

#echo.py
n=0
while True:
    s = raw_input()
    if 'query' in s:
        print n
    n+=1

This works as intended when I run it from the command line: when I type 'query', it prints a counter.

What doesn't work is when I try to link the two sides together with this commmand:

sudo socat -ddd -ddd PTY,raw,link=/dev/ttyS32,echo=0 EXEC:"python echo.py"

In this case, neither the Python client nor the socat window show any data.
What am I doing wrong? How do I link 'echo.py's stdin and stdout to the pesudo tty so that I can read/write to it like a serial device?

(Although I am using Python in this example, I have the same problem with an emulator written in C)

1 Answer 1

4
+50

Try this:

sudo socat -ddd -ddd PTY,raw,echo=0 "EXEC:'python /tmp/echo.py',pty,raw,echo=0"

(for some reason it didn't like me specifying the link= but the above provided /dev/pts/6 so I ran:

$ sudo python pyserial1.py
4
1
1
8
...

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .