Skip to content

senxor.interface.protocol

senxor.interface.protocol

IDevice

Bases: Protocol

Protocol class for the device.

This class is used to provide a uniform interface for the communication with the device, e.g. serial port, socket, etc. It must have a name attribute for identification.

Attributes:

Name Type Description
name str

The name of the device.

ISenxorInterface(device)

Bases: Protocol[TDevice]

Protocol class for the Senxor devices.

This class is used to provide a uniform interface for the communication with the device, e.g. USB, TCP/IP, etc.

Attributes:

Name Type Description
device TDevice

The device to communicate with.

is_connected bool

Whether the device is connected.

Parameters:

Name Type Description Default

device

TDevice

The device to connect to.

required

**kwargs

Any

Additional keyword arguments to pass to the interface.

required

Returns:

Type Description
None

list_devices() classmethod

List all the devices of this interface.

open()

Open the connection to the device.

If the device is already open, this method should not raise an exception.

close()

Close the connection to the device.

If the device is already closed, this method should not raise an exception.

read(block=True)

Read a frame from the senxor.

In block mode, the method should raise SenxorReadTimeoutError if no frame is available after the timeout. In non-block mode, the method should return None if no frame is available.

Parameters:

Name Type Description Default
block
bool

Whether to block the read operation until a frame is available, by default True If False, the function will return None immediately if no frame is available.

True

Raises:

Type Description
SenxorReadTimeoutError

If no frame is available after the timeout.

Returns:

Type Description
tuple[bytes | None, bytes | None]

A tuple of two bytes containing the frame header and the frame data. If no frame is available, the function will return (None, None).

read_reg(reg)

Read a register value from the senxor.

If this operation fails, this function should raise an exception depending on the error.

Parameters:

Name Type Description Default
reg
int

The register to read.

required

Returns:

Type Description
int

The value of the register.

read_regs(regs)

(Optional) Read multiple registers from the senxor.

If this operation fails, this function should raise an exception depending on the error.

Parameters:

Name Type Description Default
regs
list[int]

The list of registers to read.

required

Returns:

Type Description
dict[int, int]

The dictionary of register addresses and their values.

write_reg(reg, value)

Write a value to a register.

If this operation fails, this function should raise an exception depending on the error.

Parameters:

Name Type Description Default
reg
int

The register to write to.

required
value
int

The value to write to the register.

required

write_regs(regs)

(Optional) Write multiple registers to the senxor.

If this operation fails, this function should raise an exception depending on the error.

Parameters:

Name Type Description Default
regs
dict[int, int]

The dictionary of register addresses and their values to write.

required

on(event, listener)

on(event: Literal['open', 'close'], listener: Callable[[], None]) -> Callable[[], None]
on(
    event: Literal["data"], listener: Callable[[bytes | None, bytes], None]
) -> Callable[[], None]
on(
    event: Literal["error"], listener: Callable[[Exception], None]
) -> Callable[[], None]

Register a listener for an event.

Parameters:

Name Type Description Default
event
Literal['open', 'close', 'data', 'error']

The event to register the listener for.

required
listener
Callable

The listener to register.

required

Returns:

Type Description
Callable[[], None]

The function to clear the listener.

Raises:

Type Description
ValueError

If the event is invalid.

__repr__()

Return a string representation of the interface.

__str__()

Return a string representation of the interface.