mwings.common Module
Stores data and procedures used throughout the library.
mwings
This is a top-level module that is often used directly.
Twelite
Interface for TWELITE
Inherits:
threading.Thread
Twelite()
Twelite(port=None, rx_buffer_size=1024, timeout=100, tz=None, debugging=False)
Constructor. If a serial port is specified, it will be opened.
Name | Type | Description |
---|---|---|
port | optional str | Serial port name: disabled if None |
rx_buffer_size | int | Size of the receive buffer in bytes |
timeout | int | Timeout between receiving packets (ms) |
tz | optional tzinfo | Timezone applied to received data: UTC if None |
debugging | bool | Debug output: enabled if True |
IOError
Serial port does not exist or is busyclose()
v1.0.10+
close()
Closes the serial port if it is open.
Usually, opened serial ports are automatically closed without calling close()
.
If you wish to close it explicitly, use this function or a with
statement.
None
None
add_listener()
@overload
add_listener(event, handler)
add_listener(event, handler)
Registers a receive handler.
Name | Type | Description |
---|---|---|
event | common.PacketType | Identifier for the receive event |
handler | Callable[[common.BarePacket], None] | Handler for the receive event (raw data) |
Callable[[common.SomeCallable], None] | Handler for the receive event (parsed data) |
None
on()
on(event)
Registers a receive handler (decorator version).
Name | Type | Description |
---|---|---|
event | common.PacketType | Identifier for the receive event |
Type | Value | Description |
---|---|---|
Callable[[common.SomeCallable], common.SomeCallable] | - | Decorated function |
remove_all_listeners()
v1.0.9+
remove_all_listeners()
Removes all registered receive handlers.
None
None
parse()
@overload
parse(character, use_lf=False)
parse(character, use_lf=False)
parse(character, use_lf=False)
Parses the given received data.
Name | Type | Description |
---|---|---|
character | str | Received data character: string of length 1 |
bytes | Received data character: bytes of length 1 | |
int | Received data character: ASCII code | |
use_lf | bool | Type of newline character: LF if True |
Type | Value | Description |
---|---|---|
optional common.PacketType | common.PacketType | Parsed packet type |
None | No parsed packet |
RuntimeError
Serial port is specifiedValueError
Invalid character data detectedparse_line()
parse_line(line, use_lf=True)
Parses received data expressed as a single line string.
Name | Type | Description |
---|---|---|
line | str | Received data string: a single line |
use_lf | bool | Type of newline character: LF if True |
Type | Value | Description |
---|---|---|
optional common.PacketType | common.PacketType | Parsed packet type |
None | No parsed packet |
RuntimeError
Serial port is specifiedreceive()
receive()
Parses data received on the serial port. Waits until the interpretation of one packet is complete.
None
Type | Value | Description |
---|---|---|
common.PacketType | - | Parsed packet type |
RuntimeError
Serial port is not specifiedrun()
run()
Runs a subthread that performs reception. Overrides threading.Thread.run()
.
start()
.None
None
send()
@overload
send(data)
send(data)
Sends commands from the serial port.
Name | Type | Description |
---|---|---|
data | common.SomeCommand | Various command data |
common.BarePacket | Raw command data |
Type | Value | Description |
---|---|---|
bool | True | Success |
False | Failure |
RuntimeError
Serial port is not specifiedset_timezone()
set_timezone(tz)
Sets the timezone applied to received data.
Name | Type | Description |
---|---|---|
tz | tzinfo | Any timezone: ZoneInfo |
None | UTC |
None
start()
start()
Starts a subthread that performs reception. Overrides threading.Thread.start()
.
None
None
RuntimeError
Serial port is not specifiedstop()
stop()
Stops the subthread that performs reception. Waits until handler calls for remaining received data are finished. This may take several seconds.
None
None
RuntimeError
Serial port is not specifiedupdate()
update()
Parses data received on the serial port. Processes all data in the receive buffer and then immediately returns.
None
Type | Value | Description |
---|---|---|
optional common.PacketType | common.PacketType | Parsed packet type |
None | No parsed packet |
RuntimeError
Serial port is not specifiedtimezone
@property
update()
Type | Value | Description |
---|---|---|
tzinfo | - | Timezone applied to received data |
Stores data and procedures used throughout the library.
Parsers for interpreting packets and handling their data.
Serializers to generate commands and data structures to handle them.
mwings.utils