modbus master
上一篇帖子中我已经从无到有的完整实现了modbus slave从站,这篇帖子就来从0到1来实现modbus master主站。
Modbus 协议广泛应用在工业机器人和电机控制领域,比如机械臂,6轴或者8轴机器人领域,很多都是使用modbus来进行通信的。自从 1979 年出现工业串行链路的事实标准以来,MODBUS 使成千上万的自动化设备能够通信。
网上有很多开源的modbus协议栈,比较有名的像libmodbus, freemodbus,mbus等等,这些协议栈都是老外写的,都是别人自做的,本来学好一个协议栈最好的方法就是自己手写代码来实现完整的功能,这样才能锻炼提高自己的编码水平和能力。所以这期我就来手写一个modbus从站来代替这些开源的协议,做点与众不同的事情。多做原创!!!!
首先来分析modbus的数据帧格式:

整个协议栈分为地址域,功能码,数据和差错校验。
MODBUS 数据模型如下

modbus 主站master和从站不同
主要是给从站发数据,等待从站回复数据。
好了,直接上核心代码
1.这是主要函数

2。
3。
4。
5。
6。
7。
8。
9。
10。
11。
主函数核心代码如下:
#include "hal_data.h"
#include "usart9.h"
#include "usart6.h"
#include "modbus_master.h"
FSP_CPP_HEADER
void R_BSP_WarmStart(bsp_warm_start_event_t event);
FSP_CPP_FOOTER
void SysTick_Handler(void )
{
SysTick_Inc();
}
/*******************************************************************************************************************//**
-
main() is generated by the RA Configuration editor and is used to generate threads if an RTOS is used. This function
-
is called by main() when no RTOS is used.
*********************************************************************************************************************/
void hal_entry(void)
{
/ TODO: add your own code here */
UART9_Init();UART6_Init();
hal_systick_init();
uint8_t state=0;
uint16_t Input_Result[2];
ModbusMaster_begin();
while(1)
{
uint8_t result;
result = ModbusMaster_readInputRegisters(0x01,0x2, 2);
if (result == 0x00)
{
Input_Result[0] = ModbusMaster_getResponseBuffer(0x00);
Input_Result[1] = ModbusMaster_getResponseBuffer(0x01);
}
R_BSP_SoftwareDelay(100, BSP_DELAY_UNITS_MILLISECONDS);
}
#if BSP_TZ_SECURE_BUILD
/* Enter non-secure code */
R_BSP_NonSecureEnter();
#endif
}
/*******************************************************************************************************************//**
-
This function is called at various points during the startup process. This implementation uses the event that is
-
called right before main() to set up the pins.
-
@param[in] event Where at in the start up process the code is currently at
**********************************************************************************************************************/
void R_BSP_WarmStart (bsp_warm_start_event_t event)
{
if (BSP_WARM_START_RESET == event)
{
#if BSP_FEATURE_FLASH_LP_VERSION != 0
R_FACI_LP->DFLCTL = 1U;
#endif
}
if (BSP_WARM_START_POST_C == event)
{
R_IOPORT_Open(&IOPORT_CFG_CTRL, &IOPORT_CFG_NAME);
#if BSP_CFG_SDRAM_ENABLED
R_BSP_SdramInit(true);
#endif
}
}
#if BSP_TZ_SECURE_BUILD
FSP_CPP_HEADER
BSP_CMSE_NONSECURE_ENTRY void template_nonsecure_callable ();
/* Trustzone Secure Projects require at least one nonsecure callable function in order to build (Remove this if it is not required to build). */
BSP_CMSE_NONSECURE_ENTRY void template_nonsecure_callable ()
{
}
FSP_CPP_FOOTER
#endif

编译烧录代码


因为是modbus master
所以无需要打开modbus slave软件

抓取报文
000019-Tx:01 04 04 00 00 00 00 FB 84
000020-Rx:01 04 00 02 00 02 D0 0B
修改
000034-Rx:01 04 00 02 00 02 D0 0B
000035-Tx:01 04 04 00 01 00 02 2B 85
发现读取数据正常,至此modbus master主机完成
|