Skip to main content

Lua Script API Reference

CycBox runs Lua scripts to process messages across connections (serial, TCP, WebSocket, MQTT, UDP). Scripts interact with the engine through hook functions and global APIs.

Hook Functions

All hooks are optional. Implement only what you need.

on_start()

Called once when the Lua task starts or reload, before processing any messages.

on_receive() → boolean

Called for each received message (RX path). The global message object contains the received data with connection_id identifying the source.

Return true if message was modified, false otherwise.

on_send() → boolean

Called for each outgoing message from UI (TX path, before encoder). The global message object contains the outgoing data with connection_id controlling the destination.

Return true if message was modified, false otherwise.

on_send_confirm() → boolean

Called after a successful send (TX confirmation). The global message object contains the confirmed message.

Return true if message was modified, false otherwise.

on_timer(timestamp_ms)

Called every 100ms. timestamp_ms is the current Unix timestamp in milliseconds.

on_stop()

Called before the engine stops or before script hot-reload. On hot-reload, on_stop() runs on the old script before on_start() on the new one.

Processing Pipeline

Startup:    on_start() → [ready]
RX: Transport → Decoder → Transformer → on_receive() → UI
TX: UI → on_send() → Transformer → Encoder → Transport → on_send_confirm() → UI
Timer: [every 100ms] → on_timer(timestamp_ms)
Shutdown: on_stop() → [done]

Message Object

The global message object is available in on_receive(), on_send(), and on_send_confirm().

Properties

PropertyTypeAccessDescription
payloadstring (byte array)Read/WritePure data content after protocol framing removed. Setting nil clears it.
framestring (byte array)Read/WriteComplete wire format including all protocol overhead. Setting nil clears it.
timestampinteger (u64)Read/WriteMicroseconds since UNIX epoch.
checksum_validbooleanRead-onlyWhether the frame checksum is valid. Defaults to true if no checksum metadata exists.
connection_idinteger (u32)Read/WriteSource/target connection (0-based configuration index).
message_typestringRead/WriteMessage type: "rx", "tx", "log", "event", "request", or "response".
payload_typestringRead-onlyPayload type: "binary", "text", "mqtt", "websocket_binary", "websocket_text", etc.
highlightedbooleanRead/WriteWhether the message is highlighted in the UI.
display_hexbooleanRead/WriteWhether the message displays the hex dump view in the UI.
values_jsonstring (JSON)Read-onlyAll message values as JSON. Arrays appear as null (not yet supported).
  • payload is what you parse in on_receive() and build in on_send(). Protocol headers, checksums, and delimiters are stripped.
  • frame is the raw bytes on the wire, including all protocol overhead.

Methods

Adding Values

MethodParametersDescription
add_int_value(id, value [, timestamp_us])id: string, value: integer, timestamp_us: integer (optional, µs since epoch)Add a 64-bit signed integer value. If timestamp_us is omitted, the message timestamp is used.
add_float_value(id, value [, timestamp_us])id: string, value: number, timestamp_us: integer (optional, µs since epoch)Add a 64-bit float value. If timestamp_us is omitted, the message timestamp is used.
add_string_value(id, value [, timestamp_us])id: string, value: string, timestamp_us: integer (optional, µs since epoch)Add a string value. If timestamp_us is omitted, the message timestamp is used.
add_bool_value(id, value [, timestamp_us])id: string, value: boolean, timestamp_us: integer (optional, µs since epoch)Add a boolean value. Follows Lua truthiness: only nil and false are falsy.

Querying Values

MethodParametersDescription
get_value(id)id: string or nilReturns value with original type, or nil if not found. Integer/float values can be visualized on charts.
get_metadata(id)id: string or nilReturns metadata value by id (e.g., MQTT topic, QoS), or nil if not found.
has_value(id)id: stringReturns true if a value with the given id exists, false otherwise.
value_ids()noneReturns a table (array) of all value id strings.

Removing Values

MethodParametersDescription
remove_value(id)id: stringRemoves all values with the given id. Returns true if any were removed.
clear_values()noneRemoves all values from the message.

Global Functions

Connection Query

FunctionParametersReturns
get_connection_count()nonenumber — total active connections
get_transport(connection_id)connection_id: 0-based indexstring"serial", "tcp_client", "websocket_client", "mqtt", "udp"
get_codec(connection_id)connection_id: 0-based indexstring"modbus_rtu", "modbus_tcp", "line", "frame", "passthrough"

Utility

FunctionParametersReturns
log(level, message)level: "debug"/"info"/"warn"/"error" (default "info"); message: stringnothing
get_env(var_name)var_name: stringstring value or nil if not set
send_after(payload, delay_ms, connection_id)payload: binary string (required); delay_ms: number; connection_id: 0-based (default 0)true on success, false on failure

Binary Data Reading Functions

All reading functions take (payload, offset) where payload is a binary string and offset is 1-based (Lua convention).

Integer Readers

FunctionReturns
read_u8(payload, offset)unsigned 8-bit (0–255)
read_i8(payload, offset)signed 8-bit (-128–127)
read_u16_be(payload, offset)unsigned 16-bit big-endian
read_u16_le(payload, offset)unsigned 16-bit little-endian
read_i16_be(payload, offset)signed 16-bit big-endian
read_i16_le(payload, offset)signed 16-bit little-endian
read_u32_be(payload, offset)unsigned 32-bit big-endian
read_u32_le(payload, offset)unsigned 32-bit little-endian
read_i32_be(payload, offset)signed 32-bit big-endian
read_i32_le(payload, offset)signed 32-bit little-endian

Float Readers

FunctionReturns
read_float_be(payload, offset)32-bit float big-endian (IEEE 754)
read_float_le(payload, offset)32-bit float little-endian (IEEE 754)
read_double_be(payload, offset)64-bit double big-endian (IEEE 754)
read_double_le(payload, offset)64-bit double little-endian (IEEE 754)