psychos.triggers.SerialPort#

class psychos.triggers.SerialPort(address: str, baudrate: int = 115200, reset_value: bytes = b'\x00', log: bool = True, **kwargs)[source]#

Bases: BasePort

Serial port implementation using pyserial.

This class provides an implementation of a serial communication port using the pyserial library. It supports sending data as either integers (converted to a single byte) or as bytes, resetting the port to a predefined state, and closing the connection.

Note

This class is based on pyserial and requires it to be installed. You can install it via pip.

Examples

Using a Windows COM port: >>> sp = SerialPort(address=’COM3’, baudrate=9600, timeout=1) >>> sp.send(255) # Sends the byte corresponding to 255. >>> sp.send(b’ÿ’) # Sends the byte directly. >>> sp.reset() # Resets the port. >>> sp.close() # Closes the connection.

Using a Unix-like system port: >>> sp = SerialPort(address=’/dev/ttyUSB0’, baudrate=115200) >>> sp.send(128) # Sends the byte corresponding to 128. >>> sp.send(b’€’) # Sends the byte directly.

__init__(address: str, baudrate: int = 115200, reset_value: bytes = b'\x00', log: bool = True, **kwargs)[source]#

Initialize the SerialPort instance.

Parameters:
  • address (Optional[str], optional) – The serial port address (e.g., ‘COM3’ on Windows or ‘/dev/ttyUSB0’ on Unix-like systems). Defaults to None.

  • baudrate (int, optional) – The baud rate for the serial connection. Defaults to 115200.

  • reset_value (bytes, optional) – The byte sequence used to reset the port. Defaults to b”x00”.

  • **kwargs – Additional keyword arguments to pass to the serial.Serial constructor (e.g., timeout).

Raises:

ImportError – If the pyserial library is not installed.

Examples

>>> sp = SerialPort(address='COM3', baudrate=9600, timeout=1)
>>> sp = SerialPort(address='/dev/ttyUSB0', baudrate=115200)

Methods

__init__(address[, baudrate, reset_value, log])

Initialize the SerialPort instance.

close()

Close the serial port connection.

encode(value)

Encode a value as bytes.

reset()

Reset the serial port by sending the predefined reset value.

send(value)

Send a value over the serial port.

close()[source]#

Close the serial port connection.

This method should be called to properly release the serial port when it is no longer needed.

encode(value: int | bytes) bytes[source]#

Encode a value as bytes.

reset()[source]#

Reset the serial port by sending the predefined reset value.

This is typically used to return the port to a known, idle state.

send(value: int | bytes)[source]#

Send a value over the serial port.

If an integer is provided, it is converted to a single byte using big-endian byte order. If a multi-byte conversion is required, convert the value to bytes before calling this method.

Parameters:

value (Union[int, bytes]) – The value to send. If an integer is provided, it will be converted to a single byte. For other sizes or multi-byte values, pass the value as bytes.

Examples

>>> sp = SerialPort(address='COM3')
>>> sp.send(255)         # Converts 255 to a single byte and sends it.
>>> sp.send(b'ÿ')     # Sends the byte directly.