From 0ec7d80795d86f223b4b068f2081cfae76abf941 Mon Sep 17 00:00:00 2001 From: Wendal Chen Date: Tue, 2 Jan 2024 10:48:12 +0800 Subject: [PATCH] =?UTF-8?q?add:=20=E6=B7=BB=E5=8A=A0=E5=88=9D=E5=A7=8B?= =?UTF-8?q?=E5=8C=96=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 21 ++++++++- demo/demo.lua | 48 ++++++++++++++++++++ demo/main.lua | 32 +++++++++++++ demo/netready.lua | 39 ++++++++++++++++ lib/ntrip.lua | 113 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 252 insertions(+), 1 deletion(-) create mode 100644 demo/demo.lua create mode 100644 demo/main.lua create mode 100644 demo/netready.lua create mode 100644 lib/ntrip.lua diff --git a/README.md b/README.md index c0ffc32..9d57055 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,21 @@ # luatos-lib-ntrip -ntrip协议客户端 + +ntrip协议客户端 for LuatOS + +## 介绍 + +本客户端基于socket库, 兼容所有LuatOS平台, 只要该平台实现socket库即可. + +## 安装 + +本协议库使用纯lua编写, 所以不需要编译, 直接将源码拷贝到项目即可 + +## 使用 + +1. 请先确认ntrip账户, demo中的账户是演示用户, 通常已经过期 +2. 参考demo中的代码, 初始化ntrip客户端, 传入ntrip账户信息 +3. 调用ntrip.start()启动客户端 + +## LIcense + +[MIT License](https://opensource.org/licenses/MIT) diff --git a/demo/demo.lua b/demo/demo.lua new file mode 100644 index 0000000..36e85bf --- /dev/null +++ b/demo/demo.lua @@ -0,0 +1,48 @@ + +local demo = {} +ntrip = require("ntrip") + +local gnss_uart_id = 1 + +local function gnss_write(data) + uart.tx(gnss_uart_id, data) +end + +sys.taskInit(function() + sys.waitUntil("net_ready") + ntrip.setup({ + host = "106.55.71.75", + port = 8002, + user = "zhd556308", + password = "OZ469006", + mount = "/RTCM33_GRC", + cb = gnss_write + }) + ntrip.start() + uart.setup(gnss_uart_id, 115200) + uart.on(gnss_uart_id, "recvice", function(id, len) + local s = "" + repeat + s = uart.read(id, len) + if #s > 0 then + log.info("uart", "receive", id, #s, s) + ntrip.write(data) + end + if #s == len then + break + end + until s == "" + + end) + -- + + -- 下面的代码是PC端模拟GPS数据 + if rtos.bsp() == "PC" then + while 1 do + sys.wait(2000) + ntrip.gga("$GNGGA,021700.000,2324.4051578,N,11313.8597153,E,1,13,1.291,22.077,M,-6.122,M,,*6D\r\n") + end + end +end) + +return demo diff --git a/demo/main.lua b/demo/main.lua new file mode 100644 index 0000000..a7f4881 --- /dev/null +++ b/demo/main.lua @@ -0,0 +1,32 @@ +--[[ +NTRIP协议演示 +]] + +-- LuaTools需要PROJECT和VERSION这两个信息 +PROJECT = "ntripdemo" +VERSION = "1.0.0" + +log.info("main", PROJECT, VERSION) + +-- 一定要添加sys.lua !!!! +sys = require("sys") +sysplus = require("sysplus") + +print(rtos.bsp()) + + +-- 用户代码已开始--------------------------------------------- + +require "netready" +-- require ("ntrip") +require "demo" + +-- 用户代码已结束--------------------------------------------- + + + + +-- 结尾总是这一句 +sys.run() +-- sys.run()之后后面不要加任何语句!!!!! + diff --git a/demo/netready.lua b/demo/netready.lua new file mode 100644 index 0000000..8e6f890 --- /dev/null +++ b/demo/netready.lua @@ -0,0 +1,39 @@ + + +-- 统一联网函数 +sys.taskInit(function() + ----------------------------- + -- 统一联网函数, 可自行删减 + ---------------------------- + if wlan and wlan.connect then + -- wifi 联网, ESP32系列均支持, 要根据实际情况修改ssid和password!! + local ssid = _G.ssid or "luatos1234" + local password = _G.password or "12341234" + log.info("wifi", ssid, password) + -- TODO 改成自动配网 + wlan.init() + wlan.setMode(wlan.STATION) -- 默认也是这个模式,不调用也可以 + wlan.connect(ssid, password, 1) + elseif mobile then + -- EC618系列, 如Air780E/Air600E/Air700E + -- mobile.simid(2) -- 自动切换SIM卡, 按需启用 + -- 模块默认会自动联网, 无需额外的操作 + elseif w5500 then + -- w5500 以太网 + w5500.init(spi.HSPI_0, 24000000, pin.PC14, pin.PC01, pin.PC00) + w5500.config() --默认是DHCP模式 + w5500.bind(socket.ETH0) + elseif socket then + -- 适配了socket库也OK, 就当1秒联网吧 + sys.timerStart(sys.publish, 1000, "IP_READY") + else + -- 其他不认识的bsp, 循环提示一下吧 + while 1 do + sys.wait(1000) + log.info("bsp", "本bsp可能未适配网络层, 请查证") + end + end + -- 默认都等到联网成功 + sys.waitUntil("IP_READY") + sys.publish("net_ready") +end) diff --git a/lib/ntrip.lua b/lib/ntrip.lua new file mode 100644 index 0000000..78120d2 --- /dev/null +++ b/lib/ntrip.lua @@ -0,0 +1,113 @@ + +local ntrip = {} + +-- 编写ntrip客户端 + +local opts = {} + +function ntrip.setup(user_opts) + -- if user_opts["host"] == nil then + -- log.error("ntrip", "必须设置host") + -- return + -- end + -- if user_opts["port"] == nil then + -- log.error("ntrip", "必须设置port") + -- return + -- end + -- if user_opts["user"] == nil then + -- log.error("ntrip", "必须设置用户名user") + -- return + -- end + -- if user_opts["password"] == nil then + -- log.error("ntrip", "必须设置密码password") + -- return + -- end + -- if user_opts["mount"] == nil then + -- log.error("ntrip", "必须设置挂载点mount") + -- return + -- end + -- if user_opts["cb"] == nil then + -- log.error("ntrip", "必须设置数据回调cb") + -- return + -- end + opts = user_opts + return true +end + +function ntrip.task() + + -- 准备好所需要的接收缓冲区 + local rxbuff = zbuff.create(1024) + local netc = socket.create(opts.adapter, function(sc, event) + log.info("ntrip", "socket event", sc, event) + -- 收到数据, 或者连接断开 + if event == socket.EVENT then + log.info("ntrip", "收到数据EVENT") + while 1 do + local succ, data_len = socket.rx(sc, rxbuff) + log.info("ntrip", "接收", succ, data_len) + if not succ then + ntrip.keep = nil + break + end + if data_len and data_len > 0 then + log.info("ntrip", "接收数据", data_len, rxbuff:query()) + if ntrip.cb then + ntrip.cb(rxbuff) + rxbuff.del() + end + else + break + end + end + end + -- 连接成功 + if event == socket.ON_LINE then + log.info("ntrip", "连接成功") + -- 写入ntrip协议头 + local data = string.format("GET %s HTTP/1.0\r\nUser-Agent: NTRIP NtripClientPOSIX/1.50\r\nAccept: */*\r\n", opts.mount) + data = data .. string.format("Host: %s:%d\r\n", opts.host, opts.port) + data = data .. string.format("Connection: close\r\n") + local auth = string.format("Authorization: Basic %s\r\n\r\n", (opts.user .. ":" .. opts.password):toBase64()) + log.info("ntrip", "发送请求头", data .. auth) + if not socket.tx(sc, data .. auth) then + log.error("ntrip", "发送auth失败") + return + end + ntrip.keep = true + end + end) + socket.config(netc, nil) + ntrip.netc = netc + while true do + -- 连接服务器, 15秒超时 + log.info("ntrip", "开始连接服务器", opts.host, opts.port) + if socket.connect(netc, opts.host, opts.port) then + sys.wait(3000) + while ntrip.keep do + sys.wait(3000) + end + end + log.info("ntrip", "连接失败") + -- 能到这里, 要么服务器断开连接, 要么上报(tx)失败, 或者是主动退出 + socket.close(netc) + -- log.info(rtos.meminfo("sys")) + sys.wait(5000) -- 这是重连时长, 自行调整 + end +end + + +function ntrip.start() + if ntrip.task_id == nil then + ntrip.task_id = sys.taskInit(ntrip.task) + end +end + +function ntrip.gga(str) + if ntrip.netc then + -- TODO 仅发送gga数据 + socket.tx(ntrip.netc, str) + end +end + +return ntrip