Master library functions
Configuring the master
The configuration of the master is very important before starting the communication with the slaves. Be sure that your devices can be used with the Waspmote Modbus protocol. For example, the baud rate must be in the available range of Waspmote and the communication must be 8N1 (8 bits, 1 stop bit, no parity).
Library constructor
Before using the library, an object of the library must be created. This object will assign the physical address of the slave. This address can take values from 0 to 255 (address 0 is normally for broadcast mode). In a network, this address must be unique for each device. In this example we are going to communicate RS-485 devices, so the master must be configured to use this protocol of communication when creating an object.
Example of use:
You can create an object for managing several slaves in an easy way.
Example of use:
Begin function
In the begin()
function, the baud rate communication must be configured. This baud rate must be one of the standard values mentioned in the RS-485 and RS-232 guides. If the value is not a standard value, the device will be configured at 1200 bps.
We suggest to study the RS-232 or RS-485 Communication Guides to know how to configure these modules.
Modbus master library and function
The Modbus library for Waspmote implements the next functions. Maybe the terminology can change depending on documentation, but the code functions must be the same in all Modbus devices. The Modbus standard includes more functions, but these are the most used and important ones.
Code function
Function name
0x01
Read Coils
0x02
Read Discrete Inputs
0x05
Write Single Coil
0x0F
Write Multiple Coils
0x03
Read Holding Registers
0x04
Read Input Registers
0x06
Write Single Register
0x10
Write Multiple Registers
0x16
Mask Write Register
0x17
Read Write Multiple Registers
The Modbus library receives and analyzes the response that the slaves send back to the master. This way, the user can know if the frame was correctly processed by the slave.
Read coils
This function requests the on/off status of discrete coils from the slave device. When receiving a Modbus query message with function readCoils()
(code 0x01 in the "field" function of the Modbus frame), the slave collects the necessary output values and constructs an answer message. The length of this message is dependent on the number of values that have to be returned.
Example of use:
See an example of use here:
Read discrete inputs
Reading input values with Modbus is done in the same way as reading the status of coils. The only difference is that for inputs, Modbus function readDiscreteInput()
is used (code 0x02). Like with coils, the address of the first input, and the number of inputs to read must be put in the data field of the query message. After receiving a query message with Modbus function readDiscreteInput()
, the slave puts the requested input values in a message structure and sends this message back to the Modbus master. The length of the message depends on the number of input values returned.
Example of use:
Write single coil
This function writes a single coil to either on or off. The request message specifies the coil address to be written. The requested on/off state is specified by a constant in the request data field. The normal response is an echo of the request, returned after the coil state has been written.
Example of use:
See an example of use here:
Write multiple coils
This Modbus function code is used to write each coil in a sequence of coils to either on or off in a remote slave. The request specifies the coil references to be forced. Coils are addressed starting at zero, therefore coil numbered 1 is addressed as 0. The requested on/off states are specified by contents of the request data field. A logical '1' in a bit position of the field requests the corresponding output to be on. A logical '0' requests it to be off.
Read holding registers
This function is used to read the contents of a contiguous block of holding registers in a slave The request specifies the starting register address and the number of registers. In the registers are addressed starting at zero. Therefore registers numbered 1-16 are addressed as 0-15. The register data in the response message are packed as two bytes per register.
Example of use:
Read input registers
This function code is used to read contiguous input registers in a slave. The Request PDU specifies the starting register address and the number of registers. The registers are addressed starting at zero. The register data in the response message are packed as two bytes per register, with the binary contents right justified within each byte. For each register, the first byte contains the high order bits and the second contains the low order bits.
Example of use:
See an example of use here:
Write single register
This function code is used to write a single holding register in a remote device. The request specifies the address of the register to be written. Registers are addressed starting at zero. Therefore, the register numbered 1 is addressed as 0. The normal response is an echo of the request, returned after the register contents have been written.
Example of use:
See an example of use here:
Write multiple registers
This function is used to write a block of contiguous registers in a remote device. The requested values to write are specified in the request data field. Data is packed as two bytes per register. The normal response returns the function code, starting address, and quantity of registers written.
Example of use:
Mask write register
This function code is used to modify the contents of a specified holding register using a combination of an AND mask, an OR mask, and the register\'s current contents. The function can be used to set or clear individual bits in the register. The request specifies the holding register to be written, the data to be used as the AND mask, and the data to be used as the OR mask.
Read and write multiple registers
This function code performs a combination of one read operation and one write operation in a single Modbus transaction. The write operation is performed before the read. The request specifies the starting address and number of holding registers to be read as well as the starting address, number of holding registers, and the data to be written. The byte count specifies the number of bytes to follow in the write data field. The normal response contains the data from the group of registers that were read. The byte count field specifies the quantity of bytes to follow in the read data field.
Other library functions
Retrieve data
All data received from the slave is stored in an internal buffer in the master. This buffer has public declaration and can be read using the corresponding object, when you are using Modbus read functions.
Example of use:
Clear response buffer
This function acts like a flush of the response buffer and delete all previous information. It is recommended to use this function before starting a new reading process.
Example of use:
Last updated