また、非常に単純なセットアップで XBees が 2 つしかない場合は pySerial を使用することをお勧めしますが、より複雑なものがある場合は、ライブラリを使用することをお勧めします。
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 は任意の文字列に設定でき、正しい応答を識別するために使用されます。