Skip to content

Frame Processing Scripts ​

A frame processing script lets you transform every received frame with your own script before it is displayed. Typical uses are stripping headers and trailers, decoding, filtering heartbeat packets, and rewriting binary data as readable text.

Data flow ​

The chart below shows the steps a received frame goes through, in the same order the program runs them.

Device sends data

1. Packet splitting (worker thread)

  • HEX and String: a frame ends when no data arrives for longer than one Recv Interval. A buffer that reaches 64 KB is split by force.
  • String (split by \n): each line is a frame, and the frame keeps the trailing \n.
  • JustFloat: split at the frame tail 00 00 80 7F, and the frame keeps these 4 bytes. A segment whose length is not a multiple of 4, or is longer than 4096 bytes, is dropped.

2. Raw bytes (runs before frame processing)

Message events: sent to the Message Timing Chart and Modbus RTU plugins
Data bridge: forwarded to other devices (not used in stream modes)
Received-byte counter

3. Frame Processing (帧处理), skipped when no script is selected

Calls process_frame(data), where data is the raw bytes of this frame.
Returns data: continue with the returned data
Returns None, nil, or empty content: drop this frame
Runtime error: drop this frame and show the error in the receive box

4. Formatting (by receive mode)

In JustFloat mode, if the length minus the 4-byte frame tail is not a positive multiple of 4, the frame is not displayed and does not go to the next steps.

5. Display

Shown in the receive box. If a visualizer plugin is open, text-based plugins (Data Waveform, Circular Pointer Gauge, and others) also receive this text.
Data Analysis (数据分析), HEX mode only
Realtime Save (while saving)
Auto Answer (when enabled)

Notes

  • Step 2 runs before the script. Frames dropped by the script are still counted in the received-byte counter, still forwarded by the data bridge, and still sent to the Message Timing Chart and Modbus RTU plugins.
  • Data Analysis runs only in HEX receive mode.
  • Text-based plugins receive data only when at least one visualizer plugin is open.
  • Sent data does not go through frame processing.

How to use ​

  1. In the receive area, choose Lua Script (Lua 脚本) or Python Script (Python 脚本) in the Frame Processing (帧处理) drop-down.
  2. Select the script file in the file dialog (.lua for Lua, .py for Python).
  3. After the script loads successfully, the receive box shows a notification with the script path, the entry function and file information. From then on, every frame goes through the script before it is displayed.
  4. Choose the "no processing" entry (不处理) to turn frame processing off.

Notes:

  • If loading fails, frame processing is not enabled and the receive box shows the reason.
  • Cancelling the file dialog keeps the current state: an enabled script stays in use, and the setting stays at "no processing" if nothing was enabled.
  • The script is read once, when you select it. After editing the script file, select the script entry in the drop-down again and pick the file to apply the changes.
  • The Frame Processing choice is not saved. Every time you open a device window, it starts at "no processing".
  • Each device window has its own script environment, and windows do not affect each other.

Function specification ​

The entry function is named process_frame and takes one argument, data: the raw data of the current frame.

Python ​

python
def process_frame(data):
    # data: bytes, the raw bytes of the current frame
    # return bytes: used as the transformed frame
    # return str:   encoded as UTF-8 and used as the transformed frame
    # return None or empty content: drop this frame
    ...

Returning any type other than bytes, str or None is treated as an error.

Lua ​

lua
function process_frame(data)
  -- data: a string, the raw bytes of the current frame (may contain \0)
  -- return string: used as the transformed frame
  -- return nil or an empty string: drop this frame
end

Returning any type other than string or nil is treated as an error.

The script stays loaded

The script file runs only once, when it is loaded. After that, only process_frame is called for each frame. Variables defined at the top level of the script persist between frames, so you can use them to keep counters or to join data across frames.

Relationship with the receive mode ​

The data returned by the script is then formatted according to the current receive mode:

Receive modeHow the returned data is displayed
HEXShown as hexadecimal
StringDecoded as UTF-8. A trailing \r\n is shown as the literal text \r\n
String (split by \n)Decoded as UTF-8, with the trailing \r\n removed
JustFloatParsed as "several little-endian float32 channel values + a 4-byte frame tail"

JustFloat mode

In JustFloat mode, the frame passed to the script also includes the 4-byte frame tail 00 00 80 7F. The data returned by the script must still follow the "N float32 values + 4-byte frame tail" structure: its length minus 4 must be greater than 0 and a multiple of 4. Otherwise the frame is not displayed. If you only want to rewrite text, do not use frame processing in JustFloat mode.

To rewrite a binary frame as readable text, switch the receive mode to String and return the text from the script.

Examples ​

Python: strip the header, length and checksum, keep only the payload ​

Assume the device sends frames in the format AA 55 | length | payload | checksum:

python
def process_frame(data):
    # Drop frames that do not start with AA 55
    if len(data) < 4 or data[0] != 0xAA or data[1] != 0x55:
        return None

    length = data[2]
    return data[3:3 + length]

Python: filter heartbeat packets and convert the encoding ​

The device sends GBK-encoded text and periodically sends a heartbeat packet HB\r\n:

python
def process_frame(data):
    if data == b'HB\r\n':
        return None  # drop the heartbeat packet
    return data.decode('gbk', errors='replace')  # return str, handled as UTF-8

Python: prefix every frame with a sequence number ​

python
count = 0

def process_frame(data):
    global count
    count += 1
    return f'[{count}] '.encode() + data

Lua: strip the header, length and checksum, keep only the payload ​

lua
function process_frame(data)
  if #data < 4 or data:byte(1) ~= 0xAA or data:byte(2) ~= 0x55 then
    return nil -- drop
  end

  local len = data:byte(3)
  return data:sub(4, 3 + len)
end

Lua: rewrite a 2-byte temperature value as text ​

Each frame carries one big-endian signed 16-bit integer in units of 0.1 °C. Set the receive mode to String:

lua
function process_frame(data)
  if #data < 2 then
    return nil
  end

  local raw = string.unpack('>i2', data)
  return string.format('%.1f', raw / 10)
end

Lua: prefix every frame with a sequence number ​

lua
local count = 0

function process_frame(data)
  count = count + 1
  return string.format('[%d] ', count) .. data
end

What happens on errors ​

  • If the script fails at run time, only the failing frame is dropped. The script stays loaded and later frames are still processed.
  • The error is shown in the receive box together with the script line number. To avoid flooding the screen during high-speed reception, the error message appears at most once per second, and the number of omitted errors is appended to the next message.
  • Common messages at load time:
MessageCause and fix
Wrong file type: must be a .lua or .py file that matches the selected languageThe file extension does not match the selected script language.
Function not found in script: process_frame(data)The script does not define process_frame, or the name is misspelled.
Script syntax error / script execution errorThe script fails while loading. Use the line number in the message to check the script.

Performance ​

The script is called once per frame. During high-speed reception, avoid slow operations inside process_frame, such as heavy string concatenation, file access or network access.