------------------------------------------------------- -- H7-TOOL 串口日志 ------------------------------------------------------- local str local len local offset local count local totalstr local filepath = "0:/H7-TOOL/Lua/串口日志记录/test.csv" -- csv文件记录路径 local reccout local ret local key function printf(...) print(string.format(...)) end ------------------------------------------------------- -- 第2步:设置串口波特率115200,奇偶校验位无,数据位8,停止位1 ------------------------------------------------------- uart_cfg(1, 115200, 0, 8, 1) count = 0 offset = 0 reccout = 100 -- 接收100次 printf("进入串口日志接收模式") ------------------------------------------------------- -- 第3步:执行操作 ------------------------------------------------------- while(reccout > 0) do delayms(1) -- 加上这个延迟方便显示屏版展示信息 len, str = uart_recive(1, 100, 500) -- 串口1接收,最大100个字符,超时时间500ms if(len ~= 0) then -- 接收成功 reccout = reccout - 1 count = count + 1 printf("成功接收数据次数=%d", count) time = os.time() hms = os.date("%H:%M:%S,", time) totalstr = hms..str.."\r\n" -- 合并字符串 ret = f_write(filepath, offset, totalstr) -- 写入到eMMC if(ret == 1) then printf("成功写入CSV文件地址偏移=%d", offset) offset = offset + #totalstr else printf("写入CSV文件失败") end else -- 接收超时 printf("100ms等待超时") end key = get_key() -- 监测到C键单击,退出监测 if(key == 8) then reccout = 0 printf("终止记录") end end