Modbus Library Development Note V: Modbus RTU Slave development

Source: Internet
Author: User

Modbus is divided into slave and master on the serial link, this section we will develop slave. For the Modbus RTU from the station, the functions that need to be implemented are in fact the same as the Modbus TCP server side. The process is the same. First receive the main station Access command, the command message parsing, here we just implement the above mentioned 8 function code. Secondly, according to the results of the analysis of the corresponding operation, the specific software access structure is as follows:

It is not difficult to find the actual 3 steps:

The 1th step, after receiving the command, first parse. The parsing is in the same way as the class in the previous sections.

The 2nd step, according to the analytic results to operate. Includes a more command to modify or get the value of a variable.

3rd step, generate the response and return it to the host.

1 , parsing host commands

After receiving the command message from the host, we support the 8 function codes shown.

/* Parse the received information and return the synthesized reply message and the byte length of the message, through the callback function */

uint16_t Parsingmasteraccesscommand (uint8_t *receivedmessage,uint8_t *respondbytes,uint16_t rxLength)

The function receives the received message and generates a response message, and the return value is the length of the response message. In developing an application, the received message is sent to the function, and the resulting response information is returned to a client.

2 , result actions

After parsing, both the read command and the write command need to be manipulated accordingly. We encapsulate different operations according to different function codes:

/* 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)

Also we define a function pointer array to implement these 8 function calls:

uint16_t (*handlemastercommand[]) (uint16_t,uint16_t,uint8_t *,uint8_t *) ={handlereadcoilstatuscommand,

Handlereadinputstatuscommand,

Handlereadholdingregistercommand,

Handlereadinputregistercommand,

Handlewritesinglecoilcommand,

Handlewritesingleregistercommand,

Handlewritemultiplecoilcommand,

Handlewritemultipleregistercommand};

3 , generating a Slave response

After processing also need to generate the corresponding information from the machine, whether it is a read operation command or write command, we all in the corresponding function code processing to generate a response message. This way, when developing an application, you only need to invoke the parse function to achieve the full functionality.

Modbus Library Development Note V: Modbus RTU Slave 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.