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
  pyserial/serial "has no attribute 'Serial' " gowb0w 9 3,308 Aug-24-2023, 07:56 AM
Last Post: gowb0w
Star Pyserial not reading serial.readline fast enough while using AccelStepper on Arduino MartyTinker 4 3,859 Mar-13-2023, 04:02 PM
Last Post: deanhystad
  Reading All The RAW Data Inside a PDF NBAComputerMan 4 1,274 Nov-30-2022, 10:54 PM
Last Post: Larz60+
  Reading Data from JSON tpolim008 2 1,031 Sep-27-2022, 06:34 PM
Last Post: Larz60+
  Help reading data from serial RS485 korenron 8 13,594 Nov-14-2021, 06:49 AM
Last Post: korenron
  Help with WebSocket reading data from anoter function korenron 0 1,300 Sep-19-2021, 11:08 AM
Last Post: korenron
  Fastest Way of Writing/Reading Data JamesA 1 2,138 Jul-27-2021, 03:52 PM
Last Post: Larz60+
  Reading data to python: turn into list or dataframe hhchenfx 2 5,273 Jun-01-2021, 10:28 AM
Last Post: Larz60+
  Reading data from mysql. stsxbel 2 2,162 May-23-2021, 06:56 PM
Last Post: stsxbel
  reading canbus data as hex korenron 9 6,155 Dec-30-2020, 01:52 PM
Last Post: korenron

Forum Jump:

User Panel Messages

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