azure-iot-cli-extension icon indicating copy to clipboard operation
azure-iot-cli-extension copied to clipboard

How to send binary data with "az iot device c2d-message"?

Open hitochan777 opened this issue 4 years ago • 3 comments

Can I send binary data with az iot device c2d-message, that is represented with HEX like 0x86 0x54 0x32? I converted a string to another with backslash escapes interpreted and then feed it to az iot device c2d-message command. But I got UnicodeEncodeError as shown below. Will it work if I specify proper content encoding?

$ CMD = "\x86\x54\x32"
$ OUTPUT="$(echo -e $CMD)"
$ az iot device c2d-message send --device-id $DEVICE_ID -n $NAME --data $OUTPUT
...
UnicodeEncodeError: 'utf-8' codec can't encode character '\udc86' in position 0: surrogates not allowed

hitochan777 avatar Dec 05 '20 13:12 hitochan777

Hey @hitochan777 - unfortunately, the binary data you're sending will need to be encoded to utf-8 when it's processed by python and passed into the CLI, and some of these binary characters aren't able to be validly displayed in a utf format.

I've tried some different encodings to allow for surrogate-less binary characters: str.encode(data, 'utf-8', errors='surrogatepass') but it modifies the binary string and we don't get the same data out that we put in.

If it's possible, one solution is to use base64 to encode your binary messages to base64 strings: binary=$'\x86\x54\x32' data=$(echo -n $binary | base64)

The downside is that you'll need to base64 --decode the resulting data when you receive the message.

Let me know if this helps for your workflow or if we need to investigate further.

c-ryan-k avatar Mar 02 '21 18:03 c-ryan-k

Hey @hitochan777 - haven't heard back from you in a bit. Closing this for now, please re-open if you find it's still an issue or the workaround provided isn't working for you.

c-ryan-k avatar Apr 08 '21 23:04 c-ryan-k

@c-ryan-k

Sorry for the late reply. I appreciate your workaround, but it does not help because the receiver does not accept base64-encoded data and cannot be modified to do so...

Is it possible to add an option to accept binary data?

By the way I cannot reopen this issue (maybe because of permission)

hitochan777 avatar Apr 09 '21 10:04 hitochan777