Skip to content

A library that allows your Arduino to communicate via Modbus protocol, acting as a slave. Application layer library (OSI 7), used by all implementations over serial line and TCP/IP.

License

Notifications You must be signed in to change notification settings

epsilonrt/modbus-arduino

Repository files navigation

Modbus Library for Arduino

Allows your Arduino to communicate via Modbus protocol

GitHub release (latest by date including pre-releases) Framework

PlatformIO Registry Platform ATMELAVR Platform ATMELSAM Platform ESP8266 Platform ESP32


The Modbus is a master-slave protocol used in industrial automation and can be used in other areas, such as home automation.

The Modbus generally uses serial RS-232 or RS-485 as physical layer (then called Modbus Serial) and TCP/IP via Ethernet or WiFi (Modbus TCP).

In the current version the library allows the Arduino operate as a slave, supporting Modbus Serial and Modbus over IP. For more information about Modbus see:

Author's note (motivation and thanks):

It all started when I found the Modbus RTU Arduino library of Juan Pablo Zometa. I had extend the library to support other Modbus functions.

After researching several other Modbus libraries I realized strengths and weaknesses in all of them. I also thought it would be cool have a base library for Modbus and derive it for each type of physical layer used.

I appreciate the work of all the authors of the other libraries, of which I used several ideas to compose the modbus-arduino. At the end of this document is a list of libraries and their authors.

Features

  • Operates as a slave (master mode in development)
  • Supports Modbus over Serial line (RS-232 or RS485) and Modbus TCP
  • Reply exception messages for all supported functions
  • Modbus functions supported:
    • 0x01 - Read Coils
    • 0x02 - Read Input Status (Read Discrete Inputs)
    • 0x03 - Read Holding Registers
    • 0x04 - Read Input Registers
    • 0x05 - Write Single Coil
    • 0x06 - Write Single Register
    • 0x0F - Write Multiple Coils
    • 0x10 - Write Multiple Registers
    • 0x11 - Report Server ID

Notes:

  1. The offsets for registers are 0-based. So be careful when setting your supervisory system or your testing software. For example, in ScadaBR (http://www.scadabr.com.br) offsets are 0-based, then, a register configured as 100 in the library is set to 100 in ScadaBR. On the other hand, in the CAS Modbus Scanner (http://www.chipkin.com/products/software/modbus-software/cas-modbus-scanner/) offsets are 1-based, so a register configured as 100 in library should be 101 in this software.

  2. Early in the library Modbus.h file there is an option to limit the operation to the functions of Holding Registers, saving space in the program memory. Just comment out the following line:

     #define USE_HOLDING_REGISTERS_ONLY
    

Thus, only the following functions are supported:

  • 0x03 - Read Holding Registers
  • 0x06 - Write Single Register
  • 0x10 - Write Multiple Registers

How to

There are four classes corresponding to five headers that may be used:

By opting for Modbus Serial or Modbus TCP you must include in your sketch the corresponding header and the base library header, eg:

#include <Modbus.h>
#include <ModbusSerial.h>

Modbus Jargon

In this library was decided to use the terms used in Modbus to the methods names, then is important clarify the names of register types:

Register type Use as Access Library methods
Coil Digital Output Read/Write addCoil(), Coil()
Holding Register Analog Output Read/Write addHreg(), Hreg()
Input Status Digital Input Read Only addIsts(), Ists()
Input Register Analog Input Read Only addIreg(), Ireg()

Notes:

  1. Input Status is sometimes called Discrete Input.
  2. Holding Register or just Register is also used to store values in the slave.
  3. Examples of use: A Coil can be used to drive a lamp or LED. A Holding Register to store a counter or drive a Servo Motor. A Input Status can be used with a reed switch in a door sensor and a Input Register with a temperature sensor.

Contributions

http://github.com/epsilonrt/modbus-arduino
epsilonrt (at) gmail (dot) com
prof (at) andresarmento (dot) com

License

The code in this repo is licensed under the BSD New License. See LICENSE for more info.

About

A library that allows your Arduino to communicate via Modbus protocol, acting as a slave. Application layer library (OSI 7), used by all implementations over serial line and TCP/IP.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C++ 61.8%
  • Objective-C 38.2%