【发布时间】:2016-10-23 06:18:36
【问题描述】:
我一直在研究 xbee 的 3 路通信。我已经弄清楚了 1 个协调器和 2 个路由器的配置。问题是我似乎找不到测试这种通信的代码。我需要的只是一个简单的代码,协调器可以同时向路由器发送不同的消息。老实说,我很难过。任何人都可以帮忙吗?
【问题讨论】:
标签: api arduino communication router xbee
我一直在研究 xbee 的 3 路通信。我已经弄清楚了 1 个协调器和 2 个路由器的配置。问题是我似乎找不到测试这种通信的代码。我需要的只是一个简单的代码,协调器可以同时向路由器发送不同的消息。老实说,我很难过。任何人都可以帮忙吗?
【问题讨论】:
标签: api arduino communication router xbee
首先,您需要 Digi 提供的XCTU。如果你想发送和接收数据,你可以使用这个软件。
如果您想编写自己的程序,我建议您使用xbee。这个 python 模块有你需要的任何东西:如何读取路由器或终端设备发送的数据包的示例,以及如何将远程命令从协调器发送到远程设备的示例,反之亦然。
示例 1 - 读取从远程设备发送的数据包:
from xbee import ZigBee
import serial
PORT = '/dev/ttyAMA0' #change AMA0 to USB0 or another port if is necessary
BAUD_RATE = 9600 #the baudrate
# Open serial port
ser = serial.Serial(PORT, BAUD_RATE)
# Create API object
xbee = ZigBee(ser)
# Continuously read and print packets
while True:
try:
response = xbee.wait_read_frame()
print(response)
except KeyboardInterrupt:
ser.colose()
break
示例 2 - 向设备发送远程命令:
from xbee import ZigBee
import serial
ser = serial.Serial('/dev/ttyAMA0', 9600)
xbee = ZigBee(ser)
#send command to change Pin 4 of the Xbee to LOW
xbee.send('remote_at',
frame_id='A',
dest_addr_long='\x00\x13\xA2\x00\x40\x24\x20\x7D', #this is the serial address(like the MAC address) of the device. You can read it with XCTU(SH and SL parameters) or you can read it from the back of the device
options='\x02',
command='D4', #pin4
parameter='\x04') #change status to low(\x05 for high status)
#send 1 packet/second with the status of the pins
xbee.send('remote_at',
frame_id='B',
dest_addr_long='\x00\x13\xA2\x00\x40\x24\x20\x7D',
options='\x02',
command='IR', #sample rate parameter
parameter='\x03\xE8') #1000 in hex
#write the above changes
xbee.send('remote_at',
frame_id='C',
dest_addr_long='\x00\x13\xA2\x00\x40\x24\x20\x7D',
options='\x02',
command='WR')
我希望这会有所帮助。
【讨论】: