# 面向单独 UART 的封装的 FreeModbus **Repository Path**: mgstudio/FreeModbus ## Basic Information - **Project Name**: 面向单独 UART 的封装的 FreeModbus - **Description**: 目标是 面向单独 UART 的封装的 FreeModbus 模块化:每个UART独立,便于管理和调试。 可扩展:可以支持多个Modbus实例,主机 和 从机 ,只需要为每个实例实现对应的串口和定时器函数。 灵活性:每个实例可以有不同的配置(波特率、地址等)。 - **Primary Language**: C - **License**: MulanPSL-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-02-05 - **Last Updated**: 2026-02-05 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 面向单独 UART 的封装的 FreeModbus #### 介绍 目标是 面向单独 UART 的封装的 FreeModbus 模块化:每个UART独立,便于管理和调试。 可扩展:可以支持多个Modbus实例,主机 和 从机 ,只需要为每个实例实现对应的串口和定时器函数。 灵活性:每个实例可以有不同的配置(波特率、地址等)。 使用了 freertos 刚实现RTU基本结构,以后用的时候慢慢填补更正... #### 最大实例数由 Max_MB 定义 默认 5 #### 初始化一个 Modbus 从站实例 ```c hl_lines="3-5" linenums="1" void StartTask02(void *argument) { /* USER CODE BEGIN StartTask02 */ int16_t iMB_1 = eMBInit_Slave(MB_RTU,1, 1, 9600, MB_PAR_NONE,UART_STOPBITS_1,vMBPortSerialInit_1,vMBPortSerialEnable_1, vMBPortSerialPutByte_1,vMBPortSerialGetByte_1,vMBPortTimersInit_3,vMBPortTimersEnable_3,vMBPortTimersDisable_3); { if (-1 != iMB_1) { eMBEnable(iMB_1); } } /* Infinite loop */ uint32_t uT = 0; for(;;) { if (-1 != iMB_1) { eMBPoll(iMB_1); } if (uT > 500) { HAL_GPIO_TogglePin(GPIOE, GPIO_PIN_14); uT = 0; } uT++; osDelay(1); } /* USER CODE END StartTask02 */ } // 重写从机响应功能码 3 uint16_t eMB_OnRead_03(int16_t iMB,USHORT uAddr,eMBErrorCode* pErrCode) { *pErrCode = MB_ENOERR; switch (uAddr) { case 0: return 123; } return 0; } ```