Teach you step by step how to write and debug MODBUS slave programs through HAL library (3) --- MODBUS SLAVE program transplantation

Table of contents

1. STM32CubeMX operation

2. Program improvement 

3. Transplantation of MODBUS SLAVE program

 3.1 Program download and copy

 3.2 Program addition and compilation

3.3 Interface replacement

3.4 Modify main.c main function

4. MODBUS debugging

5. Summary 


1. STM32CubeMX operation

Teach you step by step how to write and debug MODBUS slave programs through the HAL library (1) -----STM32CubeMX operation_tangxianyu's blog-CSDN blog

2. Program improvement 

Teach you step by step how to write and debug MODBUS slave programs through the HAL library (2) - Improvement of serial ports and timing functions_tangxianyu's blog-CSDN blog

3. Transplantation of MODBUS SLAVE program

 3.1 Program download and copy

Download the modbus-slave (RTU) program athttps://download.csdn.net/download/tangxianyu/37014694 and unzip it Then copy the already written modbus.c and modbus.h to the Src and Inc folders respectively

 

 3.2 Program addition and compilation

Add the modbus.c file to the project. The specific steps are shown in the figure below and will not be described in detail.

 After the addition is completed, click Compile. You will find many errors at this time, so ignore them. We can find that modbus.h is also displayed.​ 

3.3 Interface replacement

Open the modbus.c file and modify the macro definition at the top: If the serial port used is UART4, the macro definition file can remain unchanged. If not, just change the serial port to the corresponding interface.

#include "Modbus.h"
#include "string.h"

#define MODBUS_UART uart4			//将串口改成相应的变量即可,例如用到UART1,此处改为uart1
#define MODBUS_HUART huart4			//将串口改成相应的变量即可,例如用到UART1,此处改为huart1

3.4 Modify main.c main function

Add modbus.h to the header file

 Modify the function in the previous infinite loop:

 At this point, the MODBUS-SLAVE function modification is completed. The next step is debugging.

4. MODBUS debugging

The modbus debugging tool used here is Modbus Poll, which is easy to search in CSDN. I will not include the link address here.

Open the modbus Poll software -->Connection, and set the baud rate and other settings in the Connection Setup dialog box that pops up. After setting, click OK.

The communication data is as follows:

The communication data should be 0XAA55 and 0X55AA. The decimal numbers displayed here need to be converted. Convert the Signed data type to Hex, and the displayed data is consistent with our expectations.

5. Summary 

 This article mainly implements the MODBUS-SLAVE programming of STM32F103ZET6 through STM32CUBE MX. Hope it makes some sense. It is inevitable that there will be omissions or errors in the writing process of this article. I hope readers will feel free to point it out.

Guess you like

Origin blog.csdn.net/tangxianyu/article/details/121138328