OpenHarmony开发13
OpenHarmony开发13
-
安装realterm
-
在下载链接,在不接这个USB硬件的情况下,安装此驱动
-
重启电脑,可以在端口中看到这个设备,说明安装成功
-
拔下USB,用电源跳线连接3.3v
-
插上usb,打开realterm
-
按照如图配置,切换到send界面,输入123或者abc之类的,之后选择发送acsII,可以正确在上面显示
-
新建一个txt文件,录入
12 34 56
-
选择文件录入,如图配置
-
使用
winHex
配置16进制文件,大小为16byte -
如图配置,让数据间隔一秒发送
-
将usb的tx和gnd分别与hispark板子的rx和gnd连接
-
测试运行,并且修改
hello_world
的部分代码,让我们能成功按照16进制输出
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
//#include "wifiiot_errno.h"
//#include "wifiiot_gpio.h"
//#include "wifiiot_gpio_ex.h"
//#include "wifiiot_adc.h"
//#include "wifiiot_uart.h"
#include "iot_errno.h"
#include "iot_gpio.h"
#include "iot_uart.h"
#define UART_TASK_STACK_SIZE 1024 * 8
#define UART_TASK_PRIO 25
#define UART_BUFF_SIZE 1000
#define WIFI_IOT_UART_IDX_1 1
static const char *data = "Hello, Hispark!\r\n";
static void UART_Task(void)
{
uint8_t uart_buff[UART_BUFF_SIZE] = {0};
uint8_t *uart_buff_ptr = uart_buff;
uint32_t ret;
//WifiIotUartAttribute uart_attr = {
IotUartAttribute uart_attr = {
//baud_rate: 9600
.baudRate = 9600,
//data_bits: 8bits
.dataBits = 8,
.stopBits = 1,
.parity = 0,
};
//Initialize uart driver
//ret = IoTUartInit(WIFI_IOT_UART_IDX_1, &uart_attr, NULL);
ret = IoTUartInit(WIFI_IOT_UART_IDX_1, &uart_attr);
if (ret != IOT_SUCCESS)
{
printf("Failed to init uart! Err code = %d\n", ret);
return;
}
printf("UART Test Start\n");
while (1)
{
printf("=======================================\r\n");
printf("*************UART_example**************\r\n");
printf("=======================================\r\n");
//通过串口1发送数据
IoTUartWrite(WIFI_IOT_UART_IDX_1, (unsigned char *)data, strlen(data));
//通过串口1接收数据
IoTUartRead(WIFI_IOT_UART_IDX_1, uart_buff_ptr, UART_BUFF_SIZE);
int length = strlen(uart_buff_ptr);
printf("Uart read data:\n");
for(int i=0;i<length;i++){
printf("%.2X ",uart_buff_ptr[i]);
}
printf("\n");
//printf("Uart1 read data:%x", uart_buff_ptr);
usleep(500000);
}
}
void HelloWorld(void)
{
osThreadAttr_t attr;
attr.name = "UART_Task";
attr.attr_bits = 0U;
attr.cb_mem = NULL;
attr.cb_size = 0U;
attr.stack_mem = NULL;
attr.stack_size = UART_TASK_STACK_SIZE;
attr.priority = UART_TASK_PRIO;
if (osThreadNew((osThreadFunc_t)UART_Task, NULL, &attr) == NULL)
{
printf("[ADCExample] Falied to create UART_Task!\n");
}
}
//SYS_RUN(HelloWorld);
APP_FEATURE_INIT(HelloWorld);