Python - Serial Communication with Raw Byte Transfer

36 Views Asked by At

I'm trying to setup a custom Python GUI to interface with an MCU. A simple UART protocoll shall be used for communication where 8 Bytes per Message are used. The first 4 bytes are the message id, the last 4 bytes are the message content.

The protocoll is implemented on the mcu and the communication works fine with hterm. However I've some odd problem with the Serial lib in Python. Here is a simple example:

COM_Port = "COM5"
Baudrate = 19200
SerialPort = serial.Serial(COM_Port,Baudrate,bytesize=8,parity=serial.PARITY_NONE,stopbits=1,timeout=None)
while True:
    bytestream = SerialPort.read(8)#read 8 bytes
    stream = str(bytestream).strip('b')#remove
    print(stream) # just print data
    #queue_in.put(bytestream)# later on data shell be processed outside

If i now send the MSG: {10,1,0,0,0,50,0,0} Hterm shows: {10,1,0,0,0,50,0,0} - which is correct Python terminal shows a string: '\n\x01\x00\x00\x002\x00\x00' - not correct (The first byte is \n which is 10 okay) The fifth byte is very strange with 0x002?

However if is send the MSG: {255,255,255,255,255,255,255,255} Hterm shows: {255,255,255,255,255,255,255,255} - which is correct Python terminal shows a string: '\xff\xff\xff\xff\xff\xff\xff\xff' - which is correct

My guess is that this issue is related to the read() function which does some internal conversion? Has somebody seen this behaviour before or can give me a clue of what is going on here?

0

There are 0 best solutions below