如何使用 Digi XBee Python 模块将 API 帧发送到远程 XBee?

How to send an API frame to a remote XBee using Digi XBee Python module?

我正在将代码从 Python XBee 库移动到 Digi 的 Python XBee 库,但我无法在文档中找到有关如何将 API 帧发送到远程的语法XBee。我正在使用 S1 和 S1 Pro 设备,其中 "local" 设备连接到 Beaglebone 并且 "remote" 设备在野外是独立的。

我已经把基本框架弄下来了:

from digi.xbee.devices import XBeeDevice, RemoteXBeeDevice, XBee64BitAddress

PORT = '/dev/ttyO1'
BAUD = 9600
API_FRAME = 'long-hex-string-here'

local_xbee = XBeeDevice(PORT, BAUD)
local_xbee.open()

# Instantiate a remote XBee device object.
remote_xbee = RemoteXBeeDevice(local_xbee, XBee64BitAddress.from_hex_string("0013A20040DD7DCD"))

# Transmit frame to remote_xbee - unsure of correct method and syntax
# perhaps XBeePacket.create_packet(hex string here)
# local_xbee.send_data(remote_xbee, "api frame here?")  ??

local_xbee.close()

但我无法找到有关如何传输我构建的 API 帧的语法。根据文档中的介绍部分,我认为这是正确的方法。我对广播到网络上的所有设备不感兴趣,而是对单播通信感兴趣。

当我可以使用它时,我有一些旧型号的源代码。

我有一些 WiFi Xbee 模块,我使用了一些转换器板(基板)。我用它来连接 Xbee 从一台计算机到另一台计算机的通信,例如通过 USB 而不是 UART 将随机桌面传输到 BeagleBone Black。所以,我会使用下面列出的源来连接我的 USB 加密狗,用于从 BBB 到另一个现场模块的 Xbee 通信。

他们的I/O东西可以在这里找到:https://github.com/digidotcom/xbee-python/tree/master/examples/io

此外...仅通过 USB 加密狗 WiFi 适配器板更改其源代码中的一些线路,证明对 LED 和其他传感器发出信号很有价值。

哦,您将需要他们现在所说的载板。就是我刚刚打出来的转接板。所以,如果您已经有了载板,请使用 lsusb 作为 Linux 中的命令来查找您的 USB "name."

因此,例如,如果 lsusb 调出 /dev/ttyUSB0,那么这就是端口标识。

您可以使用 lsusb 中的那个部分,然后在 Digi 的 xtcu 软件中更改您的 xbee 模块。

from digi.xbee.devices import XBeeDevice
from digi.xbee.io import IOLine, IOMode
import time
import threading

# TODO: Replace with the serial port where your local module is connected to.
PORT = "/dev/ttyUSB0"
# TODO: Replace with the baud rate of your local module.
BAUD_RATE = 9600

REMOTE_NODE_ID = "Xbee_B"

IOLINE_IN = IOLine.DIO2_AD2
IOLINE_OUT = IOLine.DIO4_AD4

def main():
    print(" +-----------------------------------------------+")
    print(" | XBee Python Library Get/Set Remote DIO Sample |")
    print(" +-----------------------------------------------+\n")

    stop = False
    th = None

    local_device = XBeeDevice(PORT, BAUD_RATE)

    try:
        local_device.open()
        print("local device: ", local_device.get_node_id())
        # Obtain the remote XBee device from the XBee network.
        xbee_network = local_device.get_network()
        remote_device = xbee_network.discover_device(REMOTE_NODE_ID)
        if local_device is None:
            print("Could not find the remote device")
            exit(2)

        def io_detection_callback():
            while not stop:
                # Read the digital value from the input line.
                io_value = remote_device.get_dio_value(IOLINE_IN)
                print("%s: %s" % (IOLINE_IN, io_value))

                # Set the previous value to the local output line.
                local_device.set_dio_value(IOLINE_OUT, io_value)

                time.sleep(2)

        th = threading.Thread(target=io_detection_callback)

        remote_device.set_io_configuration(IOLINE_IN, IOMode.DIGITAL_IN)

        local_device.set_io_configuration(IOLINE_OUT, IOMode.DIGITAL_OUT_HIGH)

        time.sleep(1)
        th.start()

        input()

    finally:
        stop = True
        if th is not None and th.is_alive():
            th.join()
        if local_device is not None and local_device.is_open():
            local_device.close()


if __name__ == '__main__':
    main()

所以,请参阅源代码的 PORT = "/dev/ttyUSB0" 部分?

这是我将 Xbee 模块连接到载板,然后通过 USB 将载板连接到 BBB 的地方。

嗯,这可能无法回答问题,但可以更深入地了解如何处理 Digi Devices/Modules。

我还认为,如果您想尝试使用 Xbee 和 BeagleBone Black 进行 UART 通信,可能会更复杂。我会继续搜索我的文字。

P.S。这本书介绍了一些连接方法、实验 10 和实验 16、您的 "BBB" 到 UART、Xbee 以及如何通信。从这本书中获得所有的交流思想有点太深入了,但就是这样:

The Hands-on XBEE Lab Manual, Experiments that Teach you XBEE Wireless Communications – Jonathan A Titus