【问题标题】:Python SerialException: Device reports readiness to read but returned no data (device disconnected?)Python SerialException:设备报告已准备好读取但未返回数据(设备已断开连接?)
【发布时间】:2022-04-18 19:33:43
【问题描述】:

我有两个 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 和 I2C 循环应该像错误的传输一样将其拾取,所以我只需要它传递空值。 有没有办法做到这一点?

【问题讨论】:

标签: python serial-port


【解决方案1】:

使用这个命令:

sudo systemctl stop serial-getty@USB0.service

【讨论】:

    【解决方案2】:

    这对我有用。

    来自官方 RPI 文档:

    "要手动更改设置,请使用sudo nano /boot/cmdline.txt 编辑内核命令行。找到引用serial0 设备的控制台条目,并将其删除,包括波特率设置。它看起来像console=serial0,115200。确保该行的其余部分保持不变,因为此配置中的错误可能会阻止 Raspberry Pi 启动。”

    只有这一行需要编辑。

    这里是链接https://www.raspberrypi.org/documentation/configuration/uart.md

    【讨论】:

      【解决方案3】:

      虽然不能解决这个问题,但我在树莓派 Pi3B+ 上遇到了同样的问题,并尝试了以下方法:

      • 运行raspi-config并在接口下打开串口,关闭控制台
      • 编辑/boot/config.txt并添加行enable_uart=1
      • /boot/cmdline.txt 中删除对控制台的引用
      • 残疾盖蒂
      • 通过使用 5V 3A 电源为电路板供电来验证这不是电源问题

      毕竟我仍然无法让它工作。我最终只使用了一个 FT232 串行到 USB 转换器并使用/dev/ttyUSB0 作为我的端口。立即工作。如果其他人遇到同样的问题,可能是一个解决方案。

      【讨论】:

        【解决方案4】:

        您可以通过 try Catch 方式运行它来克服它,并且每当捕获到串行异常时,我们都需要再次运行 Try 方法。

        【讨论】:

          猜你喜欢
          • 2017-06-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多