无线模块设计了一个计数器,用于记录启动次数,放在 count.dat 文件中。
count.dat ASCII 编码的纯文本文件,烧录时只有一个字节:0
--2G Air202 模块启动次数计数
local startCountor = 0
local function StartCount()
local file = io.open("/ldata/count.dat", "r")
--startCountor = file:read("*n") --file:read("n") 2G模块读取返回 nil
startCountor = tonumber(file:read("*l")) --这样读取就没问题了
file:close()
if startCountor == nil then
startCountor = 0
end
if startCountor >= 2147483647 then --最大的32位有符号整形数字:2147483647 = 0x7FFF FFFF
startCountor = 0
end
file = io.open("/ldata/count.dat", "w")
startCountor = startCountor + 1
file:write(startCountor)
file:close()
end
--4G Air720SL-NP 模块启动次数计数
local startCountor = 0
local function StartCount()
local file = io.open("/lua/count.dat", "r")
--startCountor = file:read("*n") --file:read("n") 4G模块读取返回 nil
startCountor = tonumber(file:read("*l")) --这样读取就没问题了
file:close()
if startCountor == nil then
startCountor = 0
end
if startCountor >= 2147483647 then --最大的32位有符号整形数字:2147483647 = 0x7FFF FFFF
startCountor = 0
end
file = io.open("/lua/count.dat", "w")
startCountor = startCountor + 1
--file:write(startCountor) --file:write(startCountor) 4G 模块写错误提示:desc=Exception - Branch to 0x0 detected
file:write(tostring(startCountor)) --无错误提示,但写不成功,内容没变
file:close()
end