We have already completed the development of the Modbus protocol stack, but this is not our goal. The purpose of our development is of course to use it to solve our practical problems. Next we will use the newly developed Modbus protocol stack to develop a Modbus TCP server application.
The development of Modbus TCP server first needs to have TCP server support and network configuration, etc., but these have nothing to do with Modbus itself, we do not discuss again. We stipulate that the network and TCP server are configured properly. Next we discuss the implementation process of Modbus TCP server.
Based on the previous encapsulation of the protocol stack, we need to refer to the relevant encapsulation of Modbus TCP server. After the command is received, the parse function is called to parse, and the parse function is prototyped:
uint16_t Parsingclientaccesscommand (uint8_t *receivedmessage,uint8_t *respondbytes);
The function is passed to the TCP Server as a function pointer and the callback resolves the received list of information.
In addition, we need to implement 8 callback functions according to the specific needs to complete the operation of the Modbus object, the prototype of the 8 functions are as follows:
/* Get the value of the amount of coil you want to read */
void Getcoilstatus (uint16_t startaddress,uint16_t quantity,bool *statuslist);
/* Get the value of the amount of inputstatus you want to read */
void Getinputstatus (uint16_t startaddress,uint16_t quantity,bool *statusvalue);
/* Get the value of the hold register you want to read */
void Getholdingregister (uint16_t startaddress,uint16_t quantity,uint16_t *registervalue);
/* Get the value of the input register you want to read */
void Getinputregister (uint16_t startaddress,uint16_t quantity,uint16_t *registervalue);
/* Set the value of a single coil */
void Setsinglecoil (uint16_t coiladdress,bool coilvalue);
/* Set the value of a single register */
void Setsingleregister (uint16_t registeraddress,uint16_t registervalue);
/* Set values for multiple coils */
void Setmultiplecoil (uint16_t startaddress,uint16_t quantity,bool *statusvalue);
/* Set values for multiple registers */
void Setmultipleregister (uint16_t startaddress,uint16_t quantity,uint16_t *registervalue);
These 8 functions implement the operation of data objects according to the specific requirements of the application, and implement several methods to determine how to implement them according to their application requirements and data structures.
Modbus Library Development Note IX: Using protocol stacks to develop Modbus TCP server applications