Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
hitochan777 opened this issue Dec 5, 2020 · 3 comments
Open

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

hitochan777 opened this issue Dec 5, 2020 · 3 comments
Assignees

Comments

@hitochan777
Copy link

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
@c-ryan-k
Copy link
Member

c-ryan-k commented Mar 2, 2021

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
Copy link
Member

c-ryan-k commented Apr 8, 2021

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 c-ryan-k closed this as completed Apr 8, 2021
@hitochan777
Copy link
Author

hitochan777 commented Apr 9, 2021

@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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants