- 1. 1. record_* API 总表
- 2. 2. 数值表格案例(添加/修改/读取/清除/查条数)
- 3. 3. 字符串表格案例
数据记录控件 + record_* API:在 HMI 上维护历史数据表——数值型(如电压曲线数据)和字符串型(如操作日志)都能存,配表格控件显示、翻页、时间戳定位。sn 是资料采样通道索引,范围 0~19 共 20 通道,控件数据来源选「资料采集」的哪个通道,API 就操作哪个。
1. record_* API 总表
| 函数 | 说明 |
|---|---|
record_get_count(sn) |
通道已存记录总条数 |
record_write_data(sn, data) |
追加一条记录,data 为字数组(索引从 1 开始) |
record_read_data(sn, index) |
读一条,返回 data(索引从 1)和 timestamp(32 位 Unix 秒) |
record_modify_data(sn, index, data) |
改一条,index 从 0 开始,须 < record_get_count(sn) |
record_write_string(sn, strings) |
追加字符串记录,字段用分号 ; 分隔 |
record_read_string(sn, index) |
读字符串记录,返回 strings 和 timestamp |
record_modify_string(sn, index, strings) |
改字符串记录 |
record_clear(sn) |
清空通道所有记录(数值/字符串都行) |
⚠️ 若采样已启用块地址存储模式,记录不可篡改——modify 会失败。
2. 数值表格案例(添加/修改/读取/清除/查条数)
资料采集配置:触发采样、记录条数 100、数据地址 LW2000、UINT16、数据个数 10、控制地址 LW1001、采样控制地址 LW1002、启动模式 OFF→ON。数据记录控件:数据来源「[0]数值数组」、时间逆序、通道数 10、每页 5 行、翻页控制 LW1004、允许翻页 LW1005(只读)、时间戳定位 LW1006(UINT32)、选中行通知 LW1008(UINT32)。5 个按钮写 LW1000,常量 1=添加/2=修改/3=读取/4=清除/5=读条数。
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
逆序表格的行映射:控件时间逆序显示,第 0 行是最新记录。选中行通知给的是显示位置,换算公式 line = cnt - select - 1。
3. 字符串表格案例
资料采集:触发采样、记录条数 100、数据地址 LW3000、STRING、数据个数 50、控制地址 LW1501、采样控制地址 LW1502、启动模式 OFF→ON。控件:数据来源「[1]字符串」、字符串表格√、GB CODE、时间顺序(注意和数值案例的逆序不同)、通道数 10、每页 5 行、翻页 LW1201、允许翻页 LW1202、时间戳定位 LW1203、选中行通知 LW1205。5 个按钮写 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
字符串记录用分号 ; 分隔字段,和表格控件解析规则一致。顺序表格选中行不用反转,直接拿 LW1205。
整理自 广州大彩科技 VisualHMI 开发文档(hmi-doc.gz-dc.com)LUA 教程「数据记录」,版权归大彩科技所有。
发表回复