Browsed by
Tag: Modbus

Modbus TCP M-Duino

Modbus TCP M-Duino

Modbus TCP can be programmed in M-Duino PLCs.

Once the Arduino IDE is configured according to Industrial Shields requirements

http://blog.industrialshields.com/es/ethernet-shield-libraries/, the follwoing library can be used to program the M-Duino as Modbus TCP slave.

Basically, this example links holding registers 40000, 40001 and 40002 to outputs Q0.1, Q0.2 and Q0.3 of the M-Duino. In this way these 3 outputs can be operated via a Modbus TCP master. You will have to modify the IP address and network parameters according to your LAN.

An illustrative and simple example to test the application is with the following android free app. https://play.google.com/store/apps/details?id=com.bencatlin.modbusdroid&hl=es
Which will allow you to operate the M-Duino outputs via your smartphone. You only need to set the IP address of the slave and  you can begin controlling the M-Duino remotely.

ModbusDroid

Now you can simply remotely control your M-Duino.

Enjoy it!

MODBUS RTU M-Duino Ardbox

MODBUS RTU M-Duino Ardbox

In this post it is shown how to implement Modbus RTU (RS485/RS232) in M-Duinos and Ardbox PLCs. The library used is a modification of the SimpleModbus, which has been adapted to the internal configuration of Industrial Shields PLCs (SimpleModbus library for Industrial Shields PLCs).  Adapted libraries to Industrial Shields PLCs  are available here

 First the code for the Master is showed below.

#include <SoftwareSerial.h>
#include <SimpleModbusMasterSoftwareSerial.h>

#define ARDBOX
//#define MDUINO

#ifdef ARDBOX
#define SSerialRX 11 // RO //RS485 Receive pin
#define SSerialTX 10 // DI //RS485 Transmit pin
#define SSerialTxControl 7 // DE //RS485 Direction pin
#define GroundRE 4 // RE
#elif MDUINO
#define SSerialRX 11 // RO //RS485 Receive pin
#define SSerialTX 10 // DI //RS485 Transmit pin
#define SSerialTxControl 14 // DE //RS485 Direction pin
#define GroundRE 13 // RE
#endif

#define baud 9600
#define timeout 1000
#define polling 200 // the scan rate 
#define retry_count 10


int SSerialRX =11 ;// RO  //RS485 Receive pin
int SSerialTX=10; // DI  //RS485 Transmit pin
int  SSerialTxControl=7;  // DE  //RS485 Direction pin
int GroundRE=4 ; // RE
SoftwareSerial RS485Serial(SSerialRX, SSerialTX);

enum
{
PACKET1,
PACKET2,
PACKET3,
PACKET4,
TOTAL_NO_OF_PACKETS // leave this last entry
};

// Create an array of Packets to be configured
Packet packets[TOTAL_NO_OF_PACKETS];

// Masters register array
#define TOTAL_NO_OF_REGISTERS 4
unsigned int regs[TOTAL_NO_OF_REGISTERS];


void setup()
{
  Serial.begin(9600);
  pinMode(SSerialTxControl, OUTPUT);  
  pinMode(GroundRE, OUTPUT);
  digitalWrite(GroundRE,LOW);

  modbus_construct(&packets[PACKET1], 1, READ_HOLDING_REGISTERS, 100, 1, 0); 
  // parameters: packet/ID Slave/ function/ Modbus address/Total numbr of registers/position in the  'regs' array.
 modbus_construct(&packets[PACKET2], 1, PRESET_SINGLE_REGISTER, 110, 1, 1);
  modbus_construct(&packets[PACKET3], 1, READ_COIL_STATUS, 0, 1, 2);
   modbus_construct(&packets[PACKET4], 1, FORCE_MULTIPLE_COILS, 16, 1,3);
  modbus_configure(&RS485Serial, baud, SERIAL_8N1, timeout, polling, retry_count, SSerialTxControl, packets, TOTAL_NO_OF_PACKETS, regs);
}

void loop()
{
  modbus_update();

regs[1]=200; //(PACKET 2) The value 200 will be written to Holdin register 110.
regs[3]=1; //  (PACKET 4)The value 1 will be written to COIL 16.
Serial.print(regs[0]); //(PACKET 1) It is printed in Serial Monitor the value contained in Holding Register 100 of the slave.
Serial.print(regs[2]);  //(PACKET 3) It is printed in Serial Monitor the value contained in the Coil 0 of the slave.
delay(1000);

}

The code for a Modbus RTU Slave is the following.


#include <SoftwareSerial.h>
#include <SimpleModbusSlaveSoftwareSerial.h>

#define ARDBOX
//#define MDUINO

#ifdef ARDBOX
#define SSerialRX 11 // RO //RS485 Receive pin
#define SSerialTX 10 // DI //RS485 Transmit pin
#define SSerialTxControl 7 // DE //RS485 Direction pin
#define GroundRE 4 // RE
#elif MDUINO
#define SSerialRX 11 // RO //RS485 Receive pin
#define SSerialTX 10 // DI //RS485 Transmit pin
#define SSerialTxControl 14 // DE //RS485 Direction pin
#define GroundRE 13 // RE
#endif

SoftwareSerial RS485Serial(SSerialRX, SSerialTX);

#define  Q05 9  

// and at a glimpse informs you of your slaves register layout.

//////////////// registers of your slave ///////////////////
enum 
{     
  // just add or remove registers and your good to go...
  // The first register starts at address 0
  ADC_VAL,     
  PWM_VAL,        
  HOLDING_REGS_SIZE // leave this one
  // total number of registers for function 3 and 16 share the same register array
  // i.e. the same address space
};

unsigned int holdingRegs[HOLDING_REGS_SIZE]; // function 3 and 16 register array
////////////////////////////////////////////////////////////

void setup()
{

  pinMode(SSerialTxControl, OUTPUT);  
  pinMode(GroundRE, OUTPUT);
  

  digitalWrite(GroundRE,LOW);
  modbus_configure(&RS485Serial, 9600, SERIAL_8N2, 1, SSerialTxControl, HOLDING_REGS_SIZE, holdingRegs);

  // modbus_update_comms(baud, byteFormat, id) is not needed but allows for easy update of the
  // port variables and slave id dynamically in any function.
  modbus_update_comms(9600, SERIAL_8N2, 1);
  
  pinMode(Q05, OUTPUT);
}

void loop()
{
  modbus_update();
  
  holdingRegs[ADC_VAL] = analogRead(A0); // update data to be read by the master to adjust the PWM
  
  analogWrite(Q05, holdingRegs[PWM_VAL]>>2); // constrain adc value from the arduino master to 255
  
}

Contact us at industrialshields@industrialshields.com if you need more information.

Real time electrical consumption monitoring with M-Duino

Real time electrical consumption monitoring with M-Duino

In this Project, we will show you how to read electrical consumption using Modbus communication. Modbus is an industry standard communications protocol for electronic device Hardware I: Equipment to be used The electrical consumption monitoring system consists of:

  • An M-Duino PLC: This controller is the heart of the system. It controls the display, generates interrupts, and controls the communication.

  • A Panel PC: Used to read on the status of a counter and display the graph of monthly consumption over the Panel Pc. –         Switch: Switch will work as the interface between slaves and Master in our control network.

PanelPc

  • A Power Meter witch Modbus TCP/IP communication capabilities (as TCP is Transmission Control Protocol and IP is Internet Protocol.)
  • Wires and some programming experience J.

Hardware II:  Communication The nice feature of Panel PC we used is that it has many interfaces such as Ethernet, USB  and I²C as well. We´ve expressly chosen Ethernet because our PLC  and our I2C panel have this Ethernet connecitivity. See the following picture to understand it: sch2   Software and code Arduino sketch essentially uses the Modbus TCP library for Arduino  (see the Arduino installer tutorial here).  Some important things to take care when we use this library:

  1. Insert the modbus_update function in the loop, because. takes care of the received modbus commands and will modify the registers accordingly in case of writing or reading requests.
  2. Declare an integer array (named holdingRegs) that stores the modbus registers.
  3. We recommend to review modbus tcp basics at http://www.simplymodbus.ca/

After that, just start sending values to the panel PC through USB  I2C communication. You can find the code to be uploaded to the PLC here: 2014111_arduino_code   You need to download  libraries:

And what about the software to install in our Panel PC to start visualizing graphs? In this case, we recommend to use Tesla Scada, wich is able to work on Android systems . The panel pc will start receiving data and print out data in graphs! Key Benefits The main benefits of the Opiron purposed solution are:

  • Time Saving: Forget to go to view and write values in notebooks, your control system will do this job for you.
  • Real time measurement: System provides reliable data quickly.
  • Digital data analysis: If you can measure it, you can improve it!
Contact us!
Do you have a process to be automated? Contact Opiron to implement the entire solution and Industrialshields to buy the proper hardware.
How to Modbus-Ethernet

How to Modbus-Ethernet

  • Modbus is a Serial communications protocol and it is used for communicating with several industrial devices. It may run over RS485, Ethernet, or whatever hardware interface.
  • If we have an ethernet connection between a lot of devices, we can use this hardware connection (eth), and over it, we can use our own protocol (custom protocol,…), or for instance, Modbus protocol. The way is the same as using RS485, but hardware connections will become much easier, the speed will be higher, and the possible frame errors will be controlled by TCP/IP layers.
  • So, if you know Modbus, you have an easier way to implement it!.