大彩串口屏读写文件问题

分区

本文使用的是大彩串口屏M系列的:

串口屏内部有三个分区,分别为A、B、C三个区:

A区:系统区,存储组态工程文件

B区:数据区,存储配置信息,记录数据、历史曲线等

C区:备份区,备份数据区文件

在进行文件读写的时候,建议卸载B区,避免破坏A区、C区文件,B取文件读写需要注意以下两点:

·手动创建文件夹:在工程目录下创建"Public"文件夹,此文件夹表示B区,此后读写操作均在B区完成

分配空间大小:在工程属性-高级设置-存储分区B(Mbyte)中,需要分配大小大于0,一般默认即可。

路径

屏内文件读取

假设对屏内的test文件读写,路径如下所示

lua 复制代码
local file_path = 'B:/test.txt'
SD卡/U盘路径读取

读写SD卡或U盘里的"test.txt"文件时,应巧妙利用LUA脚本SD卡、USB插入触发的回调函数里获取盘符,如下所示:

lua 复制代码
local file_path = ''

function  on_usb_inserted(dir)
    file_path = dir..'/'..'test.txt'
end

function  on_sd_inserted(dir)
    file_path = dir..'/'..'test.txt'
end

写文件:读写屏内文件

1.创建Public文件夹,并在该文件夹中创建config.txt文件

config.txt文件中的键值对要与代码中的键值匹配,否则会导致读取异常

2.编写LUA代码

具体代码如下:

lua 复制代码
local config_file_path = 'B:/config.txt'

-- 配置项
local config_table		= {
	device_id = "test123456",
	verify_pwd = 123456,
	alert_stage_1sd_duration_s = 25, -- 第一阶段时长,人员警报秒速
	alert_stage_2nd_duration_s = 15, -- 第二阶段时长,关闭电磁阀秒数
	is_auto_open_gas_valve = false, --人员在场是否自动打开电磁阀
	is_reverse_open_gas_valve = false,--人员在场是否取反打开电磁阀
	is_maintain_open_gas_valve = false,--人员在场是否持续打开电磁阀
	alert_capture_duration_s = 10,
	report_url = "28.19.41.69:8601/MsgPoster",
	network_param = {
		dhcp = 0,
		ipaddr = "",
		netmask = "",
		gateway = "",
		dns = ""
	}
} -- 配置文件


-- 将当前的配置参数保存起来
function save_config_table()
	local jsonStr = cjson.encode(config_table)
	set_text(1,33," ")	
	write_file(config_file_path, jsonStr)
end


-- 加载配置文件
function load_config_table()
	local config1 = read_file(config_file_path)
	
	if config1 ~= nil then
		local data1 = cjson.decode(config1)
		set_text(1,33," ")
		if data1 ~= nil then
			config_table = data1
 			set_text(1,32,"Success")
			return
		end
	end
 -- 没有加载成功,将生成默认配制
	config_table = {
		device_id = "sfm123456",
		verify_pwd = 123456,
		alert_stage_1sd_duration_s = 25,
		alert_stage_2nd_duration_s = 15,
		alert_capture_duration_s = 12,
		is_auto_open_gas_valve = false,
		is_maintain_open_gas_valve = false,
		is_reverse_open_gas_valve = false,
		report_url = "28.19.41.69:8601/MsgPoster",
		network_param = {
			dhcp = 0,
			ipaddr = "",
			netmask = "",
			gateway = "",
			dns = ""
		}
	} -- 配置文件	
	set_text(1,32,"Error")
end

---输入数据
---@param file_path string 路径
---@param info string 文件内容
function write_file(file_path, info)
	-- 以覆盖写入的方式打开文本
	local wfile = io.open(file_path, "w")
	if wfile ~= nil then
		wfile:write(info)
 		set_text(1,33," ")	
	end
	set_text(1,33," ")	
	wfile:close()
	flush_nor()
end
相关推荐
Lill_bin1 天前
Lua编程语言简介与应用
开发语言·数据库·缓存·设计模式·性能优化·lua
两水先木示1 天前
【Lua坑】Lua协程coroutine无法正常完整执行问题
开发语言·lua·协程·对象池
合宙Luat4 天前
从边缘到云端,合宙DTU&RTU打造无缝物联网解决方案
嵌入式硬件·物联网·硬件工程·lua
筑天兄5 天前
【Lua学习】Lua入门
学习·junit·lua
z2014z5 天前
Lua 与 C#交互
开发语言·c#·lua
z2014z5 天前
Lua热更
开发语言·lua
danplus5 天前
Lua发邮件:实现自动化邮件发送教程指南!
服务器·lua·api接口·邮件营销·邮件群发·蜂邮edm邮件营销·邮件接口
IT_Octopus6 天前
Springboot redis luttuce lua脚本 存储压缩的protobuf key:byte[] value:map<byte,byte[]>
spring boot·redis·lua
shall_zhao8 天前
黑马点评18——多级缓存-OpenResty
redis·缓存·lua·openresty
代码是谁9 天前
centos8构建nginx1.27.1+BoringSSL+http3+lua+openresty
nginx·lua·http3·openresty