Apr-11-2023, 12:44 AM
(This post was last modified: Apr-11-2023, 12:49 AM by deanhystad.)
This would give you an error because it is function call syntax, not array indexing syntax.
If you need a mutable array of ints (bytes is immutable, like str) make a bytearray or a list.
The story changes quite a bit if the things you are reading from the serial port arn't bytes. Let's say the 26 bytes you read were a short int and three float doubles on the sending side of the serial link. The way you reconstruct this in Python is tu use the struct library.
https://docs.python.org/3/library/struct.html
This is a short example of packing and unpacking.
Byte order is very important. You need to know the byte order of the data encode in the bytes. Choices are
@ Native
= Native
< Little Endian
> Big endian
! Network
If you have control of the sender and the receiver, use Network byte order. As long as everyone uses Network byte order the packing and unpacking always works.
I am running on a little endian machine. Look what happens if I set the byte order to Network order (Network order is big endian).
print( response(0))Serial.readline() returns bytes. Bytes are indexable and iterable. If you index a "byte" from bytes, it is an int.
b = b'Hello World' # Make bytes object print("bytes", b[1], type(b[1])) ba = bytearray(b) # Make bytearray from bytes print("bytearray", ba[1], type(ba[1])) bl = list(b) # Make list of ints from bytes print("list", bl[1], type(bl[1])) print("All equal is", b[1] == ba[1] == bl[1]) # Different types, but values are the same
Output:bytes 101 <class 'int'>
bytearray 101 <class 'int'>
list 101 <class 'int'>
All equal is True
You could not find how to turn bytes into an array of ints because it is already an array of ints.If you need a mutable array of ints (bytes is immutable, like str) make a bytearray or a list.
The story changes quite a bit if the things you are reading from the serial port arn't bytes. Let's say the 26 bytes you read were a short int and three float doubles on the sending side of the serial link. The way you reconstruct this in Python is tu use the struct library.
https://docs.python.org/3/library/struct.html
This is a short example of packing and unpacking.
values = (1047, 1.23, 4.56, 7.89) as_bytes = struct.pack("=hddd", *values) print("Bytes", as_bytes) as_values = struct.unpack("=hddd", as_bytes) print("Values", as_values)
Output:Bytes b'\x17\x04\xaeG\xe1z\x14\xae\xf3?=\n\xd7\xa3p=\x12@\x8f\xc2\xf5(\\\x8f\x1f@'
Values (1047, 1.23, 4.56, 7.89)
The "=hddd" is the format string for data to pack/unpack. = is the byte order. After the byte-order are the format characters defining the data. "hddd" is 1 short (h) and three doubles (ddd).Byte order is very important. You need to know the byte order of the data encode in the bytes. Choices are
@ Native
= Native
< Little Endian
> Big endian
! Network
If you have control of the sender and the receiver, use Network byte order. As long as everyone uses Network byte order the packing and unpacking always works.
I am running on a little endian machine. Look what happens if I set the byte order to Network order (Network order is big endian).
import struct values = (1047, 1.23, 4.56, 7.89) as_bytes = struct.pack("!hddd", *values) print("Bytes", as_bytes) as_values = struct.unpack("!hddd", as_bytes) print("Values", as_values)
Bytes b'\x04\x17?\xf3\xae\x14z\xe1G\xae@\x12=p\xa3\xd7\n=@\x1f\x8f\\(\xf5\xc2\x8f' Values (1047, 1.23, 4.56, 7.89)The values come out just fine even though the bytes are completely different. The values work out if both sides agree. When both sides don't agree you get some crazy numbers.
import struct values = (1047, 1.23, 4.56, 7.89) as_bytes = struct.pack("<hddd", *values) print("Bytes", as_bytes) as_values = struct.unpack(">hddd", as_bytes) print("Values", as_values)
Output:Bytes b'\x17\x04\xaeG\xe1z\x14\xae\xf3?=\n\xd7\xa3p=\x12@\x8f\xc2\xf5(\\\x8f\x1f@'
Values (5892, -9.60372140561512e-86, 1.19203925053298e-14, -9.539767610302094e-233)