当前位置:   article > 正文

Bearpi开发板HarmonyOS之UART读写_harmonyos串口读写

harmonyos串口读写

wifiiot_uart.h中包含声明UART接口函数

  • 初始化UART
    unsigned int UartInit(WifiIotUartIdx id, const WifiIotUartAttribute *param, const WifiIotUartExtraAttr *extraAttr);
  • 取消UART初始化
    unsigned int UartDeinit(WifiIotUartIdx id);
  • 从UART读取数据
    int UartRead(WifiIotUartIdx id, unsigned char *data, unsigned int dataLen);
  • 将数据写入UART
    int UartWrite(WifiIotUartIdx id, const unsigned char *data, unsigned int dataLen);
  • 设置UART流控制
    unsigned int UartSetFlowCtrl(WifiIotUartIdx id, WifiIotFlowCtrl flowCtrl);

uart读写测试代码,需要将GPIO5和GPIO6短接进行uart数据自发自收。

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

static const char *data = "Hello, BearPi!\r\n";
#define UART_BUFF_SIZE 1024

static void uart_task(void)
{
  uint8_t uart_buffer[UART_BUFF_SIZE] = {0};
  uint8_t *pData = uart_buffer;
  uint32_t ret;
  WifiIotUartAttribute attr = {
    .baudRate = 115200,
    .dataBits = 8,
    .stopBits = 1,
    .parity = 0,
    .pad = 0,
  };

  ret = UartInit(WIFI_IOT_UART_IDX_1,&attr,NULL);
  if(ret != WIFI_IOT_SUCCESS)
  {
    printf("Failed to init uart! Err code = %d\n", ret);
    return;
  }
   printf("UART Test Start\n");
  while(1)
  {
      UartWrite(WIFI_IOT_UART_IDX_1,(uint8_t *)data,strlen(data));

      UartRead(WIFI_IOT_UART_IDX_1,pData,UART_BUFF_SIZE);
      printf("read data from uart1============>\r\n %s",pData);

      osDelay(100);
  }

}

static void my_led_example(void)
{
   
    
     osThreadAttr_t attr;
     attr.attr_bits = 0;
     attr.name = "uart";
     attr.cb_mem = NULL;
     attr.cb_size = 0;
     attr.priority = 24;
     attr.stack_size = 1024*8;

     if(osThreadNew((osThreadFunc_t)uart_task,NULL,&attr) == NULL)
     {
        printf("Falied to create adc_func!\n");
    }
}

SYS_RUN(my_led_example);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 运行效果
    在这里插入图片描述
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/302946
推荐阅读
相关标签
  

闽ICP备14008679号