最近搞个项目,需要使用串口屏,由于需要保存大量数据,并且需要作图,还需要打印图片,为了开发方便,资源合理利用,使用串口屏的sd卡保存数据,但sd卡大数据的读写在常规的串口控制协议中没有,只有lua的有这部分内容,因此搞了几天的lua程序。
lua比较反c语言,是从i=1开始,也不需要{},多了写一些then…end这个倒是还行,主要是串口屏是需要写文件,每次修改还需要到串口屏上操作,还有就是编译器不好用,只能调试模式看问题。`–下面列出了常用的回调函数
–更多功能请阅读<<物联型LUA脚本API.pdf>>
–local cjson = require “cjson”
local usb_path = ‘’
local sc_Home = 5
local list_file_name = ‘c:/listsave.csv’
local line_list= {}
local line_data_file_name= {}
local line_data_file_name_buf= {}
local data_file_name = “”
local data_file_name_buf = “”
local cnt = 0
local cur_select_record = 0
local draw_type = 0
local pre_x=0
local pre_y=0
local tickoi = 2
– split string
– str : eg: ‘Maria;100;96;98’
– pat : pat = ;
– rst : table, t[1]=Maria, t[2]=100
function split(str, pat)
local t = {}
local last_end = 0
local s, e = string.find(str, pat, 1)
while s
do
table.insert(t, string.sub(str, last_end + 1, last_end + s - last_end - 1))
last_end = e
s, e = string.find(str, pat, last_end + 1)
end
if last_end <= #str then
cap = string.sub(str, last_end + 1)
table.insert(t, cap)
end
return t
end
– Read each line of csv file
– filename:path
function csv_read(filename)
cnt = 0
csv_line2 = {}
local rfile = io.open(filename, "r")
if rfile ~= nil
then
for line in rfile:lines()
do
cnt = cnt +1
csv_line2[cnt] = line.."\n"
end
end
rfile:close()
return csv_line2
end
– write at the end of the file
– filename: path
– info : add info
function csv_addWriteEnd(filename, info,csv_line3)
local csv_info = ‘’
local wfile = io.open(filename, ‘a+’)
for i = 1, #(csv_line3)
do
if csv_line3[i] == nil
then
csv_info = csv_info..csv_line3[i]..'\n'
end
end
csv_info = csv_info..info..'\n'