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