Modbus Library Development Note VII: Modbus OTHER auxiliary function development

Source: Internet
Author: User

A variety of applications have been developed before, but there has been no mention of a problem, and you are reading and writing to specific data. There are 4 standard data for Modbus: Coil data (Address: 0000x), input State volume data (address: 1000x), hold register data (address: 4000x) and input register data (address: 3000x). The purpose of our communication is to manipulate this data, but we do not refer to data processing in the previous package. In fact, there is no way to consider this, because the specific application data vary widely, there is no way to encapsulate. So how do we solve this problem? Next we will solve this kind of problem.

1 , the encapsulation of data processing functions

We consider that whether in the RTU master station, RTU Slave, TCP client, or TCP server to the data processing is essentially the same, as long as the specific application of data structure to determine the post-processing method is also determined. In view of this, the method we use is to define the function of the weakening type. As follows:

/* Get the value of the amount of coil you want to read */

__weak void Getcoilstatus (uint16_t startaddress,uint16_t quantity,bool *statuslist)

{

Implement specific content in Modbus TCP server/rtu slave applications if required

}

/* Get the value of the amount of inputstatus you want to read */

__weak void Getinputstatus (uint16_t startaddress,uint16_t quantity,bool *statusvalue)

{

Implement specific content in Modbus TCP server/rtu slave applications if required

}

/* Get the value of the hold register you want to read */

__weak void Getholdingregister (uint16_t startaddress,uint16_t quantity,uint16_t *registervalue)

{

Implement specific content in Modbus TCP server/rtu slave applications if required

}

/* Get the value of the input register you want to read */

__weak void Getinputregister (uint16_t startaddress,uint16_t quantity,uint16_t *registervalue)

{

Implement specific content in Modbus TCP server/rtu slave applications if required

}

/* Set the value of a single coil */

__weak void Setsinglecoil (uint16_t coiladdress,bool coilvalue)

{

Implement specific content in Modbus TCP server/rtu slave applications if required

}

/* Set the value of a single register */

__weak void Setsingleregister (uint16_t registeraddress,uint16_t registervalue)

{

Implement specific content in Modbus TCP server/rtu slave applications if required

}

/* Set values for multiple coils */

__weak void Setmultiplecoil (uint16_t startaddress,uint16_t quantity,bool *statusvalue)

{

Implement specific content in Modbus TCP server/rtu slave applications if required

}

/* Set values for multiple registers */

__weak void Setmultipleregister (uint16_t startaddress,uint16_t quantity,uint16_t *registervalue)

{

Implement specific content in Modbus TCP server/rtu slave applications if required

}

/* Update the Read-Back coil status */

__weak void Updatecoilstatus (uint16_t startaddress,uint16_t quantity,bool *statevalue)

{

Implemented in the client (master) application

}

/* Update the input status values read */

__weak void Updateinputstatus (uint16_t startaddress,uint16_t quantity,bool *statevalue)

{

Implemented in the client (master) application

}

/* Update the Read-Back coil status */

__weak void Updateholdingregister (uint16_t startaddress,uint16_t quantity,uint16_t *registervalue)

{

Implemented in the client (master) application

}

/* Update the Read-Back coil status */

__weak void Updateinputresgister (uint16_t startaddress,uint16_t quantity,uint16_t *registervalue)

{

Implemented in the client (master) application

}

In the development of specific applications, we only need to implement the corresponding function in the application can make the function complete, as to how the specific data processing, it depends on the application of the data format. Of course these functions do not all need to be implemented, just need to be implemented according to their own needs.

2 , about the size of the end of the problem

When it comes to data communication, one problem is that there is no way to avoid it, which is the problem of size and end. For the Modbus protocol, the use of the big-endian mode, is the low-level address storage high-byte data, high-level address storage low-level byte data.

In the multi-byte data communication, the size of the problem is obvious, such as a floating-point number in different systems stored in the order of the difference, you read or write down the data will appear wrong parsing. So we have to take this into account when dealing with data messages.

Modbus Library Development Note VII: Modbus OTHER auxiliary function 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.