This time we encapsulate the Modbus TCP client application. In the same way, we are not doing specific applications, but implementing the basic functionality of the TCP client. We encapsulate the functionality of the TCP client as a function so that it can be called when a specific application is developed.
For TCP clients, we mainly implement two functions: one is to generate a command to access the TCP server, a total of 8 function code. The second is to parse the information returned by the TCP server and perform various operations according to the result, which is also the operation that supports the function in 8. The specific software access structure is as follows:
1 , the generation of Access commands
The client, as the active interaction side, needs to send various operation request commands to the server. So for a TCP client, the first step is to generate a command to access the server. The build command only needs to be generated in the standard protocol format, currently we only support the 8 function codes mentioned earlier.
/* Generate a command to access the server */
uint16_t Createaccessservercommand (objaccessinfo objinfo,void *datalist,uint8_t *commandBytes)
This allows you to only call this function to generate a command to access the server when developing a specific client application.
2 and the resolution of the response information
As described in the previous section, the server receives the command, processes it according to the command, and generates response information back to the client. After the client receives the command, the response information is parsed, and the parsing process is in fact consistent with the server side. The difference is that there is no need to generate response information based on the results of the parsing.
/* Parse the received server corresponding information */
void Parsingserverrespondmessage (uint8_t *recievedmessage)
This allows us to invoke this function to parse the response information when developing the client application.
3 , Response processing
For the parsed information, we need to implement the operation according to the situation, such as modifying the value of variables, etc., should be the main supported opcode is 8, theoretically corresponding to each function code will have a different operation, but in fact, because the write operation command has no need to do any action, So the corresponding operation is actually only 4 function codes of the read operation.
/* Process read slave State amount return information, read coil status bit 0x01 function code */
static void Handlereadcoilstatusrespond (uint8_t *receivedmessage,uint16_t startaddress,uint16_t quantity)
/* Process read slave State amount return information, read input status bit 0x02 function code */
static void Handlereadinputstatusrespond (uint8_t *receivedmessage,uint16_t startaddress,uint16_t quantity)
/* Handle read return information of slave register value, read hold register 0x03 function code) */
static void Handlereadholdingregisterrespond (uint8_t *receivedmessage,uint16_t startaddress,uint16_t quantity)
/* Process The return information of the read slave register value, read input register 0x04 function code */
static void Handlereadinputregisterrespond (uint8_t *receivedmessage,uint16_t startaddress,uint16_t quantity)
Similarly, we also define a function pointer array to implement the invocation of these functions:
void (*handleserverrespond[]) (uint8_t *,uint16_t,uint16_t) ={handlereadcoilstatusrespond, Handlereadinputstatusrespond, Handlereadholdingregisterrespond, handlereadinputregisterrespond};
In this way, the encapsulation of the TCP client is complete, but the specific data processing part needs to be identified in the development of specific applications.
Modbus Library Development Note Four: Modbus TCP client Development