Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 8041

Automation, sensing and robotics • Re: Reading JSON from Arduino via Serial Missing Parts of Message

$
0
0
For debugging, perhaps shut down your application and use "minicom". Many tutorials. e.g. https://wiki.emacinc.com/wiki/Getting_S ... th_Minicom
Manually type 'd' and look for what arrives from the arduino.

Next, there is an "# empty input buffer before starting processing" loop which reads data from the serial line. For debug purpose, print out the data arriving from readString

Code:

def getSerialArduinoData():    data = ""    # empty input buffer before starting processing    while (serial.available() > 0):        discarded = serial.readString()        print( f"discarded data '{discarded }'")
Background: serial data are a byte stream and have no message concept. In linux, the data sometimes arrive in one piece, but frequently in arbitrary parts 'chunks'. It is up to the application to collect as much data to have complete message. A simple way to recognize messages is the trailing '\n' which you send from the arduino.
Many use pyserial readLine(), but see https://pyserial.readthedocs.io/en/late ... l#readline
Printing out the discarded data as proposed above should help to identify if you receive data in chunks. I would expect that the missing parts will show up there.

Possible solution: check if your incoming data are terminated by a \n and eventually repeat reading until this condition is fulfilled. Do some timeout handling. Or go back to to pyserial and use readLine with appropriate timeout.

Statistics: Posted by ghp — Fri Jul 04, 2025 4:47 am



Viewing all articles
Browse latest Browse all 8041

Trending Articles