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

add: uart.read支持直接写入到句柄

Wendal Chen 5 лет назад
Родитель
Сommit
800dfe4507
1 измененных файлов с 12 добавлено и 3 удалено
  1. 12 3
      luat/modules/luat_lib_uart.c

+ 12 - 3
luat/modules/luat_lib_uart.c

@@ -8,6 +8,7 @@
 #include "luat_uart.h"
 #include "luat_malloc.h"
 #include "luat_msgbus.h"
+#include "luat_fs.h"
 #include "string.h"
 
 #define LUAT_LOG_TAG "luat.uart"
@@ -123,6 +124,7 @@ static int l_uart_write(lua_State *L)
 @api    uart.read(id, len)
 @int 串口id, uart0写0, uart1写1
 @int 读取长度
+@int 文件句柄(可选)
 @return string 读取到的数据
 @usage
 uart.read(1, 16)
@@ -131,8 +133,8 @@ static int l_uart_read(lua_State *L)
 {
     uint8_t id = luaL_checkinteger(L, 1);
     uint32_t length = luaL_optinteger(L, 2, 1024);
-    if (length > 1024) {
-        length = 1024;
+    if (length > 4096) {
+        length = 4096;
     }
     void *recv = luat_heap_malloc(length);
     if (recv == NULL) {
@@ -143,7 +145,14 @@ static int l_uart_read(lua_State *L)
     int result = luat_uart_read(id, recv, length);
     //lua_gc(L, LUA_GCCOLLECT, 0);
     if (result > 0) {
-        lua_pushlstring(L, (const char*)recv, result);
+        if (lua_isinteger(L, 3)) {
+            uint32_t fd = luaL_checkinteger(L, 3);
+            luat_fs_fwrite(recv, 1, result, (FILE*)fd);
+        }
+        else {
+            lua_pushlstring(L, (const char*)recv, result);
+        }
+        
     }
     else
     {