ModbusTCP programming

Hello, I am looking for some help with my code. My programming skills are basic.
I have a Controllino Mega and a Fatek HMI.
I am using the MgsModbus library from http://www.myarduinoprojects.com/modbus.html.
I am trying to read data from an HMI. I am declaring a boolean variable (dint), and initializing it as false, and added an “if” statement to start a routine when a button is pressed on the HMI.
On the code, I am doing: dint = Mb.GetBit(1); to read the data sent from the HMI, but, it doesn’t work.
If I manually change the boolean variable to “true”, the routine is executed. Any support with this code is welcome.

#include <SPI.h>
#include <Controllino.h>
#include <Ethernet.h>
#include "MgsModbus.h"

MgsModbus Mb;

const int pul = 46;  
const int dir = 47;  

boolean dint=false;


void setup()
{
byte mac[] = {0x90, 0xA2, 0xDA, 0x0E, 0x94, 0xB5};
IPAddress ip(10, 7, 4, 17);
IPAddress gateway(10, 7, 4, 17);
IPAddress subnet(255, 255, 255, 0);

Ethernet.begin(mac, ip, gateway, subnet);
Serial.begin(9600);

 pinMode (pul, OUTPUT); 
 pinMode (dir, OUTPUT); 
 
}

void loop()
{

  Mb.MbsRun();
  

  dint = Mb.GetBit(1);


if (dint) {

digitalWrite(dir,HIGH);
for (int i=0; i < 400; i++)

{
  digitalWrite(pul, HIGH);
  delayMicroseconds(1000);
  digitalWrite(pul, LOW);
  delayMicroseconds(1000);
}
delay(2000);
 
digitalWrite(dir, LOW);
for (int i = 0; i < 400; i++)
  {
    digitalWrite(pul, HIGH);
    delayMicroseconds(1000);
    digitalWrite(pul, LOW);
    delayMicroseconds(1000);
  }

delay(2000);
 
}

}

Provide a link to the manual of your HMI device!

Is it a ModbusTCP server or client? In your code it should be a client but then it must know the details of your Arduino server and must actively push it's data to the Arduino. Does the HMI device really do that?

On the code, I am doing: dint = Mb.GetBit(1); to read the data sent from the HMI, but, it doesn't work.

You cannot read data from the HMI, it has to send it to the Arduino. If the HMI is configured as a server (in Modbus RTU terminology this is a slave), you must restructure your code accordingly (use the other example sketch).

Hello, thanks for your reply. This is the link of the HMI manual. http://fatekas.com/downloads/yuklemeler/FvDesigner_V1.5_Manual_EN.pdf
I modified the code, and it seems the Controllino is detecting the input. But when I first press 8, or 9, the digital input does not change to 1. I have to press 2, 3 or 7 and it changes to 1. Then I can press 8, and it changes to 0.

Instead of reading a 1 or a 0, how should I modify the code, to read the values 1,2, 3, 4, etc.

And, instead of input numbers from the HMI how can I read 1 or 0, from a switch button on the HMI.

#include <SPI.h>
#include <Controllino.h>
#include <Ethernet.h>
#include "MgsModbus.h"

MgsModbus Mb;


int aout = 123; 
boolean dint;


void setup()
{
byte mac[] = {0x90, 0xA2, 0xDA, 0x0E, 0x94, 0xB5};
IPAddress ip(10, 7, 4, 17);
IPAddress gateway(10, 7, 4, 17);
IPAddress subnet(255, 255, 255, 0);

Ethernet.begin(mac, ip, gateway, subnet);
Serial.begin(9600);

 
}

void loop()
{

  Mb.MbsRun();
  
  Mb.MbData[100] = aout;
  dint = Mb.GetBit(1);
  
  Serial.print("Analogic output:");
  Serial.print(aout);
  Serial.print(" Digital input:");
  Serial.print(dint);
  Serial.println(" ");
  
if (dint) {
 Serial.print("Input detected");
  Serial.println(" ");
}
}

Controllino_HMI.pdf (193 KB)

Don't read the bit register, read the holding register! If you read the bit you only see one bit of the value you entered.