LUA 文件读写整数问题

无线模块设计了一个计数器,用于记录启动次数,放在 count.dat 文件中。
count.dat ASCII 编码的纯文本文件,烧录时只有一个字节:0

  1. Air202/Air720SL-NP 读取:
    startCountor = file:read("*n")                  --读取返回 nil,应返回一个整数
    startCountor = tonumber(file:read("*l"))  --读取正常
  2. Air202写入:
    file:write(startCountor)                     --写入正常
  3. Air720SL-NP写入:
    file:write(startCountor)                       --有写入错误,提示:desc=Exception - Branch to 0x0 detected
    file:write(tostring(startCountor))         --无错误提示,但写不成功,内容没变


--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


请先 登录 后评论
  • 0 关注
  • 0 收藏,2328 浏览
  • ramrocket 提出于 2020-03-07 10:09

相似问题