UART-485-232 =========== 1. **简介** -------------- AIOT-3568A设有R232和485模块,可以通过SO库去调用开启,发送,接受监听数据等功能。 下图介绍R232以及485的位置以及图像介绍 |index| 2. **HDC相关指令** -------------- 查询 R232 信息 hdc命令: .. code-block:: c++ cat dev/ttyS 3. **标准API使用方法** -------------- .. note:: 串口实现不借助API,调用SO库实现串口通信。 4. **社区Demo** -------------- + **简介** 为了帮助开发者更快速的使用板子开发和学习,我们在gitee上提供了示例实现串口短接收发、设备间收发、信息监听功能。,每一个项目都是独立的DevEco Studio工程,开发者可以将工程导入到DevEco Studio中即可,通过浏览代码、编译工程、安装和运行应用示例来了解应用示例中涉及API的使用方法。 .. seealso:: `giteeCamera示例 `_ + **Demo主要实现源码** RX232.ets .. code-block:: TypeScript :linenos: import native_serialport from 'libnative_serialport.so' import { Module, FunctionItem } from "../widget/Base" import { BusinessError } from '@ohos.base'; import Logger from '../widget/Logger'; @Entry @Component struct Index { @State message: string = '串口接受数据' private TAG: string = 'serialTools' //@State message: string = 'Hello World' @State speedValue: number = 115200; @State COM_DEV_TTY: string = 'dev/'+'' @State data: string = '' modules: Module[] = [] eachModuleWidth: number = 0; openSerial() { native_serialport.openSerial(this.COM_DEV_TTY) Logger.debug(this.TAG, `native_serialport.openSerial succ`) Logger.debug(this.TAG, `native_serialport.openSerial abc`) } setOptions() { let bits = 8; let events = 0; let stops = 1; native_serialport.setOptions(this.COM_DEV_TTY, this.speedValue, bits, events, stops) Logger.debug(this.TAG, ` native_serialport.setOptions succ`) Logger.debug(this.TAG, ` this.COM_DEV_TTY ` + this.COM_DEV_TTY); Logger.debug(this.TAG, ` this.speedValue ` + this.speedValue); } closeSerial() { native_serialport.closeSerial(this.COM_DEV_TTY) Logger.debug(this.TAG, `native_serialport.closeSerial succ`) Logger.debug(this.TAG, ` this.COM_DEV_TTY ` + this.COM_DEV_TTY); } sendData() { let data = new Uint8Array([0x01, 0x01, 0x00, 0x0a, 0x01, 0x03, 0x5c, 0x09]); native_serialport.sendData(this.COM_DEV_TTY, data) Logger.debug(this.TAG, `native_serialport.sendData succ`) } async transmit() { let min = 10; let data = new Uint8Array([0x01]); let str = "" try { Logger.debug(this.TAG, ` this.COM_DEV_TTY ` + this.COM_DEV_TTY); let d: string = await native_serialport.transmit(this.COM_DEV_TTY, data, 1, min); // Logger.debug(this.TAG, ` this.COM_DEV_TTY ` + this.COM_DEV_TTY); Logger.debug(this.TAG, `native_serialport transmit type:${typeof d}`) for (let i = 0; i < d.length; i++) { str = str + d[i] + ','; // String.fromCharCode(data[i]); this.data = this.data + '\n' + str; } // str = str.substring(2, str.length - 2); // str = this.hexToString(str); } catch (e) { Logger.error(this.TAG, `native_serialport transmit catch:${JSON.stringify(e)}`) } Logger.debug(this.TAG, `native_serialport transmit recv data:${str}`) } on() { native_serialport.on(this.COM_DEV_TTY, (data: Uint8Array) => { Logger.debug(this.TAG,` serialport.on ttyS7 recv length:${data.length}`) let str = "" for (let i = 0; i < data.length; i++) { // String.fromCharCode(data[i]); str = str + data[i].toString(16).padStart(2, '0') + ','; //String.fromCharCode(data[i]); if (i % 100 == 99) { Logger.debug(this.TAG, `serial ttyS7 recv:${str}`) str = ""; } } // str = str.substring(2, str.length - 2); // str = this.hexToString(str); Logger.debug(this.TAG, `serial ttyS7 recv:${str}`) this.data = this.data + '\n' + str; Logger.debug(this.TAG, `serial ttyS7 recv: this.data :` + this.data) }) Logger.debug(this.TAG, ` native_serialport.on ...`) } off() { native_serialport.off(this.COM_DEV_TTY) Logger.debug(` native_serialport off()`) } hexToString(hex : string) { let string = ''; for (let i = 0; i < hex.length; i += 2) { string += String.fromCharCode(parseInt(hex.substr(i, 2), 16)); } return string; } build() { Row() { Scroll() { Column() { Text(this.data) .fontSize(25) .fontWeight(FontWeight.Bold) } .width('45%') .height('90%') .borderRadius(15) .backgroundColor(Color.White) .alignItems(HorizontalAlign.Center) .justifyContent(FlexAlign.SpaceEvenly) } Column() { Row() { Text('串口号 :') TextInput({ text: this.COM_DEV_TTY }) .width(200) .onChange((value: string) => { this.COM_DEV_TTY = value; Logger.debug(this.TAG, 'this.COM_DEV_TTY :' + this.COM_DEV_TTY); }) }.padding(15) Row() { Text('端口号 :') TextInput({ text: this.speedValue.toString() }) .width(200) .onChange((value: string) => { this.speedValue = Number(value); Logger.debug(this.TAG, 'this.speedValue :' + this.speedValue); }) }.padding(15) Button('打开串口') .onClick(() => { this.openSerial(); }) Button('设置串口') .onClick(() => { this.setOptions(); }) Button('发生数据') .onClick(() => { this.sendData(); }) Button('收发数据') .onClick(() => { this.transmit(); Logger.debug(this.TAG,'transmit :' +this.COM_DEV_TTY ) }) Button('数据监听') .onClick(() => { this.on(); }) Button('关闭监听') .onClick(() => { this.off(); Logger.debug(this.TAG,'transmit :' +this.COM_DEV_TTY ) }) Button('清除打印') .onClick(() => { this.data = '' }) } .width('45%') .height('90%') .borderRadius(15) .backgroundColor(Color.White) .alignItems(HorizontalAlign.Center) .justifyContent(FlexAlign.SpaceEvenly) } .justifyContent(FlexAlign.SpaceEvenly) .padding(50) .backgroundColor($r('app.color.main_bg')) .height('100%') .width('100%') } } 5. **代码编译** -------------- .. tip:: 代码编译详细流程可见,:doc:`Hello World应用以及部署<../../Quick-Start/OpenHarmony/04-应用以及部署>` 中的第二部分(构建第一个页面部分内容) 6. **代码运行效果** -------------- |RX232| .. |RX232| image:: media1/RX232.png :width: 5.75in :height: 3.92708in .. |index| image:: media1/index.png :width: 5.75in :height: 3.92708in