金沙白浪 发表于 2023-8-9 13:55:53

请教如何使用write_flash()和read_flash()存取数据表?

有这么一个参数数据表,需要在LUA中保存到flash,其中维度不变,只是其中每个元素的数值可能发生变化,请问这样的表可以实现flash存取吗?
local lookup_V2E = {       
        ={0, 0, 0, 0, 0, 0, 0, 0, 0, 0},                               
        ={0.0966, -27.361, 600, 600, 600, 600,600, 600,0,0,},                       
        ={0.1118, -31.904, 600, 600, 600, 600,600, 600,0,0,},                       
        ={0.1927, -48.621, 600, 600, 500, 425,285,0,0,0,},                       
        ={0.1822, -43.883, 600, 600, 500, 400,0,0,0,0,},                       
}

If后要接end 发表于 2023-8-9 16:51:06

转成json字符串的格式写到flash中看看

金沙白浪 发表于 2023-8-11 14:16:19

我把表转换成了一串字符串,长度大约是600,并使用函数   
write_flash(addr, lengthBytes)
write_flash(addr + 30, data)-- 地址偏移4字节,以跳过存储的长度
flush_flash()
进行存储,存储后随机读取数据        lengthBytes = read_flash(addr, 6),检查后确定读取的值和写入的值一致。

但是掉电后再次进行flash,却发现读取到的内容同之前写入的不一样,请问是什么原因导致的?已经检查了flash的起始地址,没有发生冲突的地方。是否忽略了什么控制flash读写的API?

If后要接end 发表于 2023-8-11 17:15:30

以字符串的形式写入到flash里面,不需要额外开辟一个控件保存写入的字符串长度,write_flash中已经会将需要写入的长度和字符串数据一起写到flash中,读取只需用read_flash_string(),传递之前写入的起始地址即可获取全部字符串内容

金沙白浪 发表于 2023-8-14 17:07:32

请问如何判断指定地址是否已经存储了字符串?
我是用如下语句用于判断指定地址是否已经存储了参数,但似乎每次查询结果都是未初始化,是不是我的判断方式有问题?
        local temp_string = read_flash_string(flash_addr.V2E)


        if (not temp_string)then
                print("no flash data on :"..flash_addr.V2E)

If后要接end 发表于 2023-8-14 17:12:00

金沙白浪 发表于 2023-8-14 17:07
请问如何判断指定地址是否已经存储了字符串?
我是用如下语句用于判断指定地址是否已经存储了参数,但似乎 ...

判断temp_string ~= nil

金沙白浪 发表于 2023-8-14 17:46:01

我按照您的建议修改了初始化代码,但还是不起作用,每次开机都显示读不到字符串,必须重新初始化参数,会不会是这个API能保存的字符串长度是有限制的?
        local temp_string = read_flash_string(flash_addr. V2E)


        if temp_string ~= nilthen

                print("Initflash data read Success! ")

        else
                print("no flash data on :".. flash_addr. V2E)

                -- Write initial parameters to flash
                save_table_to_flash(lookup_V2E,flash_addr. V2E)

                -- Read again
                temp_string = read_flash_string(flash_addr. V2E)
                print("temp_string:")
                print(temp_string)

        end
    print("lookup_V2E_flash data:")


使用屏幕后面的debug口,记录到的打印信息好像不太对,字符串没有打印完全
MACC_Exit...
LUA: no flash data on :300
LUA: Length: 749
LUA: temp_string:
LUA: {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0.0966, -27.361, 600, 600, 600, 600, 600, 600, 0, 0}, {0.1118, -31.904, 600, 600, 600, 600, 600, 600, 550, 450}, {0.1927, -48.621, 600, 600, 500, 425, 285, 0, 0, 0}, {0.1822, -43.883, 600, 600, 500, 400, 0, 0, 0, 0}, {0.2469, -54.929, 550, 450, 350, 300, 0, 0, 0, 0}, {0.3576, -76.54, 450, 300, 250, 200, 0, 0, 0, 0}, {0.3796, -84.46, 450, 300, 250, 200, 0, 0, 0, 0}, {0.335, -68.99, 450, 300, 250, 200, 0, 0, 0, 0}, {0.373, -79.29, 450, 300, 250, 200, 0, 0, 0, 0}, {0.346LUA: lookup_V2E_flash data:

金沙白浪 发表于 2023-8-14 17:50:31

不过读取到的数据本身应该是完整的,后面格式化后可以正常使用,也可以正常修改,但就是一旦掉电,参数就都丢了,应该没有真正保存到flash中

If后要接end 发表于 2023-8-14 17:52:18

金沙白浪 发表于 2023-8-14 17:50
不过读取到的数据本身应该是完整的,后面格式化后可以正常使用,也可以正常修改,但就是一旦掉电,参数就都 ...

看一下除了脚本读写flash之外,工程里面本身有没有控件开启了存储,开启了需要避开这一段存储区域

或者你可以分开几段分别保存看看

金沙白浪 发表于 2023-8-15 15:59:35

调试了很多次还是不能正常读取,按理使用判断语句temp_string ~= nil不该有问题,能否提供一个使用LUA操作flash存储字符串的例程?
页: [1] 2
查看完整版本: 请教如何使用write_flash()和read_flash()存取数据表?