Search code examples
pythonserial-portpyserial

How to print hex only buffer with pyserial and picocom?


Im trying to read a protocol through the USB/Serial port[ttyACM*/COM*]. I was supposed to receive a frame with the following pattern when the device returns the response:

| FF | 15 | 44 | 7D | 00 | 88 | 00 | 0D | 00 | 00 | 86 | 00 | 76 | 00 | 00 | 00 | 00 | 40 | 00 | A7 | FE | (21 hexbytes)

The configuration which i must use is the following:

  • Baudrate: 2400
  • Data bits: 8
  • Parity: None
  • Stop bits: 1
  • Handshaking: None

Now when i try to read this using CuteCom on Linux or HTerm on Windows everything works fine and i get the frame exactly how i need it when i show it as hex.

HTerm:

On HTerm/Windows i get the frames

Frame Response: FF 15 44 7C 00 88 00 00 7B 00 7C 00 73 00 1F 00 00 40 00 26 FE (21 hexbytes)

CuteCom:

On CuteCom/Linux i also get the frames

Frame Response: ff 15 44 00 00 78 00 00 00 00 01 00 79 00 18 00 00 8d 00 f0 fe (21 hexbytes)

Now why is it that when i try to read the serial using picocom, i can't get the data that i need?

Picocom: picocom -b 2400 -r -l --omap crcrlf --imap 8bithex -f n /dev/ttyACM0

Can't get the frame on picocom

Frame Response: ff D 82 88 81 82 y@> fe (6 hexbytes?)

And what's up with the weird characters in the middle of the frame?

But more importantly when trying to receive the frame in python, which i'll be using to parse the package i also can't get the frame on the same format as cutecom/hterm:

Python [Code]:

ser = serial.Serial("/dev/ttyACM0",2400,timeout=0.3, bytesize=8, parity='N',rtscts=0)
ser.close()
ser.open()
ser.flush()
ser.write(b'\xff\x09\x53\x83\x00\x00\x00\xdf\xfe')

while True:
    s = ser.read_until(b'\xfe')
    ser.flush()
    print(s)
    time.sleep(3.0)

Python:

Can't get the frame on python either

Frame Response: b'\xff\x15D\x82\x00\x88\x00\x00\x81\x00\x82\x00y\x00\x1f\x00\x00@\x00>\xfe' (16 hexbytes?)

Here i get some hex numbers of different sizes and weird characters in the middle of the frame. EDIT: (i realized the "weird chars" are actually the ASCII chars based on the hex code it's received, how can i change this buffer to be hex only?)

Any idea why this is happening with Python and Picocom and how should i read the frame when using these tools?

EDIT: I tried using ssterm to read from the serial using hex and it worked, why? How can i make it work with picocom and python using pyserial?

ssterm:

communication works with ssterm

Frame Response: ff 15 44 82 00 88 00 00 81 00 82 00 79 00 1f 00 00 40 00 3e fe (21 hexbytes)


Solution

  • Turns out python is just conveniently changing the hex characters to ASCII. But the value stays the same so the frame can be used normally. While picocom is ignoring the null characters.