1e41f4b71Sopenharmony_ci# UART
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci## Overview
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ci### Function
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ciThe Universal Asynchronous Receiver/Transmitter (UART) is a universal serial data bus used for asynchronous communication. It enables bi-directional communication between devices in full-duplex mode.
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ciA UART is connected to other modules through two wires (as shown in Figure 1) or four wires (as shown in Figure 2).
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ci  - TX: UART transmitter. It is connected to the RX of the peer UART.
12e41f4b71Sopenharmony_ci  - RX: UART receiver. It is connected to the TX of the peer UART.
13e41f4b71Sopenharmony_ci  - RTS: Request to Send signal, indicating whether the local UART is ready to receive data. It is connected to the CTS of the peer UART.
14e41f4b71Sopenharmony_ci  - CTS: Clear to Send signal, indicating whether the local UART is allowed to send data to the peer end. It is connected to the RTS of the peer UART.
15e41f4b71Sopenharmony_ci
16e41f4b71Sopenharmony_ci**Figure 1** Two-wire UART communication
17e41f4b71Sopenharmony_ci
18e41f4b71Sopenharmony_ci![image1](figures/2-wire-uart-communication.png "2-wire-uart-communication")
19e41f4b71Sopenharmony_ci
20e41f4b71Sopenharmony_ci**Figure 2** Four-wire UART communication
21e41f4b71Sopenharmony_ci
22e41f4b71Sopenharmony_ci ![image2](figures/4-wire-uart-communication.png "4-wire-uart-communication")
23e41f4b71Sopenharmony_ci
24e41f4b71Sopenharmony_ciThe UART transmitter and receiver must have the same settings on particular attributes, such as the baud rate and data format (start bit, data bits, parity bit, and stop bit) before they start to communicate. A UART sends data to the peer end over the TX and receives data from the peer end over the RX. When the size of the buffer used by a UART for storing received data reaches the preset threshold, the RTS signal of the UART changes to **1** (data cannot be received), and the peer UART stops sending data to it because its CTS signal does not allow it to send data.
25e41f4b71Sopenharmony_ci
26e41f4b71Sopenharmony_ciThe UART module provides APIs for operating UART ports, including:
27e41f4b71Sopenharmony_ci
28e41f4b71Sopenharmony_ci- Opening or closing a UART device
29e41f4b71Sopenharmony_ci- Reading or writing data
30e41f4b71Sopenharmony_ci- Setting or obtaining the baud rate of a UART device
31e41f4b71Sopenharmony_ci- Setting or obtaining UART device attributes
32e41f4b71Sopenharmony_ci
33e41f4b71Sopenharmony_ci### Basic Concepts
34e41f4b71Sopenharmony_ci
35e41f4b71Sopenharmony_ci- Asynchronous communication
36e41f4b71Sopenharmony_ci
37e41f4b71Sopenharmony_ci  In asynchronous communication, data is transmitted in frames of characters or bytes. Frames are sent and received one by one through the transmission line. The transmitter and receiver have their own clocks to control data sending and receiving. The two clock sources are independent and not synchronized with each other. 
38e41f4b71Sopenharmony_ci
39e41f4b71Sopenharmony_ci  When data is sent one character at a time, the time interval between two characters is not fixed, but the time interval between two adjacent bits in a character frame is fixed.
40e41f4b71Sopenharmony_ci
41e41f4b71Sopenharmony_ci- Full-duplex transmission
42e41f4b71Sopenharmony_ci
43e41f4b71Sopenharmony_ci  A duplex communication mode allows data to be transmitted in both directions at the same time. A duplex communication channel is equivalent to two simplex communication channels operating in opposite directions at the same time. In full-duplex mode, signals can be transmitted bidirectionally at the same time.
44e41f4b71Sopenharmony_ci
45e41f4b71Sopenharmony_ci### Working Principles
46e41f4b71Sopenharmony_ci
47e41f4b71Sopenharmony_ciIn the Hardware Driver Foundation (HDF), the UART uses the independent service mode (see Figure 3) for API adaptation. In this mode, each device independently publishes a service to process external access requests. When receiving an access request, the HDF DeviceManager extracts parameters from the request to call the internal APIs of the target device. In the independent service mode, the HDF DeviceManager provides service management capabilities. However, you need to configure a node for each device, which increases memory usage.
48e41f4b71Sopenharmony_ci
49e41f4b71Sopenharmony_ciIn the independent service mode, the core layer does not publish a service for the upper layer. Therefore, a service must be published for each controller. To achieve this purpose:
50e41f4b71Sopenharmony_ci
51e41f4b71Sopenharmony_ci- You need to implement the **Bind()** function in **HdfDriverEntry** to bind services.
52e41f4b71Sopenharmony_ci- The **policy** field of **deviceNode** in the **device_info.hcs** file can be **1** or **2**, but not **0**.
53e41f4b71Sopenharmony_ci
54e41f4b71Sopenharmony_ciThe UART module is divided into the following layers:
55e41f4b71Sopenharmony_ci
56e41f4b71Sopenharmony_ci- Interface layer: provides APIs for opening or closing a UART device, reading or writing data of the specified length, setting or obtaining the baud rate or attributes of a UART device, and setting the transmission mode.
57e41f4b71Sopenharmony_ci- Core layer: provides the capabilities of adding or removing a UART controller, and managing UART devices. The core layer interacts with the adaptation layer through hook functions.
58e41f4b71Sopenharmony_ci- Adaptation layer: instantiates the hook functions to implement specific features.
59e41f4b71Sopenharmony_ci
60e41f4b71Sopenharmony_ci**Figure 3** Independent service mode
61e41f4b71Sopenharmony_ci
62e41f4b71Sopenharmony_ci![image3](figures/independent-service-mode.png)
63e41f4b71Sopenharmony_ci
64e41f4b71Sopenharmony_ci## Usage Guidelines
65e41f4b71Sopenharmony_ci
66e41f4b71Sopenharmony_ci### When to Use
67e41f4b71Sopenharmony_ci
68e41f4b71Sopenharmony_ciThe UART module is widely used to implement low-speed serial communication between devices, for example, output the printing information. It can also connect to a variety of external GPS and Bluetooth devices.
69e41f4b71Sopenharmony_ci
70e41f4b71Sopenharmony_ci### Available APIs
71e41f4b71Sopenharmony_ci
72e41f4b71Sopenharmony_ci**Table 1** UART driver APIs
73e41f4b71Sopenharmony_ci
74e41f4b71Sopenharmony_ci| API| Description|
75e41f4b71Sopenharmony_ci| -------- | -------- |
76e41f4b71Sopenharmony_ci| DevHandle UartOpen(uint32_t port) | Opens a UART device.|
77e41f4b71Sopenharmony_ci| void UartClose(DevHandle handle) | Closes a UART device.|
78e41f4b71Sopenharmony_ci| int32_t UartRead(DevHandle handle, uint8_t *data, uint32_t size) | Reads data of the specified length from a UART device.|
79e41f4b71Sopenharmony_ci| int32_t UartWrite(DevHandle handle, uint8_t *data, uint32_t size) | Writes data of the specified length to a UART device.|
80e41f4b71Sopenharmony_ci| int32_t UartGetBaud(DevHandle handle, uint32_t *baudRate) | Obtains the UART baud rate.|
81e41f4b71Sopenharmony_ci| int32_t UartSetBaud(DevHandle handle, uint32_t baudRate) | Sets the UART baud rate.|
82e41f4b71Sopenharmony_ci| int32_t UartGetAttribute(DevHandle handle, struct UartAttribute *attribute) | Obtains UART device attributes.|
83e41f4b71Sopenharmony_ci| int32_t UartSetAttribute(DevHandle handle, struct UartAttribute *attribute) | Sets UART device attributes.|
84e41f4b71Sopenharmony_ci| int32_t UartSetTransMode(DevHandle handle, enum UartTransMode mode) | Sets the UART transmission mode.|
85e41f4b71Sopenharmony_ci
86e41f4b71Sopenharmony_ci> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
87e41f4b71Sopenharmony_ci>
88e41f4b71Sopenharmony_ci> All the UART APIs described in this document can be used in kernel mode and user mode.
89e41f4b71Sopenharmony_ci
90e41f4b71Sopenharmony_ci### How to Develop
91e41f4b71Sopenharmony_ci
92e41f4b71Sopenharmony_ciThe following figure illustrates how to use the UART APIs.
93e41f4b71Sopenharmony_ci
94e41f4b71Sopenharmony_ci**Figure 4** Using UART driver APIs
95e41f4b71Sopenharmony_ci
96e41f4b71Sopenharmony_ci![image4](figures/using-UART-process.png)
97e41f4b71Sopenharmony_ci
98e41f4b71Sopenharmony_ci
99e41f4b71Sopenharmony_ci#### Opening a UART Device
100e41f4b71Sopenharmony_ci
101e41f4b71Sopenharmony_ciBefore performing UART communication, use **UartOpen()** to obtain a UART device handle based on the port number.
102e41f4b71Sopenharmony_ci
103e41f4b71Sopenharmony_ci```c
104e41f4b71Sopenharmony_ciDevHandle UartOpen(uint32_t port);
105e41f4b71Sopenharmony_ci```
106e41f4b71Sopenharmony_ci
107e41f4b71Sopenharmony_ci**Table 2** Description of UartOpen
108e41f4b71Sopenharmony_ci
109e41f4b71Sopenharmony_ci| Parameter| Description|
110e41f4b71Sopenharmony_ci| -------- | -------- |
111e41f4b71Sopenharmony_ci| port | UART port number.|
112e41f4b71Sopenharmony_ci| **Return Value**| **Description**|
113e41f4b71Sopenharmony_ci| NULL | The operation fails.|
114e41f4b71Sopenharmony_ci| Device handle| The operation is successful. The obtained UART device handle is returned.|
115e41f4b71Sopenharmony_ci
116e41f4b71Sopenharmony_ciExample: Obtain the device handle of UART port 1.
117e41f4b71Sopenharmony_ci
118e41f4b71Sopenharmony_ci```c
119e41f4b71Sopenharmony_ciDevHandle handle = NULL;    // UART device handle.
120e41f4b71Sopenharmony_ciuint32_t port = 1;          // UART device port number.
121e41f4b71Sopenharmony_ci
122e41f4b71Sopenharmony_cihandle = UartOpen(port);
123e41f4b71Sopenharmony_ciif (handle == NULL) {
124e41f4b71Sopenharmony_ci    HDF_LOGE("UartOpen: open uart_%u failed!\n", port);
125e41f4b71Sopenharmony_ci    return;
126e41f4b71Sopenharmony_ci}
127e41f4b71Sopenharmony_ci```
128e41f4b71Sopenharmony_ci
129e41f4b71Sopenharmony_ci#### Setting the UART Baud Rate
130e41f4b71Sopenharmony_ci
131e41f4b71Sopenharmony_ciUse **UartSetBaud()** to set the UART baud rate.
132e41f4b71Sopenharmony_ci
133e41f4b71Sopenharmony_ci```c
134e41f4b71Sopenharmony_ciint32_t UartSetBaud(DevHandle handle, uint32_t baudRate);
135e41f4b71Sopenharmony_ci```
136e41f4b71Sopenharmony_ci
137e41f4b71Sopenharmony_ci**Table 3** Description of UartSetBaud
138e41f4b71Sopenharmony_ci
139e41f4b71Sopenharmony_ci| Parameter| Description|
140e41f4b71Sopenharmony_ci| -------- | -------- |
141e41f4b71Sopenharmony_ci| handle | UART device handle.|
142e41f4b71Sopenharmony_ci| baudRate | Baud rate to set.|
143e41f4b71Sopenharmony_ci| **Return Value**| **Description**|
144e41f4b71Sopenharmony_ci| HDF_SUCCESS | The operation is successful.|
145e41f4b71Sopenharmony_ci| Negative value| The operation fails.|
146e41f4b71Sopenharmony_ci
147e41f4b71Sopenharmony_ciExample: Set the UART baud rate to **9600**.
148e41f4b71Sopenharmony_ci
149e41f4b71Sopenharmony_ci```c
150e41f4b71Sopenharmony_ciint32_t ret;
151e41f4b71Sopenharmony_ci
152e41f4b71Sopenharmony_ciret = UartSetBaud(handle, 9600); // Set the UART baud rate.
153e41f4b71Sopenharmony_ciif (ret != HDF_SUCCESS) {
154e41f4b71Sopenharmony_ci    HDF_LOGE("UartSetBaud: failed, ret %d\n", ret);
155e41f4b71Sopenharmony_ci    return ret;
156e41f4b71Sopenharmony_ci}
157e41f4b71Sopenharmony_ci```
158e41f4b71Sopenharmony_ci
159e41f4b71Sopenharmony_ci#### Obtaining the UART Baud Rate
160e41f4b71Sopenharmony_ci
161e41f4b71Sopenharmony_ciUse **UartGetBaud()** to obtain the UART baud rate.
162e41f4b71Sopenharmony_ci
163e41f4b71Sopenharmony_ci```c
164e41f4b71Sopenharmony_ciint32_t UartGetBaud(DevHandle handle, uint32_t *baudRate);
165e41f4b71Sopenharmony_ci```
166e41f4b71Sopenharmony_ci
167e41f4b71Sopenharmony_ci**Table 4** Description of UartGetBaud
168e41f4b71Sopenharmony_ci
169e41f4b71Sopenharmony_ci| Parameter| Description|
170e41f4b71Sopenharmony_ci| -------- | -------- |
171e41f4b71Sopenharmony_ci| handle | UART device handle.|
172e41f4b71Sopenharmony_ci| baudRate | Pointer to the UART baud rate obtained.|
173e41f4b71Sopenharmony_ci| **Return Value**| **Description**|
174e41f4b71Sopenharmony_ci| HDF_SUCCESS | The operation is successful.|
175e41f4b71Sopenharmony_ci| Negative value| The operation fails.|
176e41f4b71Sopenharmony_ci
177e41f4b71Sopenharmony_ciExample: Obtain the UART baud rate.
178e41f4b71Sopenharmony_ci
179e41f4b71Sopenharmony_ci```c
180e41f4b71Sopenharmony_ciint32_t ret;
181e41f4b71Sopenharmony_ciuint32_t baudRate;
182e41f4b71Sopenharmony_ci
183e41f4b71Sopenharmony_ciret = UartGetBaud(handle, &baudRate);    // Obtain the UART baud rate.
184e41f4b71Sopenharmony_ciif (ret != HDF_SUCCESS) {
185e41f4b71Sopenharmony_ci    HDF_LOGE("UartGetBaud: failed, ret %d\n", ret);
186e41f4b71Sopenharmony_ci    return ret;
187e41f4b71Sopenharmony_ci}
188e41f4b71Sopenharmony_ci```
189e41f4b71Sopenharmony_ci
190e41f4b71Sopenharmony_ci#### Setting UART Device Attributes
191e41f4b71Sopenharmony_ci
192e41f4b71Sopenharmony_ciUse **UartSetAttribute()** to set UART device attributes.
193e41f4b71Sopenharmony_ci
194e41f4b71Sopenharmony_ci```c
195e41f4b71Sopenharmony_ciint32_t UartSetAttribute(DevHandle handle, struct UartAttribute *attribute);
196e41f4b71Sopenharmony_ci```
197e41f4b71Sopenharmony_ci
198e41f4b71Sopenharmony_ci**Table 5** Description of UartSetAttribute
199e41f4b71Sopenharmony_ci
200e41f4b71Sopenharmony_ci| Parameter| Description|
201e41f4b71Sopenharmony_ci| -------- | -------- |
202e41f4b71Sopenharmony_ci| handle | UART device handle.|
203e41f4b71Sopenharmony_ci| attribute | Pointer to the UART device attributes to set.|
204e41f4b71Sopenharmony_ci| **Return Value**| **Description**|
205e41f4b71Sopenharmony_ci| HDF_SUCCESS | The operation is successful.|
206e41f4b71Sopenharmony_ci| Negative value| The operation fails.|
207e41f4b71Sopenharmony_ci
208e41f4b71Sopenharmony_ciExample: Set UART device attributes.
209e41f4b71Sopenharmony_ci
210e41f4b71Sopenharmony_ci```c
211e41f4b71Sopenharmony_ciint32_t ret;
212e41f4b71Sopenharmony_cistruct UartAttribute attribute;
213e41f4b71Sopenharmony_ci
214e41f4b71Sopenharmony_ciattribute.dataBits = UART_ATTR_DATABIT_7;     // Transfer 7 bits each time.
215e41f4b71Sopenharmony_ciattribute.parity = UART_ATTR_PARITY_NONE;     // Disable parity check for the data to transfer.
216e41f4b71Sopenharmony_ciattribute.stopBits = UART_ATTR_STOPBIT_1;     // Set the stop bit to 1.
217e41f4b71Sopenharmony_ciattribute.rts = UART_ATTR_RTS_DIS;            // Disable RTS.
218e41f4b71Sopenharmony_ciattribute.cts = UART_ATTR_CTS_DIS;            // Disable CTS.
219e41f4b71Sopenharmony_ciattribute.fifoRxEn = UART_ATTR_RX_FIFO_EN;    // Enable RX FIFO.
220e41f4b71Sopenharmony_ciattribute.fifoTxEn = UART_ATTR_TX_FIFO_EN;    // Enable TX FIFO.
221e41f4b71Sopenharmony_ci
222e41f4b71Sopenharmony_ciret = UartSetAttribute(handle, &attribute);   // Set UART device attributes.
223e41f4b71Sopenharmony_ciif (ret != HDF_SUCCESS) {
224e41f4b71Sopenharmony_ci    HDF_LOGE("UartSetAttribute: failed, ret %d\n", ret);
225e41f4b71Sopenharmony_citurn ret;
226e41f4b71Sopenharmony_ci}
227e41f4b71Sopenharmony_ci```
228e41f4b71Sopenharmony_ci
229e41f4b71Sopenharmony_ci#### Obtaining UART Device Attributes
230e41f4b71Sopenharmony_ci
231e41f4b71Sopenharmony_ciUse **UartGetAttribute()** to obtain the UART device attributes.
232e41f4b71Sopenharmony_ci
233e41f4b71Sopenharmony_ci```c
234e41f4b71Sopenharmony_ciint32_t UartGetAttribute(DevHandle handle, struct UartAttribute *attribute);
235e41f4b71Sopenharmony_ci```
236e41f4b71Sopenharmony_ci
237e41f4b71Sopenharmony_ci**Table 6** Description of UartGetAttribute
238e41f4b71Sopenharmony_ci
239e41f4b71Sopenharmony_ci| Parameter| Description|
240e41f4b71Sopenharmony_ci| -------- | -------- |
241e41f4b71Sopenharmony_ci| handle | UART device handle.|
242e41f4b71Sopenharmony_ci| attribute | Pointer to the UART device attributes obtained.|
243e41f4b71Sopenharmony_ci| **Return Value**| **Description**|
244e41f4b71Sopenharmony_ci| HDF_SUCCESS | The operation is successful.|
245e41f4b71Sopenharmony_ci| Negative value| The operation fails.|
246e41f4b71Sopenharmony_ci
247e41f4b71Sopenharmony_ciExample: Obtain UART device attributes.
248e41f4b71Sopenharmony_ci
249e41f4b71Sopenharmony_ci```c
250e41f4b71Sopenharmony_ciint32_t ret;
251e41f4b71Sopenharmony_cistruct UartAttribute attribute;
252e41f4b71Sopenharmony_ci
253e41f4b71Sopenharmony_ciret = UartGetAttribute(handle, &attribute);    // Obtain the attributes of the UART device.
254e41f4b71Sopenharmony_ciif (ret != HDF_SUCCESS) {
255e41f4b71Sopenharmony_ci    HDF_LOGE("UartGetAttribute: failed, ret %d\n", ret);
256e41f4b71Sopenharmony_ci    return ret;
257e41f4b71Sopenharmony_ci}
258e41f4b71Sopenharmony_ci```
259e41f4b71Sopenharmony_ci
260e41f4b71Sopenharmony_ci#### Setting the UART Transmission Mode
261e41f4b71Sopenharmony_ci
262e41f4b71Sopenharmony_ciUse **UartSetTransMode()** to set the UART transmission mode.
263e41f4b71Sopenharmony_ci
264e41f4b71Sopenharmony_ci```c
265e41f4b71Sopenharmony_ciint32_t UartSetTransMode(DevHandle handle, enum UartTransMode mode);
266e41f4b71Sopenharmony_ci```
267e41f4b71Sopenharmony_ci
268e41f4b71Sopenharmony_ci**Table 7** Description of UartSetTransMode
269e41f4b71Sopenharmony_ci
270e41f4b71Sopenharmony_ci| Parameter| Description|
271e41f4b71Sopenharmony_ci| -------- | -------- |
272e41f4b71Sopenharmony_ci| handle | UART device handle.|
273e41f4b71Sopenharmony_ci| mode | UART transmission mode to set.|
274e41f4b71Sopenharmony_ci| **Return Value**| **Description**|
275e41f4b71Sopenharmony_ci| HDF_SUCCESS | The operation is successful.|
276e41f4b71Sopenharmony_ci| Negative value| The operation fails.|
277e41f4b71Sopenharmony_ci
278e41f4b71Sopenharmony_ciExample: Set the UART transmission mode to **UART_MODE_RD_BLOCK**.
279e41f4b71Sopenharmony_ci
280e41f4b71Sopenharmony_ci```c
281e41f4b71Sopenharmony_ciint32_t ret;
282e41f4b71Sopenharmony_ci
283e41f4b71Sopenharmony_ciret = UartSetTransMode(handle, UART_MODE_RD_BLOCK);    // Sets the UART transmission mode.
284e41f4b71Sopenharmony_ciif (ret != HDF_SUCCESS) {
285e41f4b71Sopenharmony_ci    HDF_LOGE("UartSetTransMode: failed, ret %d\n", ret);
286e41f4b71Sopenharmony_ci    return ret;
287e41f4b71Sopenharmony_ci}
288e41f4b71Sopenharmony_ci```
289e41f4b71Sopenharmony_ci
290e41f4b71Sopenharmony_ci#### Writing Data to a UART Device
291e41f4b71Sopenharmony_ci
292e41f4b71Sopenharmony_ciUse **UartWrite()** to write data of the specified length to a UART device.
293e41f4b71Sopenharmony_ci
294e41f4b71Sopenharmony_ci```c
295e41f4b71Sopenharmony_ciint32_t UartWrite(DevHandle handle, uint8_t *data, uint32_t size);
296e41f4b71Sopenharmony_ci```
297e41f4b71Sopenharmony_ci
298e41f4b71Sopenharmony_ci**Table 8** Description of UartWrite
299e41f4b71Sopenharmony_ci
300e41f4b71Sopenharmony_ci| Parameter| Description|
301e41f4b71Sopenharmony_ci| -------- | -------- |
302e41f4b71Sopenharmony_ci| handle | UART device handle.|
303e41f4b71Sopenharmony_ci| data | Pointer to the data to write.|
304e41f4b71Sopenharmony_ci| size | Length of the data to write.|
305e41f4b71Sopenharmony_ci| **Return Value**| **Description**|
306e41f4b71Sopenharmony_ci| HDF_SUCCESS | The operation is successful.|
307e41f4b71Sopenharmony_ci| Negative value| The operation fails.|
308e41f4b71Sopenharmony_ci
309e41f4b71Sopenharmony_ciExample: Write data to a UART device.
310e41f4b71Sopenharmony_ci
311e41f4b71Sopenharmony_ci```c
312e41f4b71Sopenharmony_ciint32_t ret;
313e41f4b71Sopenharmony_ciuint8_t wbuff[5] = {1, 2, 3, 4, 5};
314e41f4b71Sopenharmony_ci
315e41f4b71Sopenharmony_ciret = UartWrite(handle, wbuff, 5);    // Write data of the specified length to the UART device.
316e41f4b71Sopenharmony_ciif (ret != HDF_SUCCESS) {
317e41f4b71Sopenharmony_ci    HDF_LOGE("UartWrite: failed, ret %d\n", ret);
318e41f4b71Sopenharmony_ci    return ret;
319e41f4b71Sopenharmony_ci}
320e41f4b71Sopenharmony_ci```
321e41f4b71Sopenharmony_ci
322e41f4b71Sopenharmony_ci#### Reading Data from a UART Device
323e41f4b71Sopenharmony_ci
324e41f4b71Sopenharmony_ciUse **UartRead()** to read data of the specified length from a UART device.
325e41f4b71Sopenharmony_ci
326e41f4b71Sopenharmony_ci```c
327e41f4b71Sopenharmony_ciint32_t UartRead(DevHandle handle, uint8_t *data, uint32_t size);
328e41f4b71Sopenharmony_ci```
329e41f4b71Sopenharmony_ci
330e41f4b71Sopenharmony_ci**Table 9** Description of UartRead
331e41f4b71Sopenharmony_ci
332e41f4b71Sopenharmony_ci| Parameter| Description|
333e41f4b71Sopenharmony_ci| -------- | -------- |
334e41f4b71Sopenharmony_ci| handle | UART device handle.|
335e41f4b71Sopenharmony_ci| data | Pointer to the buffer for receiving the data.|
336e41f4b71Sopenharmony_ci| size | Length of the data to read.|
337e41f4b71Sopenharmony_ci| **Return Value**| **Description**|
338e41f4b71Sopenharmony_ci| Non-negative value| The operation is successful. The length of the data read is returned.|
339e41f4b71Sopenharmony_ci| Negative value| The operation fails.|
340e41f4b71Sopenharmony_ci
341e41f4b71Sopenharmony_ciExample: Read data of the specified length from a UART device.
342e41f4b71Sopenharmony_ci
343e41f4b71Sopenharmony_ci```c
344e41f4b71Sopenharmony_ciint32_t ret;
345e41f4b71Sopenharmony_ciuint8_t rbuff[5] = {0};
346e41f4b71Sopenharmony_ci
347e41f4b71Sopenharmony_ciret = UartRead(handle, rbuff, 5);    // Read data of the specified length from the UART device.
348e41f4b71Sopenharmony_ciif (ret < 0) {
349e41f4b71Sopenharmony_ci    HDF_LOGE("UartRead: failed, ret %d\n", ret);
350e41f4b71Sopenharmony_ci	return ret;
351e41f4b71Sopenharmony_ci}
352e41f4b71Sopenharmony_ci```
353e41f4b71Sopenharmony_ci
354e41f4b71Sopenharmony_ci> ![icon-caution.gif](../public_sys-resources/icon-caution.gif) **CAUTION**<br/>
355e41f4b71Sopenharmony_ci> Data is successfully read from the UART device if a non-negative value is returned. If **0** is returned, no valid data can be read from the UART device. A value greater than **0** indicates the length of the data read from the UART device. The data length must be less than or equal to the value of **size** and cannot exceed the maximum length of the data to read at a time specified by the UART controller in use.
356e41f4b71Sopenharmony_ci
357e41f4b71Sopenharmony_ci
358e41f4b71Sopenharmony_ci#### Closing a UART Device
359e41f4b71Sopenharmony_ci
360e41f4b71Sopenharmony_ciUse **UartClose()** to close a UART device.
361e41f4b71Sopenharmony_ci
362e41f4b71Sopenharmony_ci```c
363e41f4b71Sopenharmony_civoid UartClose(DevHandle handle);
364e41f4b71Sopenharmony_ci```
365e41f4b71Sopenharmony_ci
366e41f4b71Sopenharmony_ciThis function releases the resources requested by **UartOpen**.
367e41f4b71Sopenharmony_ci
368e41f4b71Sopenharmony_ci**Table 10** Description of UartClose
369e41f4b71Sopenharmony_ci
370e41f4b71Sopenharmony_ci| Parameter| Description|
371e41f4b71Sopenharmony_ci| -------- | -------- |
372e41f4b71Sopenharmony_ci| handle | UART device handle to close.|
373e41f4b71Sopenharmony_ci
374e41f4b71Sopenharmony_ciExample: Close a UART device.
375e41f4b71Sopenharmony_ci
376e41f4b71Sopenharmony_ci```c
377e41f4b71Sopenharmony_ciUartClose(handle);    // Close a UART device to release resources.
378e41f4b71Sopenharmony_ci```
379e41f4b71Sopenharmony_ci
380e41f4b71Sopenharmony_ci## Example
381e41f4b71Sopenharmony_ci
382e41f4b71Sopenharmony_ciThe following uses the Hi3516D V300 development board as an example to describe how to manage the UART device. The procedure is as follows: 
383e41f4b71Sopenharmony_ci
384e41f4b71Sopenharmony_ci1. Open a UART device based on the port number. The handle of the UART device opened is returned.
385e41f4b71Sopenharmony_ci2. Set the baud rate of the UART device.
386e41f4b71Sopenharmony_ci3. Obtain the baud rate of the UART device.
387e41f4b71Sopenharmony_ci4. Set the attributes of the UART device.
388e41f4b71Sopenharmony_ci5. Obtain the attributes of the UART device.
389e41f4b71Sopenharmony_ci6. Set the transmission mode of the UART device.
390e41f4b71Sopenharmony_ci7. Transfer data of the specified length.
391e41f4b71Sopenharmony_ci8. Receive data of the specified length.
392e41f4b71Sopenharmony_ci9. Closes the UART device.
393e41f4b71Sopenharmony_ci
394e41f4b71Sopenharmony_ci```c
395e41f4b71Sopenharmony_ci#include "hdf_log.h"
396e41f4b71Sopenharmony_ci#include "uart_if.h"
397e41f4b71Sopenharmony_ci
398e41f4b71Sopenharmony_civoid UartTestSample(void)
399e41f4b71Sopenharmony_ci{
400e41f4b71Sopenharmony_ci    int32_t ret;
401e41f4b71Sopenharmony_ci    uint32_t port;
402e41f4b71Sopenharmony_ci    uint32_t baud;
403e41f4b71Sopenharmony_ci    DevHandle handle = NULL;
404e41f4b71Sopenharmony_ci    uint8_t wbuff[5] = { 1, 2, 3, 4, 5 };
405e41f4b71Sopenharmony_ci    uint8_t rbuff[5] = { 0 };
406e41f4b71Sopenharmony_ci    struct UartAttribute attribute;
407e41f4b71Sopenharmony_ci
408e41f4b71Sopenharmony_ci    attribute.dataBits = UART_ATTR_DATABIT_7;                  // Transfer 7 bits each time.
409e41f4b71Sopenharmony_ci    attribute.parity = UART_ATTR_PARITY_NONE;                  // Disable parity check.
410e41f4b71Sopenharmony_ci    attribute.stopBits = UART_ATTR_STOPBIT_1;                  // Set the stop bit to 1.
411e41f4b71Sopenharmony_ci    attribute.rts = UART_ATTR_RTS_DIS;                         // Disable RTS.
412e41f4b71Sopenharmony_ci    attribute.cts = UART_ATTR_CTS_DIS;                         // Disable CTS.
413e41f4b71Sopenharmony_ci    attribute.fifoRxEn = UART_ATTR_RX_FIFO_EN;                 // Enable RX FIFO.
414e41f4b71Sopenharmony_ci    attribute.fifoTxEn = UART_ATTR_TX_FIFO_EN;                 // Enable TX FIFO.
415e41f4b71Sopenharmony_ci
416e41f4b71Sopenharmony_ci    port = 1;                                                  // UART device port number.
417e41f4b71Sopenharmony_ci
418e41f4b71Sopenharmony_ci    handle = UartOpen(port);                                   // Open a UART device.
419e41f4b71Sopenharmony_ci    if (handle == NULL) {
420e41f4b71Sopenharmony_ci        HDF_LOGE("UartOpen: open uart_%u failed!\n", port);
421e41f4b71Sopenharmony_ci        return;
422e41f4b71Sopenharmony_ci    }
423e41f4b71Sopenharmony_ci
424e41f4b71Sopenharmony_ci    ret = UartSetBaud(handle, 9600);                           // Set the UART baud rate to 9600.
425e41f4b71Sopenharmony_ci    if (ret != HDF_SUCCESS) {
426e41f4b71Sopenharmony_ci        HDF_LOGE("UartSetBaud: set baud failed, ret %d\n", ret);
427e41f4b71Sopenharmony_ci        goto ERR;
428e41f4b71Sopenharmony_ci    }
429e41f4b71Sopenharmony_ci
430e41f4b71Sopenharmony_ci    ret = UartGetBaud(handle, &baud);                          // Obtain the UART baud rate.
431e41f4b71Sopenharmony_ci    if (ret != HDF_SUCCESS) {
432e41f4b71Sopenharmony_ci        HDF_LOGE("UartGetBaud: get baud failed, ret %d\n", ret);
433e41f4b71Sopenharmony_ci        goto ERR;
434e41f4b71Sopenharmony_ci    }
435e41f4b71Sopenharmony_ci
436e41f4b71Sopenharmony_ci    ret = UartSetAttribute(handle, &attribute);                // Set the attributes of the UART device.
437e41f4b71Sopenharmony_ci    if (ret != HDF_SUCCESS) {
438e41f4b71Sopenharmony_ci        HDF_LOGE("UartSetAttribute: set attribute failed, ret %d\n", ret);
439e41f4b71Sopenharmony_ci        goto ERR;
440e41f4b71Sopenharmony_ci    }
441e41f4b71Sopenharmony_ci
442e41f4b71Sopenharmony_ci    ret = UartGetAttribute(handle, &attribute);                // Obtain the attributes of the UART device.
443e41f4b71Sopenharmony_ci    if (ret != HDF_SUCCESS) {
444e41f4b71Sopenharmony_ci        HDF_LOGE("UartGetAttribute: get attribute failed, ret %d\n", ret);
445e41f4b71Sopenharmony_ci        goto ERR;
446e41f4b71Sopenharmony_ci    }
447e41f4b71Sopenharmony_ci
448e41f4b71Sopenharmony_ci    ret = UartSetTransMode(handle, UART_MODE_RD_NONBLOCK);     // Set the UART transmission mode to non-block mode.
449e41f4b71Sopenharmony_ci    if (ret != HDF_SUCCESS) {
450e41f4b71Sopenharmony_ci        HDF_LOGE("UartSetTransMode: set trans mode failed, ret %d\n", ret);
451e41f4b71Sopenharmony_ci        goto ERR;
452e41f4b71Sopenharmony_ci    }
453e41f4b71Sopenharmony_ci
454e41f4b71Sopenharmony_ci    ret = UartWrite(handle, wbuff, 5);                         // Write 5-byte data to the UART device.
455e41f4b71Sopenharmony_ci    if (ret != HDF_SUCCESS) {
456e41f4b71Sopenharmony_ci        HDF_LOGE("UartWrite: write data failed, ret %d\n", ret);
457e41f4b71Sopenharmony_ci        goto ERR;
458e41f4b71Sopenharmony_ci    }
459e41f4b71Sopenharmony_ci
460e41f4b71Sopenharmony_ci    ret = UartRead(handle, rbuff, 5);                          // Read 5-byte data from the UART device.
461e41f4b71Sopenharmony_ci    if (ret < 0) {
462e41f4b71Sopenharmony_ci        HDF_LOGE("UartRead: read data failed, ret %d\n", ret);
463e41f4b71Sopenharmony_ci        goto ERR;
464e41f4b71Sopenharmony_ci    }
465e41f4b71Sopenharmony_ciERR:
466e41f4b71Sopenharmony_ci    UartClose(handle);                                         // Close the UART device.
467e41f4b71Sopenharmony_ci	return ret;
468e41f4b71Sopenharmony_ci}
469e41f4b71Sopenharmony_ci```
470