Python Forum
Reading mixed ASCII/binary serial input
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Reading mixed ASCII/binary serial input
#1
I am trying to read serial data from a device that outputs in a mix of ASCII and binary, using Python 3.

The message format is: "$PASHR,<msg type>,<binary payload>,<checksum>,\r\n" (minus the quotes)

To make it more interesting, there are several different message types, and they have different payload lengths, so I can't just read X bytes (I can infer the payload length based on the message type). The sequence of five messages (one of each type) is sent every 20 seconds, at 115200 baud.

I haven't been able to read this with serial.readline(), probably because of newlines embedded in the payload.

I think that if I could set the line-end character to "$PASH" that would give me a way to frame the messages -- ie, everything between one $PASH and the next is one message. But I haven't succeeded in setting serial.newline to that value.

Is there a way to set the newline to that multi-character string, or is there a different/better approach I should use?

Thanks!
Reply
#2
I think I figured it out. This seems to work with a timeout of 1 second:

    self.serial.timeout = 1
    delimiter = b'$PASHR'
    if self.serial.in_waiting:
        message = self.serial.read_until(delimiter)
        if len(message) > 6: # ignore carryover b'$PASHR'
            ***process message***
When I looked carefully at the variable length returns, I discovered that the message sequence after the first incomplete cycle was:

b'$PASHR' (6 bytes)
b',MPC,<data><checksum>\r\n$PASHR' (108 bytes)
...
b',MPC,<data><checksum>\r\n' (102 bytes)
(delay)
b'$PASHR' (6 bytes)

The delimiter from the last message in the sequence is chopped off and output at the beginning of the next sequence. I can deal with that.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  PC why serial code = converting to ascii ? trix 11 2,302 Oct-12-2024, 11:10 PM
Last Post: deanhystad
  pyserial/serial "has no attribute 'Serial' " gowb0w 11 21,884 Sep-27-2024, 12:18 PM
Last Post: NigelHalse
  Reading an ASCII text file and parsing data... oradba4u 2 1,403 Jun-08-2024, 12:41 AM
Last Post: oradba4u
  Waiting for input from serial port, then move on KenHorse 3 4,128 Apr-17-2024, 07:21 AM
Last Post: DeaD_EyE
Star Pyserial not reading serial.readline fast enough while using AccelStepper on Arduino MartyTinker 4 8,421 Mar-13-2023, 04:02 PM
Last Post: deanhystad
  Reading an Input File DaveG 1 1,837 Mar-27-2022, 02:08 AM
Last Post: deanhystad
  serial input/output barryjo 3 4,980 Dec-27-2021, 11:57 PM
Last Post: barryjo
  Help reading data from serial RS485 korenron 8 19,191 Nov-14-2021, 06:49 AM
Last Post: korenron
  Group List Elements according to the Input with the order of binary combination quest_ 19 9,174 Jan-28-2021, 03:36 AM
Last Post: bowlofred
  Reading Serial data Moris526 6 7,292 Dec-26-2020, 04:04 PM
Last Post: Moris526

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020