• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# USB Serial DDK
2
3## Overview
4
5USB Serial DDK is a development kit for USB drivers. This module provides USB Serial DDK APIs as well as the enum types and data structs used by them.
6
7Serial port communication is often used in industrial scenarios and some legacy devices, such as card issuers and ID card readers. By using the USB Serial DDK APIs, you can develop extended drivers for non-standard peripherals to implement extended functions for USB serial port devices.
8
9**System capability**: SystemCapability.Driver.SERIAL.Extension
10
11**Since**: 18
12
13
14## Summary
15
16
17### File
18
19| Name| Description|
20| -------- | -------- |
21| [usb_serial_ddk_api.h](usb__serial__ddk__api_8h.md) | Declares the USB Serial DDK APIs used by the host to access the serial port device.<br>**File to include**: &lt;serial/usb_serial_ddk_api.h&gt;<br>**Library**: libusb_serial.z.so|
22| [usb_serial_ddk_types.h](usb__serial__ddk__types_8h.md) | Provides the enum variables, structures, and macros used in USB Serial DDK APIs.<br>**File to include**: &lt;serial/usb_serial_ddk_types.h&gt;<br>**Library**: libusb_serial.z.so|
23
24
25### Structs
26
27| Name| Description|
28| -------- | -------- |
29| struct&nbsp;&nbsp;[UsbSerial_Params](_usb_serial___params.md) | Defines the USB serial port parameters for the USB Serial DDK.|
30
31
32### Types
33
34| Name| Description|
35| -------- | -------- |
36| typedef struct [UsbSerial_DeviceHandle](#usbserial_devicehandle) [UsbSerial_DeviceHandle](#usbserial_devicehandle) | Defines the data structures for the USB serial port device (opaque).|
37| typedef struct [UsbSerial_Params](_usb_serial___params.md) \__attribute\__((aligned(8))) [UsbSerial_Params](_usb_serial___params.md) | Defines the USB serial port parameters used by the USB Serial DDK.|
38
39
40### Enums
41
42| Name| Description|
43| -------- | -------- |
44| [UsbSerial_DdkRetCode](#usbserial_ddkretcode) {<br>USB_SERIAL_DDK_NO_PERM = 201, USB_SERIAL_DDK_INVALID_PARAMETER = 401, USB_SERIAL_DDK_SUCCESS = 31600000, USB_SERIAL_DDK_INVALID_OPERATION = 31600001, USB_SERIAL_DDK_INIT_ERROR = 31600002, USB_SERIAL_DDK_SERVICE_ERROR = 31600003, USB_SERIAL_DDK_MEMORY_ERROR = 31600004, USB_SERIAL_DDK_IO_ERROR = 31600005, USB_SERIAL_DDK_DEVICE_NOT_FOUND = 31600006<br>} | Defines the return codes used by the USB Serial DDK.|
45| [UsbSerial_FlowControl](#usbserial_flowcontrol) { USB_SERIAL_NO_FLOW_CONTROL = 0, USB_SERIAL_SOFTWARE_FLOW_CONTROL = 1, USB_SERIAL_HARDWARE_FLOW_CONTROL = 2 } | Defines the flow control in the USB Serial DDK.|
46| [UsbSerial_Parity](#usbserial_parity) { USB_SERIAL_PARITY_NONE = 0, USB_SERIAL_PARITY_ODD = 1, USB_SERIAL_PARITY_EVEN = 2 } | Defines the enums of the parity parameter used by the USB Serial DDK.|
47
48
49### Functions
50
51| Name| Description|
52| -------- | -------- |
53| int32_t [OH_UsbSerial_Init](#oh_usbserial_init) (void) | Initializes the USB Serial DDK.|
54| int32_t [OH_UsbSerial_Release](#oh_usbserial_release) (void) | Releases the USB Serial DDK.|
55| int32_t [OH_UsbSerial_Open](#oh_usbserial_open) (uint64_t deviceId, uint8_t interfaceIndex, [UsbSerial_DeviceHandle](#usbserial_devicehandle) \*\*dev) | Enables the USB serial port device based on the specified **deviceId** and **interfaceIndex**.|
56| int32_t [OH_UsbSerial_Close](#oh_usbserial_close) ([UsbSerial_DeviceHandle](#usbserial_devicehandle) \*dev) | Disables the USB serial port device.|
57| int32_t [OH_UsbSerial_Read](#oh_usbserial_read) ([UsbSerial_DeviceHandle](#usbserial_devicehandle) \*dev, uint8_t \*buff, uint32_t bufferSize, uint32_t \*bytesRead) | Reads data from the USB serial port device to the buffer.|
58| int32_t [OH_UsbSerial_Write](#oh_usbserial_write) ([UsbSerial_DeviceHandle](#usbserial_devicehandle) \*dev, uint8_t \*buff, uint32_t bufferSize, uint32_t \*bytesWritten) | Writes the data in the buffer to the USB serial port device.|
59| int32_t [OH_UsbSerial_SetBaudRate](#oh_usbserial_setbaudrate) ([UsbSerial_DeviceHandle](#usbserial_devicehandle) \*dev, uint32_t [baudRate](usb__serial__ddk__types_8h.md#baudrate)) | 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.|
60| int32_t [OH_UsbSerial_SetParams](#oh_usbserial_setparams) ([UsbSerial_DeviceHandle](#usbserial_devicehandle) \*dev, [UsbSerial_Params](_usb_serial___params.md) \*params) | 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.|
61| int32_t [OH_UsbSerial_SetTimeout](#oh_usbserial_settimeout) ([UsbSerial_DeviceHandle](#usbserial_devicehandle) \*dev, int timeout) | 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.|
62| int32_t [OH_UsbSerial_SetFlowControl](#oh_usbserial_setflowcontrol) ([UsbSerial_DeviceHandle](#usbserial_devicehandle) \*dev, [UsbSerial_FlowControl](#usbserial_flowcontrol) flowControl) | 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.|
63| int32_t [OH_UsbSerial_Flush](#oh_usbserial_flush) ([UsbSerial_DeviceHandle](#usbserial_devicehandle) \*dev) | 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. You can call this API to ensure that all data is sent before subsequent operations are performed.|
64| int32_t [OH_UsbSerial_FlushInput](#oh_usbserial_flushinput) ([UsbSerial_DeviceHandle](#usbserial_devicehandle) \*dev) | 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.|
65| int32_t [OH_UsbSerial_FlushOutput](#oh_usbserial_flushoutput) ([UsbSerial_DeviceHandle](#usbserial_devicehandle) \*dev) | 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.|
66
67
68## Type Description
69
70
71### UsbSerial_DeviceHandle
72
73```
74typedef struct UsbSerial_DeviceHandle UsbSerial_DeviceHandle
75```
76
77**Description**
78
79Defines the data structures for the USB serial port device (opaque).
80
81**Since**: 18
82
83
84### UsbSerial_Params
85
86```
87typedef struct UsbSerial_Params \__attribute\__((aligned(8))) UsbSerial_Params
88```
89
90**Description**
91
92Defines the USB serial port parameters for the USB Serial DDK.
93
94**Since**: 18
95
96
97## Enum Description
98
99
100### UsbSerial_DdkRetCode
101
102```
103enum UsbSerial_DdkRetCode
104```
105
106**Description**
107
108Defines the return codes used by the USB Serial DDK.
109
110**Since**: 18
111
112| Value| Description|
113| -------- | -------- |
114| USB_SERIAL_DDK_NO_PERM | No access permission.|
115| USB_SERIAL_DDK_INVALID_PARAMETER | Invalid parameter.|
116| USB_SERIAL_DDK_SUCCESS | Operation succeeded.|
117| USB_SERIAL_DDK_INVALID_OPERATION | Invalid operation.|
118| USB_SERIAL_DDK_INIT_ERROR | Initialization error.|
119| USB_SERIAL_DDK_SERVICE_ERROR | Service error.|
120| USB_SERIAL_DDK_MEMORY_ERROR | Memory-related errors, such as insufficient memory, memory data replication failure, or memory application fault.|
121| USB_SERIAL_DDK_IO_ERROR | I/O error.|
122| USB_SERIAL_DDK_DEVICE_NOT_FOUND | Device not found.|
123
124
125### UsbSerial_FlowControl
126
127```
128enum UsbSerial_FlowControl
129```
130
131**Description**
132
133Defines the flow control mode for the USB Serial DDK.
134
135**Since**: 18
136
137| Value| Description|
138| -------- | -------- |
139| USB_SERIAL_NO_FLOW_CONTROL | No flow control.|
140| USB_SERIAL_SOFTWARE_FLOW_CONTROL | Software flow control.|
141| USB_SERIAL_HARDWARE_FLOW_CONTROL | Hardware flow control.|
142
143
144### UsbSerial_Parity
145
146```
147enum UsbSerial_Parity
148```
149
150**Description**
151
152Defines the enums of the parity parameter used by the USB Serial DDK.
153
154**Since**: 18
155
156| Value| Description|
157| -------- | -------- |
158| USB_SERIAL_PARITY_NONE | No parity.|
159| USB_SERIAL_PARITY_ODD | Odd parity.|
160| USB_SERIAL_PARITY_EVEN | Even parity.|
161
162
163## Function Description
164
165
166### OH_UsbSerial_Close()
167
168```
169int32_t OH_UsbSerial_Close (UsbSerial_DeviceHandle * dev)
170```
171
172**Description**
173
174Disables the USB serial port device.
175
176**Since**: 18
177
178**Parameters**
179
180| Name| Description|
181| -------- | -------- |
182| dev | Device handle.|
183
184**Required Permissions**
185
186ohos.permission.ACCESS_DDK_USB_SERIAL
187
188**Returns**
189
190- USB_SERIAL_DDK_SUCCESS: Operation succeeded.
191
192- USB_SERIAL_DDK_NO_PERM: Permission verification failed.
193
194- USB_SERIAL_DDK_INVALID_PARAMETER: Parameter verification failed. Possible cause: The input **dev** is a null pointer.
195
196- USB_SERIAL_DDK_INIT_ERROR: DDK initialization error.
197
198- USB_SERIAL_DDK_SERVICE_ERROR: DDK service communication error.
199
200- USB_SERIAL_DDK_IO_ERROR: DDK I/O error.
201
202- USB_SERIAL_DDK_INVALID_OPERATION: Invalid operation.
203
204
205### OH_UsbSerial_Flush()
206
207```
208int32_t OH_UsbSerial_Flush (UsbSerial_DeviceHandle * dev)
209```
210
211**Description**
212
213Clears the input and output buffers after the write operation is complete.
214
215If 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. You can call this API to ensure that all data is sent before subsequent operations are performed.
216
217**Since**: 18
218
219**Parameters**
220
221| Name| Description|
222| -------- | -------- |
223| dev | Device handle.|
224
225**Required Permissions**
226
227ohos.permission.ACCESS_DDK_USB_SERIAL
228
229**Returns**
230
231- USB_SERIAL_DDK_SUCCESS: Operation succeeded.
232
233- USB_SERIAL_DDK_NO_PERM: Permission verification failed.
234
235- USB_SERIAL_DDK_INVALID_PARAMETER: Parameter verification failed. Possible cause: The input **dev** is a null pointer.
236
237- USB_SERIAL_DDK_INIT_ERROR: DDK initialization error.
238
239- USB_SERIAL_DDK_SERVICE_ERROR: DDK service communication error.
240
241- USB_SERIAL_DDK_IO_ERROR: DDK I/O error.
242
243- USB_SERIAL_DDK_INVALID_OPERATION: Invalid operation.
244
245
246### OH_UsbSerial_FlushInput()
247
248```
249int32_t OH_UsbSerial_FlushInput (UsbSerial_DeviceHandle * dev)
250```
251
252**Description**
253
254Refreshes 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.
255
256You can call this API to clear these exceptions to restore the communication.
257
258**Since**: 18
259
260**Parameters**
261
262| Name| Description|
263| -------- | -------- |
264| dev | Device handle.|
265
266**Required Permissions**
267
268ohos.permission.ACCESS_DDK_USB_SERIAL
269
270**Returns**
271
272- USB_SERIAL_DDK_SUCCESS: Operation succeeded.
273
274- USB_SERIAL_DDK_NO_PERM: Permission verification failed.
275
276- USB_SERIAL_DDK_INVALID_PARAMETER: Parameter verification failed. Possible cause: The input **dev** is a null pointer.
277
278- USB_SERIAL_DDK_INIT_ERROR: DDK initialization error.
279
280- USB_SERIAL_DDK_SERVICE_ERROR: DDK service communication error.
281
282- USB_SERIAL_DDK_IO_ERROR: DDK I/O error.
283
284- USB_SERIAL_DDK_INVALID_OPERATION: Invalid operation.
285
286
287### OH_UsbSerial_FlushOutput()
288
289```
290int32_t OH_UsbSerial_FlushOutput (UsbSerial_DeviceHandle * dev)
291```
292
293**Description**
294
295Refreshes 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.
296
297You can call this API to clear these exceptions to restore the communication.
298
299**Since**: 18
300
301**Parameters**
302
303| Name| Description|
304| -------- | -------- |
305| dev | Device handle.|
306
307**Required Permissions**
308
309ohos.permission.ACCESS_DDK_USB_SERIAL
310
311**Returns**
312
313- USB_SERIAL_DDK_SUCCESS: Operation succeeded.
314
315- USB_SERIAL_DDK_NO_PERM: Permission verification failed.
316
317- USB_SERIAL_DDK_INVALID_PARAMETER: Parameter verification failed. Possible cause: The input **dev** is a null pointer.
318
319- USB_SERIAL_DDK_INIT_ERROR: DDK initialization error.
320
321- USB_SERIAL_DDK_SERVICE_ERROR: DDK service communication error.
322
323- USB_SERIAL_DDK_IO_ERROR: DDK I/O error.
324
325- USB_SERIAL_DDK_INVALID_OPERATION: Invalid operation.
326
327
328### OH_UsbSerial_Init()
329
330```
331int32_t OH_UsbSerial_Init (void)
332```
333
334**Description**
335
336Initializes the USB Serial DDK.
337
338**Since**: 18
339
340**Required Permissions**
341
342ohos.permission.ACCESS_DDK_USB_SERIAL
343
344**Returns**
345
346- USB_SERIAL_DDK_SUCCESS: Operation succeeded.
347
348- USB_SERIAL_DDK_NO_PERM: Permission verification failed.
349
350- USB_SERIAL_DDK_INIT_ERROR: DDK initialization error.
351
352
353### OH_UsbSerial_Open()
354
355```
356int32_t OH_UsbSerial_Open (uint64_t deviceId, uint8_t interfaceIndex, UsbSerial_DeviceHandle ** dev)
357```
358
359**Description**
360
361Enables the USB serial port device based on the specified **deviceId** and **interfaceIndex**.
362
363**Since**: 18
364
365**Parameters**
366
367| Name| Description|
368| -------- | -------- |
369| deviceId | Device ID.|
370| interfaceIndex | Interface index, which corresponds to [bInterfaceNumber](usb__ddk__types_8h.md#binterfacenumber) in the USB protocol.|
371| dev | Device handle.|
372
373**Required Permissions**
374
375ohos.permission.ACCESS_DDK_USB_SERIAL
376
377**Returns**
378
379- USB_SERIAL_DDK_SUCCESS: Operation succeeded.
380
381- USB_SERIAL_DDK_NO_PERM: Permission verification failed.
382
383- USB_SERIAL_DDK_INVALID_PARAMETER: Parameter verification failed. Possible cause: The input **dev** is a null pointer.
384
385- USB_SERIAL_DDK_INIT_ERROR: DDK initialization error.
386
387- USB_SERIAL_DDK_SERVICE_ERROR: DDK service communication error.
388
389- USB_SERIAL_DDK_MEMORY_ERROR: Insufficient memory.
390
391- USB_SERIAL_DDK_IO_ERROR: DDK I/O error.
392
393- USB_SERIAL_DDK_DEVICE_NOT_FOUND: Device or interface not found.
394
395
396### OH_UsbSerial_Read()
397
398```
399int32_t OH_UsbSerial_Read (UsbSerial_DeviceHandle * dev, uint8_t * buff, uint32_t bufferSize, uint32_t * bytesRead)
400```
401
402**Description**
403
404Reads data from the USB serial port device to the buffer.
405
406**Since**: 18
407
408**Parameters**
409
410| Name| Description|
411| -------- | -------- |
412| dev | Device handle.|
413| buff | Buffer for storing the data read from the USB serial port device.|
414| bufferSize | Buffer size.|
415| 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](#oh_usbserial_settimeout).|
416
417**Required Permissions**
418
419ohos.permission.ACCESS_DDK_USB_SERIAL
420
421**Returns**
422
423- USB_SERIAL_DDK_SUCCESS: Operation succeeded.
424
425- USB_SERIAL_DDK_NO_PERM: Permission verification failed.
426
427- USB_SERIAL_DDK_INVALID_PARAMETER: Parameter verification failed. Possible causes: 1. **dev** is a null pointer. 2. **buff** is a null pointer. 3. **bufferSize** is **0**. 4. **bytesRead** is a null pointer.
428
429- USB_SERIAL_DDK_INIT_ERROR: DDK initialization error.
430
431- USB_SERIAL_DDK_SERVICE_ERROR: DDK service communication error.
432
433- USB_SERIAL_DDK_MEMORY_ERROR: Invalid buffer address.
434
435- USB_SERIAL_DDK_IO_ERROR: DDK I/O error.
436
437- USB_SERIAL_DDK_INVALID_OPERATION: Invalid operation.
438
439
440### OH_UsbSerial_Release()
441
442```
443int32_t OH_UsbSerial_Release (void)
444```
445
446**Description**
447
448Releases the USB Serial DDK.
449
450**Since**: 18
451
452**Required Permissions**
453
454ohos.permission.ACCESS_DDK_USB_SERIAL
455
456**Returns**
457
458- USB_SERIAL_DDK_SUCCESS: Operation succeeded.
459
460- USB_SERIAL_DDK_NO_PERM: Permission verification failed.
461
462- USB_SERIAL_DDK_INIT_ERROR: DDK initialization error.
463
464- USB_SERIAL_DDK_SERVICE_ERROR: DDK service communication error.
465
466
467### OH_UsbSerial_SetBaudRate()
468
469```
470int32_t OH_UsbSerial_SetBaudRate (UsbSerial_DeviceHandle * dev, uint32_t baudRate)
471```
472
473**Description**
474
475Sets 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.
476
477**Since**: 18
478
479**Parameters**
480
481| Name| Description|
482| -------- | -------- |
483| dev | Device handle.|
484| baudRate | Baud rate of the USB serial port device.|
485
486**Required Permissions**
487
488ohos.permission.ACCESS_DDK_USB_SERIAL
489
490**Returns**
491
492- USB_SERIAL_DDK_SUCCESS: Operation succeeded.
493
494- USB_SERIAL_DDK_NO_PERM: Permission verification failed.
495
496- USB_SERIAL_DDK_INVALID_PARAMETER: Parameter verification failed. Possible cause: The input **dev** is a null pointer.
497
498- USB_SERIAL_DDK_INIT_ERROR: DDK initialization error.
499
500- USB_SERIAL_DDK_SERVICE_ERROR: DDK service communication error.
501
502- USB_SERIAL_DDK_IO_ERROR: DDK I/O error.
503
504- USB_SERIAL_DDK_INVALID_OPERATION: Invalid operation.
505
506
507### OH_UsbSerial_SetFlowControl()
508
509```
510int32_t OH_UsbSerial_SetFlowControl (UsbSerial_DeviceHandle * dev, UsbSerial_FlowControl flowControl)
511```
512
513**Description**
514
515Sets 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.
516
517If 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.
518
519**Since**: 18
520
521**Parameters**
522
523| Name| Description|
524| -------- | -------- |
525| dev | Device handle.|
526| flowControl | Flow control mode. For details, see [UsbSerial_FlowControl](#usbserial_flowcontrol).|
527
528**Required Permissions**
529
530ohos.permission.ACCESS_DDK_USB_SERIAL
531
532**Returns**
533
534- USB_SERIAL_DDK_SUCCESS: Operation succeeded.
535
536- USB_SERIAL_DDK_NO_PERM: Permission verification failed.
537
538- USB_SERIAL_DDK_INVALID_PARAMETER: Parameter verification failed. Possible cause: The input **dev** is a null pointer.
539
540- USB_SERIAL_DDK_INIT_ERROR: DDK initialization error.
541
542- USB_SERIAL_DDK_SERVICE_ERROR: DDK service communication error.
543
544- USB_SERIAL_DDK_IO_ERROR: DDK I/O error.
545
546- USB_SERIAL_DDK_INVALID_OPERATION: Invalid operation.
547
548
549### OH_UsbSerial_SetParams()
550
551```
552int32_t OH_UsbSerial_SetParams (UsbSerial_DeviceHandle * dev, UsbSerial_Params * params)
553```
554
555**Description**
556
557Sets 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.
558
559**Since**: 18
560
561**Parameters**
562
563| Name| Description|
564| -------- | -------- |
565| dev | Device handle.|
566| params | USB serial port device parameters. For details, see [UsbSerial_Params](_usb_serial___params.md).|
567
568**Required Permissions**
569
570ohos.permission.ACCESS_DDK_USB_SERIAL
571
572**Returns**
573
574- USB_SERIAL_DDK_SUCCESS: Operation succeeded.
575
576- USB_SERIAL_DDK_NO_PERM: Permission verification failed.
577
578- USB_SERIAL_DDK_INVALID_PARAMETER: Parameter verification failed. Possible causes: 1. **dev** is a null pointer. 2. **params** is a null pointer.
579
580- USB_SERIAL_DDK_INIT_ERROR: DDK initialization error.
581
582- USB_SERIAL_DDK_SERVICE_ERROR: DDK service communication error.
583
584- USB_SERIAL_DDK_IO_ERROR: DDK I/O error.
585
586- USB_SERIAL_DDK_INVALID_OPERATION: Invalid operation.
587
588
589### OH_UsbSerial_SetTimeout()
590
591```
592int32_t OH_UsbSerial_SetTimeout (UsbSerial_DeviceHandle * dev, int timeout)
593```
594
595**Description**
596
597Sets the timeout interval (ms) for reading data reported by a USB serial port device.
598
599If 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.
600
601**Since**: 18
602
603**Parameters**
604
605| Name| Description|
606| -------- | -------- |
607| dev | Device handle.|
608| timeout | Timeout interval for reading data from a USB serial port device. The value range is as follows:<br>- (0, 25500]: time value in milliseconds. 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**.<br>- **0**: Data is returned immediately.<br>- **-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](#oh_usbserial_read).|
609
610**Required Permissions**
611
612ohos.permission.ACCESS_DDK_USB_SERIAL
613
614**Returns**
615
616- USB_SERIAL_DDK_SUCCESS: Operation succeeded.
617
618- USB_SERIAL_DDK_NO_PERM: Permission verification failed.
619
620- USB_SERIAL_DDK_INVALID_PARAMETER: Parameter verification failed. Possible causes: 1. **dev** is a null pointer. 2. **timeout** is less than **-1** or greater than **25500**.
621
622- USB_SERIAL_DDK_INIT_ERROR: DDK initialization error.
623
624- USB_SERIAL_DDK_SERVICE_ERROR: DDK service communication error.
625
626- USB_SERIAL_DDK_IO_ERROR: DDK I/O error.
627
628- USB_SERIAL_DDK_INVALID_OPERATION: Invalid operation.
629
630
631### OH_UsbSerial_Write()
632
633```
634int32_t OH_UsbSerial_Write (UsbSerial_DeviceHandle * dev, uint8_t * buff, uint32_t bufferSize, uint32_t * bytesWritten)
635```
636
637**Description**
638
639Writes the data in the buffer to the USB serial port device.
640
641**Since**: 18
642
643**Parameters**
644
645| Name| Description|
646| -------- | -------- |
647| dev | Device handle.|
648| buff | Buffer to which the data of the USB serial port device is written.|
649| bufferSize | Buffer size.|
650| bytesWritten | Number of bytes that are actually written.|
651
652**Required Permissions**
653
654ohos.permission.ACCESS_DDK_USB_SERIAL
655
656**Returns**
657
658- USB_SERIAL_DDK_SUCCESS: Operation succeeded.
659
660- USB_SERIAL_DDK_NO_PERM: Permission verification failed.
661
662- USB_SERIAL_DDK_INVALID_PARAMETER: Parameter verification failed. Possible causes: 1. **dev** is a null pointer. 2. **buff** is a null pointer. 3. **bufferSize** is **0**. 4. **bytesWritten** is a null pointer.
663
664- USB_SERIAL_DDK_INIT_ERROR: DDK initialization error.
665
666- USB_SERIAL_DDK_SERVICE_ERROR: DDK service communication error.
667
668- USB_SERIAL_DDK_MEMORY_ERROR: Invalid buffer address.
669
670- USB_SERIAL_DDK_IO_ERROR: DDK I/O error.
671
672- USB_SERIAL_DDK_INVALID_OPERATION: Invalid operation.
673