bpod_core.com.ChunkedSerialReader

class bpod_core.com.ChunkedSerialReader

Bases: Protocol

A protocol for reading chunked data from a serial port.

This class provides methods to buffer incoming data and retrieve it in chunks.

__init__(chunk_size, buffer=None) None

Initialize the protocol.

Parameters:
  • chunk_size (int) – The fixed size of chunks to emit to process when enough data has accumulated in the internal buffer.

  • buffer (bytearray, optional) – Pre-allocated buffer to use for accumulation. If None, a new bytearray is created.

Return type:

None

data_received(data) None

Called with snippets received from the serial port.

Parameters:

data (bytes) – The binary data received from the serial port.

Return type:

None

get(size) bytearray

Retrieve a specified amount of data from the buffer.

Parameters:

size (int) – The number of bytes to retrieve from the buffer.

Returns:

The retrieved data.

Return type:

bytearray

process(data_chunk) None

Process a chunk of data.

Subclasses should override this method to implement application-specific handling of fixed-size chunks. It is called repeatedly by data_received whenever enough bytes have accumulated to reach chunk_size.

Parameters:

data_chunk (bytearray) – A contiguous slice of bytes of length chunk_size.

Return type:

None

put(data) None

Add data to the buffer.

Parameters:

data (bytes) – The binary data to be added to the buffer.

Return type:

None