VisualHMI data recording control: record_* API numerical/string table operations

freeFree Technical Resource

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

Data recording control + record_* API: Maintain thehistorical data tableon the HMI - both numeric types (such as voltage curve data) and string types (such as operation logs) can be stored, with table controls for display, paging, and timestamp positioning. sn is thedata sampling channel index, ranging from 0 to 19 for a total of 20 channels. When selecting a channel from the "Data Collection" for the control data source, the API operates on that channel.

1. record_* API general table

functiondescription
record_get_count(sn)Total number of records stored for the channel
record_write_data(sn, data)Append a record, with data being a character array (index starts from 1)
record_read_data(sn, index)Read one, return data (index starts from 1) and timestamp (32-bit Unix seconds)
record_modify_data(sn, index, data)Modify one, index starts from 0 and must be < record_get_count(sn)
record_write_string(sn, strings)Append string record, with fields separated by semicolons (;)
record_read_string(sn, index)Read string record, return strings and timestamp
record_modify_string(sn, index, strings)Modify string record
record_clear(sn)Clear all records in the channel (either numeric or string values)

⚠️ If sampling is enabledBlock address storage mode, recordcannot be tampered with——modify will fail.

2. Numeric table case (add/modify/read/clear/check number of entries)

Data acquisition configuration: Trigger sampling, record count 100, data address LW2000, INT16, data quantity 10, control address LW1001, sampling control address LW1002, start mode OFF→ON. Data recording control: Data source "[0] numeric array", time in reverse order, channel count 10, 5 rows per page, page turning control LW1004, page turning allowed LW1005 (read-only), timestamp positioning LW1006 (UINT32), selected row notification LW1008 (UINT32). 5 buttons labeled LW1000, constants 1=add/2=modify/3=read/4=clear/5=read count.

addByteRatio = 0     -- 累加器,让每次添加的数据不同

function on_update(slave, vtype, addr)
  if vtype == VT_LW then
    if addr == 0x1100 then              -- 操作按钮
      local key = get_uint16(VT_LW, 0x1100)
      local dataTb = {}
      if key == 1 then                  -- 添加
        addByteRatio = addByteRatio + 1
        for i = 1, 10 do
          dataTb[i] = i * addByteRatio  -- 第一次 1..10,第二次 2..20
        end
        record_write_data(0, dataTb)
      elseif key == 2 then              -- 修改选中行
        local cnt = record_get_count(0)
        local line = cnt - get_uint32(VT_LW, 0x1105) - 1  -- 逆序表格行映射
        for i = 1, 10 do dataTb[i] = 12345 end
        record_modify_data(0, line, dataTb)
      elseif key == 3 then              -- 读取选中行
        local cnt = record_get_count(0)
        local line = cnt - get_uint32(VT_LW, 0x1105) - 1
        local data, timeUnix = record_read_data(0, line)
        local msg = "时间:"..timeUnix.." 数据:"
        for i = 1, 10 do msg = msg..data[i].." " end
        set_string(VT_LW, 0x1160, msg)
      elseif key == 4 then
        record_clear(0)                 -- 清除
      elseif key == 5 then
        set_string(VT_LW, 0x1160, "条数:"..record_get_count(0))
      end
    end
  end
end

Row mapping for reverse order table: Display control time in reverse order, with the latest record in row 0. The selected row is notified to the display position. Conversion formulaline = cnt - select - 1.

3. String table case

Data collection: Trigger sampling, record count 100, data address LW3000, STRING, number of data 50, control address LW1501, sampling control address LW1502, start mode OFF→ON. Controls: Data source "[1] string", string table √, GB CODEtime sequence(note that it is different from the reverse order in numerical cases), channel count 10, 5 rows per page, page turning LW1201, allow page turning LW1202, timestamp positioning LW1203, selected row notification LW1205. Write 5 buttons as LW1200.

addStr = 0
function on_update(slave, vtype, addr)
  if vtype == VT_LW and addr == 0x1200 then
    local key = get_uint16(VT_LW, 0x1200)
    if key == 1 then                    -- 添加
      local cnt = record_get_count(1)
      local line = get_uint32(VT_LW, 0x1205)   -- 顺序表格不反转
      if cnt < line - 1 then return end
      addStr = addStr + 10
      local dataStr = ""
      for i = 1, 10 do
        dataStr = dataStr.."第"..(addStr - 10 + i).."个;"
      end
      record_write_string(1, dataStr)   -- "第1个;...第10个;"
    elseif key == 2 then                -- 修改
      local line = get_uint32(VT_LW, 0x1205)
      record_modify_string(1, line, "change;change;change;change;change;change;change;change;change;change;")
    elseif key == 3 then                -- 读取
      local line = get_uint32(VT_LW, 0x1205)
      local dataStr, timeUnix = record_read_string(1, line)
      set_string(VT_LW, 0x1260, timeUnix..dataStr)
    elseif key == 4 then
      record_clear(1)
    elseif key == 5 then
      set_string(VT_LW, 0x1260, "条数:"..record_get_count(1))
    end
  end
end

String records are separated by semicolons (;) to denote fields, consistent with the parsing rules of the table control. Selected rows in the sequential tabledo not need to be reversed, directly use LW1205.

Four pitfalls:① The sn channel number must match the data collection index selected for the control data source; ② The numerical record data is an array with subscripts starting from 1, not starting from 0 as per C convention;③ For reverse-order table row mapping, it is necessary to reverse the order(line = cnt - select - 1), which is not required for sequential tables;④ In block address storage mode, records cannot be modified, and to change data, do not power off the storage.

Compiled from Guangzhou Dacai Technology VisualHMI Development Documentation (hmi-doc.gz-dc.com) LUA Tutorial "Data Recording", 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 *.