Modbus TCP/IP library for Arduino based PLC’s

Modbus TCP/IP library for Arduino based PLC’s

On this post it’s showed one of the best Modbus TCP/IP libraries. This library has been tasted on Industrial Shields labs and it fits communicating with other Modbus TCP/IP industrial devices.  

How to install?

  1. Download the zip file on Modbus TCP/IP libraries.
  2. Extract the .ZIP file.
  3. Copy and paste the mudbus.h and mudbus.cpp in the directory of the sketch that calls the mudbus library.

If using the «Ethernet2» library the mudbus.h needs to be modified. Inside the mudbus.h file there is an #include <Ethernet.h>. This has to be changed for #include <Ethernet2.h> in order to use the mudbus library for the M-Duino PLUS version.


How to use it?

By default, this Modbus library communication protocol is placed on port 502 (Modbus standard). You can change this port changing the value of MB_PORT on Mudbus.h file.

Mudbus library accept the next Modbus standard functions: 1 Read Coils , 3 Read Registers, 5 Write Coil, 6 Write Register, 15 Write multiple coils and 16 Write multiple registers. Arduino based PLC will work always as a TCP/IP server with this library. So, on the M-Duino sketch won’t be necessary to take care about which function is M-Duino using. 

There are two types of data, one for analog values or registers and other for digital values or coils. Analog values are integers and digital values are booleans.   

So, on this Modbus protocol you can use this functions or declarations:

Mb.R[0 to 125]:  is used to read or write a integer value on the Modbus protocol. 

Mb.C[0 to 128]:  is used to read or write a bool value on the Modbus protocol. 

Mb.Run(): is a function used to update the Modbus signal on M-Duino memory. 

How to update a analog or digital value from a sensor to the Modbus protocol:

Mb.R[0] = analogRead(I0_7);
Mb.C[0] = digitalRead(I0_0);

*With this simple functions after M-Duino executes the Mb.Run() function, the values places on R[0] or C[0] will be updated by the value from the sensor. 

How to activate a M-Duino output digital or analog from the Modbus protocol:

digitalWrite(Q0_0, Mb.C[100]);
analogWrite(A0_5, Mb.R[12]);

*With this simple functions after M-Duino executes the Mb.Run() function, the real values of Q0.0 and A0.5 will be updated through Modbus values.

*Important: M-Duino needs to be fed between 12V and 24V to make work the Ethernet communication. 

Next is showed a full example: 

#include <SPI.h> 
#include  <Ethernet.h> // Use Ethernet2.h if you are using a M-Duino PLUS version
#include "Mudbus.h"

Mudbus Mb;
//Function codes 1(read coils), 3(read registers), 5(write coil), 6(write register)
//signed int Mb.R[0 to 125] and bool Mb.C[0 to 128] MB_N_R MB_N_C
//Port 502 (defined in Mudbus.h) MB_PORT

void setup(){
  uint8_t mac[]     = { 0x90, 0xA2, 0xDA, 0x00, 0x51, 0x06 };
  uint8_t ip[]      = { 192, 168, 2, 130 };
  uint8_t gateway[] = { 192, 168, 2, 1 };
  uint8_t subnet[]  = { 255, 255, 255, 0 };
  Ethernet.begin(mac, ip, gateway, subnet);
  //With the last update of Industrial Shields boards it's not necessary to use function pinMode() 

void loop(){
  Mb.Run(); //Update the values of Mb.R and Mb.C every loop cycle
  //Analog inputs
  Mb.R[0] = analogRead(I0_12); //pin I0.12 to Mb.R[0]
  Mb.R[1] = analogRead(I0_11);
  Mb.R[2] = analogRead(I0_10);
  Mb.R[3] = analogRead(I0_9);
  Mb.R[4] = analogRead(I0_8);
  Mb.R[5] = analogRead(I0_7);
  //Analog outputs
  analogWrite(A0_5, Mb.R[6]); //pin A0.5 from Mb.R[6]

  //Digital inputs
  Mb.C[7] = digitalRead(I0_0); //pin I0.0 to Mb.C[7]
  //Digital outputs
  digitalWrite(Q0_2, Mb.C[100]); //pin Q0.2 from Mb.C[8]
  digitalWrite(Q0_1, Mb.C[101]); 

See Also:

Los comentarios están cerrados