Python SerialException:设备报告准备好读取但未返回任何数据(设备已断开连接?)

新手上路,请多包涵

我有两个 Raspberry Pi 使用串行端口和一对 XRF 无线电相互发送数据。通常它们工作正常并且整个程序循环多次但每隔一段时间其中一个会停止程序并出现以下错误:

 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?)

我的代码是:

 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=''

另一个 Pi 上的代码类似(如果需要我可以提供)。

从我在网上找到的内容来看,尝试读取串行端口但它是空的是一些问题。我已经看到了使用 try and catch exception 的建议,但我不确定这是否会有所帮助(或者知道如何真正做到这一点)。我需要代码在没有任何用户干扰的情况下连续运行。如果串行端口为空,那么 AK 和 I2C 循环应该将其拾取为错误传输,因此我只需要它传递空值。有什么办法吗?

原文由 user4494610 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.7k
1 个回答

使用此命令:

 sudo systemctl stop serial-getty@USB0.service

原文由 Hemjal 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题