Gitee
https://gitee.com/WangXi_Chn/RttOs_ModuleLib
开发环境
CubeMX
MDK5 IDE
STM32F103C8T6 芯片
Stm32最小系统板
RT-Thread操作系统
面向对象模块接口设计
陶晶驰HMI串口屏
串口屏使用教程
硬件条件
include
include
include
struct _MODULE_UARTHMI { / Property / char * Property_UartDevName;
/* Value */
rt_device_t Value_uart_dev;
/* Method */
void (*Method_Init)(struct _MODULE_UARTHMI *module);
void (*Method_Send)(struct _MODULE_UARTHMI *module,char* string);
}; typedef struct _MODULE_UARTHMI MODULE_UARTHMI;
void Module_UartHMI_Config(MODULE_UARTHMI *module);
endif
/** (C) COPYRIGHT 2020 WANGXI **END OF FILE**/
<a name="3JXqt"></a>
## Module_UartHMI.c
```c
/*
* Copyright (c) 2020 - ~, HIT_HERO Team
*
* UART HMI MODULE SOUCE FILE
* Used in RT-Thread Operate System
*
* Change Logs:
* Date Author Notes Mail
* 2020-09-06 WangXi first version WangXi_chn@foxmail.com
*/
#include "Module_UartHMI.h"
#include <string.h>
static void Module_UartHMIInit(MODULE_UARTHMI *module);
static void Module_UartHMISend(MODULE_UARTHMI *module,char* string);
/* Global Method */
void Module_UartHMI_Config(MODULE_UARTHMI *module)
{
if( module->Method_Init == NULL &&
module->Method_Send == NULL
){
/* Link the Method */
module->Method_Init = Module_UartHMIInit;
module->Method_Send = Module_UartHMISend;
}
else{
rt_kprintf("Warning: Module uart HMI is Configed twice\n");
return;
}
/* Device Init */
module->Method_Init(module);
return;
}
static void Module_UartHMIInit(MODULE_UARTHMI *module)
{
module->Value_uart_dev = rt_device_find(module->Property_UartDevName);
if (!module->Value_uart_dev)
{
rt_kprintf("find %s failed!\t\n", module->Value_uart_dev);
return;
}
struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT; /* 初始化配置参数 */
/* step2:修改串口配置参数 */
config.baud_rate = BAUD_RATE_9600; //修改波特率为 9600
config.data_bits = DATA_BITS_8; //数据位 8
config.stop_bits = STOP_BITS_1; //停止位 1
config.bufsz = 128; //修改缓冲区 buff size 为 128
config.parity = PARITY_NONE; //无奇偶校验位
/* step3:控制串口设备。通过控制接口传入命令控制字,与控制参数 */
rt_device_control(module->Value_uart_dev, RT_DEVICE_CTRL_CONFIG, &config);
rt_device_open(module->Value_uart_dev, RT_DEVICE_FLAG_DMA_RX);
/* make sure communicate with HMI successfully */
rt_thread_mdelay(200);
rt_uint8_t endsign[3] = {0xff,0xff,0xff};
rt_device_write( module->Value_uart_dev, 0,
endsign,
sizeof(endsign));
rt_thread_mdelay(200);
}
static void Module_UartHMISend(MODULE_UARTHMI *module,char* string)
{
rt_device_write( module->Value_uart_dev, 0,
string,
strlen(string));
rt_thread_mdelay(1);
rt_uint8_t endsign[3] = {0xff,0xff,0xff};
rt_device_write( module->Value_uart_dev, 0,
endsign,
sizeof(endsign));
}
/************************ (C) COPYRIGHT 2020 WANGXI **************END OF FILE****/
使用
...
dev_UartHMI.Property_UartDevName = "uart3";
...
...
Module_UartHMI_Config(&(dev_UartHMI));
...
char str[50];
sprintf(str, "tempreture.txt=\"%d\"", data);
dev_UartHMI.Method_Send(&dev_UartHMI,str);
- 先使用sprintf函数将数据转换成字符串内容
- 再通过发送方法从串口发送