Python Forum
Errors in serial data - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Networking (https://python-forum.io/forum-12.html)
+--- Thread: Errors in serial data (/thread-34604.html)



Errors in serial data - Joni_Engr - Aug-13-2021

Hi,

I get errors in serial data in my python program but when I use PuTTY I get clean data without errors. I am not sure if I am writing the serial routine properly or I am missing something that introduce errors.

# Serial port config 
import serial
ser = serial.Serial("COM7", 9600)

# Send character 'A' to start the program
ser.write(bytearray('A','ascii'))

# Read line  
while True:
    bs = ser.readline()
    bs = bs.replace(b'\n', b' ').replace(b'\r', b' ')
    print(bs)

# Save data in the simple text file 
    f = open("readme.txt", "a")
    f.write( "\n\n")
    f.write(str(bs))
    f.close() 



RE: Errors in serial data - j.crater - Aug-19-2021

Hi, please post full error traceback message in error tags. It's hard to figure out what could be the issue without seeing that.


RE: Errors in serial data - Joni_Engr - Aug-20-2021

The received data has errors. Is there anything missing in the python code ? The data comes in a string every 200 ms and then 800 ms. I am not sure if traceback should have anything.

The string I should receive "Received Data is 0x88844422" but actually I get errors in the string sometime one letter is missing or corrupted and the other time the other one. This is communication error in the received string. If I use PuTTY or TeraTerm I don't see the communication error.


RE: Errors in serial data - j.crater - Aug-22-2021

Ah I understand what you mean now.

I am not able to figure what could be wrong with the code. But checking this SO Q&A got me thinking, parity or stopbits settings may need to be provided at the receiving end to match the sending end.
It's been a long time since I played with serial. But I remember, sometimes I got results almost effortlessly (just modifying example codes). And sometimes I struggled a lot, thanks to some "minor" detail that made all the difference.


RE: Errors in serial data - Bicarbonaat - Dec-13-2021

i had the same problems.

but in my case it was not a software issue , but a hardware issue.

i used a aliexpress serial -usb convertor ,
and when i repaired the hardware with genuine parts ( eg max232 or ftdi )
then my serial data was ok

otherwise , add delays , cos the timing of serial (baudrate ) is rather slow , compared with a python script