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.

1 Answer 1

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.

2
  • 2
    I did disable it through raspi-condig, but I verified it again and the result was the same. However then I disabled it with: <sudo systemctl stop [email protected]> and <sudo systemctl disable [email protected]> and now everything works perfectly. Thank you, I would never have imagine that it's such a silly thing. Oct 16, 2018 at 12:20
  • @fvilagi Yeah I'm also surprised that raspi-config is so buggy. Oct 16, 2018 at 12:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.