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
  Waiting for input from serial port, then move on KenHorse 3 1,002 Apr-17-2024, 07:21 AM
Last Post: DeaD_EyE
  pyserial/serial "has no attribute 'Serial' " gowb0w 9 3,892 Aug-24-2023, 07:56 AM
Last Post: gowb0w
Star Pyserial not reading serial.readline fast enough while using AccelStepper on Arduino MartyTinker 4 4,052 Mar-13-2023, 04:02 PM
Last Post: deanhystad
  Reading an Input File DaveG 1 1,247 Mar-27-2022, 02:08 AM
Last Post: deanhystad
  serial input/output barryjo 3 2,477 Dec-27-2021, 11:57 PM
Last Post: barryjo
  Help reading data from serial RS485 korenron 8 13,925 Nov-14-2021, 06:49 AM
Last Post: korenron
  Group List Elements according to the Input with the order of binary combination quest_ 19 6,427 Jan-28-2021, 03:36 AM
Last Post: bowlofred
  Reading Serial data Moris526 6 5,377 Dec-26-2020, 04:04 PM
Last Post: Moris526
  Reading serial data and saving to a file Mohan 1 7,531 May-25-2020, 04:18 PM
Last Post: pyzyx3qwerty
  List of mixed data types to bytes medatib531 1 2,333 Mar-16-2020, 11:53 AM
Last Post: scidam

Forum Jump:

User Panel Messages

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