- 1. 1. API 总览
- 2. 2. 数值表格案例
- 3. 3. 字符串表格案例
record_* 系列 API 操作数据记录控件的历史数据:添加、修改、读取、清除、查条数。数据记录控件有”数值表格”和”字符串表格”两种形态,回调操作方式不同。
1. API 总览
| 函数 | 说明 |
|---|---|
| record_get_count(sn) | 获取通道 sn 已存储记录总条数 |
| record_write_data(sn, data) | 向通道追加一条记录,data 为字数组(索引从 1 开始) |
| record_read_data(sn, index) | 读第 index 条记录,返回 data 数组 + 32位时间戳 |
| record_modify_data(sn, index, data) | 修改第 index 条记录内容 |
| record_write_string(sn, strings) | 字符串通道追加记录,分号 ; 分隔字段 |
| record_read_string(sn, index) | 读字符串记录,返回 strings + 时间戳 |
| record_modify_string(sn, index, strings) | 修改字符串记录 |
| record_clear(sn) | 清空通道所有记录 |
sn 是资料采样通道索引(0~19,共 20 通道)。⚠️ 若采样启用了块地址存储模式,数据不可篡改(record_modify_data 无效)。

2. 数值表格案例
资料采集配置:触发采样、记录条数 100、数据地址 LW2000、UINT16、数据个数 10、控制地址 LW1001、采样控制地址 LW1002、启动模式 OFF→ON。
控件配置:数据来源”资料采集[0]数值数组”、时间逆序、显示时间、通道数 10、固定列数 2(序号+时间)、每页行数 5、翻页控制 LW1004、允许翻页 LW1005(只读)、时间戳定位 LW1006(UINT32)、选中行通知 LW1008(UINT32)。
5 个按钮:写入地址 LW1000,常量 1/2/3/4/5 = 添加/修改/读取/清除/读取记录数。
addByteRatio = 1
function on_update(slave, vtype, addr)
if vtype ~= VT_LW then return end
if addr == 0x1100 then -- 操作地址
local key = get_uint16(vtype, 0x1100)
local cnt = record_get_count(0)
-- 逆序表格: 选中行号要反转
local number_select_line = cnt - get_uint32(vtype, 0x1105) - 1
if key == 1 then
local dataTb = {}
for i=1,10 do dataTb[i] = i*addByteRatio end
addByteRatio = addByteRatio + 1
record_write_data(0, dataTb)
elseif key == 2 then
local dataTb = {}
for i=1,10 do dataTb[i] = 12345 end
record_modify_data(0, number_select_line, dataTb)
elseif key == 3 then
local dataTb, timeUnix = record_read_data(0, number_select_line)
local dataMsg = ""
for i=1,#dataTb do dataMsg = dataMsg..dataTb[i]..";" end
set_string(VT_LW, 0x1160, timeUnix..dataMsg)
elseif key == 4 then
record_clear(0)
elseif key == 5 then
set_string(VT_LW, 0x1160, record_get_count(0))
end
end
end

3. 字符串表格案例
资料采集:触发采样、记录条数 100、数据地址 LW3000、STRING、数据个数 50、控制地址 LW1501、采样控制地址 LW1502。
控件:资料采集索引”[1]字符串”、字符串表格√、字符编码 GB CODE、时间顺序、显示日期和时间、通道数 10、固定列数 2、每页行数 5、翻页控制 LW1201、允许翻页 LW1202、时间戳定位 LW1203、选中行通知 LW1205。
addStr = 1
function on_update(slave, vtype, addr)
if vtype ~= VT_LW then return end
if addr == 0x1200 then
local key = get_uint16(vtype, 0x1200)
local cnt = record_get_count(1)
-- 顺序表格: 选中行号不需要反转
local string_select_line = get_uint32(vtype, 0x1205)
if key == 1 then
if cnt < string_select_line - 1 then return end
local dataStr = ""
for i=1,10 do dataStr = dataStr.."第"..(i+addStr*10-10).."个;" end
addStr = addStr + 1
record_write_string(1, dataStr)
elseif key == 2 then
record_modify_string(1, string_select_line, "change;change;change;change;change;change;change;change;change;change;")
elseif key == 3 then
local dataStr, timeUnix = record_read_string(1, string_select_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

整理自 广州大彩科技 VisualHMI 开发文档(hmi-doc.gz-dc.com)LUA 教程「数据记录」,版权归大彩科技所有。
发表回复