Modbus over RS-485 and RS-232

The Modbus protocol can be implemented over RS-485 and RS-232 physical layers. The Waspmote platform provides the necessary hardware and software for working with both protocols. The name and use of the functions are the same for RS-232 and RS-485, and the only changes are the library to include and the instantiation of the object. The differences between the two standards are explained in the corresponding communication guides.

Both RS-232 module and RS-485 module can implement Modbus since, it is simply a software layer built on top the physical layer. That is why we suggest to start developing in RS-232 or RS-485, and after this, continue with Modbus.

The Modbus library has been tested with various devices and is compatible with the majority of commercial modules, but this does not ensure the working with all of them. Be sure that the Modbus module fits your technical requirements. The final user is the responsible to perform the task of communicating the Modbus module with other commercial devices.

Modbus over RS-232

The first step to use Modbus over RS-232 is to include the corresponding libraries and instantiate the necessary objects. Below you can see how to include this library.

// Include these libraries for using the RS-232 and Modbus functions
#include <ModbusMaster.h>

After including libraries, you have to instantiate a ModbusMaster object. Below you can see that an object called slave has been created in the address "1" (in the assumption that an actual Modbus slave is connected in that address), and the corresponding communication protocol is configured, in this case RS232_COM. When the master needs to communicate with the address "1", it must use this object.

// Instantiate ModbusMaster object as slave ID 1
ModbusMaster slave(RS232_COM, 1);

Example of code to run in slaves nodes:

// Include these libraries for using the RS-232 and Modbus functions
#include <ModbusSlave.h>

After including the libraries, you have to instantiate a ModbusSlave object.

// Create new Modbus instanceModbus
Slave mbs(RS232_COM);

Modbus over RS-485

The first step to use Modbus over RS-485 is to include the corresponding libraries and instantiate the necessary objects. Below you can see the complete process.

Example of use in master mode:

// Include these libraries for using the RS-485 and Modbus functions
#include <Wasp485.h>
#include <ModbusMaster.h>

After including libraries, you have to instantiate a ModbusMaster object. Below you can see that an object called slave has been created in the address "1" (in the assumption that an actual Modbus slave is connected in that address), and the corresponding communication protocol is configured, in this case RS485_COM. When the master needs to communicate with the address "1", it must use this object.

// Instantiate ModbusMaster object as slave ID 1
ModbusMaster485 node(RS485_COM, 1);

Example of use in slave mode:

// Include these libraries for using the RS-485 and Modbus functions
#include <Wasp485.h>
#include <ModbusSlave.h>

After including the libraries, you have to instantiate a ModbusSlave object.

// Create new mbs instance
ModbusSlave485 mbs(RS485_COM);

‚Äč