The arduino hardware is ATMega 2560.
The software is installed on windows 10, 64 bit laptop.
Below is the code with Arduino being used as a client (the MgsModbus library was downloaded from Github:[GitHub - andresarmento/modbus-arduino: A library that allows your Arduino to communicate via Modbus protocol, acting as a slave (master in development). Supports serial (RS-232, RS-485) and IP via Ethernet (Modbus IP).]
CODE:
#include <MgsModbus.h>
#include <Ethernet.h>
#include <SPI.h>
int SerialNumber;
int Last_SerialNumber;
int BoxQuantity;
int BoxQuantity_Old;
bool PolyBagDetected;
bool PolyBag_Old;
bool CamInspectionResult;
bool CamInspection_Old;
MgsModbus Mb; //Modbus Object to encapsulate all the feature of the library
int acc = 0; //Accumulator, seconds counter
// Ethernet settings (depending on MAC and Local network)
byte mac[] = {0x90, 0xA2, 0xDA, 0x0E, 0x94, 0xB5 };
IPAddress ip(192, 168, 1, 119);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
void setup() {
Serial.begin (9600);
Ethernet.begin(mac, ip, gateway, subnet); //Initializes the network connection
//Server (NI) IP address
Mb.remServerIP[0] = 192;
Mb.remServerIP[1] = 168;
Mb.remServerIP[2] = 1;
Mb.remServerIP[3] = 165;
SerialNumber = 100;
Last_SerialNumber = 100;
BoxQuantity = 30;
BoxQuantity_Old = 30;
PolyBagDetected = false;
PolyBag_Old = false;
CamInspectionResult = false;
CamInspection_Old = false;
DisplaySerialNumber();
}
void loop() {
// mb.task();
// BoxQuantity = mb.Hreg (BoxQuantity_hr); //Modify Box Quantity from the display screen
// CamInspectionResult = mb.Coil (CamInspectionResult_cl); //Modify coil status remotely from display screen
DataPoll(); // Decides which registers to poll the data from everytime the function is called
Mb.MbmRun(); //Function that runs & implements modbus Client services
NewCycleRan();
}
void DataPoll(){
delay (01); // 1ms delay
acc = acc + 1;
//Reading the value of the input register (SERIAL NUMBER OF THE POLYBAG)
if (acc == 3000){
Mb.Req(MB_FC_READ_INPUT_REGISTER, 100,1,0); //Sends a messsage to the server and initiates the reading of the register value from the server
}
if (acc == 3200){
SerialNumber = Mb.MbData [0]; //checks to see if recieved a message from the server and Stores the value in address of the input register 100 at the 1st array block (0)
}
//Reading the value of the holding register (BOX QUANTITY FOR TOTAL NUMBER OF POLYBAGS IN A BOX)
if (acc == 4000){
Mb.Req(MB_FC_READ_REGISTERS, 100,1,0); // Initiates the reading of the holding register value from the server
}
if (acc == 4200){
BoxQuantity = Mb.MbData [0]; //Stores the value in address of the holding register 100 at the 1st array block (0)
}
//Reading the status of the discrete input status (POLYBAG PRESENT OR NOT)
if (acc == 5000){
Mb.MbData [0] = 0;
Mb.Req(MB_FC_READ_DISCRETE_INPUT, 100,1,0); // Initiates the reading of the input status register value from the server
}
//Stores the value in address of the input register 100 at the 1st array block (0)
if (acc == 5200){
if (Mb.MbData [0] == 1){
PolyBagDetected = true;
}
else {
PolyBagDetected = false;
}
}
//Read coil status value
if (acc == 6000){
Mb.MbData [0] = 0;
Mb.Req(MB_FC_READ_COILS, 100,1,0); // Initiates the reading of the input status register value from the server
}
//Stores the value in address of the input register 100 at the 1st array block (0)
if (acc == 6200){
if (Mb.MbData [0] == 1){
CamInspectionResult = true;
}
else {
CamInspectionResult = false;
}
}
acc = 0;
}
//Checks for change in the data values
void NewCycleRan(){
boolean ChangeDetected = false;
if (Last_SerialNumber != SerialNumber){
ChangeDetected = true;
Serial.println ("New Cycle was ran");
Last_SerialNumber = SerialNumber;
}
if (BoxQuantity_Old != BoxQuantity){
ChangeDetected = true;
Serial.println ("Box Quantity Changed");
BoxQuantity_Old = BoxQuantity;
}
if (PolyBag_Old != PolyBagDetected){
ChangeDetected = true;
PolyBag_Old = PolyBagDetected;
}
if (CamInspection_Old != CamInspectionResult){
ChangeDetected = true;
CamInspection_Old = CamInspectionResult;
}
if (ChangeDetected == true){
DisplaySerialNumber();
}
}
void DisplaySerialNumber() {
String SrNo;
String BagStatus;
String CamStatus;
if (PolyBagDetected == true){
BagStatus = "Bag is detected, ready to inspect";
}
else{
BagStatus = "Waiting for bag, cannot initiate inspection";
}
if (CamInspectionResult == true){
CamStatus = "Label Info Correct";
}
else{
CamStatus = "Label Info Not Correct";
}
SrNo = "Product Serial No is :" + String (SerialNumber);
SrNo = SrNo + "| And Box Quantity is:" + String (BoxQuantity_Old);
SrNo = SrNo + "|" + String (BagStatus);
SrNo = SrNo + "|" + String (CamStatus);
Serial.println (SrNo);
}
Thank you for the follow up and please let me know if you need any other information from my end.