stringtools.stp (Serial transfer protocol)

Author:

Description:

This module is a submodule of stringtools 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:

[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