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: https://robu.in/arduino-pin-configuration/
-pinA0-> Analog Read(Potentiometer)
-pin03->Digital Output(Led)
-pin09->Digital Input(Switch)
3-Software will be using:
-Modscan32 (https://api.256file.com/modscan32.exe/m-download-273325.html)
-Arduino IDE (https://www.arduino.cc/en/software)
David Choquenaira F. 1
ARDUINO MODBUS TCP SERVER & MODSCAN32 CLIENT
4-Libraries will be using (Modbus TCP Server): https://github.com/andresarmento/modbus-arduino
-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. (https://github.com/andresarmento/modbus-arduino)
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() {
Serial.begin(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};
mb.config (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 *//////////////////////
mb.addIreg(Sensor_ir0); //Add a Register type: InputRegister
mb.addIreg(Sensor_ir1); //Add a Register type: InputRegister
mb.addHreg(vfdspeed_hr); //Add a Register type: HoldingRegister
mb.addIsts(SwitchPin_is); //Add a Register type: InputStatus
mb.addCoil(LedPin_cl); //Add a Register type: Coil
////////////////////////////////////////////////////////
void loop() {
mb.task(); //This method makes all magic,
//answering requests and changing the
registers if necessary
vfdspeed = mb.Hreg(vfdspeed_hr); //Write Holding Reg
digitalWrite(LedPin, mb.Coil(LedPin_cl)); //Write Outputs Coils
mb.Ireg(Sensor_ir0, analogRead(sensorAnalog0)); //Update and assign register values
mb.Hreg(vfdspeed_hr, vfdspeed); //Update and assign register values
mb.Ists(SwitchPin_is, digitalRead(SwitchPin)); //Update and assign register values
mb.Coil(LedPin_cl,digitalRead(LedPin)); //Update and assign register values
//Display
Serial.print("HoldingRegister(4x): ");
Serial.print(vfdspeed);
Serial.print(" InputReguster(3x): ");
Serial.print(analogRead(sensorAnalog0));
Serial.print(" InputStatus(2x): ");
Serial.print(digitalRead(SwitchPin));
Serial.print(" Coils(1x): ");
Serial.println(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:
- https://github.com/andresarmento/modbus-arduino
- https://naylampmechatronics.com/blog/17_tutorial-modulo-ethernet-enc28j60-y-arduino.html
-https://lnkd.in/ectSvAzv
-https://www.udemy.com/course/how-to-program-an-arduino-as-a-modbus-tcp-client-
server/learn/lecture/7394504#questions
David Choquenaira F. 9