UART
— 通用异步收发传输器¶
使用示例¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | # coding=utf-8
# This is a sample Python script.
import utime
from driver import UART
print("-------------------uart test--------------------")
print("-----How to test: haas100 connect PIN10 and PIN12-------")
print("-----How to test: haaseduk1 connect PIN19 and PIN21-------")
uart = UART();
uart.open("serial2")
utime.sleep_ms(1000)
writeBuf = bytearray([0x41, 0x42, 0x43, 0x44]);
for i in range(10):
uart.write(writeBuf)
utime.sleep_ms(1000)
readBuf = bytearray(4)
recvSize = uart.read(readBuf)
utime.sleep_ms(100)
print(recvSize)
print(readBuf)
uart.close()
|
配置信息¶
board.jon 语法和功能的详细配置项信息请参考 BoardConfig — 硬件端口配置文件(board.json)详解 。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | {
"version": "0.0.1",
"io": {
"serial2":{
"type":"UART",
"port":2,
"dataWidth":8,
"baudRate":115200,
"stopBits":1,
"flowControl":"disable",
"parity":"none"
}
},
"debugLevel": "DEBUG",
"repl":"disable"
}
|
函数接口¶
-
UART.
write
(dataBuffer, expect_size)[源代码]¶ 发送串口数据,该函数为阻塞函数,串口发送完成后才会返回
- 参数
dataBuffer – 待写入的数据
expect_size – 待写入的数据字节数
- 返回
负数表示异常,正数或0表示真实写出的数据
- 引发
OSError – EINVAL