当前位置: 代码迷 >> 综合 >> 【Luat-air551G】5 用lvgl显示GGA数据
  详细解决方案

【Luat-air551G】5 用lvgl显示GGA数据

热度:68   发布时间:2023-12-05 20:28:28.0

代码

tag_uart = "test5_uart"id = 1
len = 1024get_gga_time = 2000data_time = ""
data_longitude = ""     -- 经度
data_latitude = ""      -- 纬度
data_GGA = ""
data_GGA_status = ""
GGA_list = {
    }
GGA_list["time"] = data_time
GGA_list["x"] = data_longitude
GGA_list["y"] = data_latitude
GGA_list["status"]= data_GGA_statusfunction init_uart_air551()log.info(tag_uart,"init_uart_air551")uart.setup(1, 115200)uart_air551_NMEA_set()-- 定时发送数据-- timex = sys.timerLoopStart(uart_air551_send,1000)-- log.info(tag_uart,"time:",timex)sys.wait(1000)-- func1: 接收数据,只要收到就接收-- uart.on(id, "receive",uart_air551_receive)-- func2: 定时接收timex = sys.timerLoopStart(uart_air551_receive,get_gga_time)log.info(tag_uart,"time:",timex)init_air551G_cont()
endfunction uart_air551_send()-- log.info("uart_air551_send")uart.write(id, "test")
end-- 下发命令
function uart_air551_NMEA_set()log.info("uart_air551_NMEA_set")uart.write(id, "$PGKC242,0,0,0,1*2A\r\n")       --只接收GGA数据。注意需要传入回车符
end-- 获取串口数据
function uart_air551_receive()log.info("uart_air551_receive start---------------")local s = ""s = uart.read(id, len)if #s > 0 then -- #s 是取字符串的长度-- 如果传输二进制/十六进制数据, 部分字符不可见, 不代表没收到-- 关于收发hex值,请查阅 https://doc.openluat.com/article/583-- log.info("uart", "receive", id, #s, s) -- 这句将接收的都打印出来log.info("get air551","uart", "receive", id, #s)-- log.info("uart", "receive", id, #s, s:toHex())enddata_GGA = get_gga(s)log.info("data_GGA",data_GGA)get_gga_longitude_latitude(data_GGA)log.info("uart_air551_receive end---------------")
end--测试数据提取
function test_string_get()string_temp = "xxxxx$GNGGA,000004.811,XXX,N,XXX,E,0,0,,143.01,M,6.99,M,,*6D\r$GNGGA,000003.811,*6A"temp_start = string.find(string_temp,'$GNGGA',1)temp_end = string.find(string_temp,'\r',1)temp_gga = string.sub(string_temp,temp_start,temp_end-1)log.info("temp_x:",temp_start,temp_end,temp_gga)
end-- 提取一组数据
function get_gga(value)temp_start = string.find(value,'GGA',1)-3temp_end = string.find(value,'\r',1)temp_gga = string.sub(value,temp_start,temp_end-1)-- log.info("temp_value:",temp_start,temp_end,temp_gga)return temp_gga
endfunction get_gga_longitude_latitude(value_gga)-- log.info(type(value_gga))if value_gga == "" then     --数据第一次异常return -1end-- 获取GGA数据-- log.info("L2_temp",L2_temp)L2_temp = string.sub(value_gga,8)   -- +8是因为"$gngga"-- 获取时间data_time_h = string.sub(L2_temp,1,2)data_time_m = string.sub(L2_temp,3,4)data_time_s = string.sub(L2_temp,4,5)-- data_time_h = string.t(data_time_h)data_time_h = math.floor(data_time_h +8)-- log.info(type(data_time_h),data_time_h)if data_time_h >= 24 thendata_time_h = data_time_h -24 end-- data_time_h = tostring(data_time_h)if data_time_h <10 thendata_time_h = '0'..data_time_hend-- log.info(type(data_time_h),data_time_h)-- data_time_h = tostring(data_time_h)-- data_time_h = string.(8 + string.toValue(data_time_h))data_time = data_time_h..":"..data_time_m..":"..data_time_s-- 获取纬度L2_temp_x_start = string.find(L2_temp,',',1)+1L2_temp_x_end = string.find(L2_temp,',N',1)-1data_longitude = string.sub(L2_temp,L2_temp_x_start,L2_temp_x_end)-- 获取经度L2_temp_y_start = L2_temp_x_end+4-- +4是因为"time,xxx,N,yyy,E",",N"后面三个才是y开始L2_temp_y_end = string.find(L2_temp,',E',1)-1data_latitude = string.sub(L2_temp,L2_temp_y_start,L2_temp_y_end)-- log.info("L2_temp_gga",data_longitude,data_latitude)-- log.info("temp_value:",temp_start,temp_end,temp_gga)-- 获取定位指示",E,定位指示,"L2_temp_status_start = L2_temp_y_end + 4L2_temp_status_end = L2_temp_status_start data_GGA_status = string.sub(L2_temp,L2_temp_status_start,L2_temp_status_end)-- log.info("data_GGA_status",data_GGA_status)if data_GGA_status == "0" thenlog.info("定位指示为0,未定位")elseif data_GGA_status == "1" thenlog.info("定位指示为1,SPS 模式,定位有效")elseif data_GGA_status == "2" thenlog.info("定位指示为2,差分,SPS 模式,定位有效")endGGA_list["time"] = data_timeGGA_list["x"] = data_longitudeGGA_list["y"] = data_latitudeGGA_list["status"]= data_GGA_status-- 显示当前状态set_air551G_cont()return temp_gga
endfunction init_air551G_cont()air551G_cont = lvgl.cont_create(lvgl.scr_act(), nil)lvgl.obj_set_size(air551G_cont,128,160)lvgl.obj_set_auto_realign(air551G_cont, true) lvgl.obj_align_origo(air551G_cont, nil, lvgl.ALIGN_CENTER, 0, 0)lvgl.cont_set_fit(air551G_cont, lvgl.FIT_NONE)   --此时cont未设置自适应状态,则内容在cont中lvgl.cont_set_layout(air551G_cont, lvgl.LAYOUT_COLUMN_LEFT) --布局靠左--}log.info("scr_load",lvgl.scr_load(air551G_cont))     --显示 容器--创建标签labelGGA_label = lvgl.label_create(air551G_cont, nil)lvgl.label_set_text(GGA_label, "GGA_data")lvgl.obj_set_pos(GGA_label, 2, 5)
endfunction set_air551G_cont()string_GGA = "GGA_data"..'\r'.."time: "..GGA_list["time"]..'\r'.."x: "..GGA_list["x"]..'\r'.."y: "..GGA_list["y"]..'\r'.."status: "..GGA_list["status"]log.info("string_GGA",string_GGA)if string_GGA == "" thenreturn -1endlvgl.label_set_text(GGA_label, string_GGA)end

结果

在这里插入图片描述

在这里插入图片描述