1# @ohos.usbManager.serial (串口管理) 2 3<!--Kit: Basic Services Kit--> 4<!--Subsystem: USB--> 5<!--Owner: @hwymlgitcode--> 6<!--Designer: @w00373942--> 7<!--Tester: @dong-dongzhen--> 8<!--Adviser: @w_Machine_cc--> 9 10本模块主要提供串口管理功能,包括打开和关闭设备的串口、写入和读取数据、设置和获取串口的配置参数、权限管理等。 11 12> **说明:** 13> 14> 本模块首批接口从API version 19开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 15 16## 导入模块 17 18```ts 19import { serialManager } from '@kit.BasicServicesKit'; 20``` 21 22## serialManager.getPortList 23 24getPortList(): Readonly<SerialPort>[] 25 26查询串口设备清单,包括设备名称和对应的端口号。 27 28**系统能力:** SystemCapability.USB.USBManager.Serial 29 30**返回值:** 31 32| 类型 | 说明 | 33|-------------------------------------------|-------------| 34| Readonly<[SerialPort](#serialport)>[] | 串口信息列表。 | 35 36**示例:** 37 38> **说明:** 39> 40> 以下示例代码只是调用getPortList接口的必要流程,需要放入具体的方法中执行。实际调用时,设备开发者需要遵循设备相关协议进行调用。 41 42<!--code_no_check--> 43```ts 44import { JSON } from '@kit.ArkTS'; 45import { serialManager } from '@kit.BasicServicesKit'; 46 47// 获取串口设备清单 48let portList: serialManager.SerialPort[] = serialManager.getPortList(); 49console.info('usbSerial portList: ' + JSON.stringify(portList)); 50if (portList === undefined || portList.length === 0) { 51 console.info('usbSerial portList is empty'); 52 return; 53} 54let portId: number = portList[0].portId; 55``` 56 57## serialManager.hasSerialRight 58 59hasSerialRight(portId: number): boolean 60 61检查应用程序是否具有访问串口设备的权限。应用退出后再拉起时,需要重新申请授权。 62 63**系统能力:** SystemCapability.USB.USBManager.Serial 64 65**参数:** 66 67| 参数名 | 类型 | 必填 | 说明 | 68|--------|--------|----|-------------------------------------| 69| portId | number | 是 | 端口号。 | 70 71**返回值:** 72 73| 类型 | 说明 | 74|---------|------------------| 75| boolean | true表示已授权,false表示未授权。 | 76 77**错误码:** 78 79以下错误码的详细介绍参见[USB服务错误码](errorcode-usb.md)。 80 81| 错误码ID | 错误信息 | 82| -------- | ------------------------------------------------------------ | 83| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 84| 14400005 | Database operation exception. | 85| 31400001 | Serial port management exception. | 86| 31400003 | PortId does not exist. | 87 88**示例:** 89 90> **说明:** 91> 92> 以下示例代码只是调用hasSerialRight接口的必要流程,需要放入具体的方法中执行。实际调用时,设备开发者需要遵循设备相关协议进行调用。 93 94<!--code_no_check--> 95```ts 96import { JSON } from '@kit.ArkTS'; 97import { serialManager } from '@kit.BasicServicesKit'; 98 99// 获取串口列表 100let portList: serialManager.SerialPort[] = serialManager.getPortList(); 101console.info('portList: ', JSON.stringify(portList)); 102if (portList === undefined || portList.length === 0) { 103 console.info('portList is empty'); 104 return; 105} 106let portId: number = portList[0].portId; 107 108// 检测设备是否可被应用访问 109if (serialManager.hasSerialRight(portId)) { 110 console.info('The serial port is accessible'); 111} else { 112 console.info('No permission to access the serial port'); 113} 114``` 115 116## serialManager.requestSerialRight 117 118requestSerialRight(portId: number): Promise<boolean> 119 120请求应用程序访问串口设备的权限。应用退出自动移除对串口设备的访问权限,在应用重启后需要重新申请授权。 121 122**系统能力:** SystemCapability.USB.USBManager.Serial 123 124**参数:** 125 126| 参数名 | 类型 | 必填 | 说明 | 127|--------|--------|----|-------------------------------------| 128| portId | number | 是 | 端口号。 | 129 130**返回值:** 131 132| 类型 | 说明 | 133|------------------------|---------------| 134| Promise<boolean> | Promise对象,true表示请求权限成功,false表示请求权限失败。 | 135 136**错误码:** 137 138以下错误码的详细介绍参见[USB服务错误码](errorcode-usb.md)。 139 140| 错误码ID | 错误信息 | 141| -------- | ------------------------------------------------------------ | 142| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 143| 14400005 | Database operation exception. | 144| 31400001 | Serial port management exception. | 145| 31400003 | PortId does not exist. | 146 147**示例:** 148 149> **说明:** 150> 151> 以下示例代码只是调用requestSerialRight接口的必要流程,需要放入具体的方法中执行。实际调用时,设备开发者需要遵循设备相关协议进行调用。 152 153<!--code_no_check--> 154```ts 155import { JSON } from '@kit.ArkTS'; 156import { serialManager } from '@kit.BasicServicesKit'; 157 158// 获取串口列表 159let portList: serialManager.SerialPort[] = serialManager.getPortList(); 160console.info('usbSerial portList: ' + JSON.stringify(portList)); 161if (portList === undefined || portList.length === 0) { 162 console.info('usbSerial portList is empty'); 163 return; 164} 165let portId: number = portList[0].portId; 166 167// 检测设备是否可被应用访问 168if (!serialManager.hasSerialRight(portId)) { 169 serialManager.requestSerialRight(portId).then(result => { 170 if (!result) { 171 // 没有访问设备的权限且用户不授权则退出 172 console.info('user is not granted the operation permission'); 173 return; 174 } else { 175 console.info('grant permission successfully'); 176 } 177 }); 178} 179``` 180 181## serialManager.open 182 183open(portId: number): void 184 185打开串口设备。 186 187**系统能力:** SystemCapability.USB.USBManager.Serial 188 189**参数:** 190 191| 参数名 | 类型 | 必填 | 说明 | 192|--------|--------|----|-------------| 193| portId | number | 是 | 端口号。 | 194 195**错误码:** 196 197以下错误码的详细介绍参见[USB服务错误码](errorcode-usb.md)。 198 199| 错误码ID | 错误信息 | 200| -------- | ------------------------------------------------------------ | 201| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 202| 31400001 | Serial port management exception. | 203| 31400002 | Access denied. Call requestSerialRight to request user authorization first. | 204| 31400003 | PortId does not exist. | 205| 31400004 | The serial port device is occupied. | 206 207**示例:** 208 209> **说明:** 210> 211> 以下示例代码只是调用open接口的必要流程,需要放入具体的方法中执行。实际调用时,设备开发者需要遵循设备相关协议进行调用。 212 213<!--code_no_check--> 214```ts 215import { JSON } from '@kit.ArkTS'; 216import { serialManager } from '@kit.BasicServicesKit'; 217 218// 获取串口列表 219let portList: serialManager.SerialPort[] = serialManager.getPortList(); 220console.info('usbSerial portList: ' + JSON.stringify(portList)); 221if (portList === undefined || portList.length === 0) { 222 console.info('usbSerial portList is empty'); 223 return; 224} 225let portId: number = portList[0].portId; 226 227// 检测设备是否可被应用访问 228if (!serialManager.hasSerialRight(portId)) { 229 serialManager.requestSerialRight(portId).then(result => { 230 if (!result) { 231 // 没有访问设备的权限且用户不授权则退出 232 console.info('user is not granted the operation permission'); 233 return; 234 } else { 235 console.info('grant permission successfully'); 236 } 237 }); 238} 239 240// 打开设备 241try { 242 serialManager.open(portId) 243 console.info('open usbSerial success, portId: ' + portId); 244} catch (error) { 245 console.error('open usbSerial error, ' + JSON.stringify(error)); 246} 247``` 248 249## serialManager.getAttribute 250 251getAttribute(portId: number): Readonly<[SerialAttribute](#serialattribute)> 252 253获取指定串口的配置参数。 254 255**系统能力:** SystemCapability.USB.USBManager.Serial 256 257**参数:** 258 259| 参数名 | 类型 | 必填 | 说明 | 260|--------|--------|----|-------------| 261| portId | number | 是 | 端口号。 | 262 263**返回值:** 264 265| 类型 | 说明 | 266|--------|-------------| 267| Readonly<[SerialAttribute](#serialattribute)> | 返回串口的配置参数。 | 268 269**错误码:** 270 271以下错误码的详细介绍参见[USB服务错误码](errorcode-usb.md)。 272 273| 错误码ID | 错误信息 | 274| -------- | ------------------------------------------------------------ | 275| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 276| 31400001 | Serial port management exception. | 277| 31400003 | PortId does not exist. | 278| 31400005 | The serial port device is not opened. Call the open API first. | 279 280**示例:** 281 282> **说明:** 283> 284> 以下示例代码只是调用getAttribute接口的必要流程,需要放入具体的方法中执行。实际调用时,设备开发者需要遵循设备相关协议进行调用。 285 286<!--code_no_check--> 287```ts 288import { JSON } from '@kit.ArkTS'; 289import { serialManager } from '@kit.BasicServicesKit'; 290 291// 获取串口列表 292let portList: serialManager.SerialPort[] = serialManager.getPortList(); 293console.info('usbSerial portList: ' + JSON.stringify(portList)); 294if (portList === undefined || portList.length === 0) { 295 console.info('usbSerial portList is empty'); 296 return; 297} 298let portId: number = portList[0].portId; 299 300// 检测设备是否可被应用访问 301if (!serialManager.hasSerialRight(portId)) { 302 serialManager.requestSerialRight(portId).then(result => { 303 if (!result) { 304 // 没有访问设备的权限且用户不授权则退出 305 console.info('user is not granted the operation permission'); 306 return; 307 } else { 308 console.info('grant permission successfully'); 309 } 310 }); 311} 312 313// 打开设备 314try { 315 serialManager.open(portId) 316 console.info('open usbSerial success, portId: ' + portId); 317} catch (error) { 318 console.error('open usbSerial error, ' + JSON.stringify(error)); 319 return; 320} 321 322// 获取串口配置 323try { 324 let attribute: serialManager.SerialAttribute = serialManager.getAttribute(portId); 325 if (attribute === undefined) { 326 console.error('getAttribute usbSerial error, attribute is undefined'); 327 } else { 328 console.info('getAttribute usbSerial success, attribute: ' + JSON.stringify(attribute)); 329 } 330} catch (error) { 331 console.error('getAttribute usbSerial error, ' + JSON.stringify(error)); 332} 333``` 334 335## serialManager.setAttribute 336 337setAttribute(portId: number, attribute: [SerialAttribute](#serialattribute)): void 338 339设置串口的配置参数。如果未调用该方法,使用默认配置参数(波特率:9600bps;据位:8;校验位:0;停止位:1)。 340 341**系统能力:** SystemCapability.USB.USBManager.Serial 342 343**参数:** 344 345| 参数名 | 类型 | 必填 | 说明 | 346|-----------|-------------------------------------|----|-------------| 347| portId | number | 是 | 端口号。 | 348| attribute | [SerialAttribute](#serialattribute) | 是 | 串口参数。 | 349 350**错误码:** 351 352以下错误码的详细介绍参见[USB服务错误码](errorcode-usb.md)。 353 354| 错误码ID | 错误信息 | 355| -------- | ------------------------------------------------------------ | 356| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 357| 31400001 | Serial port management exception. | 358| 31400003 | PortId does not exist. | 359| 31400005 | The serial port device is not opened. Call the open API first. | 360 361**示例:** 362 363> **说明:** 364> 365> 以下示例代码只是调用setAttribute接口的必要流程,需要放入具体的方法中执行。实际调用时,设备开发者需要遵循设备相关协议进行调用。 366 367<!--code_no_check--> 368```ts 369import { JSON } from '@kit.ArkTS'; 370import { serialManager } from '@kit.BasicServicesKit'; 371 372// 获取串口列表 373let portList: serialManager.SerialPort[] = serialManager.getPortList(); 374console.info('usbSerial portList: ' + JSON.stringify(portList)); 375if (portList === undefined || portList.length === 0) { 376 console.info('usbSerial portList is empty'); 377 return; 378} 379let portId: number = portList[0].portId; 380 381// 检测设备是否可被应用访问 382if (!serialManager.hasSerialRight(portId)) { 383 serialManager.requestSerialRight(portId).then(result => { 384 if (!result) { 385 // 没有访问设备的权限且用户不授权则退出 386 console.info('user is not granted the operation permission'); 387 return; 388 } else { 389 console.info('grant permission successfully'); 390 } 391 }); 392} 393 394// 打开设备 395try { 396 serialManager.open(portId) 397 console.info('open usbSerial success, portId: ' + portId); 398} catch (error) { 399 console.error('open usbSerial error, ' + JSON.stringify(error)); 400 return; 401} 402 403// 设置串口配置 404try { 405 let attribute: serialManager.SerialAttribute = { 406 baudRate: serialManager.BaudRates.BAUDRATE_9600, 407 dataBits: serialManager.DataBits.DATABIT_8, 408 parity: serialManager.Parity.PARITY_NONE, 409 stopBits: serialManager.StopBits.STOPBIT_1 410 } 411 serialManager.setAttribute(portId, attribute); 412 console.info('setAttribute usbSerial success, attribute: ' + JSON.stringify(attribute)); 413} catch (error) { 414 console.error('setAttribute usbSerial error, ' + JSON.stringify(error)); 415} 416``` 417 418## serialManager.read 419 420read(portId: number, buffer: Uint8Array, timeout?: number): Promise<number> 421 422从串口设备异步读取数据。 423 424**系统能力:** SystemCapability.USB.USBManager.Serial 425 426**参数:** 427 428| 参数名 | 类型 | 必填 | 说明 | 429|---------|------------|----|------------------| 430| portId | number | 是 | 端口号。 | 431| buffer | Uint8Array | 是 | 读取数据的缓冲区。 | 432| timeout | number | 否 | 超时时间(单位:ms),可选参数,默认为0不超时,用户按需选择。 | 433 434**返回值:** 435 436| 类型 | 说明 | 437|-----------------------|----------------| 438| Promise<number> | Promise对象,返回读取数据长度。 | 439 440**错误码:** 441 442以下错误码的详细介绍参见[USB服务错误码](errorcode-usb.md)。 443 444| 错误码ID | 错误信息 | 445| -------- | ------------------------------------------------------------ | 446| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 447| 31400001 | Serial port management exception. | 448| 31400003 | PortId does not exist. | 449| 31400005 | The serial port device is not opened. Call the open API first. | 450| 31400006 | Data transfer timed out. | 451| 31400007 | I/O exception. Possible causes: 1. The transfer was canceled. 2. The device offered more data than allowed. | 452 453**示例:** 454 455> **说明:** 456> 457> 以下示例代码只是调用read接口的必要流程,需要放入具体的方法中执行。实际调用时,设备开发者需要遵循设备相关协议进行调用。 458 459<!--code_no_check--> 460```ts 461import { JSON } from '@kit.ArkTS'; 462import { serialManager } from '@kit.BasicServicesKit'; 463 464// 获取串口列表 465let portList: serialManager.SerialPort[] = serialManager.getPortList(); 466console.info('usbSerial portList: ' + JSON.stringify(portList)); 467if (portList === undefined || portList.length === 0) { 468 console.info('usbSerial portList is empty'); 469 return; 470} 471let portId: number = portList[0].portId; 472 473// 检测设备是否可被应用访问 474if (!serialManager.hasSerialRight(portId)) { 475 serialManager.requestSerialRight(portId).then(result => { 476 if (!result) { 477 // 没有访问设备的权限且用户不授权则退出 478 console.info('user is not granted the operation permission'); 479 return; 480 } else { 481 console.info('grant permission successfully'); 482 } 483 }); 484} 485 486// 打开设备 487try { 488 serialManager.open(portId) 489 console.info('open usbSerial success, portId: ' + portId); 490} catch (error) { 491 console.error('open usbSerial error, ' + JSON.stringify(error)); 492} 493 494// 异步读取 495let readBuffer: Uint8Array = new Uint8Array(64); 496serialManager.read(portId, readBuffer, 2000).then((size: number) => { 497 console.info('read usbSerial success, readBuffer: ' + readBuffer.toString()); 498}).catch((error: Error) => { 499 console.error('read usbSerial error, ' + JSON.stringify(error)); 500}) 501``` 502 503## serialManager.readSync 504 505readSync(portId: number, buffer: Uint8Array, timeout?: number): number 506 507从串口设备同步读取数据。 508 509**系统能力:** SystemCapability.USB.USBManager.Serial 510 511**参数:** 512 513| 参数名 | 类型 | 必填 | 说明 | 514|---------|------------|----|------------------| 515| portId | number | 是 | 端口号。| 516| buffer | Uint8Array | 是 | 读取数据的缓冲区。 | 517| timeout | number | 否 | 超时时间(单位:ms),可选参数,默认为0不超时,用户按需选择。 | 518 519**返回值:** 520 521| 类型 | 说明 | 522|--------|-------------| 523| number | 返回读取数据长度。 | 524 525**错误码:** 526 527以下错误码的详细介绍参见[USB服务错误码](errorcode-usb.md)。 528 529| 错误码ID | 错误信息 | 530| -------- | ------------------------------------------------------------ | 531| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 532| 31400001 | Serial port management exception. | 533| 31400003 | PortId does not exist. | 534| 31400005 | The serial port device is not opened. Call the open API first. | 535| 31400006 | Data transfer timed out. | 536| 31400007 | I/O exception. Possible causes: 1. The transfer was canceled. 2. The device offered more data than allowed. | 537 538**示例:** 539 540> **说明:** 541> 542> 以下示例代码只是调用readSync接口的必要流程,需要放入具体的方法中执行。实际调用时,设备开发者需要遵循设备相关协议进行调用。 543 544<!--code_no_check--> 545```ts 546import { JSON } from '@kit.ArkTS'; 547import { serialManager } from '@kit.BasicServicesKit'; 548 549// 获取串口列表 550let portList: serialManager.SerialPort[] = serialManager.getPortList(); 551console.info('usbSerial portList: ' + JSON.stringify(portList)); 552if (portList === undefined || portList.length === 0) { 553 console.info('usbSerial portList is empty'); 554 return; 555} 556let portId: number = portList[0].portId; 557 558// 检测设备是否可被应用访问 559if (!serialManager.hasSerialRight(portId)) { 560 serialManager.requestSerialRight(portId).then(result => { 561 if (!result) { 562 // 没有访问设备的权限且用户不授权则退出 563 console.info('user is not granted the operation permission'); 564 return; 565 } else { 566 console.info('grant permission successfully'); 567 } 568 }); 569} 570 571// 打开设备 572try { 573 serialManager.open(portId) 574 console.info('open usbSerial success, portId: ' + portId); 575} catch (error) { 576 console.error('open usbSerial error, ' + JSON.stringify(error)); 577} 578 579// 同步读取 580let readSyncBuffer: Uint8Array = new Uint8Array(64); 581try { 582 serialManager.readSync(portId, readSyncBuffer, 2000); 583 console.info('readSync usbSerial success, readSyncBuffer: ' + readSyncBuffer.toString()); 584} catch (error) { 585 console.error('readSync usbSerial error, ' + JSON.stringify(error)); 586} 587``` 588 589## serialManager.write 590 591write(portId: number, buffer: Uint8Array, timeout?: number): Promise<number> 592 593向串口设备异步写入数据。 594 595**系统能力:** SystemCapability.USB.USBManager.Serial 596 597**参数:** 598 599| 参数名 | 类型 | 必填 | 说明 | 600|---------|------------|----|------------------| 601| portId | number | 是 | 端口号。 | 602| buffer | Uint8Array | 是 | 写入数据的缓冲区。 | 603| timeout | number | 否 | 超时时间(单位:ms),可选参数,默认为0不超时,用户按需选择。 | 604 605**返回值:** 606 607| 类型 | 说明 | 608|-----------------------|-------------| 609| Promise<number> | Promise对象,返回写入数据长度。 | 610 611**错误码:** 612 613以下错误码的详细介绍参见[USB服务错误码](errorcode-usb.md)。 614 615| 错误码ID | 错误信息 | 616| -------- | ------------------------------------------------------------ | 617| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 618| 31400001 | Serial port management exception. | 619| 31400003 | PortId does not exist. | 620| 31400005 | The serial port device is not opened. Call the open API first. | 621| 31400006 | Data transfer timed out. | 622| 31400007 | I/O exception. Possible causes: 1. The transfer was canceled. 2. The device offered more data than allowed. | 623 624**示例:** 625 626> **说明:** 627> 628> 以下示例代码只是调用addSerialRight接口的必要流程,需要放入具体的方法中执行。实际调用时,设备开发者需要遵循设备相关协议进行调用。 629 630<!--code_no_check--> 631```ts 632import { JSON } from '@kit.ArkTS'; 633import { serialManager } from '@kit.BasicServicesKit'; 634 635// 获取串口列表 636let portList: serialManager.SerialPort[] = serialManager.getPortList(); 637console.info('usbSerial portList: ' + JSON.stringify(portList)); 638if (portList === undefined || portList.length === 0) { 639 console.info('usbSerial portList is empty'); 640 return; 641} 642let portId: number = portList[0].portId; 643 644// 检测设备是否可被应用访问 645if (!serialManager.hasSerialRight(portId)) { 646 serialManager.requestSerialRight(portId).then(result => { 647 if (!result) { 648 // 没有访问设备的权限且用户不授权则退出 649 console.info('user is not granted the operation permission'); 650 return; 651 } else { 652 console.info('grant permission successfully'); 653 } 654 }); 655} 656 657// 打开设备 658try { 659 serialManager.open(portId) 660 console.info('open usbSerial success, portId: ' + portId); 661} catch (error) { 662 console.error('open usbSerial error, ' + JSON.stringify(error)); 663} 664 665// 异步写入 666let writeBuffer: Uint8Array = new Uint8Array(buffer.from('Hello World', 'utf-8').buffer) 667serialManager.write(portId, writeBuffer, 2000).then((size: number) => { 668 console.info('write usbSerial success, writeBuffer: ' + writeBuffer.toString()); 669}).catch((error: Error) => { 670 console.error('write usbSerial error, ' + JSON.stringify(error)); 671}) 672``` 673 674## serialManager.writeSync 675 676writeSync(portId: number, buffer: Uint8Array, timeout?: number): number 677 678向串口设备同步写数据。 679 680**系统能力:** SystemCapability.USB.USBManager.Serial 681 682**参数:** 683 684| 参数名 | 类型 | 必填 | 说明 | 685|---------|------------|----|------------------| 686| portId | number | 是 | 端口号。 | 687| buffer | Uint8Array | 是 | 写入目标缓冲区。 | 688| timeout | number | 否 | 超时时间(单位:ms),可选参数,默认为0不超时,用户按需选择。| 689 690**返回值:** 691 692| 类型 | 说明 | 693|--------|-------------| 694| number | 返回写入数据长度。 | 695 696**错误码:** 697 698以下错误码的详细介绍参见[USB服务错误码](errorcode-usb.md)。 699 700| 错误码ID | 错误信息 | 701| -------- | ------------------------------------------------------------ | 702| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 703| 31400001 | Serial port management exception. | 704| 31400003 | PortId does not exist. | 705| 31400005 | The serial port device is not opened. Call the open API first. | 706| 31400006 | Data transfer timed out. | 707| 31400007 | I/O exception. Possible causes: 1. The transfer was canceled. 2. The device offered more data than allowed. | 708 709**示例:** 710 711> **说明:** 712> 713> 以下示例代码只是调用writeSync接口的必要流程,需要放入具体的方法中执行。实际调用时,设备开发者需要遵循设备相关协议进行调用。 714 715<!--code_no_check--> 716```ts 717import { JSON } from '@kit.ArkTS'; 718import { serialManager } from '@kit.BasicServicesKit'; 719 720// 获取串口列表 721let portList: serialManager.SerialPort[] = serialManager.getPortList(); 722console.info('usbSerial portList: ' + JSON.stringify(portList)); 723if (portList === undefined || portList.length === 0) { 724 console.info('usbSerial portList is empty'); 725 return; 726} 727let portId: number = portList[0].portId; 728 729// 检测设备是否可被应用访问 730if (!serialManager.hasSerialRight(portId)) { 731 serialManager.requestSerialRight(portId).then(result => { 732 if (!result) { 733 // 没有访问设备的权限且用户不授权则退出 734 console.info('user is not granted the operation permission'); 735 return; 736 } else { 737 console.info('grant permission successfully'); 738 } 739 }); 740} 741 742// 打开设备 743try { 744 serialManager.open(portId) 745 console.info('open usbSerial success, portId: ' + portId); 746} catch (error) { 747 console.error('open usbSerial error, ' + JSON.stringify(error)); 748} 749 750// 同步写入 751let writeSyncBuffer: Uint8Array = new Uint8Array(buffer.from('Hello World', 'utf-8').buffer) 752try { 753 serialManager.writeSync(portId, writeSyncBuffer, 2000); 754 console.info('writeSync usbSerial success, writeSyncBuffer: ' + writeSyncBuffer.toString()); 755} catch (error) { 756 console.error('writeSync usbSerial error, ' + JSON.stringify(error)); 757} 758``` 759 760## serialManager.close 761 762close(portId: number): void 763 764关闭串口。 765 766**系统能力:** SystemCapability.USB.USBManager.Serial 767 768**参数:** 769 770| 参数名 | 类型 | 必填 | 说明 | 771|--------|--------|----|-------------| 772| portId | number | 是 | 端口号。 | 773 774**错误码:** 775 776以下错误码的详细介绍参见[USB服务错误码](errorcode-usb.md)。 777 778| 错误码ID | 错误信息 | 779| -------- | ------------------------------------------------------------ | 780| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 781| 31400001 | Serial port management exception. | 782| 31400003 | PortId does not exist. | 783| 31400005 | The serial port device is not opened. Call the open API first. | 784 785**示例:** 786 787> **说明:** 788> 789> 以下示例代码只是调用close接口的必要流程,需要放入具体的方法中执行。实际调用时,设备开发者需要遵循设备相关协议进行调用。 790 791<!--code_no_check--> 792```ts 793import { JSON } from '@kit.ArkTS'; 794import { serialManager } from '@kit.BasicServicesKit'; 795 796// 获取串口列表 797let portList: serialManager.SerialPort[] = serialManager.getPortList(); 798console.info('usbSerial portList: ' + JSON.stringify(portList)); 799if (portList === undefined || portList.length === 0) { 800 console.info('usbSerial portList is empty'); 801 return; 802} 803let portId: number = portList[0].portId; 804 805// 检测设备是否可被应用访问 806if (!serialManager.hasSerialRight(portId)) { 807 serialManager.requestSerialRight(portId).then(result => { 808 if (!result) { 809 // 没有访问设备的权限且用户不授权则退出 810 console.info('user is not granted the operation permission'); 811 return; 812 } else { 813 console.info('grant permission successfully'); 814 } 815 }); 816} 817 818// 打开设备 819try { 820 serialManager.open(portId) 821 console.info('open usbSerial success, portId: ' + portId); 822} catch (error) { 823 console.error('open usbSerial error, ' + JSON.stringify(error)); 824 return; 825} 826 827// 关闭串口 828try { 829 serialManager.close(portId); 830 console.info('close usbSerial success, portId: ' + portId); 831} catch (error) { 832 console.error('close usbSerial error, ' + JSON.stringify(error)); 833} 834``` 835 836## serialManager.cancelSerialRight 837 838cancelSerialRight(portId: number): void 839 840移除应用程序运行时访问串口设备的权限。此接口会调用close关闭已打开的串口。 841 842**系统能力:** SystemCapability.USB.USBManager.Serial 843 844**参数:** 845 846| 参数名 | 类型 | 必填 | 说明 | 847|--------|--------|----|-------------------------------------| 848| portId | number | 是 | 端口号。 | 849 850**错误码:** 851 852以下错误码的详细介绍参见[USB服务错误码](errorcode-usb.md)。 853 854| 错误码ID | 错误信息 | 855| -------- | ------------------------------------------------------------ | 856| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 857| 14400005 | Database operation exception. | 858| 31400001 | Serial port management exception. | 859| 31400002 | Access denied. Call requestSerialRight to request user authorization first. | 860| 31400003 | PortId does not exist. | 861 862**示例:** 863 864> **说明:** 865> 866> 以下示例代码只是调用cancelSerialRight接口的必要流程,需要放入具体的方法中执行。实际调用时,设备开发者需要遵循设备相关协议进行调用。 867 868<!--code_no_check--> 869```ts 870import { JSON } from '@kit.ArkTS'; 871import { serialManager } from '@kit.BasicServicesKit'; 872 873// 获取串口列表 874let portList: serialManager.SerialPort[] = serialManager.getPortList(); 875console.info('usbSerial portList: ' + JSON.stringify(portList)); 876if (portList === undefined || portList.length === 0) { 877 console.info('usbSerial portList is empty'); 878 return; 879} 880let portId: number = portList[0].portId; 881 882// 检测设备是否可被应用访问 883if (!serialManager.hasSerialRight(portId)) { 884 serialManager.requestSerialRight(portId).then(result => { 885 if (!result) { 886 // 没有访问设备的权限且用户不授权则退出 887 console.info('user is not granted the operation permission'); 888 return; 889 } else { 890 console.info('grant permission successfully'); 891 } 892 }); 893} 894 895// 取消已经授予的权限 896try { 897 serialManager.cancelSerialRight(portId); 898 console.info('cancelSerialRight success, portId: ', portId); 899} catch (error) { 900 console.error('cancelSerialRight error, ', JSON.stringify(error)); 901} 902``` 903 904## SerialAttribute 905 906串口的配置参数。 907 908**系统能力:** SystemCapability.USB.USBManager.Serial 909 910| 名称 | 类型 | 只读 | 可选 | 说明 | 911|----------|--------|----------|-----------|----------------------| 912| baudRate | [BaudRates](#baudrates) | 否 | 否 | 串口波特率。 | 913| dataBits | [DataBits](#databits) | 否 | 是 | 串口数据位,默认值为8位。 | 914| parity | [Parity](#parity) | 否 | 是 | 串口奇偶校验,默认值为None,无奇偶校验。 | 915| stopBits | [StopBits](#stopbits) | 否 | 是 | 串口停止位,默认值为1位。 | 916 917## SerialPort 918 919串口参数。 920 921**系统能力:** SystemCapability.USB.USBManager.Serial 922 923| 名称 | 类型 | 只读 | 可选 | 说明 | 924|--------|--------|------|-------|--------| 925| portId | number | 否 | 否 | 端口号。 | 926| deviceName | string | 否 | 否 | 串口设备名称。 | 927 928## BaudRates 929 930表示波特率的枚举 931 932**系统能力:** SystemCapability.USB.USBManager.Serial 933 934| 名称 | 值 | 说明 | 935|-----------|-----------|-----------| 936| BAUDRATE_50 | 50 | 传输波特率为50 | 937| BAUDRATE_75 | 75 | 传输波特率为75 | 938| BAUDRATE_110 | 110 | 传输波特率为110 | 939| BAUDRATE_134 | 134 | 传输波特率为134 | 940| BAUDRATE_150 | 150 | 传输波特率为150 | 941| BAUDRATE_200 | 200 | 传输波特率为200 | 942| BAUDRATE_300 | 300 | 传输波特率为300 | 943| BAUDRATE_600 | 600 | 传输波特率为600 | 944| BAUDRATE_1200 | 1200 | 传输波特率为1200 | 945| BAUDRATE_1800 | 1800 | 传输波特率为1800 | 946| BAUDRATE_2400 | 2400 | 传输波特率为2400 | 947| BAUDRATE_4800 | 4800 | 传输波特率为4800 | 948| BAUDRATE_9600 | 9600 | 传输波特率为9600 | 949| BAUDRATE_19200 | 19200 | 传输波特率为19200 | 950| BAUDRATE_38400 | 38400 | 传输波特率为38400 | 951| BAUDRATE_57600 | 57600 | 传输波特率为57600 | 952| BAUDRATE_115200 | 115200 | 传输波特率为115200 | 953| BAUDRATE_230400 | 230400 | 传输波特率为230400 | 954| BAUDRATE_460800 | 460800 | 传输波特率为460800 | 955| BAUDRATE_500000 | 500000 | 传输波特率为500000 | 956| BAUDRATE_576000 | 576000 | 传输波特率为576000 | 957| BAUDRATE_921600 | 921600 | 传输波特率为921600 | 958| BAUDRATE_1000000 | 1000000 | 传输波特率为1000000 | 959| BAUDRATE_1152000 | 1152000 | 传输波特率为1152000 | 960| BAUDRATE_1500000 | 1500000 | 传输波特率为1500000 | 961| BAUDRATE_2000000 | 2000000 | 传输波特率为2000000 | 962| BAUDRATE_2500000 | 2500000 | 传输波特率为2500000 | 963| BAUDRATE_3000000 | 3000000 | 传输波特率为3000000 | 964| BAUDRATE_3500000 | 3500000 | 传输波特率为3500000 | 965| BAUDRATE_4000000 | 4000000 | 传输波特率为4000000 | 966 967## DataBits 968 969表示数据位宽的枚举 970 971**系统能力:** SystemCapability.USB.USBManager.Serial 972 973| 名称 | 值 | 说明 | 974|-----------|-----------|-----------| 975| DATABIT_8 | 8 | 报文的有效数据位宽为8比特 | 976| DATABIT_7 | 7 | 报文的有效数据位宽为7比特 | 977| DATABIT_6 | 6 | 报文的有效数据位宽为6比特 | 978| DATABIT_5 | 5 | 报文的有效数据位宽为5比特 | 979 980## Parity 981 982表示校验位的校验方式的枚举 983 984**系统能力:** SystemCapability.USB.USBManager.Serial 985 986| 名称 | 值 | 说明 | 987|-----------|-----------|-----------| 988| PARITY_NONE | 0 | 无校验 | 989| PARITY_ODD | 1 | 奇检验 | 990| PARITY_EVEN | 2 | 偶校验 | 991| PARITY_MARK | 3 | 固定为1 | 992| PARITY_SPACE | 4 | 固定为0 | 993 994## StopBits 995 996表示停止位宽的枚举 997 998**系统能力:** SystemCapability.USB.USBManager.Serial 999 1000| 名称 | 值 | 说明 | 1001|-----------|-----------|-----------| 1002| STOPBIT_1 | 0 | 报文的有效停止位宽为1比特 | 1003| STOPBIT_2 | 1 | 报文的有效停止位宽为2比特 | 1004