Modbus Library Development Note VI: Modbus RTU Master Development

Source: Internet
Author: User

In this section we encapsulate the last application (Modbus RTU Master application), and the development of the RTU Master is consistent with the development of the TCP client. The same we do not do specific applications, but to achieve the basic function of the main station RTU. We encapsulate the functionality of the RTU master as a function to invoke when developing a specific application.

For the RTU Master station we mainly achieve two functions: one is to generate a command to access the RTU slave, a total of 8 function codes are supported. The second is to the RTU from the station to parse the information returned and according to the results of various operations, but also support the operation of the 8 function. 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 the RTU master, 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 slave */

uint16_t Createaccessslavecommand (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 slave information */

void Parsingslaverespondmessage (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};

Here, the main station of the RTU package is complete, of course, the specific data processing parts need to develop specific applications can be determined.

Modbus Library Development Note VI: Modbus RTU Master Development

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.