2020-10-08

    科技2026-03-14  4

    鸿蒙OS Hi3861开发板平台串口通信

    利用鸿蒙系统原有的代码,可以通过printf()接口实现串口打印的功能,但是如果需要电脑与开发板平台实现双向通信,由于官方并没有实现scanf()接口的功能,所以需要额外增加部分代码实现该功能。这里利用开发板的串口2来实现串口收发功能。

    开发准备

    串口2涉及到的IO口有GPIO11和GPIO12,在这里我利用的是底板上本来用于OLED显示屏模块的部分,如图所示: 其中绿色部分就是GPIO11和GPIO12。 另外,还需要准备一个CH340串口模块,与板子上的上述两个接口和GND相连。

    代码部分

    头文件方面,需要include的有以下几个:

    #include <stdio.h> #include <string.h> #include <unistd.h> #include "ohos_init.h" #include "cmsis_os2.h" #include "wifiiot_uart.h" #include "wifiiot_uart_ex.h" #include "wifiiot_gpio.h" #include "wifiiot_gpio_ex.h"

    其中,wifiiot_gpio.h和wifiiot_gpio_ex.h用于提供初始化串口2对应的IO口的接口,wifiiot_uart.h和wifiiot_uart_ex.h用于提供初始化串口2相关的接口。

    初始化

    参考头文件对应的函数接口形式,首先完成对GPIO11和GPIO12两个IO口的初始化:

    GpioInit(); IoSetFunc(WIFI_IOT_IO_NAME_GPIO_11, WIFI_IOT_IO_FUNC_GPIO_11_UART2_TXD); IoSetFunc(WIFI_IOT_IO_NAME_GPIO_12, WIFI_IOT_IO_FUNC_GPIO_12_UART2_RXD);

    接下来是对串口2相关参数的初始化,其中波特率设置为115200,8位数据位,1位停止位,无奇偶校验位;接收和发送缓冲区大小均设置为1字节,不启用DMA:

    WifiIotUartAttribute uart2; uart2.baudRate = 115200; uart2.dataBits = WIFI_IOT_UART_DATA_BIT_8; uart2.pad = 0; uart2.parity = WIFI_IOT_UART_PARITY_NONE; uart2.stopBits = WIFI_IOT_UART_STOP_BIT_1; WifiIotUartExtraAttr uart2_a; uart2_a.txFifoLine = WIFI_IOT_FIFO_LINE_HALF; uart2_a.rxFifoLine = WIFI_IOT_FIFO_LINE_HALF; uart2_a.flowFifoLine = WIFI_IOT_FIFO_LINE_HALF; uart2_a.txBlock = WIFI_IOT_UART_BLOCK_STATE_NONE_BLOCK; uart2_a.rxBlock = WIFI_IOT_UART_BLOCK_STATE_NONE_BLOCK; uart2_a.txBufSize = 1; uart2_a.rxBufSize = 1; uart2_a.txUseDma = WIFI_IOT_UART_NONE_DMA; uart2_a.rxUseDma = WIFI_IOT_UART_NONE_DMA; UartInit(WIFI_IOT_UART_IDX_2, &uart2, &uart2_a);

    主循环

    在主循环种,我实现的是以一定的周期判断接收缓存区是否为空,如果不为空则读入数据,如果数据是0x31(字符‘1’)则将内置的标志变量flag置为1。主循环部分实现如下:

    while (1) { UartIsBufEmpty(WIFI_IOT_UART_IDX_2, &isEmpty); printf("isEmpty = %d\n", isEmpty); if (!isEmpty){ int j = UartRead(WIFI_IOT_UART_IDX_2, data, 1); printf("UartRead = %d\n", j); printf("data = %s\n", data[0]); if (data[0] == 0x31) { flag = 1; } } usleep(1000*500); }

    完整代码

    整个文件的代码如下:

    #include <stdio.h> #include <string.h> #include <unistd.h> #include "ohos_init.h" #include "cmsis_os2.h" #include "wifiiot_uart.h" #include "wifiiot_uart_ex.h" #include "wifiiot_gpio.h" #include "wifiiot_gpio_ex.h" unsigned char flag = 1; static void My_Uart_Task(void *arg) { (void)arg; unsigned char isEmpty = 0; unsigned char data[10]= {0}; GpioInit(); IoSetFunc(WIFI_IOT_IO_NAME_GPIO_11, WIFI_IOT_IO_FUNC_GPIO_11_UART2_TXD); IoSetFunc(WIFI_IOT_IO_NAME_GPIO_12, WIFI_IOT_IO_FUNC_GPIO_12_UART2_RXD); WifiIotUartAttribute uart2; uart2.baudRate = 115200; uart2.dataBits = WIFI_IOT_UART_DATA_BIT_8; uart2.pad = 0; uart2.parity = WIFI_IOT_UART_PARITY_NONE; uart2.stopBits = WIFI_IOT_UART_STOP_BIT_1; WifiIotUartExtraAttr uart2_a; uart2_a.txFifoLine = WIFI_IOT_FIFO_LINE_HALF; uart2_a.rxFifoLine = WIFI_IOT_FIFO_LINE_HALF; uart2_a.flowFifoLine = WIFI_IOT_FIFO_LINE_HALF; uart2_a.txBlock = WIFI_IOT_UART_BLOCK_STATE_NONE_BLOCK; uart2_a.rxBlock = WIFI_IOT_UART_BLOCK_STATE_NONE_BLOCK; uart2_a.txBufSize = 1; uart2_a.rxBufSize = 1; uart2_a.txUseDma = WIFI_IOT_UART_NONE_DMA; uart2_a.rxUseDma = WIFI_IOT_UART_NONE_DMA; UartInit(WIFI_IOT_UART_IDX_2, &uart2, &uart2_a); while (1) { UartIsBufEmpty(WIFI_IOT_UART_IDX_2, &isEmpty); printf("isEmpty = %d\n", isEmpty); if (!isEmpty){ int j = UartRead(WIFI_IOT_UART_IDX_2, data, 1); // printf("UartRead = %d\n", j); printf("data = %s\n", data[0]); if (data[0] == 0x31) { flag = 1; } } usleep(1000*500); } } static void My_Uart_Ctr(void) { osThreadAttr_t attr; attr.name = "My_Uart_Ctr"; attr.attr_bits = 0U; attr.cb_mem = NULL; attr.cb_size = 0U; attr.stack_mem = NULL; attr.stack_size = 10240; attr.priority = osPriorityNormal; if (osThreadNew(My_Uart_Task, NULL, &attr) == NULL) { printf("[My_Uart_Ctr] Falied to create My_Uart_Task!\n"); } } APP_FEATURE_INIT(My_Uart_Ctr);

    BUILD.gn设置

    整个代码放在源码目录的//applications/sample/wifi-iot/app/iothardware/目录下,修改该目录的BUILD.gn为:

    static_library("my_uart_ctr") { sources = [ "my_uart_ctr.c", ] include_dirs = [ "//utils/native/lite/include", "//kernel/liteos_m/components/cmsis/2.0", "//base/iot_hardware/interfaces/kits/wifiiot_lite", ] }

    修改//applications/sample/wifi-iot/app/目录下的BUILD.gn为:

    import("//build/lite/config/component/lite_component.gni") lite_component("app") { features = [ "iothardware:my_uart_ctr", ] }

    最后编译并运行,打开两个串口调试助手,一个连接串口0对应的串口设备,一个连接串口2对应的串口设备,向串口2发送字符1,即可在另一个串口调试助手上看到收到的字符。

    Processed: 0.011, SQL: 12