This is the multi-page printable view of this section. Click here to print...

Return to the regular view of this page

As of 2025-08-01

MNLib

Part of the TWELITE PAL Script responsible for reading and interpreting serial data
This folder contains the part of the TWELITE PAL Script responsible for reading and interpreting serial data.

1 - apppal.py

Class that interprets the read byte sequence and stores it in a dictionary object

Class AppPAL

This class inherits from AppBase, interprets the received payload, converts it into usable data, and stores it in a dictionary object.

Parameters for Definition

Parameters with default values are optional.

NameTypeDefaultDescription
portstringNone

Name of the serial port to open

e.g., COM3, /dev/ttyUSB0

baudint115200Baud rate
toutfloat0.1Timeout duration (in seconds) for serial communication
sformatstringAsciiThis value is fixed to Ascii
autologbooleanFalseIf True, automatically logs interpreted payload to a CSV file
errbooleanFalseIf True, outputs error messages

ReadSensorData()

This method reads the payload and interprets it according to the TWELITE PAL parent format.

Parameters

None

Return Value

If data is successfully read: True If data could not be read: False

The keys stored in the dictionary object are as follows.

KeyTypeDescription
ArriveTimedatetimeTime the payload was received
LogicalIDintLogical device ID of the end device
EndDeviceSIDintSerial number of the end device
RouterSIDint

Serial number of the first repeater to receive the packet

(0x80000000 if the parent directly receives the packet)

LQIintLink quality of received signal
SequenceNumberint

Sequence number incremented with each transmission

Starts from 1, rolls over to 0 after 65535

SensorintSensor type (fixed at 0x80)
PALIDintPAL board ID
PALVersionintPAL board version
HALLICintState of the Hall IC
TemperaturefloatTemperature (degC)
HumidityfloatHumidity (%)
IlluminanceintIlluminance (lux)
AccelerationXlist,floatAcceleration on X-axis (g)
AccelerationYlist,floatAcceleration on Y-axis (g)
AccelerationZlist,floatAcceleration on Z-axis (g)
SamplingFrequencyintSampling frequency of acceleration
EventIDlist,intCause of event and event ID
WakeupFactorlist,intData on wakeup factors, etc.

OutputCSV()

Outputs the dictionary object to a CSV file.

Parameters

None

Return Value

None

2 - appbase.py

Base class for serial data interpretation

class AppBase

This code provides a base class implementing common functions required for all TWELITE APPS. It includes operations such as opening and closing the serial port, reading serial data, and writing to log files.
The derived class apppal.py interprets the received byte sequence, stores the data in a dictionary object, and returns it to the main function.

GetDataDict()

Interprets the payload and returns a dictionary object containing the data.

Parameters

None

Return Value

TypeDescription
DictDictionary object containing the interpreted payload data

3 - mwSerial.py

Class for managing serial port operations

Class MWSerial

This class manages serial port operations such as reading and writing.

Parameters for Definition

Initial values are set and do not need to be specified.

NameTypeDefaultDescription
portstringNone

Name of the serial port to open

e.g., COM3, /dev/ttyUSB0

baudint115200Baud rate
timeoutfloat0.1Timeout duration for serial communication (in seconds)
parityintserial.PARITY_NONESpecify parity
stopint1Stop bit
byteint8Data bit length
rtsctsint0Set to 1 to enable RTS and CTS
dsrdtrint0Set to 1 to enable DSR and DTR
modestringAsciiThis value is fixed to Ascii

SerialSelect

Searches for serial ports connected to the PC and prompts the user to select one.

If only one port is available, it is automatically selected.
If no ports are found, None is returned.
If a port name is passed as an argument, that port will be used.

Parameters

NameTypeDefaultDescription
portnamestringNone

Name of the serial port to open (e.g., COM3, /dev/ttyUSB0)

Leave unspecified for automatic selection.

Return Value

None

4 - parseFmt.py parseFmt_*.py

TWELITE serial format parser

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

ParameterDescription
cThe 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().

Parameters

None

Return Value

ValueDescription
trueInterpretation succeeded. Payload is available.
falseInterpretation 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