上一贴我已经完成了modbus从机的解析,本期就来讲解modbus主机的解析,做一个完美的大结局吧!!!
在很多的开源协议栈中,比较有名的freemodbus,这个只有从机,没有主机,这个也非常的庞大,不好使用,还有一部分modbus的功能码也没有完整的实现!所以本期还是使用我自己的nanomodbus主机!!!!
建立工程之类的大家直接看我上一期的modbus从机帖子,不在重复造轮子浪费时间了。
https://bbs.elecfans.com/jishu_2488809_1_1.html
1,直接开始进入主题
打开KEIL文件
#include "hal_data.h"
#include "usart9.h"
#include "usart0.h"
#include "nanomodbus.h"
#include "port.h"
FSP_CPP_HEADER
void R_BSP_WarmStart(bsp_warm_start_event_t event);
FSP_CPP_FOOTER
// Our RTU address
#define RTU_SERVER_ADDRESS 1
/*******************************************************************************************************************//**
-
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();
UART0_Init();
clear_uart_buff();
hal_systick_init();
printf("hello RA4M2\r\n");
printf("瑞萨电子Modbus主机开发\r\n");
nmbs_platform_conf platform_conf;
nmbs_platform_conf_create(&platform_conf);
platform_conf.transport = NMBS_TRANSPORT_RTU;
platform_conf.read = read_serial;
platform_conf.write = write_serial;
nmbs_t nmbs;
nmbs_error err = nmbs_client_create(&nmbs, &platform_conf);
if (err != NMBS_ERROR_NONE)
printf("nmbs_client_create NMBS_ERROR_NONE\r\n");
nmbs_set_read_timeout(&nmbs, 1000);
nmbs_set_byte_timeout(&nmbs, 100);
nmbs_set_destination_rtu_address(&nmbs, RTU_SERVER_ADDRESS);
while(1)
{
// Write 2 coils from address 64
nmbs_bitfield coils = {0};
nmbs_bitfield_write(coils, 0, 1);
nmbs_bitfield_write(coils, 1, 1);
err = nmbs_write_multiple_coils(&nmbs, 64, 2, coils);
nmbs_bitfield_reset(coils);
err = nmbs_read_coils(&nmbs, 64, 3, coils);
uint16_t w_regs[2] = {123, 124};
err = nmbs_write_multiple_registers(&nmbs, 26, 2, w_regs);
uint16_t r_regs[2];
err = nmbs_read_holding_registers(&nmbs, 26, 2, r_regs);
HAL_Delay(500);
}
#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



打开串口助手,复位

打开modbusslave软件






可以看到RA4M2作为modbus主机给从机发数据,非常的完美
详情请看视屏