Real applications

Modbus has become a standard communications protocol and is now the most commonly available

means of communicating with industrial electronic devices. The network configuration process is very similar in all Modbus devices. The next section describes the typical steps needed to communicate Waspmote with a Modbus commercial device. In this example, we are going to use a standard Modbus device.

Figure: Modbus device

First of all, you must be sure that your device is compatible with Waspmote. In the device manual the communication parameters will be described (device address, baud rate...). You also must check that the device uses RTU format. There are many variants of Modbus protocols, but Waspmote implements the RTU format. Modbus RTU is the most common implementation available for Modbus.

Command Name

Command Number

Read Holding Register

03

Write (Preset) Single Register

06

Return Slave ID

17

The next step is to know what Modbus commands uses the device. The supported commands should be listed in a table. In the next Figure you can see an example of the Modbus commands extracted from a datasheet. The Modbus library for Waspmote is compatible with the majority of the Modbus commands.

Parameter

Setting

Baud

19200

Parity

N

Data bits

8

Stop bit

1

Flow control

None

All Modbus devices include a register map with the location and a description of the data stored in the registers. Modbus functions operate on register map to monitor, configure, and control the device\'s inputs and outputs. You have to refer to the register map of your device to gain a better understanding of its operation. Modbus registers are organized into reference types identified by the leading number of the reference address. You can see below an example of how to read and write data in a Modbus device.

Figure: Modbus typical register map

In our example, we are going to read the temperature value from our device. We can see in the register map, that the temperature value is stored in the register 30011 and is accessible with the function readInputRegisters(), and is stored in 16 bits format (2 bytes).

So the necessary function to get the value of the temperature is shown in the next code example.

Example code:

// Instantiate ModbusMaster object as slave ID 1
ModbusMaster node(RS485_COM, 1);
// Define one address for reading, extracted from the datasheet
#define address 30011
// Define the number of bytes to read, extracted from the datasheet
#define bytesQty 2
// This variable will store the result of the communication
// result = 0: no errors
// result = 1: error occurred
// Read Holding Registers is the necessary function to
// read the temperature registerint
result = node.readHoldingRegisters(address, bytesQty);
if (result != 0)
{
// If no response from the slave, print an error message
USB.println(“Communication error”);
delay(1000);
}
else
{
// If all ok USB.print(“Read value: “);
// Print the read data from the slave
USB.print(node.getResponseBuffer(0));
delay(1000);
}

The last step is to make the physically connection between the Modbus device and Waspmote. Our device uses RS-485 physical layer and uses a differential line transmission. The name of the lines can change. In many devices are named A/B, or +/-.

Figure: Waspmote RS-485 module connected to a Modbus device

If the communication has been established, you should be able to see the temperature data in your serial monitor. If the communication with the device is not correct you should view an error message. You can use this error message to make retries and as a feedback of the state of your network.

In the next section and in the development section of the web, there is a complete example if how to configure Waspmote and the RS-485 module in a wireless application.