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

binary data problem. #26

Open
thesamprice opened this issue Aug 16, 2022 · 1 comment
Open

binary data problem. #26

thesamprice opened this issue Aug 16, 2022 · 1 comment

Comments

@thesamprice
Copy link

thesamprice commented Aug 16, 2022

Im trying to send the following binary blob via send_raw.

However my 0xa0 is becoming 0xc2 0xa0. Do i need to convert the byte array to a string via some sort of encoding prior to sending.

send_raw('HW_INT',bytearray(b'\xa0'))

However on the receiving end I am seeing 2 bytes

0xffffffc2
0xffffffa0

Some other characters are causing my problems also.
Unsure how to properly encode/decode a string for this.

The cosmos console only shows this, but im unsure if its taking the length of a unicode object or a byte array.

2022/08/16 10:19:06.163  WARN: Unlogged raw data of 1 bytes being sent to interface HW_INT
@AbdelAzizMohamedMousa
Copy link

It seems like the issue is related to the encoding of the binary data. When you pass bytearray(b'\xa0') to send_raw, it's treating it as a Unicode string and encoding it with the default encoding (which is usually UTF-8). In UTF-8, the byte 0xa0 is not a valid standalone character and gets encoded as the two-byte sequence 0xc2 0xa0.

To avoid this issue, you can encode the binary data as base64 before sending it using send_raw. Here's an example of how you can do it:
`import base64

binary_data = bytearray(b'\xa0')
encoded_data = base64.b64encode(binary_data).decode('ascii')
send_raw('HW_INT', encoded_data)
`

On the receiving end, you'll need to decode the base64-encoded data to get back the original binary data. Here's an example of how you can do it:
`import base64

received_data = receive_raw('HW_INT')
decoded_data = base64.b64decode(received_data)
`
This should give you the original binary data without any encoding issues.

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

2 participants