I am using an Xbee3 pro with micropython. I'm trying to convert the device serial number to a string. Here is the code.
import xbee
from time import sleep
serial = xbee.atcmd("SL")
serial = serial.decode("utf-8")
while True:
print("Sending broadcast data >> %s" % serial)
try:
xbee.transmit(xbee.ADDR_BROADCAST, serial)
print("Data sent successfully")
except Exception as e:
print("Transmit failure: %s" % str(e))
sleep(2)
The data transmits successfully, but I only get three ugly characters that are unreadable.
The result of:
serial = xbee.atcmd("SL")
print(serial)
is
'A\x92\xa4\xbf'
I really just need to convert 'A\x92\xa4\xbf' to 4192A4BF.
I believe this should work:
''.join('{:02x}'.format(x).upper() for x in xbee.atcmd("SL"))
You're taking each byte of the bytearray (for x in ...
) and formatting it as two uppercase hexadecimal characters ('{:02x}'.format().upper()
), then joining those together with nothing in between (''.join()
).