0

I am trying to create a communication between an STM32 and a laptop.

I am trying to receive data from the serial, sent thanks to an STM32. Actual code that I am sending is 0x08 0x09 0x0A 0x0B

I checked on the oscilloscope and I am indeed sending the correct values in the correct order.

What I receive is actually :

b'\n\x0b\x08\t'

I assume that Python is not reading an input that is greater than a 3 bit size, but can not figure out why

Please find my code below :

import serial
ser = serial.Serial('COM3', 115200, bytesize=8)
while 1 :
    if(ser.inWaiting() != 0) :
        print(ser.read(4))

If someone could help, it would be nice ! :)

Shunya
  • 2,344
  • 4
  • 16
  • 28

2 Answers2

0

check your uart rate, keep the python serial rate the same for stm32

0

What comes to my mind when looking at pySerial library is that while You initialize Your COM port:

  • You are not providing read timeout parameter.
  • You are awaiting 4 bytes of data from serial port.

The problem with such approach is that Your code will wait forever until it gets these 4 bytes and if You are sending the same array from STM32 for me it looks like You received 0x0A,0x0B from older packet and 0x08,0x09 from newer packet, so python code printed these 4 bytes and in fact received into buffer also 0x0A,0x0B of newer packet but it waits until it will receive 2 more bytes before it will be allowed to return with data to print.

Putting here a timeout on read and limiting read argument to single byte might solve Your problem. Also for further development if You would like to create regular communication between microcontroller and computer I would suggest to move these received bytes into separate buffer and recognize single packets in separate thread parser. In Python it will be painful to create more complex serial communication as even with separate thread it will be quite slow.

WojciechS
  • 126
  • 4