digidotcom / xbee-python

Python library to interact with Digi International's XBee radio frequency modules.
Mozilla Public License 2.0
185 stars 93 forks source link

Extracting Transmit Statis for coordinator device #300

Closed sayaz closed 10 months ago

sayaz commented 10 months ago

Hello,

Using Digi Xbee3 mesh devices (on firmware 802.15.4, 200D). Once as Coordinator (transmitter) other as End device (receiver) (API mode w/o escapes). The transmit and receive of data is working fine. I want to extract the "Transmit Status" of the frame from the transmitter side basically for acknowledgement purpose. Unfortunately I am unable to see/recover the "Transmit Status". The only thing that I can get is the payload part. On the other hand if I use the debugging option, I am able to see, the "Transmit option" is being received (in HEX format, which I can interpret from XCTU tool). How can I extract this information in Python ? Can you please help ? Code as below:

Coordinator code:

import xbee
from digi.xbee.devices import XBeeDevice, RemoteXBeeDevice, XBee64BitAddress
import time
from datetime import datetime
import logging

PORT = "/dev/tty.usbserial-AB0OO9Y6"
BAUD_RATE = 115200

logging.basicConfig(
    format='[%(asctime)s,%(msecs)06d] - %(levelname)s : %(lineno)d - %(message)s',
    level=logging.DEBUG,
    datefmt='%m-%d-%Y %I:%M:%S'  # Exclude milliseconds from the datefmt
)

localDevice = XBeeDevice(PORT, BAUD_RATE)
localDevice.open(force_settings = True)

remoteDevice = RemoteXBeeDevice(localDevice, XBee64BitAddress.from_hex_string("0013A200420108F2"))

localDevice.send_data(remoteDevice, "Hello")

while True:
    received_msg = localDevice.read_data()
    if received_msg:
        print("Data received (before decoding) : ", received_msg )
        print("Decoded data : ", received_msg.data.decode())

End device code

from digi.xbee.devices import XBeeDevice, RemoteXBeeDevice, XBee64BitAddress, Raw802Device, RemoteRaw802Device
from datetime import datetime
import time

PORT = "/dev/tty.usbserial-AB0OPD7U"
BAUD_RATE = 115200

device = XBeeDevice(PORT, BAUD_RATE)
device.open()
coordinator_mac = XBee64BitAddress.from_hex_string("0013A20042015449")

# Define the callback.
def my_data_received_callback(xbee_message):
    address = xbee_message.remote_device.get_64bit_addr()
    data = xbee_message.data.decode("utf8")
    print("Received data from %s: %s" % (address, data))

# Add the callback.
device.add_data_received_callback(my_data_received_callback)
input()
rubenmoral commented 10 months ago

Hey @sayaz,

When using the send_data method, the library implicitly checks for the TransmitStatus packet after sending the data. If it's not received or the status is not success, the method will throw a TimeoutException or a TransmitException respectively. In contrast, the send_data_async method does not check for the TransmitStatus packet. See https://xbplib.readthedocs.io/en/latest/user_doc/communicating_with_xbee_devices.html#synchronous-operation.

If you still want to process the content of the TransmitStatus packet, you should register a packet received callback using the add_packet_received_callback method, see https://xbplib.readthedocs.io/en/latest/api/digi.xbee.devices.html#digi.xbee.devices.XBeeDevice.add_packet_received_callback.