简体   繁体   中英

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:

在 HTerm/Windows 上我得到了框架

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:

在 CuteCom/Linux 上我也得到了框架

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 -fn /dev/ttyACM0

无法在 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:

也无法在python上获取框架

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:

与 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)

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.

Picocom man page is little bit confusing.
--imap 8bithex map to hex only characters 128-255, not all

You shall use this:
picocom --imap spchex,tabhex,crhex,lfhex,8bithex,nrmhex

This will map all posible characters to hex transcription.

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