Эх сурвалжийг харах

update:更新8101 uart demo

wjq 4 сар өмнө
parent
commit
6828b28c8e

+ 52 - 0
module/Air8101/demo/uart/485_uart.lua

@@ -0,0 +1,52 @@
+--[[
+@module  485_uart
+@summary 485串口功能模块
+@version 1.0
+@date    2025.09.23
+@author  魏健强
+@usage
+本demo演示的核心功能为:
+1.开启串口,配置波特率等参数;
+2.设置接收回调函数
+3.定时向串口发送数据
+]]
+local uartid = 1        -- 根据实际设备选取不同的uartid
+local uart485Pin = 8   -- 用于控制485接收和发送的使能引脚(根据实际设备选取不同引脚)
+
+-- gpio.setup(1, 1)        --打开电源(开发板485供电脚是gpio1,用开发板测试需要开机初始化拉高gpio1)
+
+local function uart_send()
+    -- 循环两秒向串口发一次数据
+    while true do
+        sys.wait(2000)
+        uart.write(uartid, "test data.")
+    end
+end
+
+local function uart_send_cb(id)
+    log.info("uart", id , "数据发送完成回调")
+end
+
+
+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(uartid, 9600, 8, 1, uart.NONE, uart.LSB, 1024, uart485Pin, 0, 20000)
+
+-- 收取数据会触发回调, 这里的"receive" 是固定值
+uart.on(uartid, "receive", uart_cb)
+
+-- 发送数据完成会触发回调, 这里的"sent" 是固定值
+uart.on(uartid, "sent", uart_send_cb)
+
+sys.taskInit(uart_send)

+ 106 - 0
module/Air8101/demo/uart/high_volume_uart.lua

@@ -0,0 +1,106 @@
+--[[
+@module  high_volume_uart
+@summary 串口大数据收发功能模块
+@version 1.0
+@date    2025.09.23
+@author  魏健强
+@usage
+本demo演示的核心功能为:
+1.开启串口,配置波特率等参数;
+2.设置接收回调函数
+3.定时向串口发送数据
+4.使用zbuff的方式收发数据
+]] 
+local uartid = 1 -- 根据实际设备选取不同的uartid
+
+uart_mode_zbuff = true -- 是否使用zbuff方式收发数据收发
+
+local function uart_send_cb(id)
+    log.info("uart", id , "数据发送完成回调")
+end
+
+if uart_mode_zbuff then
+    local rxbuff = zbuff.create(10240) -- 接收数据的zbuff
+    local txbuff = zbuff.create(10240) -- 发送数据的zbuff
+
+    local function uart_send()
+        -- 循环两秒向串口发一次数据
+        while true do
+            sys.wait(10000)
+            --发送数据
+            uart.tx(uartid, txbuff)
+        end
+    end
+
+    local function uart_cb(id, len)  -- 串口接收数据回调函数
+        while 1 do
+            log.info("uart", "缓冲区", uart.rxSize(id))
+            local len = uart.rx(id, rxbuff)
+            if len <= 0 then
+                break
+            end
+            log.info("uart", "receive", id, rxbuff:used(), rxbuff:query())
+            rxbuff:seek(0)
+        end
+    end
+
+    txbuff:write("测试数据")    -- 向发送数据的zbuff写入数据
+
+    -- 初始化
+    uart.setup(uartid, -- 串口id
+    115200, -- 波特率
+    8, -- 数据位
+    1, -- 停止位
+    uart.NONE, -- 校验位,可选 uart.None/uart.Even/uart.Odd。默认 uart.None 无校验
+    uart.LSB, -- 大小端,默认小端 uart.LSB, 可选 uart.MSB
+    10240 -- 缓冲区大小,默认值1024,接收大数据时需要根据数据大小增大缓冲区
+    )
+
+    -- 收取数据会触发回调, 这里的"receive" 是固定值
+    uart.on(uartid, "receive", uart_cb)
+
+    -- 发送数据完成会触发回调, 这里的"sent" 是固定值
+    uart.on(uartid, "sent", uart_send_cb)
+
+    sys.taskInit(uart_send)
+else
+    local function uart_send()
+        -- 循环两秒向串口发一次数据
+        while true do
+            sys.wait(2000)
+            uart.write(uartid, "test data.")
+        end
+    end
+
+    local function uart_cb(id, len)
+        local s = ""
+        repeat
+            s = uart.read(id, 10240) -- 一次性读出所有缓冲区中的数据,避免分包
+            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(uartid, -- 串口id
+    115200, -- 波特率
+    8, -- 数据位
+    1, -- 停止位
+    uart.NONE, -- 校验位,可选 uart.None/uart.Even/uart.Odd。默认 uart.None 无校验
+    uart.LSB, -- 大小端,默认小端 uart.LSB, 可选 uart.MSB
+    10240 -- 缓冲区大小,默认值1024,接收大数据时需要根据数据大小增大缓冲区
+    )
+
+    -- 收取数据会触发回调, 这里的"receive" 是固定值
+    uart.on(uartid, "receive", uart_cb)
+
+    -- 发送数据完成会触发回调, 这里的"sent" 是固定值
+    uart.on(uartid, "sent", uart_send_cb)
+
+    sys.taskInit(uart_send)
+
+end
+

+ 75 - 0
module/Air8101/demo/uart/main.lua

@@ -0,0 +1,75 @@
+--[[
+@module  main
+@summary LuatOS用户应用脚本文件入口,总体调度应用逻辑 
+@version 1.0
+@date    2025.09.23
+@author  魏健强
+@usage
+本demo演示的核心功能为:
+演示uart串口功能的使用:
+1. 简易串口,小数据字符串收发
+2. 大数据收发串口
+3. 485串口
+4. 多串口
+5. USB虚拟串口
+]]
+--[[
+必须定义PROJECT和VERSION变量,Luatools工具会用到这两个变量,远程升级功能也会用到这两个变量
+PROJECT:项目名,ascii string类型
+        可以随便定义,只要不使用,就行
+VERSION:项目版本号,ascii string类型
+        如果使用合宙iot.openluat.com进行远程升级,必须按照"XXX.YYY.ZZZ"三段格式定义:
+            X、Y、Z各表示1位数字,三个X表示的数字可以相同,也可以不同,同理三个Y和三个Z表示的数字也是可以相同,可以不同
+            因为历史原因,YYY这三位数字必须存在,但是没有任何用处,可以一直写为000
+        如果不使用合宙iot.openluat.com进行远程升级,根据自己项目的需求,自定义格式即可
+]]-- Luatools需要PROJECT和VERSION这两个信息
+PROJECT = "uart"
+VERSION = "001.000.000"
+
+
+-- 在日志中打印项目名和项目版本号
+log.info("main", PROJECT, VERSION)
+
+
+-- 如果内核固件支持wdt看门狗功能,此处对看门狗进行初始化和定时喂狗处理
+-- 如果脚本程序死循环卡死,就会无法及时喂狗,最终会自动重启
+if wdt then
+    --配置喂狗超时时间为9秒钟
+    wdt.init(9000)
+    --启动一个循环定时器,每隔3秒钟喂一次狗
+    sys.timerLoopStart(wdt.feed, 3000)
+end
+
+
+-- 如果内核固件支持errDump功能,此处进行配置,【强烈建议打开此处的注释】
+-- 因为此功能模块可以记录并且上传脚本在运行过程中出现的语法错误或者其他自定义的错误信息,可以初步分析一些设备运行异常的问题
+-- 以下代码是最基本的用法,更复杂的用法可以详细阅读API说明文档
+-- 启动errDump日志存储并且上传功能,600秒上传一次
+-- if errDump then
+--     errDump.config(true, 600)
+-- end
+
+
+-- 使用LuatOS开发的任何一个项目,都强烈建议使用远程升级FOTA功能
+-- 可以使用合宙的iot.openluat.com平台进行远程升级
+-- 也可以使用客户自己搭建的平台进行远程升级
+-- 远程升级的详细用法,可以参考fota的demo进行使用
+
+
+-- 启动一个循环定时器
+-- 每隔3秒钟打印一次总内存,实时的已使用内存,历史最高的已使用内存情况
+-- 方便分析内存使用是否有异常
+-- sys.timerLoopStart(function()
+--     log.info("mem.lua", rtos.meminfo())
+--     log.info("mem.sys", rtos.meminfo("sys"))
+-- end, 3000)
+
+
+
+-- uart功能模块
+require "uart_manger"
+
+-- 用户代码已结束---------------------------------------------
+-- 结尾总是这一句
+sys.run()
+-- sys.run()之后后面不要加任何语句!!!!!

+ 45 - 46
module/Air8101/demo/uart/uart_two/main.lua → module/Air8101/demo/uart/multiple_uart.lua

@@ -1,20 +1,44 @@
--- Luatools需要PROJECT和VERSION这两个信息
-PROJECT = "uart_two"
-VERSION = "1.0.0"
-
-log.info("main", PROJECT, VERSION)
+--[[
+@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(
@@ -34,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)

+ 91 - 0
module/Air8101/demo/uart/readme.md

@@ -0,0 +1,91 @@
+## 演示模块概述
+
+1、main.lua:主程序入口;
+
+2、uart_manger:串口功能管理模块,用于管理以下五种串口应用场景功能;
+
+3、simple_uart:简易串口,小数据字符串收发;
+
+4、high_volume_uart:大数据收发串口;
+
+5、485_uart:485串口;
+
+6、multiple_uart:多串口;
+
+## 演示功能概述
+
+使用Air8101核心板测试串口相关功能。
+
+## 演示硬件环境
+
+![](https://docs.openluat.com/air8101/luatos/app/multinetwork/4G/image/LzuBbS3NxoVu34x4dj7c3d04nDb.jpg)
+Air8101 核心板一块 + TYPE-C USB 数据线一根 + USB转串口数据线一根
+
+- Air8101 核心板通过TYPE-C USB口供电;
+
+- TYPE-C USB数据线直接插到核心板的TYPE-C USB座子,另外一端连接电脑USB口;
+
+- USB转串口数据线,一般来说,白线连接核心板的UART1_TX,绿线连接核心板的UART1_RX,黑线连接核心板的GND,另外一端连接电脑USB口;
+
+3、不同功能测试时的接线说明:
+
+- 单串口,simple_uart和high_volume_uart 接串口1
+
+| Air8101核心板     | MCU或者串口板 |
+| ----------------- | ------------- |
+| 11/u1tx           | UART_RXD      |
+| 12/u1rx           | UART_TXD      |
+| GND               | GND           |
+
+![](https://docs.openluat.com/air8101/luatos/app/driver/uart/image/Air8101-uart1.jpg)
+
+- 多串口,multiple_uart 接串口1,串口2
+
+串口2:
+
+| Air8101核心板     | MCU或者串口板 |
+| ----------------- | ------------- |
+| 73/VSY            | UART_RXD      |
+| 3/HSY             | UART_TXD      |
+| GND               | GND           |
+
+串口1:
+
+| Air8101核心板     | MCU或者串口板 |
+| ----------------- | ------------- |
+| 11/u1tx           | UART_RXD      |
+| 12/u1rx           | UART_TXD      |
+| GND               | GND           |
+
+![](https://docs.openluat.com/air8101/luatos/app/driver/uart/image/Air8101-uart2.jpg)
+
+## 演示软件环境
+
+1、Luatools下载调试工具
+
+2、[Air8101 V1006版本固件](https://docs.openluat.com/air8101/luatos/firmware/)(理论上,最新发布的固件都可以)
+
+3、PC端的串口工具,例如SSCOM、LLCOM等都可以;
+
+## 演示核心步骤
+
+1、搭建好硬件环境
+
+2、uart_manger.lua 中加载需要用的功能模块,五个功能模块同时只能选择一个使用,其他的注释。
+
+3、Luatools 烧录内核固件和修改后的 demo 脚本代码
+
+4、烧录成功后,代码会自动运行,查看打印日志,如果正常运行,会打印串口初始化和串口收发数据等相关信息。
+
+5、simple_uart:
+![](https://docs.openluat.com/air8101/luatos/app/driver/uart/image/8101-uart1.png)
+
+6、high_volume_uart:
+![](https://docs.openluat.com/air8101/luatos/app/driver/uart/image/8101-uart6.png)
+
+7、multiple_uart
+![](https://docs.openluat.com/air8101/luatos/app/driver/uart/image/8101-uart3.png)
+
+8、485_uart:
+![](https://docs.openluat.com/air8101/luatos/app/driver/uart/image/PluHbmrGmolVZ1xLruwcBWR7njd.png)
+ 

+ 37 - 33
module/Air8101/demo/uart/uart/main.lua → module/Air8101/demo/uart/simple_uart.lua

@@ -1,19 +1,40 @@
--- Luatools需要PROJECT和VERSION这两个信息
-PROJECT = "uart"
-VERSION = "1.0.0"
-
-log.info("main", PROJECT, VERSION)
+--[[
+@module  simple_uart
+@summary 简易串口功能模块
+@version 1.0
+@date    2025.09.23
+@author  魏健强
+@usage
+本demo演示的核心功能为:
+1.开启串口,配置波特率等参数;
+2.设置接收回调函数
+3.定时向串口发送数据
+]]
+local uartid = 1 -- 根据实际设备选取不同的uartid
 
-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(uartid, "test data.")
+    end
 end
 
-log.info("main", "uart demo run......")
-
-local uartid = 1 -- 根据实际设备选取不同的uartid
+local function uart_send_cb(id)
+    log.info("uart", id , "数据发送完成回调")
+end
 
+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(
     uartid,--串口id
@@ -23,26 +44,9 @@ uart.setup(
 )
 
 -- 收取数据会触发回调, 这里的"receive" 是固定值
-uart.on(uartid, "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(uartid, "receive", uart_cb)
 
-sys.taskInit(function()
-    -- 循环两秒向串口发一次数据
-    while true do
-        sys.wait(2000)
-        uart.write(uartid, "test data.")
-    end
-end)
+-- 发送数据完成会触发回调, 这里的"sent" 是固定值
+uart.on(uartid, "sent", uart_send_cb)
 
--- 用户代码已结束---------------------------------------------
-sys.run()
--- sys.run()之后后面不要加任何语句!!!!!
+sys.taskInit(uart_send)

+ 26 - 0
module/Air8101/demo/uart/uart_manger.lua

@@ -0,0 +1,26 @@
+--[[
+@module  uart_manger
+@summary 串口功能管理模块
+@version 1.0
+@date    2025.09.23
+@author  魏健强
+@usage
+本模块为串口功能管理模块,核心业务逻辑为:根据项目需求,选择并且配置合适的串口功能
+1、simple_uart:简易串口,小数据字符串收发;
+2、high_volume_uart:大数据收发串口;
+3、485_uart:485串口;
+4、multiple_uart:多串口;
+5、usb_uart:USB虚拟串口;
+]]
+-- 根据自己的项目需求,只需要require以下五种中的一种即可;
+-- 简易串口,小数据字符串收发
+require "simple_uart"
+
+-- 大数据收发串口
+-- require "high_volume_uart"
+
+-- 485串口
+-- require "485_uart"
+
+-- 多串口
+-- require "multiple_uart"

+ 0 - 44
module/Air8101/demo/uart/uart_rs485/main.lua

@@ -1,44 +0,0 @@
--- Luatools需要PROJECT和VERSION这两个信息
-PROJECT = "uart_RS485"
-VERSION = "1.0.0"
-
-log.info("main", PROJECT, VERSION)
-
-if wdt then
-    --添加硬狗防止程序卡死,在支持的设备上启用这个功能
-    wdt.init(9000)--初始化watchdog设置为9s
-    sys.timerLoopStart(wdt.feed, 3000)--3s喂一次狗
-end
-
-log.info("main", "uart demo run......")
-
-local uartid = 1        -- 根据实际设备选取不同的uartid
-local uart485Pin = 16   -- 用于控制485接收和发送的使能引脚
-
---初始化
-uart.setup(uartid, 9600, 8, 1, uart.NONE, uart.LSB, 1024, uart485Pin, 0, 2000)
-
--- 收取数据会触发回调, 这里的"receive" 是固定值
-uart.on(uartid, "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)
-
-sys.taskInit(function()
-    -- 循环两秒向串口发一次数据
-    while true do
-        sys.wait(2000)
-        uart.write(uartid, "test data.")
-    end
-end)
-
--- 用户代码已结束---------------------------------------------
-sys.run()
--- sys.run()之后后面不要加任何语句!!!!!

BIN
module/Air8101/demo/uart/单串口流程图.png