Browse Source

fix:虚拟UART会有rx回调了

alienwalker 3 năm trước cách đây
mục cha
commit
cae78acbbb
3 tập tin đã thay đổi với 77 bổ sung8 xóa
  1. 0 0
      demo/usb_uart/Air105/main.lua
  2. 59 0
      demo/usb_uart/EC618/main.lua
  3. 18 8
      luat/modules/luat_lib_uart.c

+ 0 - 0
demo/usb_uart/main.lua → demo/usb_uart/Air105/main.lua


+ 59 - 0
demo/usb_uart/EC618/main.lua

@@ -0,0 +1,59 @@
+-- LuaTools需要PROJECT和VERSION这两个信息
+PROJECT = "usb_uart"
+VERSION = "1.0.0"
+
+log.info("main", PROJECT, VERSION)
+
+-- 引入必要的库文件(lua编写), 内部库不需要require
+sys = require("sys")
+
+
+log.info("main", "usb uart demo")
+
+
+local uartid = uart.VUART_0 -- USB虚拟串口的固定id
+
+--初始化
+local result = uart.setup(
+    uartid,--串口id
+    115200,--波特率
+    8,--数据位
+    1--停止位
+)
+
+
+-- 收取数据会触发回调, 这里的"receive" 是固定值
+uart.on(uartid, "receive", function(id, len)
+    local s = ""
+    repeat
+        -- 如果是air302, len不可信, 传1024
+        -- s = uart.read(id, 1024)
+        s = uart.read(id, len)
+        if s and #s > 0 then -- #s 是取字符串的长度
+            -- 如果传输二进制/十六进制数据, 部分字符不可见, 不代表没收到
+            -- 关于收发hex值,请查阅 https://doc.openluat.com/article/583
+            log.info("uart", "receive", id, #s, s)
+            uart.write(uart.VUART_0, s)
+            -- log.info("uart", "receive", id, #s, s:toHex())
+        end
+    until s == ""
+end)
+
+-- 并非所有设备都支持sent事件
+uart.on(uartid, "sent", function(id)
+    log.info("uart", "sent", id)
+end)
+
+sys.taskInit(function()
+
+    while 1 do
+        uart.write(uart.VUART_0, "hello test usb-uart\r\n")
+        sys.wait(1000)
+    end
+end)
+
+
+-- 用户代码已结束---------------------------------------------
+-- 结尾总是这一句
+sys.run()
+-- sys.run()之后后面不要加任何语句!!!!!

+ 18 - 8
luat/modules/luat_lib_uart.c

@@ -18,15 +18,14 @@
 #define LUAT_LOG_TAG "uart"
 #include "luat_log.h"
 
-#define MAX_DEVICE_COUNT 10
-
+#define MAX_DEVICE_COUNT 9
+#define MAX_USB_DEVICE_COUNT 1
 typedef struct luat_uart_cb {
     int received;//回调函数
     int sent;//回调函数
 } luat_uart_cb_t;
-static luat_uart_cb_t uart_cbs[MAX_DEVICE_COUNT];
-
-static luat_uart_recv_callback_t uart_app_recvs[MAX_DEVICE_COUNT];
+static luat_uart_cb_t uart_cbs[MAX_DEVICE_COUNT + MAX_USB_DEVICE_COUNT];
+static luat_uart_recv_callback_t uart_app_recvs[MAX_DEVICE_COUNT + MAX_USB_DEVICE_COUNT];
 
 void luat_uart_set_app_recv(int id, luat_uart_recv_callback_t cb) {
     if (luat_uart_exist(id)) {
@@ -45,13 +44,18 @@ int l_uart_handler(lua_State *L, void* ptr) {
         //LLOGW("not exist uart id=%ld but event fired?!", uart_id);
         return 0;
     }
+    int org_uart_id = uart_id;
+    if (uart_id >= LUAT_VUART_ID_0)
+    {
+    	uart_id = MAX_DEVICE_COUNT + uart_id - LUAT_VUART_ID_0;
+    }
     // sent event
     if (msg->arg2 == 0) {
         //LLOGD("uart%ld sent callback", uart_id);
         if (uart_cbs[uart_id].sent) {
             lua_geti(L, LUA_REGISTRYINDEX, uart_cbs[uart_id].sent);
             if (lua_isfunction(L, -1)) {
-                lua_pushinteger(L, uart_id);
+                lua_pushinteger(L, org_uart_id);
                 lua_call(L, 1, 0);
             }
         }
@@ -63,7 +67,7 @@ int l_uart_handler(lua_State *L, void* ptr) {
         if (uart_cbs[uart_id].received) {
             lua_geti(L, LUA_REGISTRYINDEX, uart_cbs[uart_id].received);
             if (lua_isfunction(L, -1)) {
-                lua_pushinteger(L, uart_id);
+                lua_pushinteger(L, org_uart_id);
                 lua_pushinteger(L, msg->arg2);
                 lua_call(L, 2, 0);
             }
@@ -265,10 +269,16 @@ end)
 */
 static int l_uart_on(lua_State *L) {
     int uart_id = luaL_checkinteger(L, 1);
+    int org_uart_id = uart_id;
     if (!luat_uart_exist(uart_id)) {
         lua_pushliteral(L, "no such uart id");
         return 1;
     }
+    if (uart_id >= LUAT_VUART_ID_0)
+    {
+    	uart_id = MAX_DEVICE_COUNT + uart_id - LUAT_VUART_ID_0;
+    }
+
     const char* event = luaL_checkstring(L, 2);
     if (!strcmp("receive", event) || !strcmp("recv", event)) {
         if (uart_cbs[uart_id].received != 0) {
@@ -290,7 +300,7 @@ static int l_uart_on(lua_State *L) {
             uart_cbs[uart_id].sent = luaL_ref(L, LUA_REGISTRYINDEX);
         }
     }
-    luat_setup_cb(uart_id, uart_cbs[uart_id].received, uart_cbs[uart_id].sent);
+    luat_setup_cb(org_uart_id, uart_cbs[uart_id].received, uart_cbs[uart_id].sent);
     return 0;
 }