bpod_core.serial.ExtendedSerial

class bpod_core.serial.ExtendedSerial

Enhances serial.Serial with additional functionality.

query(query, size=1)

Query data from the serial port.

This method is a combination of write() and read().

Parameters:
  • query (any) – Query to be sent to the serial port.

  • size (int, default: 1) – The number of bytes to receive from the serial port.

Returns:

Data returned by the serial device in response to the query.

Return type:

bytes

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:
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:

tuple[Any, ]

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 the struct module’s format specifications. See https://docs.python.org/3/library/struct.html#format-characters

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:

tuple[Any, ]

verify(query, expected_response)

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.

Parameters:
  • query (any) – The query to be sent to the serial port.

  • expected_response (bytes) – The expected response from the serial port.

Returns:

True if the response matches the expected response, False otherwise.

Return type:

bool

write(data)

Write data to the serial port.

This method extends serial.Serial.write() with support for NumPy types, unsigned 8-bit integers, strings (interpreted as utf-8) and iterables.

Parameters:

data (any) – Data to be written to the serial port.

Returns:

Number of bytes written to the serial port.

Return type:

int or None

write_struct(data, format_string)

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:
  • data (Sequence[Any]) – A sequence of data to be packed and written, corresponding to the format specifiers in format_string.

  • format_string (str) – A format string that specifies the layout of the data. It should be compatible with the struct module’s format specifications. See https://docs.python.org/3/library/struct.html#format-characters

Returns:

The number of bytes written to the serial port, or None if the write operation fails.

Return type:

int | None