我也推荐使用pySerial,如果你有一个非常简单的设置和只有两个XBees,但如果你有更复杂的事情,那么你最好用一个库。
python-xbee库的使用非常简单,但缺少任何综合性文档。要发送和使用它得到简单的信息:
from xbee import XBee
from serial import Serial
PORT = '/dev/ttyUSB0'
BAUD = 9600
ser = Serial(PORT, BAUD)
xbee = XBee(ser)
# Send the string 'Hello World' to the module with MY set to 1
xbee.tx(dest_addr='\x00\x01', data='Hello World')
# Wait for and get the response
print(xbee.wait_read_frame())
ser.close()
您可以通过执行发送AT命令:
xbee.at(frame_id='A', command='MY')
reply = xbee.wait_read_frame()
print(reply)
# Getting the integer value out of reply
import struct
print(struct.unpack('>h', reply['parameter'])[0])
您可以将frame_id设置为任何字符串,它是用来识别正确的答复。
所以的XBee串行端口将重复串到的XBee网络,并且还写它从远程XBees接收到串行的所有数据?我假设两个XBees都需要处于透明模式? –
我只用过两个Xbees直接通信,从来没有一个更大的网络。如果两者都处于透明模式,那么您在一端写入的任何字节都将出现在另一端,就像正常串行一样。 – Tim