【Luat-air551G】4 air551G连接esp32获取GGA数据

1 前言

本文简单介绍下如何从air551G提取经度纬度数据。

2 硬件连接

在这里插入图片描述
在这里插入图片描述
连接方式如图。

3 获取uart数据

tag_uart = "test5_uart"

id = 1
len = 1024

date_longitude = ""     -- 经度
date_latitude = ""      -- 纬度
date_GGA = ""

function init_uart_air551()
    log.info(tag_uart,"init_uart_air551")
    uart.setup(1, 115200)

    -- func1: 接收数据,只要收到就接收
    uart.on(id, "receive",uart_air551_receive)
end

function uart_air551_receive()
    log.info("uart_air551_receive")
    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("uart", "receive", id, #s, s:toHex())
    end
end

4 下发PGK命令

tag_uart = "test5_uart"

id = 1
len = 1024

date_longitude = ""     -- 经度
date_latitude = ""      -- 纬度
date_GGA = ""

function init_uart_air551()

    log.info(tag_uart,"init_uart_air551")
    uart.setup(1, 115200)

    uart_air551_NMEA_set()
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

在这里插入图片描述

5 提取经度纬度坐标

5.1 GGA数据结构

建议看下GGA的数据结构
在这里插入图片描述

5.2 从uart接收中提取一组

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

先执行该函数,了解一下string的两个函数find和sub。
https://www.runoob.com/lua/lua-strings.html

5.3 从一组数据中提取纬度和经度

完整代码如下

tag_uart = "test5_uart"

id = 1
len = 1024
get_gga_time = 10000

date_longitude = ""     -- 经度
date_latitude = ""      -- 纬度
date_GGA = ""

function 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)
end

function 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")
    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())
    end

	--提取一组数据
    date_GGA = get_gga(s)
    log.info("date_GGA",date_GGA)

	--提取xy
    get_gga_longitude_latitude(date_GGA)
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,'$GNGGA',1)
    temp_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
end

function get_gga_longitude_latitude(value_gga)
    -- log.info(type(value_gga))
    if value_gga == "" then	 --数据第一次异常
        return -1
    end
    L2_temp = string.sub(value_gga,8)	 -- +8是因为"$gngga"
    log.info("L2_temp",L2_temp)
    L2_temp_x_start = string.find(L2_temp,',',1)+1
    L2_temp_x_end = string.find(L2_temp,',N',1)-1

    date_longitude = string.sub(L2_temp,L2_temp_x_start,L2_temp_x_end)

    L2_temp_y_start = L2_temp_x_end+4
    L2_temp_y_end = string.find(L2_temp,',E',1)-1
    date_latitude = string.sub(L2_temp,L2_temp_y_start,L2_temp_y_end)     -- +3是因为"time,xxx,N,yyy,E",",N"后面三个才是y开始
    log.info("L2_temp_gga",date_longitude,date_latitude)
    -- log.info("temp_value:",temp_start,temp_end,temp_gga)
    return temp_gga
end

5.4 结果

在这里插入图片描述

6 小结

提取经度纬度数据成功,后续再结合lvgl显示。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值