1# HiSpark WiFi-IoT 鸿蒙套件样例开发--UART 2 3 4 5[HiSpark WiFi-IoT鸿蒙开发套件](https://item.taobao.com/item.htm?id=622343426064&scene=taobao_shop) 首发于HDC 2020,是首批支持HarmonyOS 2.0的开发套件,亦是鸿蒙官方推荐套件,由润和软件HiHope量身打造,已在鸿蒙社区和广大鸿蒙开发者中得到广泛应用。 6 7 8 9## 一、UART API 10 11使用下面的API实现UART读写 12 13#### 鸿蒙UART相关的API接口文件路径 14 15**base/iot_hardware/interfaces/kits/wifiiot_lite/wifiiot_uart.h** 16 17所使用的API接口有: 18 19| API | 接口说明 | 20| ------------------------------------------------------------ | ------------------------ | 21| unsigned int UartInit(WifiIotUartIdx id, const WifiIotUartAttribute *param, const WifiIotUartExtraAttr *extraAttr); | 初始化,配置一个UART设备 | 22| int UartRead(WifiIotUartIdx id, unsigned char *data, unsigned int dataLen) | 从UART设备中读取数据 | 23| int UartWrite(WifiIotUartIdx id, const unsigned char *data, unsigned int dataLen) | 将数据写入UART设备 | 24 25#### 26 27## 二、代码分析 28 29首先需要使能UART2 30 31vendor\hisi\hi3861\hi3861\build\config\usr_config.mk 32 33配置CONFIG_UART2_SUPPORT=y 34 35或者调用GPIO相关的API将串口所在的GPIO口设置为UART功能 36 37```c 38static void* UartDemo_Task(const char* arg) 39 40{ 41 unsigned int len = 0; 42 43 (void)arg; 44 printf("[UartDemo] UartDemo_Task()\n"); 45 46 GpioInit();//使用GPIO,都需要调用该接口 47 48 printf("UART init...\r\n"); 49 //UART参数配置 50 usr_uart_config(); 51 52 UartWrite(WIFI_IOT_UART_IDX_2, (unsigned char *)uartWriteBuff, sizeof(uartWriteBuff)); 53 while(1) 54 { 55 //读取UART2的数据 56 len = UartRead(WIFI_IOT_UART_IDX_2, uartReadBuff, sizeof(uartReadBuff)); 57 if (len > 0) { 58 printf("Uart read data:%s", uartReadBuff); 59 } 60 usleep(100000); 61 } 62 63 return NULL; 64} 65 66``` 67 68 69 70## 三、如何编译 71 721. 将此目录下的 `uart_demo.c` 和 `BUILD.gn` 复制到openharmony源码的`applications\sample\wifi-iot\app\iothardware`目录下, 732. 修改openharmony源码的`applications\sample\wifi-iot\app\BUILD.gn`文件,将其中的 `features` 改为: 74 75``` 76 features = [ 77 "iothardware:uart_demo", 78 ] 79``` 80 813. 在openharmony源码顶层目录执行:`python build.py wifiiot` 82 83## 四、运行结果 84 85``` 86##### 串口0 87RX:ready to OS start 88 89sdk ver:Hi3861V100R001C00SPC025 2020-09-03 18:10:00 90 91FileSystem mount ok. 92wifi init success! 93[UartDemo] UartDemo_Entry() 94 95[UartDemo] UartDemo_Task() 96UART init... 97 9800 00:00:00 0 132 D 0/HIVIEW: hilog init success. 9900 00:00:00 0 132 D 0/HIVIEW: log limit init success. 10000 00:00:00 0 132 I 1/SAMGR: Bootstrap core services(count:3). 10100 00:00:00 0 132 I 1/SAMGR: Init service:0x4ae958 TaskPool:0xe5238 10200 00:00:00 0 132 I 1/SAMGR: Init service:0x4ae964 TaskPool:0xe5258 10300 00:00:00 0 132 I 1/SAMGR: Init service:0x4aea8c TaskPool:0xe5278 10400 00:00:00 0 8 I 1/SAMGR: Init service 0x4ae964 <time: 0ms> success! 10500 00:00:00 0 164 I 1/SAMGR: Init service 0x4ae958 <time: 0ms> success! 10600 00:00:00 0 108 D 0/HIVIEW: hiview init success. 10700 00:00:00 0 108 I 1/SAMGR: Init service 0x4aea8c <time: 10ms> success! 10800 00:00:00 0 108 I 1/SAMGR: Initialized all core system services! 10900 00:00:00 0 164 I 1/SAMGR: Bootstrap system and application services(count:0). 11000 00:00:00 0 164 I 1/SAMGR: Initialized all system and application services! 11100 00:00:00 0 164 I 1/SAMGR: Bootstrap dynamic registered services(count:0). 112 113RX:Uart read data:hello 114 115RX:Uart read data:12345678 116 117 118 119##### 串口2 120 121RX:hello uart! 122 123TX:hello 124 125TX:12345678 126``` 127 128 129 130 131 132### 【套件支持】 133 134##### 1. 套件介绍 http://www.hihope.org/pro/pro1.aspx?mtt=8 135 136##### 2. 套件购买 https://item.taobao.com/item.htm?id=622343426064&scene=taobao_shop 137 138##### 3. 技术资料 139 140- Gitee码云网站(OpenHarmony Sample Code等) **https://gitee.com/hihopeorg** 141 142- HiHope官网-资源中心(SDK包、技术文档下载)**http://www.hihope.org/** 143 144##### 4. 互动交流 145 146- 润和HiHope鸿蒙技术交流-微信群(加群管理员微信13605188699,发送文字#申请加入润和官方鸿蒙群#,予以邀请入群) 147- HiHope开发者社区-论坛 **https://bbs.elecfans.com/group_1429** 148- 润和HiHope鸿蒙售后服务群(QQ:980599547) 149- 售后服务电话(025-52668590) 150