VisualHMI register operation: detailed explanation of the full range of get/set APIs and caching mechanism

freeFree Technical Resource

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

The register operation API is the entire toolkit for LUA to read and write data: the set/get series covers all data types, set_bit handles bits, and set_array performs batch writes. The core mechanism to remember first:get_xxx functions all read from the HMI local cache (memory mapping) without sending serial port frames-- zero communication overhead.

1. Reading mechanism (important)

get_xxx() series functionsdo not send real-time serial port requests to the PLC. The HMI background task automatically reads communication variables (such as Modbus 3x/4x) from the PLC according to the polling cycle configured by the project and caches them. Scripts only read from the cache. The advantage is to avoid communication storms caused by script logic -- you can get ten thousand times without overwhelming the bus.

2. API summary table

FunctionScope/DescriptionAddress span
set_bit(vtype,addr,value,count)Batch bit write, value low count bits correspond to consecutive bits starting from addr (count 1~16); get_bit returns 0/11
set/get_uint160~65535, truncated if out of range & 0xFFFF1
set/get_int16-32768~+327671
set/get_uint320~4294967295+2
set/get_int32-2^31~2^31-1+2
set/get_uint640~2^64-1+4
set/get_int64-2^63~2^63-1+4
set/get_floatIEEE 754 single precision+2
set/get_doubleIEEE 754 double precision+4
set_string(vtype,addr,str)Maximum 2K bytes, UTF-8/GBK; get_string defaults to 128 bytes, maximum 2048, truncatedWrite in batches by length
set_uint16_ex(vtype,addr,v1,...,vN), up to 120 registersN
set_array(vtype,addr,buff)Accepts Lua table (word array), up to 120 wordsN

3. vtype value (varies with protocol)

ConstantMeaning
VT_0xCoil
VT_1xInput
VT_MFX3U Internal Relay
VT_3xInput Register
VT_4xKeep register
VT_DFX3U D register
VT_LWInternal variable (not saved during power loss)
VT_RWPower-loss save area

4. Data replication case (general routine for 12 group modes)

Most common requirement: External data is written to LW, and the script is moved to another address or converted in format. The routine is to determine vtype==VT_LW and the source address in on_update, and use get to read and set to write the target:

function on_update(slave, vtype, addr)
  if vtype == VT_LW then
    if addr == 0x1000 then
      set_uint16(VT_LW, 0x1001, get_uint16(VT_LW, 0x1000))      -- uint16 复制
    elseif addr == 0x1004 then
      set_uint32(VT_LW, 0x1006, get_uint32(VT_LW, 0x1004))      -- uint32(跨度+2)
    elseif addr == 0x101C then
      set_float(VT_LW, 0x101E, get_float(VT_LW, 0x101C))        -- float(跨度+2)
    elseif addr == 0x1030 then
      set_string(VT_LW, 0x1040, get_string(VT_LW, 0x1030, 16))  -- string(跨度16)
    end
  end
end

-- 批量写:addr==0x1050 且值为偶数时写 10 个寄存器
function on_update(slave, vtype, addr)
  if vtype == VT_LW and addr == 0x1050 then
    local v = get_uint16(VT_LW, 0x1050)
    if v % 2 == 0 then
      set_uint16_ex(VT_LW, 0x1060, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
      -- 或
      set_array(VT_LW, 0x1060, {10, 20, 30, 40, 50, 60, 70, 80, 90, 100})
    end
  end
end
Three pitfalls:① Data types above 32 bits have address spans-- uint32/float occupy 2 registers, uint64/double occupy 4, so don't overlap addresses during continuous reading and writing;②set automatically truncates values beyond the specified range(such as & 0xFFFF), without generating an error or carrying the value over. You may not be aware of the incorrect value;③get_string by default reads 128 bytes, but if you need a longer string, you must explicitly pass in the length (up to 2048). The string is truncated at position .

This content is adapted from the VisualHMI development documentation provided by Guangzhou Dacai Technology (hmi-doc.gz-dc.com), specifically the LUA tutorial on "Register Operations". The copyright belongs to 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 *.