--函数名称:my_getedataLen_Type
--功能说明:获取对应数据的类型和长度
--形 参:data:待传入的数据
--返 回 值:datalen:数据的长度 data_type:数据的类型
function my_getdataLen_Type(data)
local datalen = -1
local data_type = type(data)
if data_type == 'number' then
-- 将数字转换为字符串后再计算长度
datalen = string.len(tostring(data))
elseif data_type == 'string' then
datalen = string.len(data)
elseif data_type == 'table' then
datalen = #data
end
return datalen, data_type
end
--函数名称:my_split
--功能说明:将一个字符串按照指定的分隔符分割成多个子字符串,并将这些子字符串存储在一个表(数组)中返回。
--形 参:str:字符串 pat:分隔符
--返 回 值:返回存储所有子字符串的表 t
function my_split(str, pat)
local i = 1 -- 初始化计数器 i,用于记录分割后的子字符串数量
local t = {} -- 创建一个空表 t,用于存储分割后的子字符串
local last_end = 0 -- 初始化变量 last_end,记录上一个分隔符结束的位置
local s, e = string.find(str, pat, 1) -- 在字符串 str 中从位置 1 开始查找分隔符 pat,返回分隔符的起始位置 s 和结束位置 e
while s -- 如果找到了分隔符(s 不为 nil)
do
table.insert(t, string.sub(str, last_end + 1, last_end + s - last_end - 1)) -- 截取从 last_end + 1 到分隔符起始位置 s - 1 ?淖幼址⒔洳迦氲奖?t 中
last_end = e -- 更新 last_end 为当前分隔符结束的位置 e
s, e = string.find(str, pat, last_end + 1) -- 在字符串 str 中从 last_end + 1 的位置开始继续查找分隔符 pat
i = i + 1 -- 增加计数器 i
end
if last_end <= #str --如果 last_end 小于等于字符串 str 的长度(即还有未处理的剩余部分)
then
cap = string.sub(str, last_end + 1) -- 截取从 last_end + 1 到字符串结尾的子字符串
table.insert(t, cap) -- 将该子字符串插入到表 t 中
end
return t -- 返回存储所有子字符串的表 t
end
--函数名称:my_ShowRecord_FromFile
--功能说明:用于处理从文件中读取的字符串数据,按行分割后,去除每行中的回车符和换行符,并在每行末尾添加分号,最后将处理后的数据添加到记录显示区域。
--形 参:mystr:读取的字符串
--返 回 值:无
function my_ShowRecord_FromFile(mystr)
local line_data = my_split(mystr, '% ') -- -- 调用 my_split 函数,将 mystr 字符串按照换行符 '\n' 分割成多个子字符串,存储在 line_data 表中
for i = 1, #(line_data) -- 遍历 line_data 表中的每个元素
do
if line_data[i] ~= nil -- 如果当前元素不为空
then
feed_dog() -- 调用 feed_dog 函数(可能是用于防止系统“看门狗”超时等操作)
-- 去除当前行数据中的回车符 ' ' 和换行符 '\n'
line_data[i] = string.gsub(line_data[i], "% ", '')
line_data[i] = string.gsub(line_data[i], "%\n", '')
--line_data[i] = line_data[i]..';' -- 在当前行数据末尾添加分号 ';'
-- 将处理后的行数据添加到记录显示区域 sc_ShowRecord 中,格式为 '$NUM;' 加上处理后的行数据
set_history_graph_value(26,6,line_data[i])
set_value(26,9,line_data[i])
end
end
end
function my_read_filedata(file)
local count = 0 --每次要读取到的字节数
local read_cnt = 0 --要读取多少次 多少块
local offset = 0 --读取偏移地址
local all_byte = 0 --要读取的所有的字节数
local read_byte_Tb = {} --用于存储从文件中读取的原始字节数据
local read_char_Tb = {} --用于将字节数据转换为字符数据
local read_str = '' --用于存储最终拼接成的字符串形式的文件内容。
local open_state = file_open(file, FA_READ) --以只读的方式进行打开
if open_state == true
then
set_text(26, 5, 'open on')
--获取当前文件大小
all_byte = file_size()
if all_byte > 0
then
read_cnt = math.modf(all_byte/WriteOnceSize) --计算出完整的有多少块
if all_byte % WriteOnceSize > 0
then
read_cnt = read_cnt + 1 --最后一块未满的一块
end
for i = 1, read_cnt
do
--复位读字节数组
read_byte_Tb = {} --用于存储从文件中读取的原始字节数据
read_char_Tb = {} --用于将字节数据转换为字符数据
--计算读取的偏移位置
offset = (i - 1) * WriteOnceSize
local offst_result = file_seek(offset)
--文件偏移失败
if offst_result == false
then
set_text(26, 5, 'offest fail')
break
else
set_text(26, 5, 'offest on')
end
--计算本次读的个数
count = WriteOnceSize
if i == read_cnt --最后一块
then
if all_byte % WriteOnceSize > 0
then
count = all_byte % WriteOnceSize
end
end
--读取字节转换为字符并拼接成字符串
read_byte_Tb = file_read(count)
if #(read_byte_Tb) > 0
then
set_text(26, 5, 'read ok')
for j = 0, #(read_byte_Tb)
do
read_char_Tb[j + 1] = string.char(read_byte_Tb[j]) --将当前字节的数据转换为字符 从下标1开始 Lua 中数组索引通常从 1 开始
end
read_str = read_str..table.concat(read_char_Tb) --将read_char_Tb数组中的每一个数据拼接到一个字符串中 追加到 read_str 字符串变量中,从而逐步构建完整的文件内容字符串
elseif read_byte_Tb ==nil --显示文件读取错误的提示信息,提醒用户重新尝试
then
set_text(26, 5, 'read fail')
break;
end
end
my_ShowRecord_FromFile(read_str)
else --区域显示文件不存在或无内容的提示信息,提醒用户检查 SD 卡中的文件
set_text(26, 5, 'dont exist')
end
else
set_text(26, 5, 'open fail')
end
--关闭文件
file_close()
end
--函数名称:my_write_filedata
--功能说明:使对应的要写入的数据的类型值 转成数值类型存到发送函数中 利用file_write() :函数进行发送
--形 参:file:文件的绝对路径 data:要写入SD卡的数据 open_mode:打开的模式
--返 回 值:无
function my_write_filedata(file, data, open_mode)
local count = 0 -- 当前写入块大小(每次)
local write_cnt = 0 -- 总写入次数 要写入几次才能写完
local seek_ops = 0 -- 写入偏移量
local all_byte = 0 -- 原文件大小(追加模式用) 在原有文件大小的后面进行添加
--获取待写入数据的数据类型和长度
local wrire_len, data_type = my_getdataLen_Type(data)
local write_byte_Tb = {} -- 写入缓存
--打开文件
local open_state = file_open(file, open_mode)
if open_state == true
then
set_text(28, 6, 'open on')
--获取当前文件大小,仅在追加文件末尾写入执行 追加模式获取原文件大小
if open_mode == add_write
then
all_byte = file_size()
end
--若有数据要写入
if wrire_len > 0
then
--计算data要写多少次 计算写入次数 也就是有多少块数据
write_cnt = math.modf(wrire_len / WriteOnceSize)
if wrire_len % WriteOnceSize > 0
then
write_cnt = write_cnt + 1
end
--循环分块写入
for i = 1, write_cnt
do
--复位写字节数组 清空临时表
write_byte_Tb = {}
--计算写的位置
seek_ops = (i - 1) * WriteOnceSize +all_byte
local offst_result = file_seek(seek_ops)
--若文件偏移失败
if offst_result == false
then --提示信息
set_text(28, 6, 'offst off')
break
else
set_text(28, 6, 'offst ok')
end
--计算本次写的个数(字节数)
count = WriteOnceSize --当前写入块的大小
if i == write_cnt --最后一块
then
if wrire_len % WriteOnceSize > 0
then
count = wrire_len % WriteOnceSize --当前写入块的大小
end
end
--填充写入字节数组 准备写入数据
for j = 1, count
do
--if data_type == 'string' or data_type == 'number'
if data_type == 'string'
then
--字符串类型,将每个字符转换为字节数组 -- 字符串转字节
--local str = (data_type == 'string') and data or tostring(data)
--write_byte_Tb[j - 1] = string.byte(str, ((i - 1) * WriteOnceSize + j), ((i - 1) * WriteOnceSize + j))
--tonumber():将结果显式转换为数值类型 --字符串类型,将每个字符转换为字节数组 填写到发送函数中
write_byte_Tb[j - 1] = tonumber(string.byte(data, ((i - 1) * WriteOnceSize + j), ((i - 1) * WriteOnceSize + j)))
elseif data_type == 'table'
then
--数组类型,字节赋值 -- 直接使用字节数组
write_byte_Tb[j - 1] = data[((i - 1) * WriteOnceSize + j)]
end
end
-- 执行写入
local IswriteOK = file_write(write_byte_Tb)
if IswriteOK == false
then
set_text(28, 6, 'write off')
else
set_text(28, 6, 'write ok')
end
end
end
else
set_text(28, 6, 'open fail')
end
--关闭文件
file_close()
end
local gas =0 --保存采集到气体的浓度值
--定时回调函数,系统每隔1秒钟自动调用
function on_systick()
gas= get_value(0,14)
--local tabl ={}
--tabl[1]=a
--local str = " ".. tostring(gas)
local str = tostring(gas).." "
my_write_filedata(sd_dir..'/'..'1.txt', str, add_write)
--读取
my_read_filedata(sd_dir..'/'..'1.txt')
end
现在读写都能够正常运行 但发现了一个bug 现在sd已经有下入的浮点数数据 我想要利用函数my_read_filedata进行只读 结果是卡在一个数进行不动了 这是什么原因
最新发布