Python从串口读取

问题描述 投票:0回答:1

我正在尝试将数据从RFID(RMD6300)读取到Python中的Raspberry Pi 1,但在读取30-40秒后出现中断,它会崩溃,并显示以下错误消息:

回溯(最近一次调用最后一次):文件“tmp.py”,第7行,在string = ser.read(20)文件“/usr/local/lib/python2.7/dist-packages/serial/serialposix.py” ,第501行,读取'设备报告准备读取但没有返回数据'serial.serialutil.SerialException:设备报告准备读取但没有返回数据(设备断开连接或端口多次访问?)

这是我的代码:

import serial
ser = serial.Serial('/dev/ttyAMA0',9600, timeout=1)
IDs = ["xxxxxxxxxx","xxxxxxxxxx"]
while True:
    bool = False;
    string = ser.read(20)
    if len(string) == 0:
        print "Insert tag"
        continue
    else:
        for i in range(len(IDs)):
            for l in range(len(string)):
                if IDs[i] in string:
                    print IDs[i]
                    bool = True
                    break
                 else:
                     string = string[1:]+string[0]
             if bool:
                 break
    if not bool:
        print "Not found"
python linux serial-port raspberry-pi
1个回答
2
投票

问题:...设备报告已准备好读取但未返回任何数据

增加你的timeout

ser = serial.Serial('/dev/ttyAMA0',9600, timeout=1)

使用try ... except

try:
    string = ser.read(20)
except serial.serialutil.SerialException:
    except_counter +=1
    if except_counter == 5:
       break

    time.sleep(1)

问题:...设备断开连接或端口多次访问?

你能排除这两点吗?

© www.soinside.com 2019 - 2024. All rights reserved.