senxor.interface.protocol
senxor.interface.protocol¶
IDevice
¶
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 |
|---|---|---|---|
|
TDevice
|
The device to connect to. |
required |
|
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 |
|---|---|---|---|
|
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_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 |
|---|---|---|---|
|
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_regs(regs)
¶
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 |
|---|---|---|---|
|
Literal['open', 'close', 'data', 'error']
|
The event to register the listener for. |
required |
|
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.