arduino modbus slave

This is a modbus RTU slave implementation for the arduino board. Below you will find an open source Arduino sketch/library that turns the microcontroller board into a Modbus RTU slave. Only functions 3, read holding registers, 6, preset_single_register, and 16, preset multiple registers, of the Modbus RTU slave over serial line have been implemented. The Arduino's Serial class has been used for communication. There are some known issues about the compliance with the Modbus specifications of this implementation.

Below you will find the following code:
  • modbus_rtu_slave.pde is an almost-ready-to-use sketch written in C. Just open it on the Arduino environment and tune it up to fit your needs.
  • ModbusSlave.tar.gz is a library written in C++.  Install it and import it into your code.
Both programs offer exactly the same functionality. You can find some examples on how to use the code here.
A small list of systems on which it has been successfully tested follows:
  • Linux RTU master, USB-Interface 115200bps, using several Modbus implementations freely available on the internet.
  • PLC ABB AC500 using the MAX485 interface chip (tested by Samuel Marco).
  • Mango, open source M2M (by Andras Tucsni,
  • Windows RTU master using a USB-to-RS485 Adapter (ADAM 4561) coupled to slave Arduino UNO together with a MAX485 chip in a analog-to-digital converter (by Jonathan McCrohan).
  • Mach3 as master in a nice Laser power control via modbus application (by Bart Dring).

This implementations does not fully comply with the Modbus over serial line specification in the following issues:
  1. In this implementation the character frame format can be an 11-bit 8E1 (8 bits, even parity, and one stop bit), 8O1 or a 10-bit 8N1. The specification requires that if no parity is used it must have 2 stop bits to fill out the 11-bit character frame. Furthermore, when using even or odd parity, be aware that the Arduino's Serial library does not check for parity errors. The hardware correctly appends the parity bit when sending a character, though (in other words, only the MB Master will be able to detect parity errors).
  2. You can only write 59 consecutive registers with a single preset_multiple_registers command. The arduino buffer takes only that much.  If you send more than that, the modbus telegram will be truncated and the slave will not send any reply. The modbus spec sets the limit to 123 registers.
  3. Only functions 3, 6, and 16 have been implemented.

* Thanks to Samuel Marco by solving the interframe delay (T35) problem, implementing the RS485 functionality, and by adding several other enhancements to the code.
* Thanks to Andras Tucsni for adding function 3 and for making other improvements to the code.

J Mz,
Aug 3, 2010, 2:45 PM
J Mz,
Dec 2, 2010, 1:12 AM