久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 5691|回復: 7
打印 上一主題 下一主題
收起左側

我的ModBus主機-UART篇

  [復制鏈接]
跳轉到指定樓層
樓主
ID:446156 發表于 2021-11-15 09:46 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
眾所周知,ModBus從機很好實現,而主機就稍微麻煩一點。下面我將介紹這幾年來我用到的ModBus主機方案,既作為分享又作為一個記錄與總結。
談到ModBus就不得不說UART,UART作為ModBus協議的承載是整個ModBus通信的基礎。
UART的基本收發功能通過文件“UartDebug.c”和“UartDebug.h”來實現,首先來看“UartDebug.h”文件的內容。
  1. #ifndef __UartDebug_H
  2. #define __UartDebug_H

  3. #include "Header.h"
  4. #include "usart.h"

  5. #define BUFMAX 32
  6. #define SLIENTTIME 5

  7. #define RE_DE1 PCout(1)
  8. #define RE_DE2 PFout(8)

  9. struct UartDebugMember
  10. {
  11.     void (*REDE)(uint8_t a);
  12.     UART_HandleTypeDef *Uart;
  13.     uint16_t *UartData;
  14.     uint8_t TransmitBuf[BUFMAX+1];
  15.     uint8_t ReceiveBuf[BUFMAX+1];
  16.     uint8_t RecPointClearEn;
  17.     uint8_t ReceivePoint;
  18.     uint8_t DataTimeCount;
  19.     uint8_t DataTimeCountEn;
  20.     uint8_t ReceiveFinish;
  21. };

  22. extern struct UartDebugMember U_D_Uart2,U_D_Uart3,U_D_Uart4,U_D_Uart7;

  23. void UartDebugInit(void);
  24. void DataReceive(struct UartDebugMember *UDM);
  25. void TimeCountReceive(struct UartDebugMember *UDM);
  26. void ClearRxData(struct UartDebugMember *UDM);
  27. void TransmitData(struct UartDebugMember *UDM,unsigned char *Buf,unsigned char Length);
  28. void SendString(struct UartDebugMember *UDM,char *String);
  29. void RS485_REDE_1(uint8_t a);
  30. void RS485_REDE_2(uint8_t a);
  31. void Null(uint8_t a);

  32. #endif
復制代碼

"Header.h"文件包含了基本的單片機信息,移植代碼的時候只需要將相應的頭文件替換掉就可以了,本章最后會貼出"Header.h"的具體內容。

“#define BUFMAX 32” 設置收發緩沖區的大小;
“#define SLIENTTIME 5” 總線靜默時間閾值,用于判斷該幀數據是否接收完畢;
“#define RE_DE1 PCout(1)”和“#define RE_DE2 PFout(8)”為RS485芯片收發控制IO;

下面將介紹“struct UartDebugMember”結構體成員
“void (*REDE)(uint8_t a);” 該函數指針為RS485芯片收發控制函數;
“UART_HandleTypeDef *Uart;” 使用的UART端口,該成員涉及到底層若更換其他MCU或者使用其他庫函數需要作出相應修改;
“uint16_t *UartData;” 串口接收到的一個字節;
“uint8_t TransmitBuf[BUFMAX+1];” 發送緩沖區;
“uint8_t ReceiveBuf[BUFMAX+1];” 接收緩沖區;
“uint8_t RecPointClearEn;” 接收字節數清零使能;
“uint8_t ReceivePoint;” 接收字節數;
“uint8_t DataTimeCount;” 當前的總線靜默時間
“uint8_t DataTimeCountEn;” 當前的總線靜默時間計時使能
“uint8_t ReceiveFinish;” 幀數據接收完成標志

關于函數我們將在下面的"UartDebug.c"中進行介紹。
  1. #include "UartDebug.h"

  2. struct UartDebugMember U_D_Uart2,U_D_Uart3,U_D_Uart4,U_D_Uart7;

  3. void UartDebugInit(void)
  4. {
  5.         U_D_Uart2.Uart = &huart2;
  6.         U_D_Uart2.UartData = &Rdata_UART2;
  7.         U_D_Uart2.REDE = Null;
  8.         HAL_UART_Receive_IT(U_D_Uart2.Uart,(uint8_t *)U_D_Uart2.UartData,1);
  9.         
  10.         U_D_Uart3.Uart = &huart3;
  11.         U_D_Uart3.UartData = &Rdata_UART3;
  12.         U_D_Uart3.REDE = Null;
  13.         HAL_UART_Receive_IT(U_D_Uart3.Uart,(uint8_t *)U_D_Uart3.UartData,1);
  14.         
  15.         U_D_Uart4.Uart = &huart4;
  16.         U_D_Uart4.UartData = &Rdata_UART4;
  17.         U_D_Uart4.REDE = RS485_REDE_1;
  18.         HAL_UART_Receive_IT(U_D_Uart4.Uart,(uint8_t *)U_D_Uart4.UartData,1);
  19.         
  20.         U_D_Uart7.Uart = &huart7;
  21.         U_D_Uart7.UartData = &Rdata_UART7;
  22.         U_D_Uart7.REDE = RS485_REDE_2;
  23.         HAL_UART_Receive_IT(U_D_Uart7.Uart,(uint8_t *)U_D_Uart7.UartData,1);
  24. }
  25. /*******************************************************************************
  26. *Function Name    : DataReceive
  27. *Input            :
  28. *Return           :
  29. *Description      : 串口接收數據
  30. *******************************************************************************/
  31. void DataReceive(struct UartDebugMember *UDM)
  32. {
  33.         if(UDM->RecPointClearEn)
  34.         {
  35.                 UDM->ReceivePoint=0;
  36.                 UDM->RecPointClearEn=0;
  37.         }
  38.         if(UDM->ReceivePoint<=BUFMAX)
  39.         {
  40.                 UDM->ReceiveBuf[UDM->ReceivePoint]=*(uint8_t *)UDM->UartData;
  41.                 UDM->ReceivePoint++;
  42.         }
  43.         UDM->DataTimeCount=0;
  44.         UDM->DataTimeCountEn=1;
  45.         HAL_UART_Receive_IT(UDM->Uart,(uint8_t *)UDM->UartData,1);
  46. }
  47. /*******************************************************************************
  48. *Function Name    : TimeCountReceive
  49. *Input            :
  50. *Return           :
  51. *Description      : 接收計時
  52. *******************************************************************************/
  53. void TimeCountReceive(struct UartDebugMember *UDM)
  54. {
  55.         if(!UDM->DataTimeCountEn)
  56.         {
  57.                 UDM->DataTimeCount=0;
  58.         }
  59.         /*需要根據波特率以及幀與幀之間的間隔時間調整觸發時間*/
  60.         else if(UDM->DataTimeCount > SLIENTTIME)
  61.         {
  62.                 UDM->ReceiveFinish=1;
  63.                 UDM->DataTimeCountEn=0;
  64.         }
  65.         else
  66.         {
  67.                 UDM->DataTimeCount++;
  68.         }
  69. }
  70. /*******************************************************************************
  71. *Function Name    : UartClearBuffer
  72. *Input            :
  73. *Return           :
  74. *Description      : 清除接收緩沖區
  75. *******************************************************************************/
  76. void ClearRxData(struct UartDebugMember *UDM)
  77. {
  78.         UDM->ReceivePoint=0;
  79.         UDM->RecPointClearEn=1;
  80.         UDM->ReceiveFinish=0;
  81. }
  82. /*******************************************************************************
  83. *Function Name    : TransmitData
  84. *Input            :
  85. *Return           :
  86. *Description      : 串口發送一幀數據
  87. *******************************************************************************/
  88. void TransmitData(struct UartDebugMember *UDM,unsigned char *Buf,unsigned char Length)
  89. {
  90.         unsigned char i;
  91.         UDM->REDE(1);
  92.         for(i=0;i<Length;i++)
  93.         {
  94.                 HAL_UART_Transmit(UDM->Uart,&Buf[i],1,1);
  95.         }
  96.         UDM->REDE(0);
  97.         HAL_UART_Receive_IT(UDM->Uart,(uint8_t *)UDM->UartData,1);
  98. }
  99. /*******************************************************************************
  100. *Function Name    : SendString
  101. *Input            :
  102. *Return           :
  103. *Description      : 串口發送字符串
  104. *******************************************************************************/
  105. void SendString(struct UartDebugMember *UDM,char *String)
  106. {
  107.         UDM->REDE(1);
  108.         while(*String!='\0')
  109.         {
  110.                 HAL_UART_Transmit(UDM->Uart,(uint8_t *)String,1,1);
  111.                 String++;
  112.         }
  113.         UDM->REDE(0);
  114.         HAL_UART_Receive_IT(UDM->Uart,(uint8_t *)UDM->UartData,1);
  115. }
  116. /**/
  117. void RS485_REDE_1(uint8_t a)
  118. {
  119.         if(a)
  120.         {
  121.                 RE_DE1 = 1;
  122.         }
  123.         else
  124.         {
  125.                 RE_DE1 = 0;
  126.         }
  127. }
  128. void RS485_REDE_2(uint8_t a)
  129. {
  130.         if(a)
  131.         {
  132.                 RE_DE2 = 1;
  133.         }
  134.         else
  135.         {
  136.                 RE_DE2 = 0;
  137.         }
  138. }
  139. void Null(uint8_t a)
  140. {

  141. }
復制代碼
“struct UartDebugMember U_D_Uart2,U_D_Uart3,U_D_Uart4,U_D_Uart7;” 因為電路板上使用了usart2、usart3、usart4、usart7,所以需要定義4個相應的UartDebugMember 結構體實體。
“void UartDebugInit(void)” 串口初始化,該函數涉及底層,若使用其他型號單片機或者使用其他庫函數需要作出相應修改。這里使用的是HAL庫。
“void DataReceive(struct UartDebugMember *UDM)” 串口接收函數,該函數需要在串口接收中斷里調用如下所示。
  1. void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
  2. {
  3.   /* NOTE: This function should not be modified, when the callback is needed,
  4.            the HAL_UART_RxCpltCallback can be implemented in the user file
  5.    */
  6.         if(huart == U_D_Uart2.Uart)
  7.         {
  8.                 DataReceive(&U_D_Uart2);
  9.         }
  10.         if(huart == U_D_Uart3.Uart)
  11.         {
  12.                 DataReceive(&U_D_Uart3);
  13.         }
  14.         if(huart == U_D_Uart4.Uart)
  15.         {
  16.                 DataReceive(&U_D_Uart4);
  17.         }
  18.         if(huart == U_D_Uart7.Uart)
  19.         {
  20.                 DataReceive(&U_D_Uart7);
  21.         }
  22. }
復制代碼


該函數的功能一是將接收到的數據存進接收緩沖區;二是將DataTimeCount清零以及將DataTimeCountEn置1,這點很重要。
“void TimeCountReceive(struct UartDebugMember *UDM)”該函數用來計算總線靜默時間和判斷幀數據接收是否完成,該函數需要每隔1ms執行一次,如下所示。
  1. static void Task_1ms(void)
  2. {
  3.     TimeCountReceive(&U_D_Uart2);
  4.     TimeCountReceive(&U_D_Uart3);
  5.     TimeCountReceive(&U_D_Uart4);
  6.     TimeCountReceive(&U_D_Uart7);
  7. }
復制代碼

“void ClearRxData(struct UartDebugMember *UDM)”該函數用來清除接收緩沖區(其實僅清除的接收個數)和接收完成標志,需要在數據處理完成后調用,具體用法會在后續章節中介紹。
“void TransmitData(struct UartDebugMember *UDM,unsigned char *Buf,unsigned char Length)”和“void SendString(struct UartDebugMember *UDM,char *String)”都是發送函數沒什么特別注意的地方。
“void RS485_REDE_1(uint8_t a)”、“void RS485_REDE_2(uint8_t a)”和“void Null(uint8_t a)”都是RS485芯片收發控制的函數,可以在“void UartDebugInit(void)”中看到“U_D_Uart2”和“U_D_Uart3”使用的是函數“Null”而“U_D_Uart4”和“U_D_Uart7”分別使用了函數“void RS485_REDE_1(uint8_t a)”和“void RS485_REDE_2(uint8_t a)”這是因為usart2和usart3連接的是RS232芯片而usart4和usart7連接的是RS485芯片。
至此UART篇就介紹完了,下面是"Header.h"的具體內容。
  1. #ifndef __HEADER_H
  2. #define __HEADER_H

  3. #include "stm32f4xx_hal.h"
  4. #include "gpio_bool-M4.h"

  5. #endif
復制代碼
  1. #ifndef __GPIO_BOOL_H
  2. #define __GPIO_BOOL_H

  3. #include "stm32f4xx_hal.h"


  4. //位帶操作,實現51類似的GPIO控制功能
  5. //具體實現思想,參考<<CM3權威指南>>第五章(87頁~92頁).M4同M3類似,只是寄存器地址變了.
  6. //IO口操作宏定義
  7. #define BITBAND(addr, bitnum) ((addr & 0xF0000000)+0x2000000+((addr &0xFFFFF)<<5)+(bitnum<<2))
  8. #define MEM_ADDR(addr) *((volatile unsigned long *)(addr))
  9. #define BIT_ADDR(addr, bitnum) MEM_ADDR(BITBAND(addr, bitnum))
  10. //IO口地址映射
  11. #define GPIOA_ODR_Addr (GPIOA_BASE+20) //0x40020014
  12. #define GPIOB_ODR_Addr (GPIOB_BASE+20) //0x40020414
  13. #define GPIOC_ODR_Addr (GPIOC_BASE+20) //0x40020814
  14. #define GPIOD_ODR_Addr (GPIOD_BASE+20) //0x40020C14
  15. #define GPIOE_ODR_Addr (GPIOE_BASE+20) //0x40021014
  16. #define GPIOF_ODR_Addr (GPIOF_BASE+20) //0x40021414
  17. #define GPIOG_ODR_Addr (GPIOG_BASE+20) //0x40021814
  18. #define GPIOH_ODR_Addr (GPIOH_BASE+20) //0x40021C14
  19. #define GPIOI_ODR_Addr (GPIOI_BASE+20) //0x40022014
  20. #define GPIOJ_ODR_ADDr (GPIOJ_BASE+20) //0x40022414
  21. #define GPIOK_ODR_ADDr (GPIOK_BASE+20) //0x40022814

  22. #define GPIOA_IDR_Addr (GPIOA_BASE+16) //0x40020010
  23. #define GPIOB_IDR_Addr (GPIOB_BASE+16) //0x40020410
  24. #define GPIOC_IDR_Addr (GPIOC_BASE+16) //0x40020810
  25. #define GPIOD_IDR_Addr (GPIOD_BASE+16) //0x40020C10
  26. #define GPIOE_IDR_Addr (GPIOE_BASE+16) //0x40021010
  27. #define GPIOF_IDR_Addr (GPIOF_BASE+16) //0x40021410
  28. #define GPIOG_IDR_Addr (GPIOG_BASE+16) //0x40021810
  29. #define GPIOH_IDR_Addr (GPIOH_BASE+16) //0x40021C10
  30. #define GPIOI_IDR_Addr (GPIOI_BASE+16) //0x40022010
  31. #define GPIOJ_IDR_Addr (GPIOJ_BASE+16) //0x40022410
  32. #define GPIOK_IDR_Addr (GPIOK_BASE+16) //0x40022810

  33. //IO口操作,只對單一的IO口!
  34. //確保n的值小于16!
  35. #define PAout(n) BIT_ADDR(GPIOA_ODR_Addr,n) //輸出
  36. #define PAin(n) BIT_ADDR(GPIOA_IDR_Addr,n) //輸入

  37. #define PBout(n) BIT_ADDR(GPIOB_ODR_Addr,n) //輸出
  38. #define PBin(n) BIT_ADDR(GPIOB_IDR_Addr,n) //輸入

  39. #define PCout(n) BIT_ADDR(GPIOC_ODR_Addr,n) //輸出
  40. #define PCin(n) BIT_ADDR(GPIOC_IDR_Addr,n) //輸入

  41. #define PDout(n) BIT_ADDR(GPIOD_ODR_Addr,n) //輸出
  42. #define PDin(n) BIT_ADDR(GPIOD_IDR_Addr,n) //輸入

  43. #define PEout(n) BIT_ADDR(GPIOE_ODR_Addr,n) //輸出
  44. #define PEin(n) BIT_ADDR(GPIOE_IDR_Addr,n) //輸入

  45. #define PFout(n) BIT_ADDR(GPIOF_ODR_Addr,n) //輸出
  46. #define PFin(n) BIT_ADDR(GPIOF_IDR_Addr,n) //輸入

  47. #define PGout(n) BIT_ADDR(GPIOG_ODR_Addr,n) //輸出
  48. #define PGin(n) BIT_ADDR(GPIOG_IDR_Addr,n) //輸入

  49. #define PHout(n) BIT_ADDR(GPIOH_ODR_Addr,n) //輸出
  50. #define PHin(n) BIT_ADDR(GPIOH_IDR_Addr,n) //輸入

  51. #define PIout(n) BIT_ADDR(GPIOI_ODR_Addr,n) //輸出
  52. #define PIin(n) BIT_ADDR(GPIOI_IDR_Addr,n) //輸入

  53. #define PJout(n) BIT_ADDR(GPIOJ_ODR_Addr,n) //輸出
  54. #define PJin(n) BIT_ADDR(GPIOJ_IDR_Addr,n) //輸入

  55. #define PKout(n) BIT_ADDR(GPIOK_ODR_Addr,n) //輸出
  56. #define PKin(n) BIT_ADDR(GPIOK_IDR_Addr,n) //輸入

  57. #endif
復制代碼

評分

參與人數 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎勵!

查看全部評分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏11 分享淘帖 頂1 踩
回復

使用道具 舉報

沙發
ID:88060 發表于 2022-1-4 14:14 | 只看該作者
留個腳印
回復

使用道具 舉報

板凳
ID:467334 發表于 2022-1-15 11:26 | 只看該作者
努力學習一下,雖然對初學的我有點難
回復

使用道具 舉報

地板
ID:476652 發表于 2022-1-31 13:34 | 只看該作者
樓主講了這么多,辛苦了!!
若能完美制作一個Modbus RTU主從站,配合著主從站例程講解,大家就能夠更好的理解樓主的Modbus了。
是對大家都科普,也是對樓主知識的驗證和提高!!
畢竟Modbus RTU在單片機里不容易做好的,能把這個做好,還是有2把刷子的,
回復

使用道具 舉報

5#
ID:27536 發表于 2023-5-12 16:45 | 只看該作者
對初學的我有點難,工作需要啊
1
回復

使用道具 舉報

6#
ID:1109866 發表于 2024-1-23 16:31 | 只看該作者
樓主太有心了,寫的這么詳細,對學習很有用
回復

使用道具 舉報

7#
ID:50577 發表于 2024-7-10 15:34 | 只看該作者
對初學的我有點難,請問這是哪個那個型號單片機呢
回復

使用道具 舉報

8#
ID:446156 發表于 2024-11-11 14:30 | 只看該作者
linlin1 發表于 2024-7-10 15:34
對初學的我有點難,請問這是哪個那個型號單片機呢

驅動是不分單片機的
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 亚洲精品视频一区二区三区 | 久久久91精品国产一区二区三区 | 999久久久久久久久6666 | 日韩欧美精品在线 | 成人黄色av网站 | 看a网站| 午夜久久久 | 亚洲一二三区精品 | 草草影院ccyy | 久久三区 | 男女网站视频 | 亚洲免费在线视频 | 精品一二区 | 国产伦精品一区二区三毛 | 午夜理伦三级理论三级在线观看 | 国产一区二区免费 | 国产一区二区电影 | 欧美激情综合色综合啪啪五月 | 69福利影院 | 国精久久 | 国产精品777一区二区 | 九色 在线| 天天爽网站 | 欧美一区二区三区在线观看 | 精品久久久久久久久久久下田 | 男人天堂网址 | 在线欧美亚洲 | 欧美大片久久久 | 国产精品久久久久久久久久三级 | 日韩在线一区二区 | 国产一二区免费视频 | 日本国产精品视频 | 亚州中文字幕 | 成人影院在线视频 | 久久综合av | 国产欧美精品一区 | 亚洲久久在线 | 黄网站在线观看 | 日本中出视频 | 91一区二区三区在线观看 | 成人在线视频一区 |