我也推薦使用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