• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# usb_serial_api.h
2<!--Kit: Driver Development Kit-->
3<!--Subsystem: Driver-->
4<!--Owner: @lixinsheng2-->
5<!--Designer: @w00373942-->
6<!--Tester: @dong-dongzhen-->
7<!--Adviser: @w_Machine_cc-->
8
9## Overview
10
11Declares the USB Serial DDK APIs used by the host to access the serial port device.
12
13**File to include**: <usb_serial/usb_serial_api.h>
14
15**Library**: libusb_serial.z.so
16
17**System capability**: SystemCapability.Driver.UsbSerial.Extension
18
19**Since**: 18
20
21**Related module**: [SerialDdk](capi-serialddk.md)
22
23## Summary
24
25### Functions
26
27| Name| Description|
28| -- | -- |
29| [int32_t OH_UsbSerial_Init(void)](#oh_usbserial_init) | Initializes the USB Serial DDK.|
30| [int32_t OH_UsbSerial_Release(void)](#oh_usbserial_release) | Releases the USB Serial DDK.|
31| [int32_t OH_UsbSerial_Open(uint64_t deviceId, uint8_t interfaceIndex, UsbSerial_DeviceHandle **dev)](#oh_usbserial_open) | Enables the USB serial port device based on the specified **deviceId** and **interfaceIndex**.|
32| [int32_t OH_UsbSerial_Close(UsbSerial_DeviceHandle *dev)](#oh_usbserial_close) | Disables the USB serial port device.|
33| [int32_t OH_UsbSerial_Read(UsbSerial_DeviceHandle *dev, uint8_t *buff, uint32_t bufferSize, uint32_t *bytesRead)](#oh_usbserial_read) | Reads data from the USB serial port device to the buffer.|
34| [int32_t OH_UsbSerial_Write(UsbSerial_DeviceHandle *dev, uint8_t *buff, uint32_t bufferSize, uint32_t *bytesWritten)](#oh_usbserial_write) | Writes the data in the buffer to the USB serial port device.|
35| [int32_t OH_UsbSerial_SetBaudRate(UsbSerial_DeviceHandle *dev, uint32_t baudRate)](#oh_usbserial_setbaudrate) | Sets the baud rate for a USB serial port device. If the parameters of the USB serial port device are set to the default values (the data bit is **8**, the stop bit is **1**, and parity is disabled for data transfer), you only need to call this API to set the baud rate.|
36| [int32_t OH_UsbSerial_SetParams(UsbSerial_DeviceHandle *dev, UsbSerial_Params *params)](#oh_usbserial_setparams) | Sets the parameters of the USB serial port device. If the parameters of the USB serial port device are not set to the default values (the data bit is **8**, the stop bit is **1**, and parity is disabled for data transfer), you only need to call this API to set the related parameters.|
37| [int32_t OH_UsbSerial_SetTimeout(UsbSerial_DeviceHandle *dev, int timeout)](#oh_usbserial_settimeout) | Sets the timeout interval (ms) for reading data reported by a USB serial port device. If this function is not called, the timeout value is **0** by default, indicating that data is returned immediately regardless of whether data is read. If you need to wait for a certain period of time or data must be read, call this API to set the timeout interval.|
38| [int32_t OH_UsbSerial_SetFlowControl(UsbSerial_DeviceHandle *dev, UsbSerial_FlowControl flowControl)](#oh_usbserial_setflowcontrol) | Sets flow control parameters. Flow control is used to manage the data transfer rate during communication with the USB serial port device to ensure that the sender does not send data that exceeds the processing capability of the receiver.<br> If flow control is required, call this API to set flow control parameters. If this API is not called, flow control is not performed by default.|
39| [int32_t OH_UsbSerial_Flush(UsbSerial_DeviceHandle *dev)](#oh_usbserial_flush) | Clears the input and output buffers after the write operation is complete. If a large amount of data is to be transmitted to the USB serial port device, the data may be buffered in the kernel for transmission. If the application closes the file descriptor or exits before the data is completely sent out, some data may be lost.<br> If the data is not sent out, some data may be lost. You can call this API to ensure that all data is sent before subsequent operations are performed.|
40| [int32_t OH_UsbSerial_FlushInput(UsbSerial_DeviceHandle *dev)](#oh_usbserial_flushinput) | Refreshes the input buffer. The data in the buffer is cleared immediately. During the communication with the USB serial port device, especially in the debugging phase, disordered data packets or other exceptions may occur.<br> You can call this API to clear these exceptions to restore the communication.|
41| [int32_t OH_UsbSerial_FlushOutput(UsbSerial_DeviceHandle *dev)](#oh_usbserial_flushoutput) | Refreshes the output buffer. The data in the buffer is cleared immediately. During the communication with the USB serial port device, especially in the debugging phase, disordered data packets or other exceptions may occur.<br> You can call this API to clear these exceptions to restore the communication.|
42
43## Function Description
44
45### OH_UsbSerial_Init()
46
47```
48int32_t OH_UsbSerial_Init(void)
49```
50
51**Description**
52
53Initializes the USB Serial DDK.
54
55**Required permissions**: ohos.permission.ACCESS_DDK_USB_SERIAL
56
57**Since**: 18
58
59**Returns**
60
61| Type| Description                                                                                                                                                                                                                               |
62| -- |-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
63| int32_t | [USB_SERIAL_DDK_SUCCESS](capi-usb-serial-types-h.md#usbserial_ddkretcode): The operation is successful.<br>         [USB_SERIAL_DDK_NO_PERM](capi-usb-serial-types-h.md#usbserial_ddkretcode): The permission verification fails.<br>         [USB_SERIAL_DDK_INIT_ERROR](capi-usb-serial-types-h.md#usbserial_ddkretcode): The DDK initialization fails.|
64
65### OH_UsbSerial_Release()
66
67```
68int32_t OH_UsbSerial_Release(void)
69```
70
71**Description**
72
73Releases the USB Serial DDK.
74
75**Required permissions**: ohos.permission.ACCESS_DDK_USB_SERIAL
76
77**Since**: 18
78
79**Returns**
80
81| Type| Description|
82| -- | -- |
83| int32_t | [USB_SERIAL_DDK_SUCCESS](capi-usb-serial-types-h.md#usbserial_ddkretcode): The operation is successful.<br>         [USB_SERIAL_DDK_NO_PERM](capi-usb-serial-types-h.md#usbserial_ddkretcode): The permission verification fails.<br>         [USB_SERIAL_DDK_INIT_ERROR](capi-usb-serial-types-h.md#usbserial_ddkretcode): The DDK initialization fails.<br>         [USB_SERIAL_DDK_SERVICE_ERROR](capi-usb-serial-types-h.md#usbserial_ddkretcode): The DDK service communication fails.|
84
85### OH_UsbSerial_Open()
86
87```
88int32_t OH_UsbSerial_Open(uint64_t deviceId, uint8_t interfaceIndex, UsbSerial_DeviceHandle **dev)
89```
90
91**Description**
92
93Enables the USB serial port device based on the specified **deviceId** and **interfaceIndex**.
94
95**Required permissions**: ohos.permission.ACCESS_DDK_USB_SERIAL
96
97**Since**: 18
98
99
100**Parameters**
101
102| Name                             | Description                                        |
103|----------------------------------|--------------------------------------------|
104| uint64_t deviceId                | Device ID.                            |
105| uint8_t interfaceIndex           | Interface index, which corresponds to [bInterfaceNumber](capi-usbddk-usbinterfacedescriptor.md) in the USB protocol.|
106| [UsbSerial_DeviceHandle](capi-serialddk-usbserial-devicehandle.md) **dev | Device handle.                                     |
107
108**Returns**
109
110| Type| Description                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        |
111| -- |--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
112| int32_t | [USB_SERIAL_DDK_SUCCESS](capi-usb-serial-types-h.md#usbserial_ddkretcode): The operation is successful.<br>         [USB_SERIAL_DDK_NO_PERM](capi-usb-serial-types-h.md#usbserial_ddkretcode): The permission verification fails.<br>         [USB_SERIAL_DDK_INVALID_PARAMETER](capi-usb-serial-types-h.md#usbserial_ddkretcode): The parameter verification fails. Possible cause: The input **dev** is a null pointer.<br>         [USB_SERIAL_DDK_INIT_ERROR](capi-usb-serial-types-h.md#usbserial_ddkretcode): The DDK initialization fails.<br>         [USB_SERIAL_DDK_SERVICE_ERROR](capi-usb-serial-types-h.md#usbserial_ddkretcode): The DDK service communication fails.<br>         [USB_SERIAL_DDK_MEMORY_ERROR](capi-usb-serial-types-h.md#usbserial_ddkretcode): The memory is insufficient.<br>         [USB_SERIAL_DDK_IO_ERROR](capi-usb-serial-types-h.md#usbserial_ddkretcode): An I/O exception occurs.<br>         [USB_SERIAL_DDK_DEVICE_NOT_FOUND](capi-usb-serial-types-h.md#usbserial_ddkretcode): The device or interface is not found.|
113
114### OH_UsbSerial_Close()
115
116```
117int32_t OH_UsbSerial_Close(UsbSerial_DeviceHandle *dev)
118```
119
120**Description**
121
122Disables the USB serial port device.
123
124**Required permissions**: ohos.permission.ACCESS_DDK_USB_SERIAL
125
126**Since**: 18
127
128
129**Parameters**
130
131| Name| Description|
132| -- | -- |
133| [UsbSerial_DeviceHandle](capi-serialddk-usbserial-devicehandle.md) *dev | Device handle.|
134
135**Returns**
136
137| Type| Description                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    |
138| -- |----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
139| int32_t | [USB_SERIAL_DDK_SUCCESS](capi-usb-serial-types-h.md#usbserial_ddkretcode): The operation is successful.<br>         [USB_SERIAL_DDK_NO_PERM](capi-usb-serial-types-h.md#usbserial_ddkretcode): The permission verification fails.<br>         [USB_SERIAL_DDK_INVALID_PARAMETER](capi-usb-serial-types-h.md#usbserial_ddkretcode): The parameter verification fails. Possible cause: The input **dev** is a null pointer.<br>         [USB_SERIAL_DDK_INIT_ERROR](capi-usb-serial-types-h.md#usbserial_ddkretcode): The DDK initialization fails.<br>         [USB_SERIAL_DDK_SERVICE_ERROR](capi-usb-serial-types-h.md#usbserial_ddkretcode): The DDK service communication fails.<br>         [USB_SERIAL_DDK_IO_ERROR](capi-usb-serial-types-h.md#usbserial_ddkretcode): An I/O exception occurs.<br>         [USB_SERIAL_DDK_INVALID_OPERATION](capi-usb-serial-types-h.md#usbserial_ddkretcode): The operation is invalid.|
140
141### OH_UsbSerial_Read()
142
143```
144int32_t OH_UsbSerial_Read(UsbSerial_DeviceHandle *dev, uint8_t *buff, uint32_t bufferSize, uint32_t *bytesRead)
145```
146
147**Description**
148
149Reads data from the USB serial port device to the buffer.
150
151**Required permissions**: ohos.permission.ACCESS_DDK_USB_SERIAL
152
153**Since**: 18
154
155
156**Parameters**
157
158| Name| Description|
159| -- | -- |
160| [UsbSerial_DeviceHandle](capi-serialddk-usbserial-devicehandle.md) *dev | Device handle.|
161| uint8_t *buff | Buffer for storing the data read from the USB serial port device.|
162| uint32_t bufferSize | Buffer size.|
163| uint32_t *bytesRead | Number of bytes that are actually read. If the block mode is set, the number of bytes that are actually read is returned only when it is equal to the value of **bufferSize**.<br>                  For details, see [OH_UsbSerial_SetTimeout](capi-usb-serial-api-h.md#oh_usbserial_settimeout).|
164
165**Returns**
166
167| Type| Description|
168| -- | -- |
169| int32_t | [USB_SERIAL_DDK_SUCCESS](capi-usb-serial-types-h.md#usbserial_ddkretcode): The operation is successful.<br>         [USB_SERIAL_DDK_NO_PERM](capi-usb-serial-types-h.md#usbserial_ddkretcode): The permission verification fails.<br>         [USB_SERIAL_DDK_INVALID_PARAMETER](capi-usb-serial-types-h.md#usbserial_ddkretcode): The parameter verification fails. Possible causes: 1. **dev** is a null pointer.<br>         2. **buff** is a null pointer. 3. **bufferSize** is **0**. 4. **bytesRead** is a null pointer.<br>         [USB_SERIAL_DDK_INIT_ERROR](capi-usb-serial-types-h.md#usbserial_ddkretcode): The DDK initialization fails.<br>         [USB_SERIAL_DDK_SERVICE_ERROR](capi-usb-serial-types-h.md#usbserial_ddkretcode): The DDK service communication fails.<br>         [USB_SERIAL_DDK_MEMORY_ERROR](capi-usb-serial-types-h.md#usbserial_ddkretcode): The buffer address is invalid.<br>         [USB_SERIAL_DDK_IO_ERROR](capi-usb-serial-types-h.md#usbserial_ddkretcode): An I/O exception occurs.<br>         [USB_SERIAL_DDK_INVALID_OPERATION](capi-usb-serial-types-h.md#usbserial_ddkretcode): The operation is invalid.|
170
171### OH_UsbSerial_Write()
172
173```
174int32_t OH_UsbSerial_Write(UsbSerial_DeviceHandle *dev, uint8_t *buff, uint32_t bufferSize, uint32_t *bytesWritten)
175```
176
177**Description**
178
179Writes the data in the buffer to the USB serial port device.
180
181**Required permissions**: ohos.permission.ACCESS_DDK_USB_SERIAL
182
183**Since**: 18
184
185
186**Parameters**
187
188| Name| Description|
189| -- | -- |
190| [UsbSerial_DeviceHandle](capi-serialddk-usbserial-devicehandle.md) *dev | Device handle.|
191| uint8_t *buff | Buffer to which the data of the USB serial port device is written.|
192| uint32_t bufferSize | Buffer size.|
193| uint32_t *bytesWritten | Number of bytes that are actually written.|
194
195**Returns**
196
197| Type| Description|
198| -- | -- |
199| int32_t | [USB_SERIAL_DDK_SUCCESS](capi-usb-serial-types-h.md#usbserial_ddkretcode): The operation is successful.<br>         [USB_SERIAL_DDK_NO_PERM](capi-usb-serial-types-h.md#usbserial_ddkretcode): The permission verification fails.<br>         [USB_SERIAL_DDK_INVALID_PARAMETER](capi-usb-serial-types-h.md#usbserial_ddkretcode): The parameter verification fails. Possible causes: 1. **dev** is a null pointer.<br>         2. **buff** is a null pointer. 3. **bufferSize** is **0**. 4. **bytesWritten** is a null pointer.<br>         [USB_SERIAL_DDK_INIT_ERROR](capi-usb-serial-types-h.md#usbserial_ddkretcode): The DDK initialization fails.<br>         [USB_SERIAL_DDK_SERVICE_ERROR](capi-usb-serial-types-h.md#usbserial_ddkretcode): The DDK service communication fails.<br>         [USB_SERIAL_DDK_MEMORY_ERROR](capi-usb-serial-types-h.md#usbserial_ddkretcode): The buffer address is invalid.<br>         [USB_SERIAL_DDK_IO_ERROR](capi-usb-serial-types-h.md#usbserial_ddkretcode): An I/O exception occurs.<br>         [USB_SERIAL_DDK_INVALID_OPERATION](capi-usb-serial-types-h.md#usbserial_ddkretcode): The operation is invalid.|
200
201### OH_UsbSerial_SetBaudRate()
202
203```
204int32_t OH_UsbSerial_SetBaudRate(UsbSerial_DeviceHandle *dev, uint32_t baudRate)
205```
206
207**Description**
208
209Sets the baud rate for a USB serial port device. If the parameters of the USB serial port device are set to the default values (the data bit is **8**, the stop bit is **1**, and parity is disabled for data transfer), you only need to call this API to set the baud rate.
210
211**Required permissions**: ohos.permission.ACCESS_DDK_USB_SERIAL
212
213**Since**: 18
214
215
216**Parameters**
217
218| Name| Description|
219| -- | -- |
220| [UsbSerial_DeviceHandle](capi-serialddk-usbserial-devicehandle.md) *dev | Device handle.|
221| uint32_t baudRate | Baud rate of the USB serial port device.|
222
223**Returns**
224
225| Type| Description|
226| -- | -- |
227| int32_t | [USB_SERIAL_DDK_SUCCESS](capi-usb-serial-types-h.md#usbserial_ddkretcode): The operation is successful.<br>         [USB_SERIAL_DDK_NO_PERM](capi-usb-serial-types-h.md#usbserial_ddkretcode): The permission verification fails.<br>         [USB_SERIAL_DDK_INVALID_PARAMETER](capi-usb-serial-types-h.md#usbserial_ddkretcode): The parameter verification fails. Possible cause: The input **dev** is a null pointer.<br>         [USB_SERIAL_DDK_INIT_ERROR](capi-usb-serial-types-h.md#usbserial_ddkretcode): The DDK initialization fails.<br>         [USB_SERIAL_DDK_SERVICE_ERROR](capi-usb-serial-types-h.md#usbserial_ddkretcode): The DDK service communication fails.<br>         [USB_SERIAL_DDK_IO_ERROR](capi-usb-serial-types-h.md#usbserial_ddkretcode): An I/O exception occurs.<br>         [USB_SERIAL_DDK_INVALID_OPERATION](capi-usb-serial-types-h.md#usbserial_ddkretcode): The operation is invalid.|
228
229### OH_UsbSerial_SetParams()
230
231```
232int32_t OH_UsbSerial_SetParams(UsbSerial_DeviceHandle *dev, UsbSerial_Params *params)
233```
234
235**Description**
236
237Sets the parameters of the USB serial port device. If the parameters of the USB serial port device are not set to the default values (the data bit is **8**, the stop bit is **1**, and parity is disabled for data transfer), you only need to call this API to set the related parameters.
238
239**Required permissions**: ohos.permission.ACCESS_DDK_USB_SERIAL
240
241**Since**: 18
242
243
244**Parameters**
245
246| Name                                                                    | Description|
247|-------------------------------------------------------------------------| -- |
248| [UsbSerial_DeviceHandle](capi-serialddk-usbserial-devicehandle.md) *dev | Device handle.|
249| [UsbSerial_Params](capi-serialddk-usbserial-params.md) *params                                            | USB serial port device parameters. For details, see [UsbSerial_Params](capi-serialddk-usbserial-params.md).|
250
251**Returns**
252
253| Type| Description|
254| -- | -- |
255| int32_t | [USB_SERIAL_DDK_SUCCESS](capi-usb-serial-types-h.md#usbserial_ddkretcode): The operation is successful.<br>         [USB_SERIAL_DDK_NO_PERM](capi-usb-serial-types-h.md#usbserial_ddkretcode): The permission verification fails.<br>         [USB_SERIAL_DDK_INVALID_PARAMETER](capi-usb-serial-types-h.md#usbserial_ddkretcode): The parameter verification fails. Possible causes: 1. **dev** is a null pointer.<br>         2. **params** is a null pointer.<br>         [USB_SERIAL_DDK_INIT_ERROR](capi-usb-serial-types-h.md#usbserial_ddkretcode): The DDK initialization fails.<br>         [USB_SERIAL_DDK_SERVICE_ERROR](capi-usb-serial-types-h.md#usbserial_ddkretcode): The DDK service communication fails.<br>         [USB_SERIAL_DDK_IO_ERROR](capi-usb-serial-types-h.md#usbserial_ddkretcode): An I/O exception occurs.<br>         [USB_SERIAL_DDK_INVALID_OPERATION](capi-usb-serial-types-h.md#usbserial_ddkretcode): The operation is invalid.|
256
257### OH_UsbSerial_SetTimeout()
258
259```
260int32_t OH_UsbSerial_SetTimeout(UsbSerial_DeviceHandle *dev, int timeout)
261```
262
263**Description**
264
265Sets the timeout interval (ms) for reading data reported by a USB serial port device. If this function is not called, the timeout value is **0** by default, indicating that data is returned immediately regardless of whether data is read. If you need to wait for a certain period of time or data must be read, call this API to set the timeout interval.
266
267**Required permissions**: ohos.permission.ACCESS_DDK_USB_SERIAL
268
269**Since**: 18
270
271
272**Parameters**
273
274| Name| Description|
275| -- | -- |
276| [UsbSerial_DeviceHandle](capi-serialddk-usbserial-devicehandle.md) *dev | Device handle.|
277| int timeout | Timeout interval for reading data from a USB serial port device, in milliseconds. The value range is - (0, 25500]. The value is rounded off to the nearest 100 milliseconds as the actual timeout interval. For example, if the value is set to **12321**, the effective timeout interval is **12300**. - **0**: Data is returned immediately. - **-1**: Data is read in block mode. That is, data is returned only after data of the specified length is read. For details, see [OH_UsbSerial_Read](capi-usb-serial-api-h.md#oh_usbserial_read).|
278
279**Returns**
280
281| Type| Description|
282| -- | -- |
283| int32_t | [USB_SERIAL_DDK_SUCCESS](capi-usb-serial-types-h.md#usbserial_ddkretcode): The operation is successful.<br>         [USB_SERIAL_DDK_NO_PERM](capi-usb-serial-types-h.md#usbserial_ddkretcode): The permission verification fails.<br>         [USB_SERIAL_DDK_INVALID_PARAMETER](capi-usb-serial-types-h.md#usbserial_ddkretcode): The parameter verification fails. Possible causes: 1. **dev** is a null pointer.<br>         2.timeout < -1 or timeout > 25500.<br>         [USB_SERIAL_DDK_INIT_ERROR](capi-usb-serial-types-h.md#usbserial_ddkretcode): The DDK initialization fails.<br>         [USB_SERIAL_DDK_SERVICE_ERROR](capi-usb-serial-types-h.md#usbserial_ddkretcode): The DDK service communication fails.<br>         [USB_SERIAL_DDK_IO_ERROR](capi-usb-serial-types-h.md#usbserial_ddkretcode): An I/O exception occurs.<br>         [USB_SERIAL_DDK_INVALID_OPERATION](capi-usb-serial-types-h.md#usbserial_ddkretcode): The operation is invalid.|
284
285### OH_UsbSerial_SetFlowControl()
286
287```
288int32_t OH_UsbSerial_SetFlowControl(UsbSerial_DeviceHandle *dev, UsbSerial_FlowControl flowControl)
289```
290
291**Description**
292
293Sets flow control parameters. Flow control is used to manage the data transfer rate during communication with the USB serial port device to ensure that the sender does not send data that exceeds the processing capability of the receiver.<br> If flow control is required, call this API to set flow control parameters. If this API is not called, flow control is not performed by default.
294
295**Required permissions**: ohos.permission.ACCESS_DDK_USB_SERIAL
296
297**Since**: 18
298
299
300**Parameters**
301
302| Name                                                                                  | Description|
303|---------------------------------------------------------------------------------------| -- |
304| [UsbSerial_DeviceHandle](capi-serialddk-usbserial-devicehandle.md) *dev               | Device handle.|
305| [UsbSerial_FlowControl](capi-usb-serial-types-h.md#usbserial_flowcontrol) flowControl | Flow control mode. For details, see [UsbSerial_FlowControl](capi-usb-serial-types-h.md#usbserial_flowcontrol).|
306
307**Returns**
308
309| Type| Description|
310| -- | -- |
311| int32_t | [USB_SERIAL_DDK_SUCCESS](capi-usb-serial-types-h.md#usbserial_ddkretcode): The operation is successful.<br>         [USB_SERIAL_DDK_NO_PERM](capi-usb-serial-types-h.md#usbserial_ddkretcode): The permission verification fails.<br>         [USB_SERIAL_DDK_INVALID_PARAMETER](capi-usb-serial-types-h.md#usbserial_ddkretcode): The parameter verification fails. Possible cause: The input **dev** is a null pointer.<br>         [USB_SERIAL_DDK_INIT_ERROR](capi-usb-serial-types-h.md#usbserial_ddkretcode): The DDK initialization fails.<br>         [USB_SERIAL_DDK_SERVICE_ERROR](capi-usb-serial-types-h.md#usbserial_ddkretcode): The DDK service communication fails.<br>         [USB_SERIAL_DDK_IO_ERROR](capi-usb-serial-types-h.md#usbserial_ddkretcode): An I/O exception occurs.<br>         [USB_SERIAL_DDK_INVALID_OPERATION](capi-usb-serial-types-h.md#usbserial_ddkretcode): The operation is invalid.|
312
313### OH_UsbSerial_Flush()
314
315```
316int32_t OH_UsbSerial_Flush(UsbSerial_DeviceHandle *dev)
317```
318
319**Description**
320
321Clears the input and output buffers after the write operation is complete. If a large amount of data is to be transmitted to the USB serial port device, the data may be buffered in the kernel for transmission. If the application closes the file descriptor or exits before the data is completely sent out, some data may be lost.<br> If the data is not sent out, some data may be lost. You can call this API to ensure that all data is sent before subsequent operations are performed.
322
323**Required permissions**: ohos.permission.ACCESS_DDK_USB_SERIAL
324
325**Since**: 18
326
327
328**Parameters**
329
330| Name| Description|
331| -- | -- |
332| [UsbSerial_DeviceHandle](capi-serialddk-usbserial-devicehandle.md) *dev | Device handle.|
333
334**Returns**
335
336| Type| Description|
337| -- | -- |
338| int32_t | [USB_SERIAL_DDK_SUCCESS](capi-usb-serial-types-h.md#usbserial_ddkretcode): The operation is successful.<br>         [USB_SERIAL_DDK_NO_PERM](capi-usb-serial-types-h.md#usbserial_ddkretcode): The permission verification fails.<br>         [USB_SERIAL_DDK_INVALID_PARAMETER](capi-usb-serial-types-h.md#usbserial_ddkretcode): The parameter verification fails. Possible cause: The input **dev** is a null pointer.<br>         [USB_SERIAL_DDK_INIT_ERROR](capi-usb-serial-types-h.md#usbserial_ddkretcode): The DDK initialization fails.<br>         [USB_SERIAL_DDK_SERVICE_ERROR](capi-usb-serial-types-h.md#usbserial_ddkretcode): The DDK service communication fails.<br>         [USB_SERIAL_DDK_IO_ERROR](capi-usb-serial-types-h.md#usbserial_ddkretcode): An I/O exception occurs.<br>         [USB_SERIAL_DDK_INVALID_OPERATION](capi-usb-serial-types-h.md#usbserial_ddkretcode): The operation is invalid.|
339
340### OH_UsbSerial_FlushInput()
341
342```
343int32_t OH_UsbSerial_FlushInput(UsbSerial_DeviceHandle *dev)
344```
345
346**Description**
347
348Refreshes the input buffer. The data in the buffer is cleared immediately. During the communication with the USB serial port device, especially in the debugging phase, disordered data packets or other exceptions may occur.<br> You can call this API to clear these exceptions to restore the communication.
349
350**Required permissions**: ohos.permission.ACCESS_DDK_USB_SERIAL
351
352**Since**: 18
353
354
355**Parameters**
356
357| Name| Description|
358| -- | -- |
359| [UsbSerial_DeviceHandle](capi-serialddk-usbserial-devicehandle.md) *dev | Device handle.|
360
361**Returns**
362
363| Type| Description|
364| -- | -- |
365| int32_t | [USB_SERIAL_DDK_SUCCESS](capi-usb-serial-types-h.md#usbserial_ddkretcode): The operation is successful.<br>         [USB_SERIAL_DDK_NO_PERM](capi-usb-serial-types-h.md#usbserial_ddkretcode): The permission verification fails.<br>         [USB_SERIAL_DDK_INVALID_PARAMETER](capi-usb-serial-types-h.md#usbserial_ddkretcode): The parameter verification fails. Possible cause: The input **dev** is a null pointer.<br>         [USB_SERIAL_DDK_INIT_ERROR](capi-usb-serial-types-h.md#usbserial_ddkretcode): The DDK initialization fails.<br>         [USB_SERIAL_DDK_SERVICE_ERROR](capi-usb-serial-types-h.md#usbserial_ddkretcode): The DDK service communication fails.<br>         [USB_SERIAL_DDK_IO_ERROR](capi-usb-serial-types-h.md#usbserial_ddkretcode): An I/O exception occurs.<br>         [USB_SERIAL_DDK_INVALID_OPERATION](capi-usb-serial-types-h.md#usbserial_ddkretcode): The operation is invalid.|
366
367### OH_UsbSerial_FlushOutput()
368
369```
370int32_t OH_UsbSerial_FlushOutput(UsbSerial_DeviceHandle *dev)
371```
372
373**Description**
374
375Refreshes the output buffer. The data in the buffer is cleared immediately. During the communication with the USB serial port device, especially in the debugging phase, disordered data packets or other exceptions may occur.<br> You can call this API to clear these exceptions to restore the communication.
376
377**Required permissions**: ohos.permission.ACCESS_DDK_USB_SERIAL
378
379**Since**: 18
380
381
382**Parameters**
383
384| Name| Description|
385| -- | -- |
386| [UsbSerial_DeviceHandle](capi-serialddk-usbserial-devicehandle.md) *dev | Device handle.|
387
388**Returns**
389
390| Type| Description|
391| -- | -- |
392| int32_t | [USB_SERIAL_DDK_SUCCESS](capi-usb-serial-types-h.md#usbserial_ddkretcode): The operation is successful.<br>         [USB_SERIAL_DDK_NO_PERM](capi-usb-serial-types-h.md#usbserial_ddkretcode): The permission verification fails.<br>         [USB_SERIAL_DDK_INVALID_PARAMETER](capi-usb-serial-types-h.md#usbserial_ddkretcode): The parameter verification fails. Possible cause: The input **dev** is a null pointer.<br>         [USB_SERIAL_DDK_INIT_ERROR](capi-usb-serial-types-h.md#usbserial_ddkretcode): The DDK initialization fails.<br>         [USB_SERIAL_DDK_SERVICE_ERROR](capi-usb-serial-types-h.md#usbserial_ddkretcode): The DDK service communication fails.<br>         [USB_SERIAL_DDK_IO_ERROR](capi-usb-serial-types-h.md#usbserial_ddkretcode): An I/O exception occurs.<br>         [USB_SERIAL_DDK_INVALID_OPERATION](capi-usb-serial-types-h.md#usbserial_ddkretcode): The operation is invalid.|
393