Device
This module provides base classes for device connections.
ATTRIBUTE | DESCRIPTION |
---|---|
READ_FORMAT |
default read format for device connections
TYPE:
|
WRITE_FORMAT |
default write format for device connections
TYPE:
|
Data |
default data type for device connections
TYPE:
|
CLASS | DESCRIPTION |
---|---|
Device |
Protocol for device connection classes |
StreamingDevice |
Protocol for streaming device connection classes |
TimedDeviceMixin |
Mixin class for timed device operations |
BaseDevice |
Base class for device connections |
SerialDevice |
Class for serial device connections |
SocketDevice |
Class for socket device connections |
Documentation last updated: 2025-02-22
BaseDevice
BaseDevice(
*,
connection_details: dict | None = None,
init_timeout: int = 1,
data_type: NamedTuple = Data,
read_format: str = READ_FORMAT,
write_format: str = WRITE_FORMAT,
simulation: bool = False,
verbose: bool = False,
**kwargs,
)
BaseDevice provides an interface for handling device connections
ATTRIBUTE | DESCRIPTION |
---|---|
connection |
connection object for the device
TYPE:
|
connection_details |
connection details for the device
TYPE:
|
flags |
flags for the device
TYPE:
|
init_timeout |
timeout for initialization
TYPE:
|
data_type |
data type for the device
TYPE:
|
read_format |
read format for the device
TYPE:
|
write_format |
write format for the device
TYPE:
|
eol |
end of line character for the read format
TYPE:
|
buffer |
buffer for storing streamed data
TYPE:
|
data_queue |
queue for storing processed data
TYPE:
|
show_event |
event for showing streamed data
TYPE:
|
stream_event |
event for controlling streaming
TYPE:
|
threads |
dictionary of threads used in streaming
TYPE:
|
METHOD | DESCRIPTION |
---|---|
clear |
clear the input and output buffers, and reset the data queue and buffer |
connect |
connect to the device |
disconnect |
disconnect from the device |
checkDeviceConnection |
check the connection to the device |
checkDeviceBuffer |
check the connection buffer |
clearDeviceBuffer |
clear the device input and output buffers |
read |
read data from the device |
readAll |
read all data from the device |
write |
write data to the device |
poll |
poll the device (i.e. write and read data) |
processInput |
process the input data |
processOutput |
process the output data |
query |
query the device (i.e. write and read data) |
startStream |
start the stream |
stopStream |
stop the stream |
stream |
toggle the stream |
showStream |
show the stream |
Initialize BaseDevice class
PARAMETER | DESCRIPTION |
---|---|
connection_details
|
connection details for the device. Defaults to None.
TYPE:
|
init_timeout
|
timeout for initialization. Defaults to 1.
TYPE:
|
data_type
|
data type for the device. Defaults to Data.
TYPE:
|
read_format
|
read format for the device. Defaults to READ_FORMAT.
TYPE:
|
write_format
|
write format for the device. Defaults to WRITE_FORMAT.
TYPE:
|
simulation
|
whether to simulate the device. Defaults to False.
TYPE:
|
verbose
|
verbosity of class. Defaults to False.
TYPE:
|
checkDeviceBuffer
Check the connection buffer
RETURNS | DESCRIPTION |
---|---|
bool
|
whether there is data in the connection buffer
TYPE:
|
checkDeviceConnection
Check the connection to the device
RETURNS | DESCRIPTION |
---|---|
bool
|
whether the device is connected
TYPE:
|
poll
Poll the device
PARAMETER | DESCRIPTION |
---|---|
data
|
data to write to the device. Defaults to None.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
str
|
str|None: data read from the device |
processInput
Process the input
PARAMETER | DESCRIPTION |
---|---|
data
|
data to process. Defaults to None.
TYPE:
|
format_in
|
format for the data. Defaults to None.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
str | None
|
str|None: processed input data |
processOutput
processOutput(
data: str,
format_out: str | None = None,
data_type: NamedTuple | None = None,
timestamp: datetime | None = None,
) -> tuple[Any, datetime | None]
Process the output
PARAMETER | DESCRIPTION |
---|---|
data
|
data to process
TYPE:
|
format_out
|
format for the data. Defaults to None.
TYPE:
|
data_type
|
data type for the data. Defaults to None.
TYPE:
|
timestamp
|
timestamp for the data. Defaults to None.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
tuple[Any, datetime | None]
|
tuple[Any, datetime|None]: processed output data and timestamp |
query
query(
data: Any,
multi_out: bool = True,
*,
timeout: int | float = 1,
format_in: str | None = None,
format_out: str | None = None,
data_type: NamedTuple | None = None,
timestamp: bool = False,
**kwargs,
) -> Any | None
Query the device
PARAMETER | DESCRIPTION |
---|---|
data
|
data to query
TYPE:
|
multi_out
|
whether to return multiple outputs. Defaults to True.
TYPE:
|
timeout
|
timeout for the query. Defaults to 1.
TYPE:
|
format_in
|
format for the input data. Defaults to None.
TYPE:
|
format_out
|
format for the output data. Defaults to None.
TYPE:
|
data_type
|
data type for the data. Defaults to None.
TYPE:
|
timestamp
|
whether to return the timestamp. Defaults to False.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Any | None
|
Any|None: queried data |
read
Read data from the device
RETURNS | DESCRIPTION |
---|---|
str
|
str|None: data read from the device |
readAll
Read all data from the device
RETURNS | DESCRIPTION |
---|---|
list[str]
|
list[str]|None: data read from the device |
showStream
Show the stream
PARAMETER | DESCRIPTION |
---|---|
on
|
whether to show the stream
TYPE:
|
startStream
startStream(
data: str | None = None,
buffer: deque | None = None,
*,
format_out: str | None = None,
data_type: NamedTuple | None = None,
show: bool = False,
sync_start: Barrier | None = None,
split_stream: bool = True,
callback: Callable[[str], Any] | None = None,
)
Start the stream
PARAMETER | DESCRIPTION |
---|---|
data
|
data to stream. Defaults to None.
TYPE:
|
buffer
|
buffer to store the streamed data. Defaults to None.
TYPE:
|
format_out
|
format for the data. Defaults to None.
TYPE:
|
data_type
|
data type for the data. Defaults to None.
TYPE:
|
show
|
whether to show the stream. Defaults to False.
TYPE:
|
sync_start
|
synchronization barrier. Defaults to None.
TYPE:
|
split_stream
|
whether to split the stream and data processing threads. Defaults to True.
TYPE:
|
callback
|
callback function to call with the streamed data. Defaults to None.
TYPE:
|
stream
stream(
on: bool,
data: str | None = None,
buffer: deque | None = None,
*,
sync_start: Barrier | None = None,
split_stream: bool = True,
callback: Callable[[str], Any] | None = None,
**kwargs,
)
Toggle the stream
PARAMETER | DESCRIPTION |
---|---|
on
|
whether to start or stop the stream
TYPE:
|
data
|
data to stream. Defaults to None.
TYPE:
|
buffer
|
buffer to store the streamed data. Defaults to None.
TYPE:
|
sync_start
|
synchronization barrier. Defaults to None.
TYPE:
|
split_stream
|
whether to split the stream and data processing threads. Defaults to True.
TYPE:
|
callback
|
callback function to call with the streamed data. Defaults to None.
TYPE:
|
Device
SerialDevice
SerialDevice(
port: str | None = None,
baudrate: int = 9600,
timeout: int = 1,
*,
init_timeout: int = 1,
data_type: NamedTuple = Data,
read_format: str = READ_FORMAT,
write_format: str = WRITE_FORMAT,
simulation: bool = False,
verbose: bool = False,
**kwargs,
)
Bases: BaseDevice
SerialDevice provides an interface for handling serial devices
ATTRIBUTE | DESCRIPTION |
---|---|
port |
device serial port
TYPE:
|
baudrate |
device baudrate
TYPE:
|
timeout |
device timeout
TYPE:
|
connection_details |
connection details for the device
TYPE:
|
serial |
serial object for the device
TYPE:
|
init_timeout |
timeout for initialization
TYPE:
|
message_end |
message end character
TYPE:
|
flags |
flags for the device
TYPE:
|
is_connected |
whether the device is connected
TYPE:
|
verbose |
verbosity of class
TYPE:
|
METHOD | DESCRIPTION |
---|---|
clear |
clear the input and output buffers, and reset the data queue and buffer |
connect |
connect to the device |
disconnect |
disconnect from the device |
checkDeviceConnection |
check the connection to the device |
checkDeviceBuffer |
check the connection buffer |
clearDeviceBuffer |
clear the device input and output buffers |
read |
read data from the device |
readAll |
read all data from the device |
write |
write data to the device |
poll |
poll the device (i.e. write and read data) |
processInput |
process the input data |
processOutput |
process the output data |
query |
query the device (i.e. write and read data) |
startStream |
start the stream |
stopStream |
stop the stream |
stream |
toggle the stream |
showStream |
show the stream |
Initialize SerialDevice class
PARAMETER | DESCRIPTION |
---|---|
port
|
serial port for the device. Defaults to None.
TYPE:
|
baudrate
|
baudrate for the device. Defaults to 9600.
TYPE:
|
timeout
|
timeout for the device. Defaults to 1.
TYPE:
|
init_timeout
|
timeout for initialization. Defaults to 2.
TYPE:
|
data_type
|
data type for the device. Defaults to Data.
TYPE:
|
read_format
|
read format for the device. Defaults to READ_FORMAT.
TYPE:
|
write_format
|
write format for the device. Defaults to WRITE_FORMAT.
TYPE:
|
simulation
|
whether to simulate the device. Defaults to False.
TYPE:
|
verbose
|
verbosity of class. Defaults to False.
TYPE:
|
SocketDevice
SocketDevice(
host: str,
port: int,
timeout: int = 0,
*,
byte_size: int = 1024,
simulation: bool = False,
verbose: bool = False,
**kwargs,
)
Bases: BaseDevice
SocketDevice provides an interface for handling socket devices
ATTRIBUTE | DESCRIPTION |
---|---|
host |
device host
TYPE:
|
port |
device port
TYPE:
|
timeout |
device timeout
TYPE:
|
byte_size |
size of the byte buffer
TYPE:
|
connection_details |
connection details for the device
TYPE:
|
socket |
socket object for the device
TYPE:
|
flags |
flags for the device
TYPE:
|
is_connected |
whether the device is connected
TYPE:
|
verbose |
verbosity of class
TYPE:
|
METHOD | DESCRIPTION |
---|---|
clear |
clear the input and output buffers, and reset the data queue and buffer |
connect |
connect to the device |
disconnect |
disconnect from the device |
checkDeviceConnection |
check the connection to the device |
checkDeviceBuffer |
check the connection buffer |
clearDeviceBuffer |
clear the device input and output buffers |
read |
read data from the device |
readAll |
read all data from the device |
write |
write data to the device |
poll |
poll the device (i.e. write and read data) |
processInput |
process the input data |
processOutput |
process the output data |
query |
query the device (i.e. write and read data) |
startStream |
start the stream |
stopStream |
stop the stream |
stream |
toggle the stream |
showStream |
show the stream |
Initialize SocketDevice class
PARAMETER | DESCRIPTION |
---|---|
host
|
host for the device
TYPE:
|
port
|
port for the device
TYPE:
|
timeout
|
timeout for the device. Defaults to 1.
TYPE:
|
byte_size
|
size of the byte buffer. Defaults to 1024.
TYPE:
|
simulation
|
whether to simulate the device. Defaults to False.
TYPE:
|
verbose
|
verbosity of class. Defaults to False.
TYPE:
|
StreamingDevice
Bases: Protocol
Protocol for device connection classes
TimedDeviceMixin
Mixin class for timed device operations
METHOD | DESCRIPTION |
---|---|
stopTimer |
stop a timer |
setValue |
set a value |
setValueDelayed |
set a value after a delay |
setValue
Set a value
PARAMETER | DESCRIPTION |
---|---|
value
|
value to set
TYPE:
|
event
|
event to set or clear. Defaults to None.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
bool
|
whether the value was set
TYPE:
|
setValueDelayed
setValueDelayed(
duration: int | float,
initial: Any | None = None,
final: Any | None = None,
blocking: bool = True,
*,
event: Event | None = None,
**kwargs,
) -> Timer | None
Set a value after a delay
PARAMETER | DESCRIPTION |
---|---|
duration
|
duration of the delay
TYPE:
|
initial
|
initial value. Defaults to None.
TYPE:
|
final
|
final value. Defaults to None.
TYPE:
|
blocking
|
whether to block the main thread. Defaults to True.
TYPE:
|
event
|
event to set or clear. Defaults to None.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Timer | None
|
threading.Timer|None: timer object if blocking is False |