1 前言
打算将数据存入文件系统。
2 luatos文件系统
https://wiki.luatos.com/develop/filesystems.html
3 fs——查看文件信息
3.1 接口
3.2 测试代码
tag = "fsTest"function fs_info()if fs == nil thenlog.error(tag, "this fireware is not support fs")returnendlog.info(tag, "START")log.info(tag .. ".fsstat:", fs.fsstat())log.info(tag .. ".fsstat:/", fs.fsstat("/"))log.info(tag .. ".fsstat:/luadb/", fs.fsstat("/luadb/"))log.info(tag .. ".fsize:/luadb/code.png", fs.fsize("/luadb/code.png"))log.info(tag .. ".fsize:/luadb/main.luac", fs.fsize("/luadb/main.luac"))log.info(tag .. ".fsize:/luadb/main.lua", fs.fsize("/luadb/main.lua"))-- names = io.lsdir("/ldata/")-- log.info("dir",names)-- log.info(tag .. ".fsize", fs.fsize("/luadb/main.luac"))log.info(tag, "DONE")
end
3.3 结果
fsstat是针对文件系统,例如"/luadb" “/sd”
没有挂载sd卡也会报错。
4 io接口
LuatOS-master\LuatOS\lua\src\liolib.c
当前支持的io接口
update_luadb_inline.lua (LuatOS-master\LuatOS\bsp\win32\tools)
5 IO读取luadb目录下文件
5.1 接口io.read
io.read -> f_read -> g_read
5.2 下载txt文件进luadb
创建一个txt文件,然后输入aaaa
5.3 代码
function fs_read_txt()local file_path = "/luadb/boot_time.txt" --luadb只读local f_read_txt = io.open(file_path, "rb")log.info("f_read_txt:",type(f_read_txt)) -- userdataif f_read_txt thenlog.info("f_read_txt ok")local data = f_read_txt:read("*a")log.info("data type = "..type(data)) -- string-- if data == "" then-- data = "0"-- endlog.info("fs","data", "["..data.."]", type(data))local data_temp = data:toHex()log.info("fs","data","["..data_temp.."]", type(data_temp))f_read_txt:close()endif fs thenlog.info("fsstat", fs.fsize(file_path))end
end
5.4 结果
toHEX可以将字符串转为ascii码值
6 IO写入
6.1 接口io.write
io.write -> f_write -> g_write
6.2 代码
fs_read_txt("/luadb/boot_time.txt")function fs_write_txt(file_path)log.info("fs_write_txt------------------{")-- local file_path = "/test_write.txt" --luadb只读local f_write_txt = io.open(file_path, "wb+") --读写方式打开或建立一个二进制文件,允许读和写log.info("fs_write_txt:",type(f_write_txt)) -- userdataif f_write_txt thenlog.info("f_write_txt ok")local data = f_write_txt:write("aaa")log.info("fs","data", "["..data.."]", type(data))f_write_txt:close()endif fs thenlog.info("fsstat", fs.fsize(file_path))endlog.info("fs_write_txt------------------}")
end
无法运行,看起来无法在"/"目录下直接创建文件
7 小结
后续尝试加上sd卡,不过由于esp32只有一个spi,需要实现spi复用,打算使用105看看。