Arduino Modbus TCP Server Example

100% found this document useful (1 vote)
588 views10 pages
This document describes connecting an Arduino Uno to act as a Modbus TCP server communicating with a Modscan32 client. It includes: 1) An overview of the hardware and software used, includi…

Uploaded by

D.MILITO MILITO
  • Industrial Communication
  • Introduction
  • Hardware Setup
  • Pin Modes
  • Libraries
  • Code
  • Read/Write Registers
  • Related Links

Arduino UNO - ENC28J60 - Modscan32 –ModbusTCP

Modscan32

Industrial Communication:
Arduino Modbus TCP Server and
Modscan32 Client Communication

David Choquenaira F.
ARDUINO MODBUS TCP SERVER & MODSCAN32 CLIENT

0-INTRODUCTION
Arduino Uno ModbusTCP Server & Modscan32 Client
-This Arduino example makes a communication between Arduino Uno(Server)and Modscan32(Client) via
Modbus TCP:

Register type Use as Access Library methods Address

Coil Digital Output Read/Write addCoil(), Coil() 1xxx

Holding Register Analog Output Read/Write addHreg(), Hreg() 4xxx

Input Status Digital Input Read Only addIsts(), Ists() 2xxx

Input Register Analog Input Read Only addIreg(), Ireg() 3xxx

-Also there is an implementation to read Digital and Analog Values from physical devices(Protoboard) and store
them inside Modbus Registers.

1-Hardware will be using:


-ENC28J60 Ethernet Module

-Arduino Uno

-Switch/Modem-Router

-Ethernet Cables

-Protoboard: 1 Led, 10k potentiometer, 2x1k resistor, 1 push button.

2-PinModes: [Link]
-pinA0-> Analog Read(Potentiometer)

-pin03->Digital Output(Led)

-pin09->Digital Input(Switch)

3-Software will be using:


-Modscan32 ([Link]
-Arduino IDE ([Link]

David Choquenaira F. 1
ARDUINO MODBUS TCP SERVER & MODSCAN32 CLIENT

4-Libraries will be using (Modbus TCP Server): [Link]


-EtherCard.h

-Modbus.h

- ModbusIP_ENC28J60.h

5-Architecture will be using:

David Choquenaira F. 2
ARDUINO MODBUS TCP SERVER & MODSCAN32 CLIENT

You can connect the Arduino Uno with the shield directly to your computer or use the internet moden-router to
connect, as in the picture.

6-Enc28j60 Arduino connection:

David Choquenaira F. 3
ARDUINO MODBUS TCP SERVER & MODSCAN32 CLIENT

7-Code:
-What I see and there is no such clarification in many of the tutorials out there is that the common libraries
for Ethernet and Arduino do not work for the ENC28J60 chip.
-In the documentation of Modbus-Arduino says that it works when use the UIPEthernet library but for me it
doesn’t work. ([Link]

David Choquenaira F. 4
ARDUINO MODBUS TCP SERVER & MODSCAN32 CLIENT

-The right path to work with the ENC28J60 is to use the libraries below:

*copy and paste this code in the Arduino IDE.*

/*
* Arduino Uno ModbusTCP Server & Modscan32 Client
*
* This Arduino example makes a communication between Arduino Uno(Server)
* and Modscan32(Client) via Modbus TCP using :Inputs,Coils, Holding Registers
* and Input Registers.
* Also there is an implementation to read Digital and Analog Values from
* physical devices(Protoboard) and store them inside Modbus Registers.
*
* The Circuit:
* -ENC28J60 Ethernet Modue
* -Arduino Uno
* -Switch/Hub
* -Ethernet Cables
* -Protoboard: 1 Led, 10k potenciometer, 2x1k resistor, 1 switch
*
* PinModes:
* pinA0-> Analog Read(Potenciometer),pin03->Digital Output(Led), pin09->Digital
Input(Switch)
*
* Arduino Board Pins Connections:
// +-\/-+
// | | GND-->Protoboard CKT
// | | 13-->ENC28J60(SCK)
// | | 12-->ENC28J60(OS)
// Rst | | 11-->ENC28J60(SI)
// ENC28J60(Vcc)<--3.3V| | 10-->ENC28J60(CS)
// Protoboard CKT<--5V | | 9-->SwitchPin(DI)
// ENC28J60(GND)<--GND | | 8
// GND | | 7
// Vin | | 6
// Pot. Signal<--A0 | | U 5
// A1 | | N 4
// A2 | | 0 3-->LedPin(DO)
// A3 | | 2
// A4 | | 1
// A5 | | 0
// +----+
*
* David Choquenaira F. - January 2021
*/
#include <EtherCard.h>
#include <Modbus.h>
#include <ModbusIP_ENC28J60.h>

const int Sensor_ir0 = 0; //Modbus Address Definition(InputRegister)(3x)


const int vfdspeed_hr = 105; //Modbus Address Definition(HoldingRegister)(4x)
const int SwitchPin_is = 110; //Modbus Address Definition(InputStatus)(2x)
const int LedPin_cl = 115; //Modbus Address Definition(Coils)(1x)

const int sensorAnalog0 = A0; //Data to be assigned to the Modbus Address(3x)

David Choquenaira F. 5
ARDUINO MODBUS TCP SERVER & MODSCAN32 CLIENT

int vfdspeed; //Data to be assigned to the Modbus Address(4x)


const int SwitchPin = 9; //Data to be assigned to the Modbus Address(2x)
const int LedPin = 3; //Data to be assigned to the Modbus Address(1x)

//Modbus IP Object
ModbusIP mb;

void setup() {
[Link](9600); //Initialize serial comm.

// Enter a MAC address and IP address for your controller below.


// The IP address will be dependent on your local network.
// gateway and subnet are optional:
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; //this is a generic MAC
//IP Address
byte ip [] = {192, 168, 1, 120};
[Link] (mac, ip); //Assign address to the module

pinMode(LedPin, OUTPUT); //sets the digital pin as output


pinMode(SwitchPin, INPUT); //sets the digital pin as input
///////////////* Modbus Address *//////////////////////
[Link](Sensor_ir0); //Add a Register type: InputRegister
[Link](Sensor_ir1); //Add a Register type: InputRegister
[Link](vfdspeed_hr); //Add a Register type: HoldingRegister
[Link](SwitchPin_is); //Add a Register type: InputStatus
[Link](LedPin_cl); //Add a Register type: Coil
////////////////////////////////////////////////////////

void loop() {

[Link](); //This method makes all magic,


//answering requests and changing the
registers if necessary
vfdspeed = [Link](vfdspeed_hr); //Write Holding Reg
digitalWrite(LedPin, [Link](LedPin_cl)); //Write Outputs Coils

[Link](Sensor_ir0, analogRead(sensorAnalog0)); //Update and assign register values


[Link](vfdspeed_hr, vfdspeed); //Update and assign register values
[Link](SwitchPin_is, digitalRead(SwitchPin)); //Update and assign register values
[Link](LedPin_cl,digitalRead(LedPin)); //Update and assign register values

//Display
[Link]("HoldingRegister(4x): ");
[Link](vfdspeed);
[Link](" InputReguster(3x): ");
[Link](analogRead(sensorAnalog0));
[Link](" InputStatus(2x): ");
[Link](digitalRead(SwitchPin));
[Link](" Coils(1x): ");
[Link](digitalRead(LedPin));
}

David Choquenaira F. 6
ARDUINO MODBUS TCP SERVER & MODSCAN32 CLIENT

8-Read/Write Arduino Modbus TCP registers


-We test the writing and reading of the registers 1x, 2x, 3x, 4x.

-Modscan32(Modbus client) is based on offset +1, which means that to request values to the Arduino we must
add +1 the position in the register.

-Example:

-Register 0100 in Arduino; in Modscan32 we would put 0101

Defined address- Arduino Uno Address requested -Modscan32


105 40106

David Choquenaira F. 7
ARDUINO MODBUS TCP SERVER & MODSCAN32 CLIENT

Defined address- Arduino Uno Address requested -Modscan32


0 30001

Defined address- Arduino Uno Address requested -Modscan32


110 10111

Defined address- Arduino Uno Address requested -Modscan32


115 00116

David Choquenaira F. 8
ARDUINO MODBUS TCP SERVER & MODSCAN32 CLIENT

9-Related Links:

- [Link]

- [Link]

-[Link]

-[Link]
server/learn/lecture/7394504#questions

David Choquenaira F. 9

Arduino Modbus TCP Server and 
Modscan32 Client Communication 
David Choquenaira F. 
Arduino UNO - ENC28J60 - Modsca
ARDUINO MODBUS TCP SERVER & MODSCAN32 CLIENT 
 
 
  
  
 
 
David Choquenaira F. 
1 
 
0-INTRODUCTION 
Arduino Uno ModbusTCP
ARDUINO MODBUS TCP SERVER & MODSCAN32 CLIENT 
 
 
  
  
 
 
David Choquenaira F. 
2 
 
4-Libraries will be using (Modbus TCP
ARDUINO MODBUS TCP SERVER & MODSCAN32 CLIENT 
 
 
  
  
 
 
David Choquenaira F. 
3 
 
 
You can connect the Arduino Uno wit
ARDUINO MODBUS TCP SERVER & MODSCAN32 CLIENT 
 
 
  
  
 
 
David Choquenaira F. 
4 
 
 
 
 
 
7-Code: 
-What I see and ther
ARDUINO MODBUS TCP SERVER & MODSCAN32 CLIENT 
 
 
  
  
 
 
David Choquenaira F. 
5 
 
 
-The right path to work with the EN
ARDUINO MODBUS TCP SERVER & MODSCAN32 CLIENT 
 
 
  
  
 
 
David Choquenaira F. 
6 
 
int vfdspeed;
ARDUINO MODBUS TCP SERVER & MODSCAN32 CLIENT 
 
 
  
  
 
 
David Choquenaira F. 
7 
 
 
 
 
 
8-Read/Write Arduino Modbus T
ARDUINO MODBUS TCP SERVER & MODSCAN32 CLIENT 
 
 
  
  
 
 
David Choquenaira F. 
8 
 
Defined address- Arduino Uno 
Address
ARDUINO MODBUS TCP SERVER & MODSCAN32 CLIENT 
 
 
  
  
 
 
David Choquenaira F. 
9 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9-Related L

You might also like