stringtools.stp (Serial transfer protocol)¶
Author:
- Dirk Alders <sudo-dirk@mount-mockery.de>
Description:
This module is a submodule ofstringtools
and creates an serial frame to transmit and receive messages via an serial interface.
Submodules:
-
stringtools.stp.
DATA_CLEAR_BUFFER
= b'<'¶ The clear buffer byte (‘\x3a\x3c’ -> start of message)
-
stringtools.stp.
DATA_STORE_SYNC_VALUE
= b'='¶ The store sync value byte (‘\x3a\x3d’ -> ‘\x3a’ inside a message)
-
stringtools.stp.
DATA_SYNC
= b':'¶ The data sync byte
-
stringtools.stp.
DATA_VALID_MSG
= b'>'¶ The valid message byte (‘\x3a\x3e’ -> end of message)
-
stringtools.stp.
STP_STATE_ESCAPE_1
= 1¶ Escape 1 state definition (‘\x3a\x3c’ found)
-
stringtools.stp.
STP_STATE_ESCAPE_2
= 2¶ Escape 2 state definition (‘\x3a’ found inside a message)
-
stringtools.stp.
STP_STATE_IDLE
= 0¶ Idle state definition (default)
-
stringtools.stp.
STP_STATE_STORE_DATA
= 3¶ Store data state definition (start of message found; data will be stored)
-
stringtools.stp.
build_frame
(data)¶ This Method builds an “stp-frame” to be transfered via a stream.
Parameters: data (str) – A String (Bytes) to be framed Returns: The “stp-framed” message to be sent Return type: str Example:
import sys sys.path.append('../..') import stringtools print(repr(stringtools.stp.build_frame(b':message:')))
Will result to the following output:
b':<:=message:=:>'
-
class
stringtools.stp.
stp
¶ This class extracts messages from an “stp-stream”.
Example:
import sys sys.path.append('../..') import report import stringtools report.stdoutLoggingConfigure(log_name_lvl=[('root', 'DEBUG'), ]) s = stringtools.stp.stp() for byte in b':<:=message:=:>': data = s.process(bytes([byte])) if len(data) > 0: print(data)
Will result to the following output:
2023-10-26 00:01:04,836: root.stringtools.stp - DEBUG - STP: data sync (3a) received => changing state STP_STATE_IDLE -> STP_STATE_ESCAPE_1 2023-10-26 00:01:04,836: root.stringtools.stp - DEBUG - STP: start pattern (3a 3c) received => changing state STP_STATE_ESCAPE_1 -> STP_STATE_STORE_DATA 2023-10-26 00:01:04,836: root.stringtools.stp - DEBUG - STP: data sync (3a) received => changing state STP_STATE_STORE_DATA -> STP_STATE_ESCAPE_2 2023-10-26 00:01:04,836: root.stringtools.stp - DEBUG - STP: store sync pattern (3a 3d) received => changing state STP_STATE_ESCAPE_2 -> STP_STATE_STORE_DATA 2023-10-26 00:01:04,836: root.stringtools.stp - DEBUG - STP: data sync (3a) received => changing state STP_STATE_STORE_DATA -> STP_STATE_ESCAPE_2 2023-10-26 00:01:04,837: root.stringtools.stp - DEBUG - STP: store sync pattern (3a 3d) received => changing state STP_STATE_ESCAPE_2 -> STP_STATE_STORE_DATA 2023-10-26 00:01:04,837: root.stringtools.stp - DEBUG - STP: data sync (3a) received => changing state STP_STATE_STORE_DATA -> STP_STATE_ESCAPE_2 2023-10-26 00:01:04,837: root.stringtools.stp - DEBUG - STP: end pattern (3a 3e) received => storing message and changing state STP_STATE_ESCAPE_2 -> STP_STATE_IDLE 2023-10-26 00:01:04,837: root.stringtools.stp - INFO - STP: message identified - (9): 3a 6d 65 73 73 61 67 65 3a [b':message:']
-
process
(data)¶ This processes a byte out of a “stp-stream”.
Parameters: data (bytes) – A byte stream Returns: The extracted message or None, if no message is identified yet Return type: str
-