Просмотр исходного кода

add:虚拟UART对端(PC串口工具等)打开关闭通知

alienwalker 9 месяцев назад
Родитель
Сommit
09efb3669d
3 измененных файлов с 25 добавлено и 0 удалено
  1. 5 0
      demo/usb_uart/EC618/main.lua
  2. 6 0
      luat/include/luat_uart_legacy.h
  3. 14 0
      luat/modules/luat_lib_uart.c

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

@@ -36,6 +36,11 @@ log.info("main", "usb uart demo")
 
 local uartid = uart.VUART_0 -- USB虚拟串口的固定id
 
+-- 2025年5月6日新增,虚拟UART对端(比如PC串口工具)打开和关闭通知
+sys.subscribe("VUART_STATE", function(vuart_id, state)
+    log.info("vuart", vuart_id, "is connect", state)
+end)
+
 --初始化
 local result = uart.setup(
     uartid,--串口id

+ 6 - 0
luat/include/luat_uart_legacy.h

@@ -4,7 +4,13 @@
 #include "luat_base.h"
 
 #ifdef __LUATOS__
+enum
+{
+	LUAT_VUART_STATE_CONNECT,
+	LUAT_VUART_STATE_DISCONNECT,
+};
 int l_uart_handler(lua_State *L, void* ptr);
+int l_vuart_state_handler(lua_State *L, void* ptr);
 #endif
 
 #ifdef LUAT_FORCE_WIN32

+ 14 - 0
luat/modules/luat_lib_uart.c

@@ -345,6 +345,20 @@ void luat_uart_set_app_recv(int id, luat_uart_recv_callback_t cb) {
 	}
 }
 
+int l_vuart_state_handler(lua_State *L, void* ptr) {
+    (void)ptr;
+    rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
+    lua_getglobal(L, "sys_pub");
+    if (lua_isfunction(L, -1)) {
+    	lua_pushstring(L, "VUART_STATE");
+        lua_pushinteger(L, msg->arg1);
+        lua_pushboolean(L, !msg->arg2);
+        lua_call(L, 3, 0);
+    }
+    lua_pushinteger(L, 0);
+    return 1;
+}
+
 int l_uart_handler(lua_State *L, void* ptr) {
     (void)ptr;
     rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);