Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Reading Serial data
#1
Hi.

Im sending data (a 3 digit number and a letter like ¨423 A¨) from PC to Raspberry zero via serial through USB gadget mode.

Im using this code

import serial
ser= serial.Serial('/dev/ttyGS0', 9600)

while (True):
    
    if (ser.inWaiting()>0): 
        data_str = ser.read(ser.inWaiting())
        print(data_str)
I can send ten or twenty messages and then a get the error


Error:
Tracebak (most recent call last): File "the.py", line 12, in <module> if (ser.inWaiting()>0): File "/usr/local/lib/python3.7/dist-packages/serial/serialutil.py", line 594, in inWaiting return self.in_waiting File "/usr/local/lib/python3.7/dist-packages/serial/serialposix.py", line 549, in in_waiting s = fcntl.ioctl (self.fd, TIOCINQ, TIOCM_zero_str) OSError: [Errno 5] input/output error
Any idea?
Reply
#2
perhaps the pi leaves the serial line down for short periods of time?.

I might suggest just wrapping the block in try/except and retrying. If that works okay, probably best you can do.

try:
    if (ser.inWaiting()>0): 
        data_str = ser.read(ser.inWaiting())
        print(data_str)
except OSError:
    # ignore or log...  Let the loop retry.
    pass
        
Reply
#3
I did that

The error does not appear, the program doesnt stop working, but it stops receiving the data
Reply
#4
Hmm. I haven't worked with the serial stuff much. I see some notes around that suggest when it gets in that state to destroy the object and re-open the serial port. That sounds annoying, but I'm not sure how to investigate what the reason is for the failure.

Can you see the incoming data with a regular serial terminal?
Reply
#5
Sorry

What is serial regular terminal?
Reply
#6
Sorry, I meant a serial terminal emulator. something else that can read the data from the serial line. Maybe TeraTerm or Putty on windows. Minicom or putty on linux. Something that would be able to give an indication that the serial hardware and the signal from the other device is working properly.
Reply
#7
(Dec-17-2020, 02:56 AM)bowlofred Wrote: Sorry, I meant a serial terminal emulator. something else that can read the data from the serial line. Maybe TeraTerm or Putty on windows. Minicom or putty on linux. Something that would be able to give an indication that the serial hardware and the signal from the other device is working properly.

Thank you
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help reading data from serial RS485 korenron 8 676 Nov-14-2021, 06:49 AM
Last Post: korenron
  Help with WebSocket reading data from anoter function korenron 0 384 Sep-19-2021, 11:08 AM
Last Post: korenron
  Fastest Way of Writing/Reading Data JamesA 1 769 Jul-27-2021, 03:52 PM
Last Post: Larz60+
  Reading data to python: turn into list or dataframe hhchenfx 2 1,149 Jun-01-2021, 10:28 AM
Last Post: Larz60+
  Reading data from mysql. stsxbel 2 910 May-23-2021, 06:56 PM
Last Post: stsxbel
  reading canbus data as hex korenron 9 2,311 Dec-30-2020, 01:52 PM
Last Post: korenron
  wrong data reading on uart fahri 6 1,467 Sep-29-2020, 03:07 PM
Last Post: Larz60+
  Taking serial data to perform key.press functions ausbollinger13 1 1,056 Sep-04-2020, 10:26 PM
Last Post: bowlofred
  Put Serial data to list PA3040 5 2,459 Sep-01-2020, 02:34 PM
Last Post: deanhystad
  Reading serial data and saving to a file Mohan 1 3,430 May-25-2020, 04:18 PM
Last Post: pyzyx3qwerty

Forum Jump:

User Panel Messages

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