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
| Function | Scope/Description | Address 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/1 | 1 |
set/get_uint16 | 0~65535, truncated if out of range & 0xFFFF | 1 |
set/get_int16 | -32768~+32767 | 1 |
set/get_uint32 | 0~4294967295 | +2 |
set/get_int32 | -2^31~2^31-1 | +2 |
set/get_uint64 | 0~2^64-1 | +4 |
set/get_int64 | -2^63~2^63-1 | +4 |
set/get_float | IEEE 754 single precision | +2 |
set/get_double | IEEE 754 double precision | +4 |
set_string(vtype,addr,str) | Maximum 2K bytes, UTF-8/GBK; get_string defaults to 128 bytes, maximum 2048, truncated | Write in batches by length |
set_uint16_ex(vtype,addr,v1,...,vN) | , up to 120 registers | N |
set_array(vtype,addr,buff) | Accepts Lua table (word array), up to 120 words | N |
3. vtype value (varies with protocol)
| Constant | Meaning |
|---|---|
| VT_0x | Coil |
| VT_1x | Input |
| VT_M | FX3U Internal Relay |
| VT_3x | Input Register |
| VT_4x | Keep register |
| VT_D | FX3U D register |
| VT_LW | Internal variable (not saved during power loss) |
| VT_RW | Power-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
endThis 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.
Leave a Reply