Modbus programming help

Hello. I have a code to rotate a stepper motor that is constantly running. (one revoluton CW, one CCW).
I am trying to execute the code just when I press a button on an HMI that is connected to my Controllino Mega via ethernet.
I need help with the part of the code that reads the signal from the HMI.
I’m new to programming. Any help would be appreciated.

#include <SPI.h>
#include <Controllino.h>
#include <Ethernet.h>
#include "MgsModbus.h"
MgsModbus Mb;
const int pul = 46;  // pin 46 to PUL -
const int dir = 47;  // pin 47 to DIR -
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);
    delay(5000); // Time to open the terminal
    Serial.begin(9600);

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


void loop()
 {

 Mb.MbsRun();

  digitalWrite(dir, HIGH); //Writes the direction to the  DIR pin. (HIGH is clockwise).
  /*Turns the motor 400 steps*/
  for (int i = 0; i < 400; i++)
  {
    digitalWrite(pul, HIGH);
    delayMicroseconds(1000);
    digitalWrite(pul, LOW);
    delayMicroseconds(1000);
  }

 delay(1000);
  digitalWrite(dir, LOW); //Writes the direction to the DIR pin. (LOW is counter clockwise).
/*Turns the motor 400 steps*/
  for (int i = 0; i < 400; i++)
  {
    digitalWrite(pul, HIGH);
    delayMicroseconds(1000);
    digitalWrite(pul, LOW);
    delayMicroseconds(1000);
  }

delay(1000);

 }

Hey mate,

how does your Human Machine Interface look like? Is it just a button you can directly input to your arduino? Or is it an extra Part like a shield etc? Then maybe you require a library for it? Please provide more info about it.

Except of that, if can only encourage you to remove all those delays and add millis() for timing of your actions. Especially when you are waiting for a whole second. Else, you will have issues with "button released too fast". Here is a tutorial how to do such a thing:

Hi dr-o, thanks for your reply. The HMI is a touchscreen that is connected to the controllino via ethernet cable. You are correct, the library is already included in the code. MgsModbus.h library.
That's what I need help with. On what lines of code need to be added, so the controllino, gets the signal from the HMI and starts executing the portion of the code that controls the stepper motor.

Hello emcom09,

Firstly, you should avoid using delay() function in your code because it blocks other operations. Use mills() function instead. See blink without delay

Secondly. For communication between your HMI and Arduino. You needs

  • Identify your HMI is Modbus/TCP Master or Modbus Slave
  • If HMI is Modbus/TCP Master, Arduino code will be Modbus Slave and vice versa
  • Determine which Modbus function code your HMI used (check HMI User Manual)
  • Write your program.

I recommend you use ArduinoModbus Library. You can also refer to a Modbus/TCP project on Arduino Project Hub