Closed edunn106 closed 4 years ago
Hi @edunn106,
I'm not sure what you are trying to do, but let's go with what I understand: you would like to receive 0x95 Node Identification Indicator frames and manipulate them (you mention a "join notification packet" which I guess is a 0x95 Node Identification Indicator frame):
1.To receive all join notification frames, you cannot add/remove/add the callback, since you can receive new frames while manipulating the previous one.
I'm afraid the library does not include a 0x95 Node Identification Indicator class, so you have to work with UnknownXBeePacket
class. Here you have an example:
def my_packet_received_callback(xbee_packet):
if xbee_packet.get_frame_type_value() != 0x95:
return
# Ignore first byte: frame type byte (0x95)
data = xbee_packet.get_frame_spec_data()[1:]
# Manipulate the data in the packet
print("Data: %s" % utils.hex_to_string(data))
print("64-bit source address: %s" % XBee64BitAddress(data[0:8]))
print("16-bit source address: %s" % XBee16BitAddress(data[8:10]))
print("Is broadcast: %s" % bool(data[10] & 0x02))
print("16-bit remote address: %s" % XBee16BitAddress(data[11:13]))
print("64-bit remote address: %s" % XBee64BitAddress(data[13:21]))
# node ID goes from 11 to the next 0x00.
i = 21
while data[i] != 0x00:
i += 1
print("Node identifier: %s" % data[21:i].decode())
i += 1
print("Parent address (16-bit): %s" % XBee16BitAddress(data[i:i + 2]))
i += 2
print("Device type: %d" % data[i])
i += 1
print("Source event: %d" % data[i])
i += 1
print("Digi profile id: %s" % utils.hex_to_string(data[i:i + 2], pretty=False))
i += 2
print("Digi manufacturer id: %s" % utils.hex_to_string(data[i:i + 2], pretty=False))
xbee = XBeeDevice(PORT, BAUD_RATE)
xbee.open()
xbee.add_packet_received_callback(my_packet_received_callback)
[...]
xbee.del_packet_received_callback(my_packet_received_callback)
xbee.close()
Best Regards.
hello, below is a snippet from my digi forum post.. as you read you'll come to understand what im attempting hopefully.
ME:
Question about that add_packet_received_callback:
if a packet is received does it trigger that functrion no matter what? im trying to make it trigger on the first packet (the one I am interested) but instead it triggers there and then now any time I do anything that requires a packet sent in the background, it gets triggered and the callback does its thing. I don't want that. commented 1 hour ago by edunn106 New to the Community
PERSON FROM DIGI FORUM:
Yes it triggers no matter what. If you want to disable the callback you should be able to use del_packet_received_callback ( https://xbplib.readthedocs.io/en/latest/api/digi.xbee.reader.html?highlight=Join%20notification#digi.xbee.reader.PacketListener.del_packet_received_callback ). Otherwise you could write your code to ignore calls/return immediately if you don't care about the packet.
ME:
yeah, that's what I'm trying to implement.
I tried to basically have the following flow but doesn't quite work out yet:
local_device.add_packet_received_callback(my_packet_received_callback1)
which goes to:
def my_packet_received_callback1(xbee_packet): local_device.del_packet_received_callback(my_packet_received_callback1) my_packet_received_callback(xbee_packet)
which, I thought, should delete the callback and then send you(while relaying the xbee_packet) to:
def my_packet_received_callback(xbee_packet):
which finally manipulate the received packet... and then recalls the function that the initial callback_add trigger while loop is in, thereby awaiting another join notification packet (which is my intention after im done manipulating the first reception event).
does that make sense to you? because my issue was that I would receive the packet through the callback trigger, then hops me to the function, but then when I set other configs for the an xbee of course that would prompt packets sends that would trigger the callback again...
so through the above code I was trying to hide/delete the callback trigger so I could just get the first packet and then manipulate it and then when done recall the function that reimplements the callback trigger. but its not quite right.
at first I was deleting the callback right after the callback_add In the while loop but of course the callback_add would send you to the callback function through the passed parameter...
so then I tried to delete the callback in the function that the callback_add passed parameter would send you to... but then that's weird cuz the callback_del has the same function passed as the parameter lol.. so hence the code flow I posted above: callback_add -> callback function that deletes then passes on the xbee_packet to the next function that manipulates the xbee_packet
any ummm obvious terribleness and/or better/actual ways to achieve this?
thanks