1e41f4b71Sopenharmony_ci# UART 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci## 概述 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci### 功能简介 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ciUART指异步收发传输器(Universal Asynchronous Receiver/Transmitter),是通用串行数据总线,用于异步通信。该总线双向通信,可以实现全双工传输。 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci两个UART设备的连接示意图如下,UART与其他模块一般用2线(图1)或4线(图2)相连,它们分别是: 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci- TX:发送数据端,和对端的RX相连。 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci- RX:接收数据端,和对端的TX相连。 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci- RTS:发送请求信号,用于指示本设备是否准备好,可接受数据,和对端CTS相连。 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci- CTS:允许发送信号,用于判断是否可以向对端发送数据,和对端RTS相连。 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ci**图 1** 2线UART设备连接示意图 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci**图 2** 4线UART设备连接示意图 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ci 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ciUART通信之前,收发双方需要约定好一些参数:波特率、数据格式(起始位、数据位、校验位、停止位)等。通信过程中,UART通过TX发送给对端数据,通过RX接收对端发送的数据。当UART接收缓存达到预定的门限值时,RTS变为不可发送数据,对端的CTS检测到不可发送数据,则停止发送数据。 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ciUART接口定义了操作UART端口的通用方法集合,包括: 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ci- 打开/关闭UART设备 32e41f4b71Sopenharmony_ci 33e41f4b71Sopenharmony_ci- 读写数据 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ci- 设置/获取UART设备波特率 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ci- 设置/获取UART设备属性 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ci### 基本概念 40e41f4b71Sopenharmony_ci 41e41f4b71Sopenharmony_ci- 异步通信 42e41f4b71Sopenharmony_ci 43e41f4b71Sopenharmony_ci 异步通信中,数据通常以字符或者字节为单位组成字符帧传送。字符帧由发送端逐帧发送,通过传输线被接收设备逐帧接收。发送端和接收端可以由各自的时钟来控制数据的发送和接收,这两个时钟源彼此独立,互不同步。异步通信以一个字符为传输单位,通信中两个字符间的时间间隔是不固定的,然而在同一个字符中的两个相邻位代码间的时间间隔是固定的。 44e41f4b71Sopenharmony_ci 45e41f4b71Sopenharmony_ci- 全双工传输(Full Duplex) 46e41f4b71Sopenharmony_ci 47e41f4b71Sopenharmony_ci 此通信模式允许数据在两个方向上同时传输,它在能力上相当于两个单工通信方式的结合。全双工可以同时进行信号的双向传输。 48e41f4b71Sopenharmony_ci 49e41f4b71Sopenharmony_ci### 运作机制 50e41f4b71Sopenharmony_ci 51e41f4b71Sopenharmony_ci在HDF框架中,UART接口适配模式采用独立服务模式(如图3所示)。在这种模式下,每一个设备对象会独立发布一个设备服务来处理外部访问,设备管理器收到API的访问请求之后,通过提取该请求的参数,达到调用实际设备对象的相应内部方法的目的。独立服务模式可以直接借助HDF设备管理器的服务管理能力,但需要为每个设备单独配置设备节点,增加内存占用。 52e41f4b71Sopenharmony_ci 53e41f4b71Sopenharmony_ci独立服务模式下,核心层不会统一发布一个服务供上层使用,因此这种模式下驱动要为每个控制器发布一个服务,具体表现为: 54e41f4b71Sopenharmony_ci 55e41f4b71Sopenharmony_ci- 驱动适配者需要实现HdfDriverEntry的Bind钩子函数以绑定服务。 56e41f4b71Sopenharmony_ci 57e41f4b71Sopenharmony_ci- device_info.hcs文件中deviceNode的policy字段为1或2,不能为0。 58e41f4b71Sopenharmony_ci 59e41f4b71Sopenharmony_ciUART模块各分层作用: 60e41f4b71Sopenharmony_ci 61e41f4b71Sopenharmony_ci- 接口层提供打开UART设备、UART设备读取指定长度数据、UART设备写入指定长度数据、设置UART设备波特率、获取设UART设备波特率、设置UART设备属性、获取UART设备波特率、设置UART设备传输模式、关闭UART设备的接口。 62e41f4b71Sopenharmony_ci 63e41f4b71Sopenharmony_ci- 核心层主要提供UART控制器的创建、移除以及管理的能力,通过钩子函数与适配层交互。 64e41f4b71Sopenharmony_ci 65e41f4b71Sopenharmony_ci- 适配层主要是将钩子函数的功能实例化,实现具体的功能。 66e41f4b71Sopenharmony_ci 67e41f4b71Sopenharmony_ci**图 3** UART独立服务模式结构图 68e41f4b71Sopenharmony_ci 69e41f4b71Sopenharmony_ci 70e41f4b71Sopenharmony_ci 71e41f4b71Sopenharmony_ci### 约束与限制 72e41f4b71Sopenharmony_ci 73e41f4b71Sopenharmony_ciUART模块UartSetTransMode接口设置传输模式在Linux中不支持,仅为空实现。 74e41f4b71Sopenharmony_ci 75e41f4b71Sopenharmony_ci## 使用指导 76e41f4b71Sopenharmony_ci 77e41f4b71Sopenharmony_ci### 场景介绍 78e41f4b71Sopenharmony_ci 79e41f4b71Sopenharmony_ciUART模块应用比较广泛,主要用于实现设备之间的低速串行通信,例如输出打印信息,当然也可以外接各种模块,如GPS、蓝牙等。 80e41f4b71Sopenharmony_ci 81e41f4b71Sopenharmony_ci### 接口说明 82e41f4b71Sopenharmony_ci 83e41f4b71Sopenharmony_ciUART模块提供的主要接口如表1所示,具体API详见//drivers/hdf_core/framework/include/platform/uart_if.h。 84e41f4b71Sopenharmony_ci 85e41f4b71Sopenharmony_ci**表 1** UART驱动API接口功能介绍 86e41f4b71Sopenharmony_ci 87e41f4b71Sopenharmony_ci| 接口名 | 接口描述 | 88e41f4b71Sopenharmony_ci| -------- | -------- | 89e41f4b71Sopenharmony_ci| DevHandle UartOpen(uint32_t port) | UART获取设备句柄 | 90e41f4b71Sopenharmony_ci| void UartClose(DevHandle handle) | UART释放设备句柄 | 91e41f4b71Sopenharmony_ci| int32_t UartRead(DevHandle handle, uint8_t \*data, uint32_t size) | 从UART设备中读取指定长度的数据 | 92e41f4b71Sopenharmony_ci| int32_t UartWrite(DevHandle handle, uint8_t \*data, uint32_t size) | 向UART设备中写入指定长度的数据 | 93e41f4b71Sopenharmony_ci| int32_t UartGetBaud(DevHandle handle, uint32_t \*baudRate) | UART获取波特率 | 94e41f4b71Sopenharmony_ci| int32_t UartSetBaud(DevHandle handle, uint32_t baudRate) | UART设置波特率 | 95e41f4b71Sopenharmony_ci| int32_t UartGetAttribute(DevHandle handle, struct UartAttribute \*attribute) | UART获取设备属性 | 96e41f4b71Sopenharmony_ci| int32_t UartSetAttribute(DevHandle handle, struct UartAttribute \*attribute) | UART设置设备属性 | 97e41f4b71Sopenharmony_ci| int32_t UartSetTransMode(DevHandle handle, enum UartTransMode mode) | UART设置传输模式 | 98e41f4b71Sopenharmony_ci 99e41f4b71Sopenharmony_ci>  **说明:**<br> 100e41f4b71Sopenharmony_ci> 本文涉及的UART所有接口,支持内核态及用户态使用。 101e41f4b71Sopenharmony_ci 102e41f4b71Sopenharmony_ci### 开发步骤 103e41f4b71Sopenharmony_ci 104e41f4b71Sopenharmony_ci使用UART的一般流程如下图所示。 105e41f4b71Sopenharmony_ci 106e41f4b71Sopenharmony_ci**图 4** UART使用流程图 107e41f4b71Sopenharmony_ci 108e41f4b71Sopenharmony_ci 109e41f4b71Sopenharmony_ci 110e41f4b71Sopenharmony_ci 111e41f4b71Sopenharmony_ci#### 获取UART设备句柄 112e41f4b71Sopenharmony_ci 113e41f4b71Sopenharmony_ci在使用UART进行通信时,首先要调用UartOpen获取UART设备句柄,该函数会返回指定端口号的UART设备句柄。 114e41f4b71Sopenharmony_ci 115e41f4b71Sopenharmony_ci```c 116e41f4b71Sopenharmony_ciDevHandle UartOpen(uint32_t port); 117e41f4b71Sopenharmony_ci``` 118e41f4b71Sopenharmony_ci 119e41f4b71Sopenharmony_ci**表 2** UartOpen参数和返回值描述 120e41f4b71Sopenharmony_ci 121e41f4b71Sopenharmony_ci| 参数 | 参数描述 | 122e41f4b71Sopenharmony_ci| -------- | -------- | 123e41f4b71Sopenharmony_ci| port | uint32_t类型,UART设备号 | 124e41f4b71Sopenharmony_ci| **返回值** | **返回值描述** | 125e41f4b71Sopenharmony_ci| NULL | 获取UART设备句柄失败 | 126e41f4b71Sopenharmony_ci| 设备句柄 | UART设备句柄 | 127e41f4b71Sopenharmony_ci 128e41f4b71Sopenharmony_ci假设系统中的UART端口号为1,获取该UART设备句柄的示例如下: 129e41f4b71Sopenharmony_ci 130e41f4b71Sopenharmony_ci```c 131e41f4b71Sopenharmony_ciDevHandle handle = NULL; // UART设备句柄 132e41f4b71Sopenharmony_ciuint32_t port = 1; // UART设备端口号 133e41f4b71Sopenharmony_ci 134e41f4b71Sopenharmony_cihandle = UartOpen(port); 135e41f4b71Sopenharmony_ciif (handle == NULL) { 136e41f4b71Sopenharmony_ci HDF_LOGE("UartOpen: open uart_%u failed!\n", port); 137e41f4b71Sopenharmony_ci return; 138e41f4b71Sopenharmony_ci} 139e41f4b71Sopenharmony_ci``` 140e41f4b71Sopenharmony_ci 141e41f4b71Sopenharmony_ci#### UART设置波特率 142e41f4b71Sopenharmony_ci 143e41f4b71Sopenharmony_ci在通信之前,需要设置UART的波特率,设置波特率的函数如下所示: 144e41f4b71Sopenharmony_ci 145e41f4b71Sopenharmony_ci```c 146e41f4b71Sopenharmony_ciint32_t UartSetBaud(DevHandle handle, uint32_t baudRate); 147e41f4b71Sopenharmony_ci``` 148e41f4b71Sopenharmony_ci 149e41f4b71Sopenharmony_ci**表 3** UartSetBaud参数和返回值描述 150e41f4b71Sopenharmony_ci 151e41f4b71Sopenharmony_ci| 参数 | 参数描述 | 152e41f4b71Sopenharmony_ci| -------- | -------- | 153e41f4b71Sopenharmony_ci| handle | DevHandle类型,UART设备句柄 | 154e41f4b71Sopenharmony_ci| baudRate | uint32_t类型,待设置的波特率值 | 155e41f4b71Sopenharmony_ci| **返回值** | **返回值描述** | 156e41f4b71Sopenharmony_ci| HDF_SUCCESS | UART设置波特率成功 | 157e41f4b71Sopenharmony_ci| 负数 | UART设置波特率失败 | 158e41f4b71Sopenharmony_ci 159e41f4b71Sopenharmony_ci假设需要设置的UART波特率为9600,设置波特率的实例如下: 160e41f4b71Sopenharmony_ci 161e41f4b71Sopenharmony_ci```c 162e41f4b71Sopenharmony_ciint32_t ret; 163e41f4b71Sopenharmony_ci 164e41f4b71Sopenharmony_ciret = UartSetBaud(handle, 9600); // 设置UART波特率 165e41f4b71Sopenharmony_ciif (ret != HDF_SUCCESS) { 166e41f4b71Sopenharmony_ci HDF_LOGE("UartSetBaud: failed, ret %d\n", ret); 167e41f4b71Sopenharmony_ci return ret; 168e41f4b71Sopenharmony_ci} 169e41f4b71Sopenharmony_ci``` 170e41f4b71Sopenharmony_ci 171e41f4b71Sopenharmony_ci#### UART获取波特率 172e41f4b71Sopenharmony_ci 173e41f4b71Sopenharmony_ci设置UART的波特率后,可以通过获取波特率接口来查看UART当前的波特率,获取波特率的函数如下所示: 174e41f4b71Sopenharmony_ci 175e41f4b71Sopenharmony_ci```c 176e41f4b71Sopenharmony_ciint32_t UartGetBaud(DevHandle handle, uint32_t *baudRate); 177e41f4b71Sopenharmony_ci``` 178e41f4b71Sopenharmony_ci 179e41f4b71Sopenharmony_ci**表 4** UartGetBaud参数和返回值描述 180e41f4b71Sopenharmony_ci 181e41f4b71Sopenharmony_ci| 参数 | 参数描述 | 182e41f4b71Sopenharmony_ci| -------- | -------- | 183e41f4b71Sopenharmony_ci| handle | DevHandle类型,UART设备句柄 | 184e41f4b71Sopenharmony_ci| baudRate | uint32_t类型指针,用于接收波特率的值 | 185e41f4b71Sopenharmony_ci| **返回值** | **返回值描述** | 186e41f4b71Sopenharmony_ci| HDF_SUCCESS | UART获取波特率成功 | 187e41f4b71Sopenharmony_ci| 负数 | UART获取波特率失败 | 188e41f4b71Sopenharmony_ci 189e41f4b71Sopenharmony_ci获取波特率的实例如下: 190e41f4b71Sopenharmony_ci 191e41f4b71Sopenharmony_ci```c 192e41f4b71Sopenharmony_ciint32_t ret; 193e41f4b71Sopenharmony_ciuint32_t baudRate; 194e41f4b71Sopenharmony_ci 195e41f4b71Sopenharmony_ciret = UartGetBaud(handle, &baudRate); // 获取UART波特率 196e41f4b71Sopenharmony_ciif (ret != HDF_SUCCESS) { 197e41f4b71Sopenharmony_ci HDF_LOGE("UartGetBaud: failed, ret %d\n", ret); 198e41f4b71Sopenharmony_ci return ret; 199e41f4b71Sopenharmony_ci} 200e41f4b71Sopenharmony_ci``` 201e41f4b71Sopenharmony_ci 202e41f4b71Sopenharmony_ci#### UART设置设备属性 203e41f4b71Sopenharmony_ci 204e41f4b71Sopenharmony_ci在通信之前,需要设置UART的设备属性,设置设备属性的函数如下所示: 205e41f4b71Sopenharmony_ci 206e41f4b71Sopenharmony_ci```c 207e41f4b71Sopenharmony_ciint32_t UartSetAttribute(DevHandle handle, struct UartAttribute *attribute); 208e41f4b71Sopenharmony_ci``` 209e41f4b71Sopenharmony_ci 210e41f4b71Sopenharmony_ci**表 5** UartSetAttribute参数和返回值描述 211e41f4b71Sopenharmony_ci 212e41f4b71Sopenharmony_ci| 参数 | 参数描述 | 213e41f4b71Sopenharmony_ci| -------- | -------- | 214e41f4b71Sopenharmony_ci| handle | DevHandle类型,UART设备句柄 | 215e41f4b71Sopenharmony_ci| attribute | 结构体指针,待设置的设备属性 | 216e41f4b71Sopenharmony_ci| **返回值** | **返回值描述** | 217e41f4b71Sopenharmony_ci| HDF_SUCCESS | UART设置设备属性成功 | 218e41f4b71Sopenharmony_ci| 负数 | UART设置设备属性失败 | 219e41f4b71Sopenharmony_ci 220e41f4b71Sopenharmony_ci设置UART的设备属性的实例如下: 221e41f4b71Sopenharmony_ci 222e41f4b71Sopenharmony_ci```c 223e41f4b71Sopenharmony_ciint32_t ret; 224e41f4b71Sopenharmony_cistruct UartAttribute attribute; 225e41f4b71Sopenharmony_ci 226e41f4b71Sopenharmony_ciattribute.dataBits = UART_ATTR_DATABIT_7; // UART传输数据位宽,一次传输7个bit 227e41f4b71Sopenharmony_ciattribute.parity = UART_ATTR_PARITY_NONE; // UART传输数据无校检 228e41f4b71Sopenharmony_ciattribute.stopBits = UART_ATTR_STOPBIT_1; // UART传输数据停止位为1位 229e41f4b71Sopenharmony_ciattribute.rts = UART_ATTR_RTS_DIS; // UART禁用RTS 230e41f4b71Sopenharmony_ciattribute.cts = UART_ATTR_CTS_DIS; // UART禁用CTS 231e41f4b71Sopenharmony_ciattribute.fifoRxEn = UART_ATTR_RX_FIFO_EN; // UART使能RX FIFO 232e41f4b71Sopenharmony_ciattribute.fifoTxEn = UART_ATTR_TX_FIFO_EN; // UART使能TX FIFO 233e41f4b71Sopenharmony_ci 234e41f4b71Sopenharmony_ciret = UartSetAttribute(handle, &attribute); // 设置UART设备属性 235e41f4b71Sopenharmony_ciif (ret != HDF_SUCCESS) { 236e41f4b71Sopenharmony_ci HDF_LOGE("UartSetAttribute: failed, ret %d\n", ret); 237e41f4b71Sopenharmony_citurn ret; 238e41f4b71Sopenharmony_ci} 239e41f4b71Sopenharmony_ci``` 240e41f4b71Sopenharmony_ci 241e41f4b71Sopenharmony_ci#### UART获取设备属性 242e41f4b71Sopenharmony_ci 243e41f4b71Sopenharmony_ci设置UART的设备属性后,可以通过获取设备属性接口来查看UART当前的设备属性,获取设备属性的函数如下所示: 244e41f4b71Sopenharmony_ci 245e41f4b71Sopenharmony_ci```c 246e41f4b71Sopenharmony_ciint32_t UartGetAttribute(DevHandle handle, struct UartAttribute *attribute); 247e41f4b71Sopenharmony_ci``` 248e41f4b71Sopenharmony_ci 249e41f4b71Sopenharmony_ci**表 6** UartGetAttribute参数和返回值描述 250e41f4b71Sopenharmony_ci 251e41f4b71Sopenharmony_ci| 参数 | 参数描述 | 252e41f4b71Sopenharmony_ci| -------- | -------- | 253e41f4b71Sopenharmony_ci| handle | DevHandle类型,UART设备句柄 | 254e41f4b71Sopenharmony_ci| attribute | 结构体指针,接收UART设备属性的指针 | 255e41f4b71Sopenharmony_ci| **返回值** | **返回值描述** | 256e41f4b71Sopenharmony_ci| HDF_SUCCESS | UART获取设备属性成功 | 257e41f4b71Sopenharmony_ci| 负数 | UART获取设备属性失败 | 258e41f4b71Sopenharmony_ci 259e41f4b71Sopenharmony_ci获取UART的设备属性的实例如下: 260e41f4b71Sopenharmony_ci 261e41f4b71Sopenharmony_ci```c 262e41f4b71Sopenharmony_ciint32_t ret; 263e41f4b71Sopenharmony_cistruct UartAttribute attribute; 264e41f4b71Sopenharmony_ci 265e41f4b71Sopenharmony_ciret = UartGetAttribute(handle, &attribute); // 获取UART设备属性 266e41f4b71Sopenharmony_ciif (ret != HDF_SUCCESS) { 267e41f4b71Sopenharmony_ci HDF_LOGE("UartGetAttribute: failed, ret %d\n", ret); 268e41f4b71Sopenharmony_ci return ret; 269e41f4b71Sopenharmony_ci} 270e41f4b71Sopenharmony_ci``` 271e41f4b71Sopenharmony_ci 272e41f4b71Sopenharmony_ci#### 设置UART传输模式 273e41f4b71Sopenharmony_ci 274e41f4b71Sopenharmony_ci在通信之前,需要设置UART的传输模式,设置传输模式的函数如下所示: 275e41f4b71Sopenharmony_ci 276e41f4b71Sopenharmony_ci```c 277e41f4b71Sopenharmony_ciint32_t UartSetTransMode(DevHandle handle, enum UartTransMode mode); 278e41f4b71Sopenharmony_ci``` 279e41f4b71Sopenharmony_ci 280e41f4b71Sopenharmony_ci**表 7** UartSetTransMode参数和返回值描述 281e41f4b71Sopenharmony_ci 282e41f4b71Sopenharmony_ci| 参数 | 参数描述 | 283e41f4b71Sopenharmony_ci| -------- | -------- | 284e41f4b71Sopenharmony_ci| handle | DevHandle类型,UART设备句柄 | 285e41f4b71Sopenharmony_ci| mode | 枚举类型,待设置的传输模式 | 286e41f4b71Sopenharmony_ci| **返回值** | **返回值描述** | 287e41f4b71Sopenharmony_ci| HDF_SUCCESS | UART设置传输模式成功 | 288e41f4b71Sopenharmony_ci| 负数 | UART设置传输模式失败 | 289e41f4b71Sopenharmony_ci 290e41f4b71Sopenharmony_ci假设需要设置的UART传输模式为UART_MODE_RD_BLOCK,设置传输模式的实例如下: 291e41f4b71Sopenharmony_ci 292e41f4b71Sopenharmony_ci```c 293e41f4b71Sopenharmony_ciint32_t ret; 294e41f4b71Sopenharmony_ci 295e41f4b71Sopenharmony_ciret = UartSetTransMode(handle, UART_MODE_RD_BLOCK); // 设置UART传输模式 296e41f4b71Sopenharmony_ciif (ret != HDF_SUCCESS) { 297e41f4b71Sopenharmony_ci HDF_LOGE("UartSetTransMode: failed, ret %d\n", ret); 298e41f4b71Sopenharmony_ci return ret; 299e41f4b71Sopenharmony_ci} 300e41f4b71Sopenharmony_ci``` 301e41f4b71Sopenharmony_ci 302e41f4b71Sopenharmony_ci#### 向UART设备写入指定长度的数据 303e41f4b71Sopenharmony_ci 304e41f4b71Sopenharmony_ci对应的接口函数如下所示: 305e41f4b71Sopenharmony_ci 306e41f4b71Sopenharmony_ci```c 307e41f4b71Sopenharmony_ciint32_t UartWrite(DevHandle handle, uint8_t *data, uint32_t size); 308e41f4b71Sopenharmony_ci``` 309e41f4b71Sopenharmony_ci 310e41f4b71Sopenharmony_ci**表 8** UartWrite参数和返回值描述 311e41f4b71Sopenharmony_ci 312e41f4b71Sopenharmony_ci| 参数 | 参数描述 | 313e41f4b71Sopenharmony_ci| -------- | -------- | 314e41f4b71Sopenharmony_ci| handle | DevHandle类型,UART设备句柄 | 315e41f4b71Sopenharmony_ci| data | uint8_t类型指针,待写入数据的 | 316e41f4b71Sopenharmony_ci| size | uint32_t类型,待写入数据的长度 | 317e41f4b71Sopenharmony_ci| **返回值** | **返回值描述** | 318e41f4b71Sopenharmony_ci| HDF_SUCCESS | UART写数据成功 | 319e41f4b71Sopenharmony_ci| 负数 | UART写数据失败 | 320e41f4b71Sopenharmony_ci 321e41f4b71Sopenharmony_ci写入指定长度数据的实例如下: 322e41f4b71Sopenharmony_ci 323e41f4b71Sopenharmony_ci```c 324e41f4b71Sopenharmony_ciint32_t ret; 325e41f4b71Sopenharmony_ciuint8_t wbuff[5] = {1, 2, 3, 4, 5}; 326e41f4b71Sopenharmony_ci 327e41f4b71Sopenharmony_ciret = UartWrite(handle, wbuff, 5); // 向UART设备写入指定长度的数据 328e41f4b71Sopenharmony_ciif (ret != HDF_SUCCESS) { 329e41f4b71Sopenharmony_ci HDF_LOGE("UartWrite: failed, ret %d\n", ret); 330e41f4b71Sopenharmony_ci return ret; 331e41f4b71Sopenharmony_ci} 332e41f4b71Sopenharmony_ci``` 333e41f4b71Sopenharmony_ci 334e41f4b71Sopenharmony_ci#### 从UART设备中读取指定长度的数据 335e41f4b71Sopenharmony_ci 336e41f4b71Sopenharmony_ci对应的接口函数如下所示: 337e41f4b71Sopenharmony_ci 338e41f4b71Sopenharmony_ci```c 339e41f4b71Sopenharmony_ciint32_t UartRead(DevHandle handle, uint8_t *data, uint32_t size); 340e41f4b71Sopenharmony_ci``` 341e41f4b71Sopenharmony_ci 342e41f4b71Sopenharmony_ci**表 9** UartRead参数和返回值描述 343e41f4b71Sopenharmony_ci 344e41f4b71Sopenharmony_ci| 参数 | 参数描述 | 345e41f4b71Sopenharmony_ci| -------- | -------- | 346e41f4b71Sopenharmony_ci| handle | DevHandle类型,UART设备句柄 | 347e41f4b71Sopenharmony_ci| data | uint8_t类型指针,接收读取数据 | 348e41f4b71Sopenharmony_ci| size | uint32_t类型,待读取数据的长度 | 349e41f4b71Sopenharmony_ci| **返回值** | **返回值描述** | 350e41f4b71Sopenharmony_ci| 非负数 | UART读取到的数据长度 | 351e41f4b71Sopenharmony_ci| 负数 | UART读取数据失败 | 352e41f4b71Sopenharmony_ci 353e41f4b71Sopenharmony_ci读取指定长度数据的实例如下: 354e41f4b71Sopenharmony_ci 355e41f4b71Sopenharmony_ci```c 356e41f4b71Sopenharmony_ciint32_t ret; 357e41f4b71Sopenharmony_ciuint8_t rbuff[5] = {0}; 358e41f4b71Sopenharmony_ci 359e41f4b71Sopenharmony_ciret = UartRead(handle, rbuff, 5); // 从UART设备读取指定长度的数据 360e41f4b71Sopenharmony_ciif (ret < 0) { 361e41f4b71Sopenharmony_ci HDF_LOGE("UartRead: failed, ret %d\n", ret); 362e41f4b71Sopenharmony_ci return ret; 363e41f4b71Sopenharmony_ci} 364e41f4b71Sopenharmony_ci``` 365e41f4b71Sopenharmony_ci 366e41f4b71Sopenharmony_ci>  **注意:** 367e41f4b71Sopenharmony_ci> UART返回值为非负值,表示UART读取成功。若返回值等于0,表示UART无有效数据可以读取。若返回值大于0,表示实际读取到的数据长度,该长度小于或等于传入的参数size的大小,并且不超过当前正在使用的UART控制器规定的最大单次读取数据长度的值。 368e41f4b71Sopenharmony_ci 369e41f4b71Sopenharmony_ci 370e41f4b71Sopenharmony_ci#### 销毁UART设备句柄 371e41f4b71Sopenharmony_ci 372e41f4b71Sopenharmony_ciUART通信完成之后,需要销毁UART设备句柄,函数如下所示: 373e41f4b71Sopenharmony_ci 374e41f4b71Sopenharmony_ci```c 375e41f4b71Sopenharmony_civoid UartClose(DevHandle handle); 376e41f4b71Sopenharmony_ci``` 377e41f4b71Sopenharmony_ci 378e41f4b71Sopenharmony_ci该函数会释放申请的资源。 379e41f4b71Sopenharmony_ci 380e41f4b71Sopenharmony_ci**表 10** UartClose参数和返回值描述 381e41f4b71Sopenharmony_ci 382e41f4b71Sopenharmony_ci| 参数 | 参数描述 | 383e41f4b71Sopenharmony_ci| -------- | -------- | 384e41f4b71Sopenharmony_ci| handle | UART设备句柄 | 385e41f4b71Sopenharmony_ci 386e41f4b71Sopenharmony_ci销毁UART设备句柄的实例如下: 387e41f4b71Sopenharmony_ci 388e41f4b71Sopenharmony_ci```c 389e41f4b71Sopenharmony_ciUartClose(handle); // 销毁UART设备句柄 390e41f4b71Sopenharmony_ci``` 391e41f4b71Sopenharmony_ci 392e41f4b71Sopenharmony_ci## 使用实例 393e41f4b71Sopenharmony_ci 394e41f4b71Sopenharmony_ci下面将基于Hi3516DV300开发板展示使用UART完整操作,步骤主要如下: 395e41f4b71Sopenharmony_ci 396e41f4b71Sopenharmony_ci1. 传入UART端口号num,打开端口号对应的UART设备并获得UART设备句柄。 397e41f4b71Sopenharmony_ci 398e41f4b71Sopenharmony_ci2. 通过UART设备句柄及设置的波特率,设置UART设备的波特率。 399e41f4b71Sopenharmony_ci 400e41f4b71Sopenharmony_ci3. 通过UART设备句柄及待获取的波特率,获取UART设备的波特率。 401e41f4b71Sopenharmony_ci 402e41f4b71Sopenharmony_ci4. 通过UART设备句柄及待设置的设备属性,设置UART设备的设备属性。 403e41f4b71Sopenharmony_ci 404e41f4b71Sopenharmony_ci5. 通过UART设备句柄及待获取的设备属性,获取UART设备的设备属性。 405e41f4b71Sopenharmony_ci 406e41f4b71Sopenharmony_ci6. 通过UART设备句柄及待设置的传输模式,设置UART设备的传输模式。 407e41f4b71Sopenharmony_ci 408e41f4b71Sopenharmony_ci7. 通过UART设备句柄及待传输的数据及大小,传输指定长度的数据。 409e41f4b71Sopenharmony_ci 410e41f4b71Sopenharmony_ci8. 通过UART设备句柄及待接收的数据及大小,接收指定长度的数据。 411e41f4b71Sopenharmony_ci 412e41f4b71Sopenharmony_ci9. 通过UART设备句柄,关闭UART设备。 413e41f4b71Sopenharmony_ci 414e41f4b71Sopenharmony_ci```c 415e41f4b71Sopenharmony_ci#include "hdf_log.h" 416e41f4b71Sopenharmony_ci#include "uart_if.h" 417e41f4b71Sopenharmony_ci 418e41f4b71Sopenharmony_cistatic int32_t UartTestSample(void) 419e41f4b71Sopenharmony_ci{ 420e41f4b71Sopenharmony_ci int32_t ret; 421e41f4b71Sopenharmony_ci uint32_t port; 422e41f4b71Sopenharmony_ci uint32_t baud; 423e41f4b71Sopenharmony_ci DevHandle handle = NULL; 424e41f4b71Sopenharmony_ci uint8_t wbuff[5] = { 1, 2, 3, 4, 5 }; 425e41f4b71Sopenharmony_ci uint8_t rbuff[5] = { 0 }; 426e41f4b71Sopenharmony_ci struct UartAttribute attribute; 427e41f4b71Sopenharmony_ci 428e41f4b71Sopenharmony_ci attribute.dataBits = UART_ATTR_DATABIT_7; // UART传输数据位宽,一次传输7个bit 429e41f4b71Sopenharmony_ci attribute.parity = UART_ATTR_PARITY_NONE; // UART传输数据无校检 430e41f4b71Sopenharmony_ci attribute.stopBits = UART_ATTR_STOPBIT_1; // UART传输数据停止位为1位 431e41f4b71Sopenharmony_ci attribute.rts = UART_ATTR_RTS_DIS; // UART禁用RTS 432e41f4b71Sopenharmony_ci attribute.cts = UART_ATTR_CTS_DIS; // UART禁用CTS 433e41f4b71Sopenharmony_ci attribute.fifoRxEn = UART_ATTR_RX_FIFO_EN; // UART使能RX FIFO 434e41f4b71Sopenharmony_ci attribute.fifoTxEn = UART_ATTR_TX_FIFO_EN; // UART使能TX FIFO 435e41f4b71Sopenharmony_ci 436e41f4b71Sopenharmony_ci port = 1; // UART设备端口号,要填写实际平台上的端口号 437e41f4b71Sopenharmony_ci 438e41f4b71Sopenharmony_ci handle = UartOpen(port); // 获取UART设备句柄 439e41f4b71Sopenharmony_ci if (handle == NULL) { 440e41f4b71Sopenharmony_ci HDF_LOGE("UartOpen: open uart_%u failed!\n", port); 441e41f4b71Sopenharmony_ci return HDF_FAILURE; 442e41f4b71Sopenharmony_ci } 443e41f4b71Sopenharmony_ci 444e41f4b71Sopenharmony_ci ret = UartSetBaud(handle, 9600); // 设置UART波特率为9600 445e41f4b71Sopenharmony_ci if (ret != HDF_SUCCESS) { 446e41f4b71Sopenharmony_ci HDF_LOGE("UartSetBaud: set baud failed, ret %d\n", ret); 447e41f4b71Sopenharmony_ci goto ERR; 448e41f4b71Sopenharmony_ci } 449e41f4b71Sopenharmony_ci 450e41f4b71Sopenharmony_ci ret = UartGetBaud(handle, &baud); // 获取UART波特率 451e41f4b71Sopenharmony_ci if (ret != HDF_SUCCESS) { 452e41f4b71Sopenharmony_ci HDF_LOGE("UartGetBaud: get baud failed, ret %d\n", ret); 453e41f4b71Sopenharmony_ci goto ERR; 454e41f4b71Sopenharmony_ci } 455e41f4b71Sopenharmony_ci 456e41f4b71Sopenharmony_ci ret = UartSetAttribute(handle, &attribute); // 设置UART设备属性 457e41f4b71Sopenharmony_ci if (ret != HDF_SUCCESS) { 458e41f4b71Sopenharmony_ci HDF_LOGE("UartSetAttribute: set attribute failed, ret %d\n", ret); 459e41f4b71Sopenharmony_ci goto ERR; 460e41f4b71Sopenharmony_ci } 461e41f4b71Sopenharmony_ci 462e41f4b71Sopenharmony_ci ret = UartGetAttribute(handle, &attribute); // 获取UART设备属性 463e41f4b71Sopenharmony_ci if (ret != HDF_SUCCESS) { 464e41f4b71Sopenharmony_ci HDF_LOGE("UartGetAttribute: get attribute failed, ret %d\n", ret); 465e41f4b71Sopenharmony_ci goto ERR; 466e41f4b71Sopenharmony_ci } 467e41f4b71Sopenharmony_ci 468e41f4b71Sopenharmony_ci ret = UartSetTransMode(handle, UART_MODE_RD_NONBLOCK); // 设置UART传输模式为非阻塞模式 469e41f4b71Sopenharmony_ci if (ret != HDF_SUCCESS) { 470e41f4b71Sopenharmony_ci HDF_LOGE("UartSetTransMode: set trans mode failed, ret %d\n", ret); 471e41f4b71Sopenharmony_ci goto ERR; 472e41f4b71Sopenharmony_ci } 473e41f4b71Sopenharmony_ci 474e41f4b71Sopenharmony_ci ret = UartWrite(handle, wbuff, 5); // 向UART设备写入5字节的数据 475e41f4b71Sopenharmony_ci if (ret != HDF_SUCCESS) { 476e41f4b71Sopenharmony_ci HDF_LOGE("UartWrite: write data failed, ret %d\n", ret); 477e41f4b71Sopenharmony_ci goto ERR; 478e41f4b71Sopenharmony_ci } 479e41f4b71Sopenharmony_ci 480e41f4b71Sopenharmony_ci ret = UartRead(handle, rbuff, 5); // 从UART设备读取5字节的数据 481e41f4b71Sopenharmony_ci if (ret < 0) { 482e41f4b71Sopenharmony_ci HDF_LOGE("UartRead: read data failed, ret %d\n", ret); 483e41f4b71Sopenharmony_ci goto ERR; 484e41f4b71Sopenharmony_ci } 485e41f4b71Sopenharmony_ci HDF_LOGI("%s: function tests end, %d", __func__, ret); 486e41f4b71Sopenharmony_ciERR: 487e41f4b71Sopenharmony_ci UartClose(handle); // 销毁UART设备句柄 488e41f4b71Sopenharmony_ci return ret; 489e41f4b71Sopenharmony_ci} 490e41f4b71Sopenharmony_ci``` 491