编程语言
首页 > 编程语言> > 无法在API模式下使用Xbee发送/接收(python)

无法在API模式下使用Xbee发送/接收(python)

作者:互联网

我有两个Xbee Pro 900,每个都连接到Raspberry Pi.两者都更新到版本1061并设置为带启动的API启用.它们也具有相同的7FFF调制解调器VID.两个Pi都安装了PySerial和python-xbee库.

Xbee 1(接收器)的序列号为0013A200409A1BB8
Xbee 2(发件人)的序列号为0013A200709A1BE9

我在下面包含了我的代码,这只是我在网上找到的示例代码.我的问题是我没有在适当的Xbee上收到任何东西.我完全不知道出了什么问题,我已经三次检查目标地址和两个Xbee的配置设置.

Xbee 2代码(发件人):

#! /usr/bin/python

import time

from xbee import XBee
import serial

PORT = '/dev/ttyUSB0'
BAUD_RATE = 9600

# Open serial port
ser = serial.Serial(PORT, BAUD_RATE)

# Create API object
xbee = XBee(ser,escaped=True)
import pprint
pprint.pprint(xbee.api_commands)

DEST_ADDR_LONG = "\x00\x13\xA2\x00\x40\x9A\x1B\xB8"

# Continuously read and print packets
while True:
    try:
        print "send data"
        xbee.tx_long_addr(frame='0x1', dest_addr=DEST_ADDR_LONG, data='AB')
        time.sleep(1)
    except KeyboardInterrupt:
        break

ser.close()

Xbee 1代码(接收器):

#! /usr/bin/python

from xbee import XBee
import serial

PORT = '/dev/ttyUSB0'
BAUD_RATE = 9600

# Open serial port
ser = serial.Serial(PORT, BAUD_RATE)

# Create API object
xbee = XBee(ser,escaped=True)

# Continuously read and print packets
while True:
    try:
        print "waiting"
        response = xbee.wait_read_frame()
        print response
    except KeyboardInterrupt:
        break

ser.close()

当两个程序都在运行时,发送Xbee上的Tx指示灯闪烁,但我在接收Xbee上没有收到任何信息.有什么我想念的吗?谢谢你的时间!

解决方法:

您使用的是XBee还是XBeePro?我有同样的问题,this post给了我很多帮助.

尝试按以下方式修改Receiver Code:

import config
import serial
import time
from xbee import ZigBee

def toHex(s):
    lst = []
    for ch in s:
        hv = hex(ord(ch)).replace('0x', '')
        if len(hv) == 1:
            hv = '0'+hv
        hv = '0x' + hv
        lst.append(hv)

def decodeReceivedFrame(data):
            source_addr_long = toHex(data['source_addr_long'])
            source_addr = toHex(data['source_addr'])
            id = data['id']
            samples = data['samples']
            options = toHex(data['options'])
            return [source_addr_long, source_addr, id, samples]

PORT = '/dev/ttyUSB0'
BAUD_RATE = 9600

# Open serial port
ser = serial.Serial(PORT, BAUD_RATE)

zb = ZigBee(ser, escaped = True)

while True:
    try:
        data = zb.wait_read_frame()
        decodedData = decodeReceivedFrame(data)
        print decodedData

    except KeyboardInterrupt:
        break

在我的情况下,上面的代码输出以下内容:

[['0x00', '0x13', '0xa2', '0x00', '0x40', '0x9b', '0xaf', '0x4e'], ['0x68', '0x3f'], 'rx_io_data_long_addr', [{'adc-0': 524}]]

Here我共享控制器节点的配置设置(与X-CTU兼容)

标签:python,raspberry-pi,api,xbee
来源: https://codeday.me/bug/20190709/1411308.html