6

I'm trying to read an continuous stream of data from my device (laser altimeter). The device is connected to the UART Rx pin of raspberry Pi 3B+. Everything works fine with this python code:

import time
import serial

try:
    ser = serial.Serial(
        port='/dev/serial0',
        baudrate = 115200,
        parity=serial.PARITY_NONE,
        stopbits=serial.STOPBITS_ONE,
        bytesize=serial.EIGHTBITS,
        timeout=1
        )

except IOError:
    print ("Failed at setting port\n") 

while 1:
   x=ser.readline()
   print(x)

But after random amount of succesful reads, I get this error:

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/serial/serialposix.py", line 490, 
in read
    'device reports readiness to read but returned no data '
serial.serialutil.SerialException: device reports readiness to read but 
returned no data (device disconnected or multiple access on port?)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/pi/readSerial.py", line 25, in <module>
  x=ser.readline()
 File "/usr/lib/python3/dist-packages/serial/serialposix.py", line 497,    
 in read
   raise SerialException('read failed: {}'.format(e))
   serial.serialutil.SerialException: read failed: device reports 
   readiness to read but returned no data (device disconnected or 
   multiple access on port?)

When I re-run the same code everything works again, for a short period of time (randomly from 1 to 30s). I need to read data from the sensor for few hours. Can someone help me understanding the problem or find some workaround? I'm totally lost. Thanks.

FrantišekV
  • 63
  • 1
  • 4

1 Answers1

4

Check if you have disabled the login prompt on /dev/serial0. What you see may be due to two processes (your script and getty) reading from the same port.

Login prompt on serial port can be disabled by running sudo raspi-config, navigating to "Advanced", "Serial" and answering "No" to the question about the login shell.

Alternatively, get a cheap USB-to-UART dongle and connect your device to it.

Dmitry Grigoryev
  • 28,277
  • 6
  • 54
  • 147