Giter VIP home page Giter VIP logo

modbus-arduino's Introduction

Modbus Library for Arduino

This library allows your Arduino 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).

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:

http://pt.wikipedia.org/wiki/Modbus http://www.modbus.org/docs/Modbus_Application_Protocol_V1_1b.pdf http://www.modbus.org/docs/Modbus_Messaging_Implementation_Guide_V1_0b.pdf

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 Serial (RS-232 or RS485) and Modbus IP (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

Notes:

  1. When using Modbus IP the transport protocol is TCP (port 502) and, by default, the connection is terminated to each transmitted message, that is, is not a keep-alive type connection. If you need a TCP keep-alive connection you have to remove comments of this line in ModbusIP.h header (or ModbusIP_* headers):
#define TCP_KEEP_ALIVE
  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
  1. When using Modbus Serial is possible to choose between Hardware Serial(default) or Software Serial. In this case you must edit the ModbusSerial.h file and comment out the following line:
#define USE_SOFTWARE_SERIAL

Now, You can build your main program putting all necessary includes:

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

And in the setup() function:

SoftwareSerial myserial(2,3);
mb.config(&myserial, 38400);   // mb.config(mb.config(&myserial, 38400, 4) for RS-485

How to

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

  • Modbus - Base Library
  • ModbusSerial - Modbus Serial Library (RS-232 and RS-485)
  • ModbusIP - Modbus IP Library (standard Ethernet Shield)
  • ModbusIP_ENC28J60 - Modbus IP Library (for ENC28J60 chip)
  • ModbusIP_ESP8266AT - Modbus IP Library (for ESP8266 chip with AT firmware)

If you want to use Modbus in ESP8266 without the Arduino, I have news: http://www.github.com/andresarmento/modbus-esp8266

By opting for Modbus Serial or Modbus IP 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.

Modbus Serial

There are four examples that can be accessed from the Arduino interface, once you have installed the library. Let's look at the example Lamp.ino (only the parts concerning Modbus will be commented):

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

Inclusion of the necessary libraries.

const int LAMP1_COIL = 100;

Sets the Modbus register to represent a lamp or LED. This value is the offset (0-based) to be placed in its supervisory or testing software. Note that if your software uses offsets 1-based the set value there should be 101, for this example.

ModbusSerial mb;

Create the mb instance (ModbusSerial) to be used.

mb.config (&Serial, 38400, SERIAL_8N1);
mb.setSlaveId (10);

Sets the serial port and the slave Id. Note that the serial port is passed as reference, which permits the use of other serial ports in other Arduino models. The bitrate and byte format (8N1) is being set. If you are using RS-485 the configuration of another pin to control transmission/reception is required. This is done as follows:

mb.config (& Serial, 38400, SERIAL_8N1, 2);

In this case, the pin 2 will be used to control TX/RX.

mb.addCoil (LAMP1_COIL);

Adds the register type Coil (digital output) that will be responsible for activating the LED or lamp and verify their status. The library allows you to set an initial value for the register:

mb.addCoil (LAMP1_COIL, true);

In this case the register is added and set to true. If you use the first form the default value is false.

mb.task ();

This method makes all magic, answering requests and changing the registers if necessary, it should be called only once, early in the loop.

digitalWrite (ledPin, mb.Coil (LAMP1_COIL));

Finally the value of LAMP1_COIL register is used to drive the lamp or LED.

In much the same way, the other examples show the use of other methods available in the library:

void addCoil (offset word, bool value)
void addHreg (offset word, word value)
void addIsts (offset word, bool value)
void addIreg (offset word, word value)

Adds registers and configures initial value if specified.

bool Coil (offset word, bool value)
bool Hreg (offset word, word value)
bool Ists (offset word, bool value)
bool IREG (offset word, word value)

Sets a value to the register.

bool Coil (offset word)
Hreg word (word offset)
bool Ists (offset word)
IREG word (word offset)

Returns the value of a register.

Modbus IP

There are four examples that can be accessed from the Arduino interface, once you have installed the library. Let's look at the example Switch.ino (only the parts concerning Modbus will be commented):

#include <SPI.h>
#include <Ethernet.h>
#include <Modbus.h>
#include <ModbusIP.h>

Inclusion of the necessary libraries.

const int SWITCH_ISTS = 100;

Sets the Modbus register to represent the switch. This value is the offset (0-based) to be placed in its supervisory or testing software. Note that if your software uses offsets 1-based the set value there should be 101, for this example.

ModbusIP mb;

Create the mb instance (ModbusIP) to be used.

mac byte [] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
ip byte [] = {192, 168, 1, 120};
mb.config (mac, ip);

Sets the Ethernet shield. The values ​​of the MAC Address and the IP are passed by the config() method. The syntax is equal to Arduino Ethernet class, and supports the following formats:

void config (uint8_t * mac)
void config (uint8_t * mac, IPAddress ip)
void config (uint8_t * mac, IPAddress ip, IPAddress dns)
void config (uint8_t * mac, IPAddress ip, IPAddress dns, gateway IPAddress)
void config (uint8_t * mac, IPAddress ip, IPAddress dns, IPAddress gateway, subnet IPAddress)

Then we have:

mb.addIsts (SWITCH_ISTS);

Adds the register type Input Status (digital input) that is responsible for detecting if a switch is in state on or off. The library allows you to set an initial value for the register:

mb.addIsts (SWITCH_ISTS, true);

In this case the register is added and set to true. If you use the first form the default value is false.

mb.task ();

This method makes all magic, answering requests and changing the registers if necessary, it should be called only once, early in the loop.

mb.Ists (SWITCH_ISTS, digitalRead (switchPin));

Finally the value of SWITCH_ISTS register changes as the state of the selected digital input.

Modbus IP(ENC28J60)

The Arduino standard Ethernet shield is based on chip WIZnet W5100, therefore the IDE comes with this library installed. If you have a shield based on ENC28J60 from Microchip you must install another Ethernet library. Among several available we chose EtherCard.

Download the EtherCard in https://github.com/jcw/ethercard and install it in your IDE. Use the following includes in your sketches:

#include <EtherCard.h>
#include <ModbusIP_ENC28J60.h>
#include <Modbus.h>

Done! The use of Modbus functions is identical to the ModbusIP library described above.

Notes:

  1. EtherCard is configured to use the pins 10, 11, 12 and 13.

  2. The voltage for shields based on ENC28J60 is generally 3.3V.

  3. Another alternative is to use the ENC28J60 UIPEthernet library, available from https://github.com/ntruchsess/arduino_uip. This library was made so that mimics the same standard Ethernet library functions, whose work is done by Wiznet W5100 chip. As the ENC28J60 not have all the features of the other chip, the library UIPEthernet uses a lot of memory, as it has to do in software what in the shield Wiznet is made in hardware. If for some reason you need to use this library, just change the file ModbusIP.h and your sketches, changing the lines:

#include <Ethernet.h>

by

#include <UIPEthernet.h>

Then, you can use the ModbusIP library (not the ModbusIP_ENC28J60). In fact it allows any library or skecth, made for Wiznet shield be used in shield ENC28J60. The big problem with this approach (and why we chose EtherCard) is that UIPEthernet library + ModbusIP uses about 60% arduino program memory, whereas with Ethercard + ModbusIP_ENC28J60 this value drops to 30%!

Modbus IP (ESP8266 AT)

Modules based on ESP8266 are quite successful and cheap. With firmware that responds to AT commands (standard on many modules) you can use them as a simple wireless network interface to the Arduino.

The firmware used in the module (at_v0.20_on_SDKv0.9.3) is available at: http://www.electrodragon.com/w/ESP8266_AT-command_firmware

(Other AT firmwares compatible with ITEAD WeeESP8266 Library should work)

Warning: Firmware such as NodeMCU and MicroPython does not work because libraries used here depend on a firmware that responds to AT commands via serial interface. The firmware mentioned are used when you want to use ESP8266 modules without the Arduino.

You will need the WeeESP8266 library (ITEAD) for the Arduino. Download from:

https://github.com/itead/ITEADLIB_Arduino_WeeESP8266 and install in your IDE.

Notes:

  1. The ESP8266 library can be used with a serial interface by hardware (HardwareSerial) or by software (SoftwareSerial). By default it will use HardwareSerial, to change edit the file ESP8266.h removing the comments from line:
#define ESP8266_USE_SOFTWARE_SERIAL
  1. Remember that the power of ESP8266 module is 3.3V.

For Modbus IP (ESP8266 AT) there is four examples that can be accessed from the Arduino interface. Let's look at the example Lamp.ino (only the parts concerning Modbus will be commented):

#include <ESP8266.h>
#include <SoftwareSerial.h>   //Apenas se utilizar Softwareserial para se comunicar com o módulo
#include <Modbus.h>
#include <ModbusIP_ESP8266AT.h>

Inclusion of the necessary libraries.

SoftwareSerial wifiSerial(2 , 3);

Creates the serial interface via software using pins 2 (RX) and 3 (TX). So it can use hardware for the serial communication with the PC (e.g. for debugging purposes) in Arduino models that have only one serial (Ex .: Arduino UNO).

ESP8266 wifi(wifiSerial, 9600);

Create the wifi object (ESP8266) specifying the rate in bps. Warning: If you use SoftwareSerial do not specify a baud rate of 115200bps or more for the serial because it will not function. Some firmware / modules comes with 115200bps by default. You will have to change the module via AT command:

AT+CIOBAUD=9600

Continuing with our example:

const int LAMP1_COIL = 100;

Sets the Modbus register to represent a lamp or LED. This value is the offset (0-based) to be placed in its supervisory or testing software. Note that if your software uses offsets 1-based the set value there should be 101, for this example.

ModbusIP mb;

Create the mb instance (ModbusSerial) to be used.

mb.config(wifi, "your_ssid", "your_password");

Configure ESP8266 module. The values quoted correspond to the network name (SSID) and security key. By default IP configuration is received via DHCP. See the end of the section how to have an Static IP (important so you do not need to change the master / supervisor if the IP changes).

Folowing, we have:

mb.addCoil (LAMP1_COIL);

Adds the register type Coil (digital output) that will be responsible for activating the LED or lamp and verify their status. The library allows you to set an initial value for the register:

mb.addCoil (LAMP1_COIL, true);

In this case the register is added and set to true. If you use the first form the default value is false.

mb.task();

This method makes all magic, answering requests and changing the registers if necessary, it should be called only once, early in the loop.

digitalWrite(ledPin, mb.Coil(LAMP1_COIL));

Finally the value of LAMP1_COIL register is used to drive the lamp or LED.

Quite similarly to other examples show the use of other methods available in the library.

Using a static IP on the ESP8266 module

We are aware today of two options:

  1. In your router configure the MAC address of the module so that the IP address provided by DHCP is always the same (Most routers have this feature).

  2. In your code, include two lines to change the IP address after the module configuration:

mb.config(wifi, "your_ssid", "your_password");
delay(1000);
wifiSerial.println("AT+CIPSTA=\"192.168.1.44\"");

Note .: For the module to receive IP via DHCP again you will need to remove the lines and run (at least once) the command: AT + CWDHCP = 1.1 via direct connection to the module, either:

wifiSerial.println("AT+CWDHCP=1,1");

Other Modbus libraries

Arduino Modbus RTU
Author: Juan Pablo Zometa, Samuel and Marco Andras Tucsni
Year: 2010
Website: https://sites.google.com/site/jpmzometa/

Simple Modbus
Author: Bester.J
Year: 2013 Website: https://code.google.com/p/simple-modbus/

Arduino-Modbus slave
Jason Vreeland [CodeRage]
Year: 2010
Website: http://code.google.com/p/arduino-modbus-slave/

Mudbus (Modbus TCP)
Author: Dee Wykoff
Year: 2011
Website: http://code.google.com/p/mudbus/

ModbusMaster Library for Arduino
Author: Doc Walker
Year: 2012
Website: https://github.com/4-20ma/ModbusMaster
Website: http://playground.arduino.cc/Code/ModbusMaster

Contributions

http://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.

modbus-arduino's People

Contributors

andresarmento avatar michal-konopinski avatar skefer avatar tomatlab avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

modbus-arduino's Issues

ModbusIP Atmega168 7,32Mhz

Hi,

I use the Modbus library with board UNO and was working perfect.
The same program I compile to ATmega168 with clock 7,32Mhz (MiniCore boards) and looks that is stack. The program use 99% of the memory.
The test values are send to outputs of MCP23017 only if I disable line mb.task();

The problem is the memory or clock?

Modbus Ip - It works but with Timeout Error

Hello ! I could connect an arduino Due with a w5100 mini ethernet shield. I can use the entire library. I use modbus poll or radzio for a modbus tcp ip master. The problem is that for example if I read a hold reg, i get a timeout error or ( write error read error ) and then I can read. So it is Yes-No-Yes-No-Yes-No. It´s like i lose connection, maybe something about times. I use mudbus library and mgsmodbus and I didn´t have that problem !

problem

As you see , i can run it OK but first an error and then it goes, error.. goes.. error .. goes ? Don´t you think it can be a time problem at the library ?

mail : [email protected]

freing _frame too early (writeMultipleCoils sets wrong values)

In this function for example _frame is freed and after that frame[6+i] is read. But frame actually points to _frame:
void Modbus::writeMultipleCoils(byte* frame,word startreg, word numoutputs, byte bytecount)

See code changes below:

Original code:

//Clean frame buffer
free(_frame);
_len = 5;
_frame = (byte *) malloc(_len);
if (!_frame) {
this->exceptionResponse(MB_FC_WRITE_COILS, MB_EX_SLAVE_FAILURE);
return;
}

_frame[0] = MB_FC_WRITE_COILS;
_frame[1] = startreg >> 8;
_frame[2] = startreg & 0x00FF;
_frame[3] = numoutputs >> 8;
_frame[4] = numoutputs & 0x00FF;

byte bitn = 0;
word totoutputs = numoutputs;
word i;
while (numoutputs--) {
    i = (totoutputs - numoutputs) / 8;
    this->Coil(startreg, bitRead(frame[6+i], bitn));
    //increment the bit index
    bitn++;
    if (bitn == 8) bitn = 0;
    //increment the register
    startreg++;
}

Changed code:

byte bitn = 0;
word totoutputs = numoutputs;
word i;
word tempNumoutputs = numoutputs;
word tempStartreg = startreg;
while (tempNumoutputs) {
    i = (totoutputs - tempNumoutputs) / 8;
    this->Coil(tempStartreg, bitRead(frame[6+i], bitn));
    //increment the bit index
    bitn++;
    if (bitn == 8) bitn = 0;
    //increment the register
    tempStartreg++;
	tempNumoutputs--;
}

//Clean frame buffer
**free(_frame);**
_len = 5;
_frame = (byte *) malloc(_len);
if (!_frame) {
    this->exceptionResponse(MB_FC_WRITE_COILS, MB_EX_SLAVE_FAILURE);
    return;
}

_frame[0] = MB_FC_WRITE_COILS;
_frame[1] = startreg >> 8;
_frame[2] = startreg & 0x00FF;
_frame[3] = numoutputs >> 8;
_frame[4] = numoutputs & 0x00FF;

See issue #35 as well.

Register type Coil doesn't work

Hi,
the library method Coil(), used as digital output is not working. Tested with an Arduino Nano.
I guess this is a bug...

Biblioteca ESP8266

Olá. Estou utilizando a IDE do Arduino para compilar o seu exemplo (LAMP) com modbus e ESP8266(nodeMCU 1.0) porém está ocorrendo um erro de compilação porque o programa não reconheceu a biblioteca ESP8266.h e consequentemente também não reconhece a biblioteca ModbusIP_ESP8266AT
ele reconheceu apenas as bibliotecas Modbus,h e SoftwareSerial.h . Onde posso encontrar a biblioteca ESP8266.h ? Já baixei um pacote de bibliotecas (ESP8266 by ESP8266 Comunnity) porém mesmo assim ele não reconhece essa biblioteca, somente a ESP8266WiFi.h

The result is an error.

I am doing a coding test using Arduino board.
Unwanted results occur during testing.
If you try to read data remotely after inserting m_Modbus.Ists value, an error occurs in multiples of 8.
I think that the Modbus.cpp file contains an error in the void Modbus :: readInputStatus () function.
please check.
di-error2
di error 1

Temporarily modifying and testing as below.

#if 0 while (numregs--) { i = (totregs - numregs) / 8; if (this->Ists(startreg)) bitSet(_frame[2+i], bitn); else bitClear(_frame[2+i], bitn); //increment the bit index bitn++; if (bitn == 8) bitn = 0; //increment the register startreg++; } #else do{ i = (totregs - numregs) / 8; if (this->Ists(startreg)) bitSet(_frame[2+i], bitn); else bitClear(_frame[2+i], bitn); //increment the bit index bitn++; if (bitn == 8) bitn = 0; //increment the register startreg++; } while (numregs--); #endif

di-ok

Modbus scanner not working? Siemens HMI implementation.

Hello,

I am trying to use the Modbus IP lib to connect to an Siemens HMI.

As of now no luck to get it working.

After a lot of testing, I found out that I could not make a connection with the CAS modbus scanner.

If someone can help me further, it would be very appreciated

Kind regards,
Raphael

Modbus RTU slave: auto baud rate detection

Hi,

is possible to use auto baud detection for Modbus RTU slave?

Something like this: https://gist.github.com/tuxmartin/46ca107057c650fbe6f63dcae5ee067d

My idea:

  • Modbus Master (Raspberry Pi) set serial port speed to X.
  • ? (Modbus Master send broadcast message for synchronization every X seconds.) ?
  • Modbus Slave (Arduino) run detection of speed every X seconds.
  • Modbus Master periodically read data from all slaves - if any of slaves do not respond, slow down serial speed.

Thanks.

How can i know the slave status is good or bad?

Hello
i have a problem that some times the status in server is bad...and its very anoying . how can i found the client connection is good and if the connection is bad i disconnect wifi and reconnect ...someone please help me .
thank you

Coils go to 0 at start regardless of what the set value is

I have this block of code in my setup(), which should theoretically set all coils to 1, but when I turn on the arduino the coils go to 0, and only go to the correct value when I send a modbus command to the arduino (even a read). Is there something that I am not understanding or is this a bug?

    const bool defaultCoilValue = 1;
    for (int i = startCoilAddress; i <= numberOfCoils; i++) {
        mb.addCoil(i, defaultCoilValue);
        mb.addIsts(i, defaultCoilValue);
    }

Arduino Due

Hi, Is this library compatible with the Arduino Due? I am traying run a Basic lamp example and I get a error with the hardware serial format, can you help me?

AT command mode not working

hello
i program the esp8266 at command mode lamp.ino on mega2560 and connect it with OPC but its always get Bad data and not responding..:(
what is the problem here?
(i use esp-01 black versian and i dont flash it.)

ESP8266 modbus tcpip shows compiling errors even including all library

i got following errors while compiling.what should i do..? sorry for my poor english

Arduino: 1.6.9 (Windows XP), Board: "Arduino Nano, ATmega328"

Lamp:13: error: no matching function for call to 'ESP8266::ESP8266(SoftwareSerial&, int)'

ESP8266 wifi(wifiSerial,9600);

                     ^

C:\DOCUME1\sree\LOCALS1\Temp\arduino_modified_sketch_153828\Lamp.ino:13:29: note: candidates are:

In file included from C:\DOCUME1\sree\LOCALS1\Temp\arduino_modified_sketch_153828\Lamp.ino:7:0:

C:\Documents and Settings\sree\My Documents\Arduino\libraries\ITEADLIB_Arduino_WeeESP8266-master/ESP8266.h:60:5: note: ESP8266::ESP8266(HardwareSerial&, uint32_t)

ESP8266(HardwareSerial &uart, uint32_t baud = 9600);

^
C:\Documents and Settings\sree\My Documents\Arduino\libraries\ITEADLIB_Arduino_WeeESP8266-master/ESP8266.h:60:5: note: no known conversion for argument 1 from 'SoftwareSerial' to 'HardwareSerial&'

C:\Documents and Settings\sree\My Documents\Arduino\libraries\ITEADLIB_Arduino_WeeESP8266-master/ESP8266.h:38:7: note: constexpr ESP8266::ESP8266(const ESP8266&)

class ESP8266 {

^
C:\Documents and Settings\sree\My Documents\Arduino\libraries\ITEADLIB_Arduino_WeeESP8266-master/ESP8266.h:38:7: note: candidate expects 1 argument, 2 provided

C:\Documents and Settings\sree\My Documents\Arduino\libraries\ITEADLIB_Arduino_WeeESP8266-master/ESP8266.h:38:7: note: constexpr ESP8266::ESP8266(ESP8266&&)

C:\Documents and Settings\sree\My Documents\Arduino\libraries\ITEADLIB_Arduino_WeeESP8266-master/ESP8266.h:38:7: note: candidate expects 1 argument, 2 provided

exit status 1
no matching function for call to 'ESP8266::ESP8266(SoftwareSerial&, int)'

Software Serial Doesnt work for Modbus RTU

Hello
I have been using this Modbus Serial Library for a while. For one of my project I need to use SoftwareSerial. But I am getting the following errors

libraries\Modbus\Modbus.cpp.o (symbol from plugin): In function Modbus::Modbus()': (.text+0x0): multiple definition of Modbus::Modbus()'
sketch\Modbus.cpp.o (symbol from plugin):(.text+0x0): first defined here
libraries\Modbus\Modbus.cpp.o (symbol from plugin): In function Modbus::Modbus()': (.text+0x0): multiple definition of Modbus::Modbus()'
sketch\Modbus.cpp.o (symbol from plugin):(.text+0x0): first defined here
libraries\Modbus\Modbus.cpp.o (symbol from plugin): In function Modbus::Modbus()': (.text+0x0): multiple definition of Modbus::searchRegister(unsigned int)'
sketch\Modbus.cpp.o (symbol from plugin):(.text+0x0): first defined here
libraries\Modbus\Modbus.cpp.o (symbol from plugin): In function Modbus::Modbus()': (.text+0x0): multiple definition of Modbus::addReg(unsigned int, unsigned int)'
sketch\Modbus.cpp.o (symbol from plugin):(.text+0x0): first defined here
libraries\Modbus\Modbus.cpp.o (symbol from plugin): In function Modbus::Modbus()': (.text+0x0): multiple definition of Modbus::Reg(unsigned int, unsigned int)'
sketch\Modbus.cpp.o (symbol from plugin):(.text+0x0): first defined here
libraries\Modbus\Modbus.cpp.o (symbol from plugin): In function Modbus::Modbus()': (.text+0x0): multiple definition of Modbus::Reg(unsigned int)'
sketch\Modbus.cpp.o (symbol from plugin):(.text+0x0): first defined here
libraries\Modbus\Modbus.cpp.o (symbol from plugin): In function Modbus::Modbus()': (.text+0x0): multiple definition of Modbus::addHreg(unsigned int, unsigned int)'
sketch\Modbus.cpp.o (symbol from plugin):(.text+0x0): first defined here
libraries\Modbus\Modbus.cpp.o (symbol from plugin): In function Modbus::Modbus()': (.text+0x0): multiple definition of Modbus::Hreg(unsigned int, unsigned int)'
sketch\Modbus.cpp.o (symbol from plugin):(.text+0x0): first defined here
libraries\Modbus\Modbus.cpp.o (symbol from plugin): In function Modbus::Modbus()': (.text+0x0): multiple definition of Modbus::Hreg(unsigned int)'
sketch\Modbus.cpp.o (symbol from plugin):(.text+0x0): first defined here
libraries\Modbus\Modbus.cpp.o (symbol from plugin): In function Modbus::Modbus()': (.text+0x0): multiple definition of Modbus::addCoil(unsigned int, bool)'
sketch\Modbus.cpp.o (symbol from plugin):(.text+0x0): first defined here
libraries\Modbus\Modbus.cpp.o (symbol from plugin): In function Modbus::Modbus()': (.text+0x0): multiple definition of Modbus::addIsts(unsigned int, bool)'
sketch\Modbus.cpp.o (symbol from plugin):(.text+0x0): first defined here
libraries\Modbus\Modbus.cpp.o (symbol from plugin): In function Modbus::Modbus()': (.text+0x0): multiple definition of Modbus::addIreg(unsigned int, unsigned int)'
sketch\Modbus.cpp.o (symbol from plugin):(.text+0x0): first defined here
libraries\Modbus\Modbus.cpp.o (symbol from plugin): In function Modbus::Modbus()': (.text+0x0): multiple definition of Modbus::Coil(unsigned int, bool)'
sketch\Modbus.cpp.o (symbol from plugin):(.text+0x0): first defined here
libraries\Modbus\Modbus.cpp.o (symbol from plugin): In function Modbus::Modbus()': (.text+0x0): multiple definition of Modbus::Ists(unsigned int, bool)'
sketch\Modbus.cpp.o (symbol from plugin):(.text+0x0): first defined here
libraries\Modbus\Modbus.cpp.o (symbol from plugin): In function Modbus::Modbus()': (.text+0x0): multiple definition of Modbus::Ireg(unsigned int, unsigned int)'
sketch\Modbus.cpp.o (symbol from plugin):(.text+0x0): first defined here
libraries\Modbus\Modbus.cpp.o (symbol from plugin): In function Modbus::Modbus()': (.text+0x0): multiple definition of Modbus::Coil(unsigned int)'
sketch\Modbus.cpp.o (symbol from plugin):(.text+0x0): first defined here
libraries\Modbus\Modbus.cpp.o (symbol from plugin): In function Modbus::Modbus()': (.text+0x0): multiple definition of Modbus::Ists(unsigned int)'
sketch\Modbus.cpp.o (symbol from plugin):(.text+0x0): first defined here
libraries\Modbus\Modbus.cpp.o (symbol from plugin): In function Modbus::Modbus()': (.text+0x0): multiple definition of Modbus::Ireg(unsigned int)'
sketch\Modbus.cpp.o (symbol from plugin):(.text+0x0): first defined here
libraries\Modbus\Modbus.cpp.o (symbol from plugin): In function Modbus::Modbus()': (.text+0x0): multiple definition of Modbus::exceptionResponse(unsigned char, unsigned char)'
sketch\Modbus.cpp.o (symbol from plugin):(.text+0x0): first defined here
libraries\Modbus\Modbus.cpp.o (symbol from plugin): In function Modbus::Modbus()': (.text+0x0): multiple definition of Modbus::readRegisters(unsigned int, unsigned int)'
sketch\Modbus.cpp.o (symbol from plugin):(.text+0x0): first defined here
libraries\Modbus\Modbus.cpp.o (symbol from plugin): In function Modbus::Modbus()': (.text+0x0): multiple definition of Modbus::writeSingleRegister(unsigned int, unsigned int)'
sketch\Modbus.cpp.o (symbol from plugin):(.text+0x0): first defined here
libraries\Modbus\Modbus.cpp.o (symbol from plugin): In function Modbus::Modbus()': (.text+0x0): multiple definition of Modbus::writeMultipleRegisters(unsigned char*, unsigned int, unsigned int, unsigned char)'
sketch\Modbus.cpp.o (symbol from plugin):(.text+0x0): first defined here
libraries\Modbus\Modbus.cpp.o (symbol from plugin): In function Modbus::Modbus()': (.text+0x0): multiple definition of Modbus::readCoils(unsigned int, unsigned int)'
sketch\Modbus.cpp.o (symbol from plugin):(.text+0x0): first defined here
libraries\Modbus\Modbus.cpp.o (symbol from plugin): In function Modbus::Modbus()': (.text+0x0): multiple definition of Modbus::readInputStatus(unsigned int, unsigned int)'
sketch\Modbus.cpp.o (symbol from plugin):(.text+0x0): first defined here
libraries\Modbus\Modbus.cpp.o (symbol from plugin): In function Modbus::Modbus()': (.text+0x0): multiple definition of Modbus::readInputRegisters(unsigned int, unsigned int)'
sketch\Modbus.cpp.o (symbol from plugin):(.text+0x0): first defined here
libraries\Modbus\Modbus.cpp.o (symbol from plugin): In function Modbus::Modbus()': (.text+0x0): multiple definition of Modbus::writeSingleCoil(unsigned int, unsigned int)'
sketch\Modbus.cpp.o (symbol from plugin):(.text+0x0): first defined here
libraries\Modbus\Modbus.cpp.o (symbol from plugin): In function Modbus::Modbus()': (.text+0x0): multiple definition of Modbus::writeMultipleCoils(unsigned char*, unsigned int, unsigned int, unsigned char)'
sketch\Modbus.cpp.o (symbol from plugin):(.text+0x0): first defined here
libraries\Modbus\Modbus.cpp.o (symbol from plugin): In function Modbus::Modbus()': (.text+0x0): multiple definition of Modbus::receivePDU(unsigned char*)'
sketch\Modbus.cpp.o (symbol from plugin):(.text+0x0): first defined here
collect2.exe: error: ld returned 1 exit status
Multiple libraries were found for "Modbus.h"
Used: C:\Users\pviha\OneDrive\Documents\Arduino\libraries\Modbus
Not used: C:\Users\pviha\OneDrive\Documents\Arduino\libraries\modbus-esp8266
exit status 1
Error compiling for board Arduino Nano.

Please guide

Thanks

id change

Hi, i'm using modbus tcp as server on arduino from this project, is any posibility to change ID number of my server? i can read the same values on different ID's, and i think its little weird

Register 4th and 5th are corrupt when writing multiple registers

Hi,
Could you please help us to fix the issue?
we are connecting our micro controller to Ethernet Shield V2 using ModbusIP and Modbus libraries.
We tried to write values to 12 registers from 106 to 116. 4th byte and 5th byte got a different value that we wrote from our software. However, the other registers had right values.
We test with chatserver program to read raw data and compared with data that we wrote down. It showed right value when we read raw data. So when we use Modbus library. we have these 2 bytes with wrong value.
Here is the code!
Thank you for your help!

#include <ModbusIP.h>
#include <Modbus.h>
#include <Ethernet2.h>
#include <SPI.h>

int tanklevel;
int tanklevel_old;
int vfdspeed;
word vfdspeed106;
word vfdspeed107;
word vfdspeed108;
word vfdspeed109;
word vfdspeed110;
word vfdspeed111;
word vfdspeed112;
word vfdspeed113;
word vfdspeed114;
word vfdspeed115;
word vfdspeed116;
int vfdspeed_old;
bool valvepos;
bool valvepos_old;
bool pump;
bool pump_old;

const int tanklevel_ir = 100;
const int vfdspeed_hr = 105;
const word vfdspeed_hr106 = 106;
const word vfdspeed_hr107 = 107;
const word vfdspeed_hr108 = 108;
const word vfdspeed_hr109 = 109;
const word vfdspeed_hr110 = 110;
const word vfdspeed_hr111 = 111;
const word vfdspeed_hr112 = 112;
const word vfdspeed_hr113 = 113;
const word vfdspeed_hr114 = 114;
const word vfdspeed_hr115 = 115;
const word vfdspeed_hr116 = 116;
const int valvepos_is = 110;
const int pump_cl = 115;

//Modbus IP Object
ModbusIP mb;

void setup() {
Serial.begin(9600);

// The media access control (ethernet hardware) address for the shield
byte mac[] = { 0x90, 0xA2, 0xDA, 0x11, 0x15, 0x66 };
// The IP address for the shield
byte ip[] = { 192, 168, 1, 138 };
//Config Modbus IP
mb.config(mac, ip);

mb.addIreg(tanklevel_ir);
mb.addHreg(vfdspeed_hr);
mb.addHreg(vfdspeed_hr106);
mb.addHreg(vfdspeed_hr107);
mb.addHreg(vfdspeed_hr108);
mb.addHreg(vfdspeed_hr109);
mb.addHreg(vfdspeed_hr110);
mb.addHreg(vfdspeed_hr111);
mb.addHreg(vfdspeed_hr112);
mb.addHreg(vfdspeed_hr113);
mb.addHreg(vfdspeed_hr114);
mb.addHreg(vfdspeed_hr115);
mb.addHreg(vfdspeed_hr116);
mb.addIsts(valvepos_is);
mb.addCoil(pump_cl);

tanklevel = 12;
tanklevel_old = 12;
vfdspeed = 150;
vfdspeed_old = 150;
valvepos = true;
valvepos_old = true;
pump = false;
pump_old = false;

mb.Ireg (tanklevel_ir, tanklevel);
mb.Hreg (vfdspeed_hr, vfdspeed);
mb.Ists (valvepos_is, valvepos);
mb.Coil (pump_cl, pump);

DisplayCurrentValues();

}

void loop() {

mb.task();

vfdspeed = mb.Hreg (vfdspeed_hr);
vfdspeed106 = mb.Hreg (vfdspeed_hr106);
vfdspeed107 = mb.Hreg (vfdspeed_hr107);
vfdspeed108 = mb.Hreg (vfdspeed_hr108);
vfdspeed109 = mb.Hreg (vfdspeed_hr109);
vfdspeed110 = mb.Hreg (vfdspeed_hr110);
vfdspeed111 = mb.Hreg (vfdspeed_hr111);
vfdspeed112 = mb.Hreg (vfdspeed_hr112);
vfdspeed113 = mb.Hreg (vfdspeed_hr113);
vfdspeed114 = mb.Hreg (vfdspeed_hr114);
vfdspeed115 = mb.Hreg (vfdspeed_hr115);
vfdspeed116 = mb.Hreg (vfdspeed_hr116);
pump = mb.Coil (pump_cl);

// send updated values
CheckForDataChange();

}

void CheckForDataChange() {

boolean data_has_changed = false;

if (tanklevel_old != tanklevel) {
data_has_changed = true;
tanklevel_old = tanklevel;
}

if (vfdspeed_old != vfdspeed) {
data_has_changed = true;
vfdspeed_old = vfdspeed;
}

if (valvepos_old != valvepos) {
data_has_changed = true;
valvepos_old = valvepos;
}

if (pump_old != pump) {
data_has_changed = true;
pump_old = pump;
}

if (data_has_changed == true) {
DisplayCurrentValues();
}

}

void DisplayCurrentValues() {

String tmpstr;
String pos;
String pstat;

if (valvepos == true) {
pos = "Closed";
}
else {
pos = "Open";
}

if (pump == true) {
pstat = "Run";
}
else {
pstat = "Stop";
}

tmpstr = "Tank Level: " + String(tanklevel) + " ft";
tmpstr = tmpstr + " | VFD Speed: " + vfdspeed + " rpm";
tmpstr = tmpstr + " | Valve: " + pos;
tmpstr = tmpstr + " | Pump: " + pstat;
tmpstr = tmpstr + " | Register106: " + vfdspeed106;
tmpstr = tmpstr + " | Register107: " + vfdspeed107;
tmpstr = tmpstr + " | Register108: " + vfdspeed108;
tmpstr = tmpstr + " | Register109: " + vfdspeed109;
tmpstr = tmpstr + " | Register110: " + vfdspeed110;
tmpstr = tmpstr + " | Register111: " + vfdspeed111;
tmpstr = tmpstr + " | Register112: " + vfdspeed112;
tmpstr = tmpstr + " | Register113: " + vfdspeed113;
tmpstr = tmpstr + " | Register114: " + vfdspeed114;
tmpstr = tmpstr + " | Register115: " + vfdspeed115;
tmpstr = tmpstr + " | Register116: " + vfdspeed116;
Serial.println(tmpstr);

}

Switch state not changing

I am currently trying to get this to work with the Universal Robots software, but I am having some problems with the inputs.

When I send the command from the robot to turn the coil on, it does that perfectly. But reading status of a input always returns it as HIGH, even when arduino reports it as low.

Anyone got an idea to why that is happening?

#include <SPI.h>
#include <Ethernet.h>
#include <Modbus.h>
#include <ModbusIP.h>

//Modbus Registers Offsets (0-9999)
const int DO1_COIL = 1;
//Used Pins
const int DO1_PIN = 9;

const int DI1_ISTS = 16;
const int DI1_PIN = 3;

//ModbusIP object
ModbusIP mb;

unsigned long statusLast = 0;

void setup()
{
  Serial.begin(9600);
  
  // The media access control (ethernet hardware) address for the shield
  byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

  // The IP address for the shield
  byte ip[] = { 192, 168, 1, 12 };

  //Config Modbus IP
  mb.config(mac, ip);

  //Set ledPin mode
  pinMode(DO1_PIN, OUTPUT);
  pinMode(DI1_PIN, INPUT);

  // Add LAMP1_COIL register - Use addCoil() for digital outputs
  mb.addCoil(DO1_COIL);

  // Add SWITCH_ISTS register - Use addIsts() for digital inputs
  mb.addIsts(DI1_ISTS);
}

void loop()
{
  //Call once inside loop() - all magic here
  mb.task();

  //Attach switchPin to SWITCH_ISTS register
  mb.Ists(DI1_ISTS, digitalRead(DI1_PIN));

  //Attach ledPin to LAMP1_COIL register
  digitalWrite(DO1_PIN, mb.Coil(DO1_COIL));


  if (millis() - statusLast >= 1000)
  {
    statusLast = millis();
    Serial.print("DI1 = ");
    Serial.println(digitalRead(DI1_PIN));
  }
}

ModbusIP_ESP8266AT

Arduino:1.8.2 (Windows 10), Kart:"Arduino/Genuino Uno"

sketch_may07b:13: error: no matching function for call to 'ESP8266::ESP8266(SoftwareSerial&, int)'

ESP8266 wifi(wifiSerial, 9600);

                          ^

C:\Users\Oguzhan Ozdogan\Desktop\sketch_may07b\sketch_may07b.ino:13:30: note: candidates are:

In file included from C:\Users\Oguzhan Ozdogan\Desktop\sketch_may07b\sketch_may07b.ino:7:0:

C:\Users\Oguzhan Ozdogan\Documents\Arduino\libraries\ESP8266/ESP8266.h:60:5: note: ESP8266::ESP8266(HardwareSerial&, uint32_t)

 ESP8266(HardwareSerial &uart, uint32_t baud = 9600);

 ^

C:\Users\Oguzhan Ozdogan\Documents\Arduino\libraries\ESP8266/ESP8266.h:60:5: note: no known conversion for argument 1 from 'SoftwareSerial' to 'HardwareSerial&'

C:\Users\Oguzhan Ozdogan\Documents\Arduino\libraries\ESP8266/ESP8266.h:38:7: note: constexpr ESP8266::ESP8266(const ESP8266&)

class ESP8266 {

   ^

C:\Users\Oguzhan Ozdogan\Documents\Arduino\libraries\ESP8266/ESP8266.h:38:7: note: candidate expects 1 argument, 2 provided

C:\Users\Oguzhan Ozdogan\Documents\Arduino\libraries\ESP8266/ESP8266.h:38:7: note: constexpr ESP8266::ESP8266(ESP8266&&)

C:\Users\Oguzhan Ozdogan\Documents\Arduino\libraries\ESP8266/ESP8266.h:38:7: note: candidate expects 1 argument, 2 provided

exit status 1
no matching function for call to 'ESP8266::ESP8266(SoftwareSerial&, int)'

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

Hi andresarmento, I am trying your lamp Modbus IP ESP8266 AT. I get this error. Can you help me?

How Can I add Ethernet2.h library?

I am using W5500 Chip for ethernet communication. However, I have to use ethernet2.h for all examples.
When I declare ethernet2.h for Lamp or Switch Examples. It shows error with "Error compiling for board Teensy 3.6.". I use teensy 3.6
Thank you

example of how Hreg is updated?

I am using your library in Arduini IDE on my ESP8266. I can only see the inital value I set with AddReg.
how can I update the value within the Loop()

right baud (ESP13 shield)

I just try to use Lamp.ino but I have some not clear stuff whit it. I have mega2560 end esp13 shield. What is the right baud in Lamp.ino /ESP8266 wifi(wifiSerial, xxxx)? Do I have to modify the baud in ESP8266.h / ESP8266 wifi(wifiSerial, xxxx) too? Now the phenomenon is that the Arduino send me an answer for my question but it is not right Modbus telegram. Telegram is too short (FF FF) when 9600 baud is in used and telegram is long short when 115200 is in used. Of course I change the baud in ESP8266 side to. I have tried too with hardware serial too but it has was same. Could anybody help me?
Thank you for all your help!

ReadHoldingRegisters

So I hope that somebody has gone though the same issue as me;
I'm trying to obtain the content of a holding register through the serial port. I can see the content but it's not understandable. What I'm getting It's not ASCII or any other variable type.
resute = node.readHoldingRegisters(25, 6);

W5100-Uno Issues

Hi All,

I know this project is likely dead, but I was hoping for some charity this time of year...

I have been testing the TCP on the Uno with a W5100 as a slave and cannot seem to read anything off it using a scanning program or other. Other http procols work and other Modbus TCP files sort of work.

I have just been testing the lastest code off here and the Lamp example. Nothing else. I can connect with my scanner prgram to its IP bit I get timout errors and cannot read Lamp status at or around its address.

Any thoughts?? I am open to amy suggestions!!

write multiple holding registers

Hello,

I found a bug with the write multiple holding registers function. The problem was that the variable was freed before the values were set in the registers. I'm not sure how to commit my fix so here's the code if someone want to do it.

void Modbus::writeMultipleRegisters(byte* frame,word startreg, word numoutputs, byte bytecount) {
//Check value
if (numoutputs < 0x0001 || numoutputs > 0x007B || bytecount != 2 * numoutputs) {
this->exceptionResponse(MB_FC_WRITE_REGS, MB_EX_ILLEGAL_VALUE);
return;
}

//Check Address (startreg...startreg + numregs)
for (int k = 0; k < numoutputs; k++) {
    if (!this->searchRegister(startreg + 40001 + k)) {
        this->exceptionResponse(MB_FC_WRITE_REGS, MB_EX_ILLEGAL_ADDRESS);
        return;
    }
}

int nbOutputs = numoutputs;
word val;
word i = 0;
while(nbOutputs--) {
	val = val = (word)frame[6+i*2] << 8 | (word)frame[7+i*2];
    this->Hreg(startreg + i, val);
    i++;
}	

//Clean frame buffer
free(_frame);
_len = 5;
_frame = (byte *) malloc(_len);
if (!_frame) {
    this->exceptionResponse(MB_FC_WRITE_REGS, MB_EX_SLAVE_FAILURE);
    return;
}

_frame[0] = MB_FC_WRITE_REGS;
_frame[1] = startreg >> 8;
_frame[2] = startreg & 0x00FF;
_frame[3] = numoutputs >> 8;
_frame[4] = numoutputs & 0x00FF;

_reply = MB_REPLY_NORMAL;

}

Reading 8 Ists & Coils and writing multiple register bug fix

I found some bugs in the library. You reading Ists and Coils for 8 registers, the 8th register will always be 0. Also, when we write multiple holding registers, the last value will be lost.

I have fix the bug in my local copy. I want to submit the bug but I do not know how.

W5500

Can I use W5500 module the same way as W5100 shield?

ENC28J60 not getting a connection

I got a genuine Arduino Ethernet, where I can use the ModbusIP examples just fine, but then I also got this board http://www.ebay.co.uk/itm/192120690138 which got an ENC28J60 ethernet chip on it, but I cant get any of the examples to work with it.

The EtherCard library examples work as expected.

When using the Modbus ENC28J60 version I don't even see the ip on the network.

mbpoll

how cani write;
//Modbus Registers Offsets (0-9999)
const int SENSOR_IREG = 100;
like 3001 or 1001

Float value support?

Hey, is this project dead these days? I haven't seen much activity this year.

Do you plan on ever adding support for modbus float values? The modbus protocol achieves this by using two 16 bit int registers for the value.

This would save me a lot of time if I could write float values directly from sensors on the arduino to registers. Right now since this library only supports modbus integer registers I have to multiply sensor data by 10 and then divide the result on my modbus master/plc by 10 to get decimal values from sensors.

info - (method 1 is the most popular and would give the greatest compatibility) http://www.digi.com/wiki/developer/index.php/Modbus_Floating_Points

more info - http://www.chipkin.com/how-real-floating-point-and-32-bit-data-is-encoded-in-modbus-rtu-messages/

Memory leak?

I am not the most experienced coder, so i could have something wrong, but I have tried to minimize the code to replicate the issue. The code i do now is simple create a single modbus registry, and just re-add data to it.

I get less then 200 loops (often around 175), then the Arduino crashes.
If I modify to add more/less variables, just changes when it crashes.
If I use a mega, instead of an uno, it just takes longer as it has more memory, but same result.

#include <Ethernet.h>
#include <Modbus.h>
#include <ModbusIP.h>


//Modbus Registers Offsets (0-9999)
const int reg01CurXPos = 1;  // Current X Position

//ModbusIP object
ModbusIP mb;

//Variables
unsigned int looper = 0;


void setup() {
    Serial.begin(9600);
    // The media access control (ethernet hardware) address for the shield
    byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
    // The IP address for the shield
    byte ip[] = { 192, 168, 1, 175};
    //Config Modbus IP
    mb.config(mac, ip);
    
    Serial.println("Setup done");
    delay(1000);
}

void loop() {
  mb.addHreg(reg01CurXPos, 1);
  delay(10);
  mb.task(); // Call update on registrys for Modbus.

  Serial.print("Loop - ");
  Serial.println(looper);
  looper++;

  delay(10);
}

Any ideas on what is going on?

There is an error in Modbus.cpp writeMultipleRegisters

There is an error in the Modbus library's Modbus.cpp file, specifically in the Modbus::writeMultipleRegisters function.

Old Code:

void Modbus::writeMultipleRegisters(byte* frame,word startreg, word numoutputs, byte bytecount) {
    //Check value
    if (numoutputs < 0x0001 || numoutputs > 0x007B || bytecount != 2 * numoutputs) {
        this->exceptionResponse(MB_FC_WRITE_REGS, MB_EX_ILLEGAL_VALUE);
        return;
    }

    //Check Address (startreg...startreg + numregs)
    for (int k = 0; k < numoutputs; k++) {
        if (!this->searchRegister(startreg + 40001 + k)) {
            this->exceptionResponse(MB_FC_WRITE_REGS, MB_EX_ILLEGAL_ADDRESS);
            return;
        }
    }

    //Clean frame buffer
    free(_frame);
	_len = 5;
    _frame = (byte *) malloc(_len);
    if (!_frame) {
        this->exceptionResponse(MB_FC_WRITE_REGS, MB_EX_SLAVE_FAILURE);
        return;
    }

    _frame[0] = MB_FC_WRITE_REGS;
    _frame[1] = startreg >> 8;
    _frame[2] = startreg & 0x00FF;
    _frame[3] = numoutputs >> 8;
    _frame[4] = numoutputs & 0x00FF;

    word val;
    word i = 0;
	while(numoutputs--) {
        val = (word)frame[6+i*2] << 8 | (word)frame[7+i*2];
        this->Hreg(startreg + i, val);
        i++;
	}

    _reply = MB_REPLY_NORMAL;
}

New Code:

void Modbus::writeMultipleRegisters(byte* frame,word startreg, word numoutputs, byte bytecount) {
    //Check value
    if (numoutputs < 0x0001 || numoutputs > 0x007B || bytecount != 2 * numoutputs) {
        this->exceptionResponse(MB_FC_WRITE_REGS, MB_EX_ILLEGAL_VALUE);
        return;
    }

    //Check Address (startreg...startreg + numregs)
    for (int k = 0; k < numoutputs; k++) {
        if (!this->searchRegister(startreg + 40001 + k)) {
            this->exceptionResponse(MB_FC_WRITE_REGS, MB_EX_ILLEGAL_ADDRESS);
            return;
        }
    }
    _len = 5;
    _frame = (byte *) malloc(_len);

    if (!_frame) {
        this->exceptionResponse(MB_FC_WRITE_REGS, MB_EX_SLAVE_FAILURE);
        return;
    }
    _frame[0] = MB_FC_WRITE_REGS;
    _frame[1] = startreg >> 8;
    _frame[2] = startreg & 0x00FF;
    _frame[3] = numoutputs >> 8;
    _frame[4] = numoutputs & 0x00FF;



    word val;
    word i = 0;
	while(numoutputs--) {
        val = (word)frame[6+i*2] << 8 | (word)frame[7+i*2];
        this->Hreg(startreg + i, val);
        i++;
	}

    _reply = MB_REPLY_NORMAL;

        //Clean frame buffer
    free(_frame);
	
}

Help with multiple coils/inputs

Hi!

  1. All thumbs up for this great work.

Now my problem, how to modify the code for use with multiple coils.

Here my code:

`/*

I2C Adressen:

0x20 - Relais
0x21 - Eingänge (5 Taster und 3 Extern)
0x22 - LCD 4x20 Zeichen

*/

include <Wire.h>

include <PCF8574.h>

include <LiquidCrystal_I2C.h>

include <EtherCard.h>

include <Modbus.h>

include <ModbusIP_ENC28J60.h>

LiquidCrystal_I2C lcd(0x22, 4, 5, 6, 0, 1, 2, 3, 7, POSITIVE); // Set the LCD I2C address

ModbusIP mb;
//PCF8574 Objekt
PCF8574 inputs; // PCF8574 mit Adresse 0x21
PCF8574 outputs; // PCF8574 mit Adresse 0x20

void setup()
{
// The media access control (ethernet hardware) address for the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// The IP address for the shield
byte ip[] = { 192, 168, 0, 44 };
//Config Modbus IP
mb.config(mac, ip);

for (int i=100;i<8;i++)
{
mb.addIsts(i);
}

for (int i=200;i<8;i++)
{
mb.addCoil(i);
}

io_setup(); // Eingänge und Ausgänge initialisieren

lcd.begin(20,4);
}

void loop()
{
// poll messages
mb.task();

lcd.home ();
lcd.print ("Adr.: 192.168.0.44:502");
lcd.print (" ");

// link the Arduino pins to the Modbus array
io_poll();
}

void io_setup()
{
inputs.begin(0x21);
for (int i=0;i<8;i++)
{
inputs.pinMode(i, INPUT_PULLUP);
}

outputs.begin(0x20);
for (int i=0;i<8;i++)
{
outputs.pinMode(i, OUTPUT);
}
}

/**

  • Link between the Arduino pins and the Modbus array
    */
    void io_poll() {

//Digitale Eingange abfragen
for (int i=100;i<8;i++)
{
mb.Ists(i, inputs.digitalRead( i-100 ) );
}

//Digitale Ausgänge
//Adresse 16-31
outputs.digitalWrite( 0, mb.Coil(202));
outputs.digitalWrite( 1, mb.Coil(201));
outputs.digitalWrite( 2, mb.Coil(200));
outputs.digitalWrite( 3, mb.Coil(203));
outputs.digitalWrite( 4, mb.Coil(207));
outputs.digitalWrite( 5, mb.Coil(206));
outputs.digitalWrite( 6, mb.Coil(205));
outputs.digitalWrite( 7, mb.Coil(204));

}`

I can only read the input at adress 100.

What am i doing wrong?

Thank u for help.

ESP8266 Arduino

I have started working on an ESP8266 with native Arduino (ie: NOT AT mode) version of this (at least for IP). Is this project still alive? I'd be happy to contribute, but I need a little help / guidance on a few things as I'm new to GitHub.

mb.config(mac, ip) mb does not name type

Board: Adafruit ESP Feather with Ethernet FeatherWing
Upload Speed: 115200
Flash Frequency: 80 MHz

I would like to use this ModbusIP thing and tried first to do it like the example, but there is this error message, that mb does not name type.

#include <SPI.h>
#include <Arduino.h>
#include <Ethernet.h>
#include <Modbus.h>
#include <ModbusIP.h>

const int SWITCH_ISTS = 100;
ModbusIP mb();
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
byte ip[] = {0,0,0,0};
mb.config (mac,ip);

void setup() {
// put your setup code here, to run once:

}

void loop() {
// put your main code here, to run repeatedly:

}

sketch_jan17c:12:1: error: 'mb' does not name a type
mb.config (mac,ip);
^
exit status 1
'mb' does not name a type

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.