VisualHMI Free UART Protocol (Passive): on_uart_recv Custom Frame Parsing

freeFree Technical Resource

This content is free to read, suitable for basic learning and search traffic.

Custom Protocol (Passive): VisualHMI integrates protocols such as DCBUS, XGUS, Modbus (RTU/TCP Master/Slave), Mitsubishi FX2N, Delta DVP, Siemens PPI, etc., but in actual field scenarios, there are alwaysproprietary protocolsthat need to be interfaced with. LUA is used for low-level control of serial ports, enabling the transmission, reception, and parsing of custom frames. This article focuses on the passive mode:Data received triggers a callback, and you only need to parse.

1. API Summary Table

FunctionDescription
uart_setup(ch, baudrate, databits, stopbit, parity)Configure Serial Port: stopbit 0=1 bit/1=1.5 bits; parity 0=None/1=Odd/2=Even.The secondary serial port must be called to be enabled, and the primary serial port (ch=0) is only configured for data
on_uart_recv(ch, packet)passive reception callback, with packet being a byte table with subscripts starting from 1.Packet splitting/consolidation may occurmust buffer and parse the byte table
uart_send(ch, packet)itself. 1=hand over to the script for the main UART port (free protocol); 0=restore the original protocol configured in the project
set_free_protocol(en)sent by

. The condition for triggering on_uart_recv on the main UART port (ch=0) is: set the project configuration protocol to 'Custom', or set_free_protocol(1) in the script.When the main UART port is occupied by built-in protocols such as Modbus, on_uart_recv will not be called. The DCBUS/XGUS UART port parameters are fixed: 1 stop bit, no parity.

2. Protocol example: LED light control

Frame structure:

DirectionFrame header (1B)Length (1B)Function code (2B)Data (nB)Frame Tail (2B)
Device → Screen0x5ANumber of bytes from function code to frame tail0x0001~0xFFFFVariable0xFC 0xFF
Screen → Device0xA9DittoDittoVariable0xFC 0xFF

Length = Function Code 2B + Data nB + Frame Tail 2B = n+4, Big-endian. Function Code: 0x0001=Switch, 0x0002=Brightness, 0x0003=Change Name.

3. State Machine Parsing (Handling Packet Fragmentation/Coalescing)

recvBuf = {}        -- 接收缓冲
STATE_IDLE, STATE_LENGTH, STATE_DATA = 0, 1, 2
state = STATE_IDLE
frameLen = 0

function on_uart_recv(ch, packet)
  for i = 1, #packet do
    local b = packet[i]
    if state == STATE_IDLE then
      if b == 0x5A then
        state = STATE_LENGTH
        recvBuf = {}
      end
    elseif state == STATE_LENGTH then
      frameLen = b                 -- 第2字节:长度
      state = STATE_DATA
      recvBuf = {b}
    elseif state == STATE_DATA then
      table.insert(recvBuf, b)
      if #recvBuf >= frameLen then
        -- 收满一帧,解析
        parse_frame(recvBuf)
        state = STATE_IDLE
      end
    end
  end
end

function parse_frame(buf)
  local func = (buf[2] <> 8) & 0xFF, func & 0xFF}
  for i = 1, #payload do table.insert(pkt, payload[i]) end
  table.insert(pkt, 0xFC); table.insert(pkt, 0xFF)
  uart_send(0, pkt)
end

Callbacks are triggered bysystem task polling, not hardware interrupts-- Avoid performing time-consuming operations (large string concatenation, file reading and writing) in callbacks, as this can slow down the system.

Four pitfalls:① Auxiliary serial ports are disabled if uart_setup is not called,initialize all auxiliary serial ports to be used in on_init;② Packet does not guarantee a complete frame-- Packet fragmentation and coalescing must be buffered by the state machine for parsing, which is the source of 90% of bugs in passive protocols;③ When the main serial port is occupied by a built-in protocol, no callback can be received. Either set "Custom" in the project or call set_free_protocol(1);④ Calculate the length field accurately(in this example = function code 2 + data n + frame tail 2). If one frame is calculated incorrectly, it cannot be decrypted.

Compiled from Guangzhou Dacai Technology VisualHMI development documentation (hmi-doc.gz-dc.com) LUA tutorial "Custom Protocol - Passive Receiving", copyright reserved by Dacai Technology.

Put this resource to use in a real project?

Go to the Tool Center for message parsing, CRC verification and device debugging, or submit your requirements for selection and integration advice.

Engineer Membership

Turn this article into actionable debugging resources

After activation, you can use advanced message parsing, resource pack downloads, code examples, engineering cases and priority technical support, suitable for real project delivery.

Unlimited Advanced Tools
Resource & Code Packs
Complete Engineering Case Library
Priority Technical Support

Leave a Reply

Your email address will not be published. Required fields are marked *.