/ModbusMaster

Arduino class library for communicating with Modbus slaves over RS232/485

Primary LanguageC++GNU General Public License v3.0GPL-3.0

##Overview## This is an Arduino class library for communicating with Modbus slaves over RS232/485 (via RTU protocol). Updated to support Arduino 1.0.

##Features## The following Modbus functions have been implemented:

Discrete Coils/Flags

  • 0x01 - Read Coils
  • 0x02 - Read Discrete Inputs
  • 0x05 - Write Single Coil
  • 0x0F - Write Multiple Coils

Registers

  • 0x03 - Read Holding Registers
  • 0x04 - Read Input Registers
  • 0x06 - Write Single Register
  • 0x10 - Write Multiple Registers
  • 0x16 - Mask Write Register
  • 0x17 - Read Write Multiple Registers

##Uploading Sketches## Arduinos prior to the Mega have one serial port which must be connected to USB (FTDI) for uploading sketches and to the RS232/485 device/network for running sketches. You will need to disconnect pin 0 (RX) while uploading sketches. After a successful upload, you can reconnect pin 0.

##Hardware## This library has been tested with an Arduino Duemilanove, PHOENIX CONTACT nanoLine controller, connected via RS485 using a Maxim MAX488EPA transceiver.

##Installation##

  • Arduino 17 (or later):

    Determine the location of your sketchbook by selecting File > Preferences from within the Arduino IDE. If you don't already have a libraries folder within your sketchbook, create one and unzip the archive there. See this for more information.

  • Arduino 16 (or earlier):

    Download the zip file, extract and copy the ModbusMaster folder to ARDUINO_HOME/hardware/libraries. If you are upgrading from a previous version, be sure to delete ModbusMaster.o.

##Support## Please report any bugs on the Issue Tracker.

##Questions/Feedback## I can be contacted at dfwmountaineers at gmail.

##History##

Date Rev Description
4-Dec-11 v0.9 Add support for Arduino 1.0
9-Nov-11 v0.8 Add support for ATmega2560
9-Feb-10 v0.7 Modified capitalization of functions to match Arduino style where first letter is lower-case
4-Feb-10 v0.6 Added documentation via Doxygen comments, modified methods used to get/set storage buffers
30-Jan-10 v0.5 Added ability to select serial port 0..3, modified methods used to get/set storage arrays, miscellaneous bug fixes
29-Jan-10 v0.3
25-Jan-10 v0.2
24-Jan-10 v0.1 Initial public release

##Example## The library contains a few sketches that demonstrate use of the ModbusMaster library. You can find these in the examples folder.

/*

  Basic.pde - example using ModbusMaster library
  
  This file is part of ModbusMaster.
  
  ModbusMaster is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.
  
  ModbusMaster is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  
  You should have received a copy of the GNU General Public License
  along with ModbusMaster.  If not, see <http://www.gnu.org/licenses/>.
  
  Written by Doc Walker (Rx)
  Copyright © 2009-2011 Doc Walker <dfwmountaineers at gmail dot com>
  
*/

#include <ModbusMaster.h>


// instantiate ModbusMaster object as slave ID 2
// defaults to serial port 0 since no port was specified
ModbusMaster node(2);


void setup()
{
  // initialize Modbus communication baud rate
  node.begin(19200);
}


void loop()
{
  static uint32_t i;
  uint8_t j, result;
  uint16_t data[6];
  
  i++;
  
  // set word 0 of TX buffer to least-significant word of counter (bits 15..0)
  node.setTransmitBuffer(0, lowWord(i));
  
  // set word 1 of TX buffer to most-significant word of counter (bits 31..16)
  node.setTransmitBuffer(1, highWord(i));
  
  // slave: write TX buffer to (2) 16-bit registers starting at register 0
  result = node.writeMultipleRegisters(0, 2);
  
  // slave: read (6) 16-bit registers starting at register 2 to RX buffer
  result = node.readHoldingRegisters(2, 6);
  
  // do something with data if read is successful
  if (result == node.ku8MBSuccess)
  {
    for (j = 0; j < 6; j++)
    {
      data[j] = node.getResponseBuffer(j);
    }
  }
}

Project inspired by Arduino Modbus Master.