Gitee

https://gitee.com/WangXi_Chn/RttOs_ModuleLib

开发环境

CubeMX
MDK5 IDE
STM32F103C8T6 芯片
Stm32最小系统板
RT-Thread操作系统
面向对象模块接口设计
陶晶驰HMI串口屏
串口屏使用教程

硬件条件

  • 串口屏界面设计工作完成
  • 已知串口屏界面控件对象名称
  • STM32与屏幕串口连接正确

    核心思想

  • 驱动串口

  • 可正常发送指定字符串和帧结束符

    源文件

    Module_UartHMI.h

    ```c /*
    • Copyright (c) 2020 - ~, HIT_HERO Team *
    • UART HMI MODULE HEAD FILE
    • Used in RT-Thread Operate System *
    • Change Logs:
    • Date Author Notes Mail
    • 2020-09-06 WangXi first version WangXi_chn@foxmail.com */

      ifndef MODULE_UARTHMI

      define MODULE_UARTHMI

include

include

include

struct _MODULE_UARTHMI { / Property / char * Property_UartDevName;

  1. /* Value */
  2. rt_device_t Value_uart_dev;
  3. /* Method */
  4. void (*Method_Init)(struct _MODULE_UARTHMI *module);
  5. 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**/

  1. <a name="3JXqt"></a>
  2. ## Module_UartHMI.c
  3. ```c
  4. /*
  5. * Copyright (c) 2020 - ~, HIT_HERO Team
  6. *
  7. * UART HMI MODULE SOUCE FILE
  8. * Used in RT-Thread Operate System
  9. *
  10. * Change Logs:
  11. * Date Author Notes Mail
  12. * 2020-09-06 WangXi first version WangXi_chn@foxmail.com
  13. */
  14. #include "Module_UartHMI.h"
  15. #include <string.h>
  16. static void Module_UartHMIInit(MODULE_UARTHMI *module);
  17. static void Module_UartHMISend(MODULE_UARTHMI *module,char* string);
  18. /* Global Method */
  19. void Module_UartHMI_Config(MODULE_UARTHMI *module)
  20. {
  21. if( module->Method_Init == NULL &&
  22. module->Method_Send == NULL
  23. ){
  24. /* Link the Method */
  25. module->Method_Init = Module_UartHMIInit;
  26. module->Method_Send = Module_UartHMISend;
  27. }
  28. else{
  29. rt_kprintf("Warning: Module uart HMI is Configed twice\n");
  30. return;
  31. }
  32. /* Device Init */
  33. module->Method_Init(module);
  34. return;
  35. }
  36. static void Module_UartHMIInit(MODULE_UARTHMI *module)
  37. {
  38. module->Value_uart_dev = rt_device_find(module->Property_UartDevName);
  39. if (!module->Value_uart_dev)
  40. {
  41. rt_kprintf("find %s failed!\t\n", module->Value_uart_dev);
  42. return;
  43. }
  44. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT; /* 初始化配置参数 */
  45. /* step2:修改串口配置参数 */
  46. config.baud_rate = BAUD_RATE_9600; //修改波特率为 9600
  47. config.data_bits = DATA_BITS_8; //数据位 8
  48. config.stop_bits = STOP_BITS_1; //停止位 1
  49. config.bufsz = 128; //修改缓冲区 buff size 为 128
  50. config.parity = PARITY_NONE; //无奇偶校验位
  51. /* step3:控制串口设备。通过控制接口传入命令控制字,与控制参数 */
  52. rt_device_control(module->Value_uart_dev, RT_DEVICE_CTRL_CONFIG, &config);
  53. rt_device_open(module->Value_uart_dev, RT_DEVICE_FLAG_DMA_RX);
  54. /* make sure communicate with HMI successfully */
  55. rt_thread_mdelay(200);
  56. rt_uint8_t endsign[3] = {0xff,0xff,0xff};
  57. rt_device_write( module->Value_uart_dev, 0,
  58. endsign,
  59. sizeof(endsign));
  60. rt_thread_mdelay(200);
  61. }
  62. static void Module_UartHMISend(MODULE_UARTHMI *module,char* string)
  63. {
  64. rt_device_write( module->Value_uart_dev, 0,
  65. string,
  66. strlen(string));
  67. rt_thread_mdelay(1);
  68. rt_uint8_t endsign[3] = {0xff,0xff,0xff};
  69. rt_device_write( module->Value_uart_dev, 0,
  70. endsign,
  71. sizeof(endsign));
  72. }
  73. /************************ (C) COPYRIGHT 2020 WANGXI **************END OF FILE****/

使用

  1. ...
  2. dev_UartHMI.Property_UartDevName = "uart3";
  3. ...
  4. ...
  5. Module_UartHMI_Config(&(dev_UartHMI));
  6. ...
  7. char str[50];
  8. sprintf(str, "tempreture.txt=\"%d\"", data);
  9. dev_UartHMI.Method_Send(&dev_UartHMI,str);
  • 先使用sprintf函数将数据转换成字符串内容
  • 再通过发送方法从串口发送