Python SerialException: Device reports readiness to read but returned no data (device disconnected?)
Asked Answered
E

5

18

I've got two Raspberry Pi's sending data to each other using the Serial Port and a pair of XRF Radio's. Generally they work fine and the full program loops multiple times but every once in a while one of them stops the program with a error along the lines of:

File "BaseListener.py, line 56, in <module>
recieved=serialport.read()
File "/usr/lib/python2.7/dist-packages.serial/serialposix.py", line 465, in read raise SerialException('Device reports readiness to read but returned no data (device disconnected?)')
serial.serialutil.SerialException: device reports readiness to read but returned no data (device disconnected?)

My Code is:

import sys
import serial
import time
import datetime

date = datetime.date.today()
strdate = str(date)
serialport=serial.Serial("/dev/ttyAMA0", 9600, timeout=0.25)
command=''
loop=0
recieving=False
recieving2=False
format = "%Y-%m-%d %H:%M:%S"

while True:
    while (recieving==False):
        loop = 0
        command=''
        while (loop<30):
            recieved = serialport.read()
            command = command + recieved
            loop = loop+1
        if "DR" in command:
            print"DR Recieved"
            serialport.write("BSAKAKBS")
            recieving=True
    while (recieving ==True):
        loop = 0
        command=''
        while (loop<30):
            recieved = serialport.read()
            command = command + recieved
            loop = loop+1
        sensorid = command[0:2]
        print ("Command: "+command)
        print ("SensorID: "+sensorid)
        graintemp = command[2:6]
        print "GrainTemp Recieved"
        serialport.write("BS"+graintemp+"BS")
        print (str(graintemp))
        loop = 0
        command=''
        while (loop<30):
            recieved = serialport.read()
            command = command + recieved
            loop = loop+1
        if sensorid in command:
            if "AK" in command:
                print "GrainTemp AK recieved"
                serialport.write("BSAKAKBS")
                recieving2=True
                while (recieving2==True):
                    loop=0
                    command=''
                    while (loop<30):
                        recieved = serialport.read()
                        command = command + recieved
                        loop = loop+1
                    print ("Command: "+command)
                    airtemp = command[2:6]
                    print "AirTemp Signal Recieved"
                    serialport.write("BS"+airtemp+"BS")
                    print ("AirTemp: "+str(airtemp))
                    loop = 0
                    command=''
                    while (loop<30):
                        recieved = serialport.read()
                        command = command + recieved
                        loop = loop+1
                    if sensorid in command: 
                        if "AK" in command:
                            print ("AK command: ")
                            print "AirTemp AK Recieved"
                            serialport.write("BSAKAKBS")
                            #File Storage
                            today = datetime.datetime.today()
                            fulltime = today.strftime(format)
                            strtime = str(fulltime)
                            graindata = fulltime + ' ' + graintemp +'\n'
                            airdata = fulltime + ' ' + airtemp +'\n'
                            file = open(sensorid+"Graindata.dat", "a")
                            file.write(graindata)
                            file.close
                            file = open(sensorid+"Airdata.dat", "a")
                            file.write(airdata)
                            file.close
                            recieving=False
                            recieving2=False
                            loop=0
                            command=''
                            graindata=''
                            airdata=''
                            graintemp=0
                            airtemp=0
                            print "Files stored. Restarting"
                        else:
                            print ("IC Command: ")
                            print "Airtemp IC Recieved"
                            serialport.write("BSICICBS")
                            loop = 0
                            command=''
                    else:
                        print "Airtemp ID IC Recieved"
                        serialport.write("BSICICBS")
                        loop = 0
                        command=''
            else:
                serialport.write("BSICICBS")
                print "Graintemp IC Recieved"
                loop = 0
                command=''
        else:
            serialport.write("BSICICBS")
            print "Graintemp ID IC Recieved"
            loop = 0
            command=''

The code on the other Pi is similar (I can provide if needed).

From what I've found online, it's some issue to do with trying to read the serial port but it being empty. I've seen suggestions to use a try and catch exception but im not sure that will help (or know how to do that really). I need the code to run continuously without any interference at all from a user. If the serial port is empty then the AK and IC loops should pick it up the same as an incorrect transmission so I just need it to pass the empty value on. Is there any way to do this?

Empathize answered 5/2, 2015 at 12:26 Comment(5)
possible duplicate of Serial Receiving from Arduino to Raspberry Pi with PySerial stops after a whileChitter
It's a similar issue but I'm not sure the answers resolve my problem as I'm not using Arduino.Empathize
Please scroll down to the second answer in the link above. Further more, the problem in that post was on the receivers site and that was the PiChitter
I already have getty disabled.Empathize
Check out #42145199Hinkley
S
10

Use this command:

sudo systemctl stop [email protected]
Secretarial answered 4/2, 2020 at 8:8 Comment(1)
I had the same issue and this command somehow worked for me +1Abdominal
A
5

This worked for me.

From official RPI documenation:

"To manually change the settings, edit the kernel command line with sudo nano /boot/firmware/cmdline.txt. Find the console entry that refers to the serial0 device, and remove it, including the baud rate setting. It will look something like console=serial0,115200. Make sure the rest of the line remains the same, as errors in this configuration can stop the Raspberry Pi from booting."

Just this line needed to be edited.

Here is link https://www.raspberrypi.org/documentation/configuration/uart.md

Accalia answered 28/4, 2020 at 10:53 Comment(1)
Note from 2024: this file has been moved to /boot/firmware/cmdline.txtCrawfish
D
0

While not a solution to this problem, I was having the same problem on a raspberry Pi3B+ and tried the following:

  • run raspi-config and under interfaces turn on serial, turn off console
  • Edit /boot/config.txt and add the line enable_uart=1
  • In /boot/cmdline.txt remove the references to console
  • Disabled getty
  • Verify that it is not a power issue by powering the board from a 5V 3A supply

After all that I still couldn't get it to work. I ended up just using an FT232 serial to usb converter and using /dev/ttyUSB0 as my port. Worked instantly. May be a solution if anyone else is having the same issue.

Dogface answered 24/11, 2020 at 21:30 Comment(0)
T
0

First of all, you must check if you have some other process connected to the port. For example, I got the same error message at Arduino IDE in my Acer laptop. It was because I had forgotten to close the minicam. When I quit it, Arduino IDE ran OK.

Tandy answered 9/5 at 21:16 Comment(0)
S
-4

you can Overcome it By Running it in a try Catch Manner, and Whenever the Serial Exception is Caught we need to Run the Try Method Again.

Shrunk answered 18/4, 2022 at 11:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.