parseFmt.py parseFmt_*.py
class FmtBase
The base class for format parsers, defining common procedures. Subclasses such as FmtAscii (ASCII format) and FmtBinary (binary format) inherit from this.
Format parsers are designed for serial input. For ASCII format, interpretation is done per line; for binary format, it is done byte by byte. Once the input sequence satisfies the defined header, footer, and checksum, the parsing is considered complete and the content excluding the header and footer (the payload) is stored.
process(c)
Interprets the input string. After interpretation, if is_complete()
returns true
, the interpretation succeeded and the payload can be obtained via get_payload()
. Since subsequent process()
calls may invalidate the payload, it should be retrieved immediately after completion.
To interpret another sequence, call process()
again.
Parameters
Parameter | Description |
---|---|
c | The input sequence to interpret. Supports both single-byte and sequence-level inputs. For single-byte input: int (ASCII code), str, bytes, or list of length 1. For sequence-level input: list, str, or bytes representing a complete sequence. Sequences that are incomplete or contain multiple series cannot be processed. |
Return Value
None
is_comp()
Called after process()
to indicate whether format interpretation is complete. If true
, the payload can be retrieved using get_payload()
or get_payload_in_str()
.
process()
may initialize or overwrite the internal payload storage, copy the data immediately.Parameters
None
Return Value
Value | Description |
---|---|
true | Interpretation succeeded. Payload is available. |
false | Interpretation failed or is incomplete. |
get_payload()
Returns the payload.
Parameters
None
Return Value
Returns the payload portion excluding header and footer as a list of bytes.
reinit()
Explicitly resets the internal state.
Parameters
None
Return Value
None
Other methods
Several internal-use methods are defined. Please refer to the source code for details.
Code Examples
Sequence-level Interpretation
Interprets a str
sequence a
and stores the payload in pay
. For example, pay
will contain [ 0x78, 0x80, 0x01, ... , 0x00 ]
.
import parseFmt_Ascii
fmta=parseFmt_Ascii.FmtAscii()
a = ':7880010F0F0380030002800200DF'
pay = []
fmta.process(a)
if fmta.is_comp():
pay = fmta.get_payload()
Byte-by-byte Interpretation
For binary sequence b
, parsing is performed one byte at a time using the process()
method. When the end byte 0x04
is input, parsing completes and the payload is stored in pay
.
import parseFmt_Binary
fmtb=parseFmt_Binary.FmtBinary()
b = [0xA5, 0x5A, 0x80, 0x05, 0x78, 0x00, 0x11, 0x22, 0x33, 0x78, 0x04]
pay = []
for x in b:
fmtb.process(x)
if fmtb.is_comp():
pay = fmtb.get_payload()
break