Real applications
Last updated
Last updated
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.
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.
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.
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.
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:
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 +/-.
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.
Command Name
Command Number
Read Holding Register
03
Write (Preset) Single Register
06
Return Slave ID
17
Parameter
Setting
Baud
19200
Parity
N
Data bits
8
Stop bit
1
Flow control
None