BallAerospace / python-ballcosmos

Python Support for Ball Aerospace COSMOS v4
Other
18 stars 4 forks source link

binary data problem. #26

Open thesamprice opened 2 years ago

thesamprice commented 2 years ago

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 commented 1 year ago

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.