Skip to content

A library that allows your ESP8266/ESP32 to communicate via Modbus protocol, acting as a master, slave or both. Supports IP via wireless network (Modbus IP) and RTU via Serial line (Modbus RTU).

License

Notifications You must be signed in to change notification settings

m2m-solutions/library-arduino-modbus-esp8266

 
 

Repository files navigation

ModbusRTU and ModbusTCP Library for ESP8266/ESP32

If the library is helpful for your projects you can support it by a glass of beer paypal

Visit Releases page for stable one.


This library allows your ESP8266/ESP32 to communicate via Modbus protocol. 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 IP).

For more information about Modbus see:

Features

  • Supported platforms are
    • ESP8266
    • ESP32
    • STM32F103 and probably others (Modbus RTU only)
  • Operates in any combination of multiple instances of
    • ModbusRTU slave
    • ModbusRTU master
    • ModbusTCP server
    • ModbusTCP client
  • 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
  • Callbacks for
    • Client connect (ModbusTCP)
    • Server/Client disconnect (ModbusTCP)
    • Read specific Register
    • Write specific Register
    • Slave transaction finish

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 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 offsets are 1-based, so a register configured as 100 in library should be 101 in this software.
  2. For API refer API.md
  3. ModbusRTU maximum incoming frame size is determinated by HardwareSerial buffer size. For SoftwareSerial buffer must be set to 256 bytes.
  4. Probably it's possible to use ModbusRTU with other AVR boards using from ArduinoSTL.
  5. RS-485 transivers based on MAX-485 is working on at least up to 115200. XY-017/XY-485 working only up to 9600 for some reason.

Last Changes

// 3.0.3
+ ModbusRTU: Add inverse logic support for transmit control
// 3.0.2
+ ModbusTCP Client: ESP32 fix unexpected transaction timeout 
// 3.0.1
+ ModbusRTU: ESP32 possible send\receive failure fix
+ ModbusRTU: Non-ESP devices support
+ Restriction to registers count removed
+ Added bridge example
// 3.0.0
+ ModbusRTU Slave
+ ModbusRTU Master
+ Registers are now shared between Modbus* instances by default
+ Fix functions register count limits to follow Modbus specification (or RX buffer limitations)
+ ModbusRTU: Examples added
+ Test multiple Modbus* instances
+ Change to 'uint32_t eventSource()'. Implemented for ModbusRTU and ModbusIP both
+ Client: Allow to specify local TCP port (default is 502)
+ Server: Allow to specify TCP remote port for connection (default is 502)
+ Master\Client: Fix crash on Write Multiple Hregs
+ Master\Client: Fix crash on no callback function on read\write remote
+ Tests added
// ToDo later
- 0x14 - Read File Records function
- 0x15 - Write File Records function
- 0x16 - Write Mask Register function
- 0x17 - Read/Write Registers function
- ModbusIP: Support for non-ESP boards (using W5x00)

Contributions

https://github.com/emelianov/modbus-esp8266

a.m.emelianov@gmail.com

Original version:

https://github.com/andresarmento/modbus-esp8266

https://github.com/andresarmento/modbus-arduino

prof (at) andresarmento (dot) com

License

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

About

A library that allows your ESP8266/ESP32 to communicate via Modbus protocol, acting as a master, slave or both. Supports IP via wireless network (Modbus IP) and RTU via Serial line (Modbus RTU).

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C++ 95.5%
  • C 4.5%