After the completion of the previous work, we can achieve targeted applications, first of all, we implement Modbus TCP server-side application. Of course, we do not do specific applications, but the Modbus TCP server-side applications are encapsulated to be called when necessary.
Here we do not involve TCP protocol, this part is not necessarily related to Modbus, we just run the Modbus protocol in its application layer.
For the Modbus TCP server we need to implement several functions: First, the received client command parsing, we only implement the previously mentioned 8 common features of the support. Second, after the completion of the resolution, we want to implement the corresponding function code operation. The following are the specific architectures:
1 , Command parsing
After the server receives a request from the client as the passive side, the installation request is processed. So the server receives the information first to parse it, here we only need a parse function can be completed, when in a specific application, as long as the received information called this function parsing can be.
/* Parse the received information and return the length of the response command */
uint16_t Parsingclientaccesscommand (uint8_t *receivedmessage,uint8_t *respondbytes)
In fact, only this function is visible externally. This function is called when we are developing an application for TCP server.
2 , command processing
After the command parsing out, according to different function code to carry out different processing, we support 8 function code, each function is corresponding to the processing part, many times we will be in the analysis of the resolution of the results of processing, which will require a large analytic function. To simplify the operation, we encapsulate each function code corresponding to the processing part as a function. These functions are then simplified by using a function pointer array to dynamically invoke these functions.
First we need 8 functions to handle the corresponding function code operations:
/* Process Read COIL status command */
Static uint16_t Handlereadcoilstatuscommand (uint16_t startaddress,uint16_t quantity,uint8_t *receivedMessage,uint8_ T *respondbytes)
/* Process Read input status command */
Static uint16_t Handlereadinputstatuscommand (uint16_t startaddress,uint16_t quantity,uint8_t *receivedMessage,uint8 _t *respondbytes)
/* Process read hold register command */
Static uint16_t Handlereadholdingregistercommand (uint16_t startaddress,uint16_t quantity,uint8_t *receivedMessage, uint8_t *respondbytes)
/* Process Read Input Register command */
Static uint16_t Handlereadinputregistercommand (uint16_t startaddress,uint16_t quantity,uint8_t *receivedMessage, uint8_t *respondbytes)
/* Handle Write Single coil command */
Static uint16_t Handlewritesinglecoilcommand (uint16_t coiladdress,uint16_t coilvalue,uint8_t *receivedMessage,uint8 _t *respondbytes)
/* Process Write Single register command */
Static uint16_t Handlewritesingleregistercommand (uint16_t registeraddress,uint16_t registervalue,uint8_t * receivedmessage,uint8_t *respondbytes)
/* Handle write multiple coil status */
Static uint16_t Handlewritemultiplecoilcommand (uint16_t startaddress,uint16_t quantity,uint8_t *receivedMessage, uint8_t *respondbytes)
/* Handle Write multiple register states */
Static uint16_t Handlewritemultipleregistercommand (uint16_t startaddress,uint16_t quantity,uint8_t *receivedMessage , uint8_t *respondbytes)
We then define an array of function pointers to invoke this function:
uint16_t (*handleclientcommand[]) (uint16_t,uint16_t,uint8_t *,uint8_t *) ={handlereadcoilstatuscommand,
Handlereadinputstatuscommand,
Handlereadholdingregistercommand,
Handlereadinputregistercommand,
Handlewritesinglecoilcommand,
Handlewritesingleregistercommand,
Handlewritemultiplecoilcommand,
Handlewritemultipleregistercommand};
3 , the generation of responses
For the response command generated in the second article has been said, it is necessary to explain the various types of data to write the specific values and to obtain the data read the various specific values we will be in a separate file to implement, because this part of the TCP and RTU is the same, we will be described in the following chapters.
Modbus Library Development Note III: Modbus TCP Server Development