|
|
@@ -1,23 +1,44 @@
|
|
|
--- Luatools需要PROJECT和VERSION这两个信息
|
|
|
-PROJECT = "uart_two"
|
|
|
-VERSION = "1.0.0"
|
|
|
-
|
|
|
-log.info("main", PROJECT, VERSION)
|
|
|
-
|
|
|
--- 引入必要的库文件(lua编写), 内部库不需要require
|
|
|
-sys = require("sys")
|
|
|
+--[[
|
|
|
+@module multiple_uart
|
|
|
+@summary 多串口功能模块
|
|
|
+@version 1.0
|
|
|
+@date 2025.09.23
|
|
|
+@author 魏健强
|
|
|
+@usage
|
|
|
+本demo演示的核心功能为:
|
|
|
+1.开启串口,配置波特率等参数;
|
|
|
+2.设置接收回调函数
|
|
|
+3.定时向串口发送数据
|
|
|
+]]
|
|
|
+-- 根据实际设备选取不同的uartid
|
|
|
+local uartid1 = 1 -- 第一个串口id
|
|
|
+local uartid2 = 2 -- 第二个串口id
|
|
|
+--local uartid2 = 3 --第三个串口id(Air780EGH用第三个串口,UART2已经用作通信主芯片和GNSS芯片之间的通信用)
|
|
|
|
|
|
-if wdt then
|
|
|
- --添加硬狗防止程序卡死,在支持的设备上启用这个功能
|
|
|
- wdt.init(9000)--初始化watchdog设置为9s
|
|
|
- sys.timerLoopStart(wdt.feed, 3000)--3s喂一次狗
|
|
|
+local function uart_send()
|
|
|
+ -- 循环两秒分别向两个串口发一次数据
|
|
|
+ while true do
|
|
|
+ sys.wait(2000)
|
|
|
+ uart.write(uartid1, "uart1 test data.")
|
|
|
+ uart.write(uartid2, "uart2 test data.")
|
|
|
+ end
|
|
|
end
|
|
|
|
|
|
-log.info("main", "uart_two demo run......")
|
|
|
+local function uart_send_cb(id)
|
|
|
+ log.info("uart", id , "数据发送完成回调")
|
|
|
+end
|
|
|
|
|
|
--- 根据实际设备选取不同的uartid
|
|
|
-local uartid1 = 1 -- 第一个串口id
|
|
|
-local uartid2 = 2 -- 第二个串口id
|
|
|
+local function uart_cb(id, len)
|
|
|
+ local s = ""
|
|
|
+ repeat
|
|
|
+ s = uart.read(id, 128) -- 读取缓冲区中的数据,这里设置的一次读最多128字节
|
|
|
+ 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
|
|
|
+ until s == ""
|
|
|
+end
|
|
|
|
|
|
-- 初始化第一个串口
|
|
|
uart.setup(
|
|
|
@@ -37,41 +58,16 @@ uart.setup(
|
|
|
|
|
|
-- 第一个串口接收数据回调函数
|
|
|
-- 收取数据会触发回调, 这里的"receive" 是固定值
|
|
|
-uart.on(uartid1, "receive", function(id, len)
|
|
|
- local s = ""
|
|
|
- repeat
|
|
|
- s = uart.read(id, 128)
|
|
|
- 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
|
|
|
- until s == ""
|
|
|
-end)
|
|
|
+uart.on(uartid1, "receive", uart_cb)
|
|
|
|
|
|
-- 第二个串口接收数据回调函数
|
|
|
-- 收取数据会触发回调, 这里的"receive" 是固定值
|
|
|
-uart.on(uartid2, "receive", function(id, len)
|
|
|
- local s = ""
|
|
|
- repeat
|
|
|
- s = uart.read(id, 128)
|
|
|
- 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
|
|
|
- until s == ""
|
|
|
-end)
|
|
|
+uart.on(uartid2, "receive", uart_cb)
|
|
|
|
|
|
-sys.taskInit(function()
|
|
|
- -- 循环两秒分别向两个串口发一次数据
|
|
|
- while true do
|
|
|
- sys.wait(2000)
|
|
|
- uart.write(uartid1, "uart1 test data.")
|
|
|
- uart.write(uartid2, "uart2 test data.")
|
|
|
- end
|
|
|
-end)
|
|
|
+-- 发送数据完成会触发回调, 这里的"sent" 是固定值
|
|
|
+uart.on(uartid1, "sent", uart_send_cb)
|
|
|
+
|
|
|
+-- 发送数据完成会触发回调, 这里的"sent" 是固定值
|
|
|
+uart.on(uartid2, "sent", uart_send_cb)
|
|
|
|
|
|
--- 用户代码已结束---------------------------------------------
|
|
|
-sys.run()
|
|
|
--- sys.run()之后后面不要加任何语句!!!!!
|
|
|
+sys.taskInit(uart_send)
|