Search code examples
pythonpyserialsocat

Problem sending Bytes with pySerial and socat


I want to send some bytes via pySerial. I created virtual serial ports with socat for testing purposes:

socat PTY,link=./ptyp1,b9600 PTY,link=./ptyp2,b9600

Here's the python code:

ser = serial.Serial('./ptyp1')
x = struct.pack('B',2)
print binascii.hexlify(x) # 02
ser.write(x)
y = ser.read(2)
print binascii.hexlify(y) # 5e42

The ouput I get:

02   # x
5e42 # y

The output I expect:

02 # x
02 # y

What am I doing wrong here? Is it socat or python?

Edit:

I just noticed some other strange behavior for different x values. Here the ouput:

x = 12  => y = 5E 52 0D 0A 5E 50
x = 100 => y = 100 # why does it work here?

Solution:

The problem was that I read on the same port I wrote to. If I get it right socat "connects" the two ports as "in" and "out". So I have to read on ./ptyp2 if I write to ./ptyp1. After that, everything is fine.


Solution

  • The problem was that I read on the same port I wrote to. If I get it right socat "connects" the two ports as "in" and "out". So I have to read on ./ptyp2 if I write to ./ptyp1. After that, everything is fine.