首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Python SerialException:设备报告已准备好读取,但未返回任何数据(设备断开连接?)

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

Stack Overflow用户
提问于 2015-02-05 20:26:02
回答 1查看 30.2K关注 0票数 12

我有两个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异常的建议,但我不确定这是否会有帮助(或者知道如何做到这一点)。我需要的代码,以连续运行,没有任何干扰,从用户。如果串口是空的,那么AK和IC环路应该会像不正确的传输一样检测到它,所以我只需要它来传递空值。有没有办法做到这一点?

EN

回答 1

Stack Overflow用户

发布于 2020-02-04 16:08:45

使用以下命令:

sudo systemctl stop serial-getty@USB0.service
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28343941

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档