FiPy | ubinascii | base64 decode
Hello Pycom Team,
I am trying to convert a base64 string received from
UARTusing the below code. The Message size is approx 55000 bytes and transfer fine over uart.
The below code works perfectly fine for small payloads. However for a bigger payload, it give Unicode error for the line
uart_messages[counter] = msg.decode('utf-8')that is probably because the base64 string in
msg = ubinascii.a2b_base64(temp)is not decoded correctly. Have also attached a screenshot of
print msg. As we can see, it converted it half and then failed. Any suggestions to resolve this would be appreciated.
for counter, item in enumerate(uart_messages): # Message should be in format of :ML:70,base64string\n if item.startswith(":ML:"): temp = item.split(",", 1) msg = ubinascii.a2b_base64(temp) print(msg) try: uart_messages[counter] = msg.decode('utf-8') except Exception as e: print(e) return uart_messages
@bitvijays can you also print the base64 text before it is decoded?
It seems to be the base64 text must have been missing a character, which then resulted in the the garbled output. If you look at the output, you’ll see it repeats every 15 characters, like the original text. I haven’t checked if that actually matches the binary day shifted by a few bits, but it’s most likely.
UARTs can miss characters, especially at higher speeds. You need to add some way to ensure data is correct (a checksum or CRC), and either ignore bad data if that’s appropriate for your workflow, or add a way to retransmit bad data.