uart1_8000.lua 808 B

1234567891011121314151617181920212223242526272829303132
  1. local uartid = 1 -- 根据实际设备选取不同的uartid
  2. --初始化
  3. uart.setup(
  4. uartid,--串口id
  5. 115200,--波特率
  6. 8,--数据位
  7. 1--停止位
  8. )
  9. -- 收取数据会触发回调, 这里的"receive" 是固定值
  10. uart.on(uartid, "receive", function(id, len)
  11. local s = ""
  12. repeat
  13. s = uart.read(id, 2048)
  14. if #s > 0 then -- #s 是取字符串的长度
  15. -- 如果传输二进制/十六进制数据, 部分字符不可见, 不代表没收到
  16. -- 关于收发hex值,请查阅 https://doc.openluat.com/article/583
  17. log.info("uart1", "receive", id, #s, s)
  18. uart.write(2, s)
  19. end
  20. until s == ""
  21. end)
  22. -- 并非所有设备都支持sent事件
  23. uart.on(uartid, "sent", function(id)
  24. log.info("uart", "sent", id)
  25. end)