Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Errors in serial data
#1
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() 
Reply
#2
Hi, please post full error traceback message in error tags. It's hard to figure out what could be the issue without seeing that.
Reply
#3
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.
Reply
#4
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.
Reply
#5
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Serial toggles serial, but no data Saltwater 2 1,298 Sep-08-2019, 12:20 PM
Last Post: Saltwater

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020