XBee API Python库缺少超时设置
我在使用这个库的时候遇到了问题:
https://code.google.com/p/python-xbee/
里面有一个叫 xbee.wait_read_frame()
的函数,但它没有设置超时时间。
我正在测试所有的串口信号,但如果我不能设置超时,就没办法控制尝试的时间。
在Python中,有没有办法在不修改这个库的情况下升级这个函数?或者只做一些小改动就行?
ports_available = []
for port in range(0,20):
try:
ser = serial.Serial(port, 9600)
ports_available.append(port)
ser.close()
except:
pass
print(ports_available)
for port in ports_available:
ser = serial.Serial(port, 9600)
bee = ZigBee(ser)
bee.at(command=b"MY")
print(bee.wait_read_frame()) #<----------
ser.close()
1 个回答
1
看起来你需要使用文档第3页上提到的异步模式。这个可能有点复杂,特别是当数据帧没有包含接收它的串口参数时。如果没有这个参数,你就没办法把数据和接收它的端口连接起来。
import serial
import time
from xbee import XBee
serial_port = serial.Serial('/dev/ttyUSB0', 9600)
def print_data(data): """
This method is called whenever data is received
from the associated XBee device. Its first and
only argument is the data contained within the
frame.
"""
print data
xbee = XBee(serial_port, callback=print_data)
while True:
try:
time.sleep(0.001)
except KeyboardInterrupt:
break
xbee.halt()
serial_port.close()