ExtendedSerial¶
- class bpod_core.com.ExtendedSerial ¶
Bases:
SerialEnhances
serial.Serialwith additional functionality.- query(query, size=1) ¶
Query data from the serial port.
This method is a combination of
write()andread().- Parameters:
- Returns:
Data returned by the serial device in response to the query.
- Return type:
Examples
Send a command and read back multiple bytes:
response = serial_port.query(b'\x4A', size=4)
- query_struct(query, format_string) ¶
Query structured data from the serial port.
This method queries a specified number of bytes from the serial port and unpacks it into a tuple according to the provided format string.
- Parameters:
query (
Buffer) – Query to be sent to the serial port.format_string (
str) – A format string that specifies the layout of the data to be read. It should be compatible with thestructmodule’s format specifications.
- Returns:
A tuple containing the unpacked data read from the serial port. The structure of the tuple corresponds to the format specified in format_string.
- Return type:
Examples
Send a command and unpack the response as two unsigned 8-bit integers:
major, minor = serial_port.query_struct(b'\x4a', 'BB')
- read_struct(format_string) ¶
Read structured data from the serial port.
This method reads a specified number of bytes from the serial port and unpacks it into a tuple according to the provided format string.
- Parameters:
format_string (
str) – A format string that specifies the layout of the data to be read. It should be compatible with thestructmodule’s format specifications.- Returns:
A tuple containing the unpacked data read from the serial port. The structure of the tuple corresponds to the format specified in format_string.
- Return type:
Examples
Read one unsigned 16-bit integer followed by two unsigned 8-bit integers:
major, minor, patch = serial_port.read_struct('<HBB')
- read_struct_iter(fmt, n=1, *, flatten=False) ¶
Read structured data from the serial port as an iterator.
- Parameters:
fmt (
strorstruct.Struct) – A pre-compiled struct or a format string compatible with thestructmodule’s format specifications.n (
int, default:1) – Number of records to read.flatten (
bool, default:False) – IfTrue, yield individual values instead of tuples.
- Yields:
tupleorAny– Each unpacked record as a tuple, or individual values ifflatten=True.- Return type:
Notes
All bytes are read in a single call before any records are yielded. Use
stream_struct()instead if records should be yielded as they arrive.Examples
Read three records as tuples:
for value, flag in serial_port.read_struct_iter('<HB', 3): print(value, flag)
Read two records as individual integers:
v1, f1, v2, f2 = serial_port.read_struct_iter('<HB', 2, flatten=True)
- read_uint16() ¶
Read a 16-bit unsigned integer from the serial port (little-endian).
- Return type:
- read_uint32() ¶
Read a 32-bit unsigned integer from the serial port (little-endian).
- Return type:
- read_uint64() ¶
Read a 64-bit unsigned integer from the serial port (little-endian).
- Return type:
- stream_struct(fmt, n, *, flatten=True) ¶
Stream structured data from the serial port, yielding one record at a time.
- Parameters:
fmt (
strorstruct.Struct) – A pre-compiled struct or a format string compatible with thestructmodule’s format specifications.n (
int) – Number of records to read.flatten (
bool, default:True) – IfTrue, yield individual values instead of tuples.
- Yields:
Anyortuple– Individual values ifflatten=True, otherwise one tuple per record.- Return type:
Notes
Each record is read and yielded as soon as its bytes arrive, using one
readinto()call per record. Useread_struct_iter()instead if all data is available upfront and a single read call is preferred.Examples
Stream 100 uint32 samples, yielding each as it arrives:
for sample in serial_port.stream_struct('<I', 100): process(sample)
Stream 10 records of 3 floats as tuples:
for x, y, z in serial_port.stream_struct('<3f', 10, flatten=False): print(x, y, z)
- verify(query=b'', expected_response=b'\\x01') ¶
Verify the response of the serial port.
This method sends a query to the serial port and checks if the response matches the expected response.
- write_int16(value) ¶
Write a 16-bit signed integer to the serial port (little-endian).
- write_int32(value) ¶
Write a 32-bit signed integer to the serial port (little-endian).
- write_int64(value) ¶
Write a 64-bit signed integer to the serial port (little-endian).
- write_struct(format_string, *data) ¶
Write structured data to the serial port.
This method packs the provided data into a binary format according to the specified format string and writes it to the serial port.
- Parameters:
format_string (
str) – A format string that specifies the layout of the data. It should be compatible with thestructmodule’s format specifications.*data (
Any) – Variable-length arguments representing the data to be packed and written, corresponding to the format specifiers in format_string.
- Returns:
The number of bytes written to the serial port, or None if the write operation fails.
- Return type:
- Raises:
struct.error – Error occurred during packing of the data into binary format.
SerialTimeoutException – In case a write timeout is configured for the port and the time is exceeded.
Examples
Write a command byte followed by a 16-bit unsigned integer:
serial_port.write_struct('<BH', 0x4A, 1000)
- write_uint16(value) ¶
Write a 16-bit unsigned integer to the serial port (little-endian).
- write_uint32(value) ¶
Write a 32-bit unsigned integer to the serial port (little-endian).
- write_uint64(value) ¶
Write a 64-bit unsigned integer to the serial port (little-endian).