Search code examples
pythonbinarypyserial

Reading serial interface with Python returns distorted data


I am trying to read binary serial port using Python. Each source message consists of 3 floating point values, 12 bytes in total. Using the Python code shown below, I was expecting to get a 12-byte bytearray message. However, running the code I found out it is not always 12 bytes and sometimes it is gibberish. What am I doing wrong here or why is it so?

Pyhon code:

#!/usr/bin/env python
import serial
import time

serialPort = 'COM3'
serialBaud = 921600
dataNumBytes = 4
numData = 3
rawData = bytearray(numData * dataNumBytes)

# Connect to serial port
print('Trying to connect to ' + str(serialPort) +
      ' at ' + str(serialBaud) + ' BAUD.')
try:
    s = serial.Serial(serialPort, serialBaud, timeout=4)
    print('Connected!')
except:
    print("Failed to connect with " + str(serialPort) +
          ' at ' + str(serialBaud) + ' BAUD.')

s.reset_input_buffer()  # flush input buffer
while (True):
    time.sleep(0.1)
    s.readinto(rawData)
    print(rawData)

Terminal output:

bytearray(b'r u?\xb78\x0c\xbe\x1dN\x82>')
bytearray(b'@cu?\xb78\x0c\xbe0\xa7\x82>')
bytearray(b'\xca\x8fu?\x03\x9d\r\xbeno\x81>')
bytearray(b'@cu?\xb78\x0c\xbeno\x81>')
bytearray(b'\x0e\xa6u?\xb78\x0c\xbe\n\xf5\x81>')
bytearray(b'\x0e\xa6u?\xca\x91\x0c\xbe\n\xf5\x81>')
bytearray(b'\x98\xd2u?\xf0C\r\xbe\x81\xc8\x81>')
bytearray(b'\xca\x8fu?\x03\x9d\r\xbe0\xa7\x82>')
bytearray(b'\xca\x8fu?\xb78\x0c\xbe\xb9\xd3\x82>')
bytearray(b'@cu?\xb78\x0c\xbe\x1dN\x82>')
bytearray(b'@cu?\xb78\x0c\xbe\x1dN\x82>')
bytearray(b'\xfcLu?\xdd\xea\x0c\xbe\x94!\x82>')

Solution

  • All right. Apparently, the data output is correct and everything works fine. My understanding of 12-byte data is wrong. Here is an example to read the first line of the example shown above:

    >>> from struct import *
    >>> data = bytearray(b'r u?\xb78\x0c\xbe\x1dN\x82>')    # define 12-byte data
    >>> unpack('f', data[0:4])                              # unpack 1st floating value
    (0.9575263261795044,)
    >>> unpack('f', data[4:8])                              # unpack 2nd floating value
    (-0.13693509995937347,)
    >>> unpack('f', data[8:12])                             # unpack 3rd floating value
    (0.25450220704078674,)