Arduino TCP no connection

Hello,

For a current project i want to make a TCP server, but i got a problem connecting it to my network.

A while ago i tested my software on a arduino mega2560 with the ethernetshield(2nd version), this worked flawless as it connected to my network and i could reach it and read its registers.

A few weeks later my custom PCB arrived, i uploaded the bootloader with my sketch(same software) but i did not connect to my network.
So i went to test my software again to be sure on my arduino, but it also did not connect to my network.
I cant get it working again, i tested a few things:
-tested 2 arduino mega
-arduino uno
-tested 2 shields
-tested 2 ethernet cable
-created a little network with a router with only my pc and arduino on it

The code i am using will be in the attachments.

The IP-adress is free on the network and cant reach it tough CMD and my modbus TCP test woftware, also the MAC-adress is the same in the software as the hardware.
Does anyone know what the problem is or what i should be checking?

Kind Regards,

#include <Ethernet.h>
#include <ArduinoModbus.h>

unsigned long HoldingResult, InputResult;
int  holding, holding1;
int ledpin = 11;
int ledpin1 = 9;
int inpin = A0;
int val = 0;
int Coils = 2-4; 
int Input;

EthernetServer EthServer(502);
ModbusTCPServer modbusTCPServer;

void setup() {


  pinMode(ledpin, OUTPUT);
  pinMode(ledpin1, OUTPUT);
  pinMode(inpin, INPUT);
  pinMode(29, OUTPUT);




 // byte mac[] = { 0xA8, 0x61, 0x0A, 0xAE, 0x8A, 0x08 };
  byte ip[] = { 192, 168, 1, 150 };
  byte subnet[] = { 255, 255, 255, 0 };


  Ethernet.begin(ip, subnet);
  EthServer.begin();
  modbusTCPServer.begin();


  HoldingResult = modbusTCPServer.configureHoldingRegisters(0, 5);
  InputResult   = modbusTCPServer.configureInputRegisters(0, 100);
//Coils         = modbusTCPServer.configureCoils(0x00, 3);
 






}

void loop() {

  EthernetClient client = EthServer.available();

  if (client.connected()) {
    modbusTCPServer.accept(client);

    modbusTCPServer.poll();
  }




  holding  = modbusTCPServer.holdingRegisterRead(0);
  holding1 = modbusTCPServer.holdingRegisterRead(1);

 
  
  {
    if (holding)
    {
      digitalWrite(ledpin, HIGH);
    }
    else
    {
      digitalWrite(ledpin, LOW);
    }
    {
   
    }
  }

/*
  {
    if (holding1)
    {
      digitalWrite(ledpin1, HIGH);
    }
    else
    {
      digitalWrite(ledpin1, LOW);
    }
    {
      lcd.setCursor(3, 0);
      lcd.print(holding1);
    }
  } */
  val = digitalRead(inpin);
  {
    if (val)
    {
     int inputRegisterWrite(2, 2);     
    }
    else
    {
    int inputRegisterWrite(2,2);
    }    
  }
  

  digitalWrite(29, HIGH); 
  delay(200);                       
  digitalWrite(29, LOW);      
  delay(200); 
}

Do you think that it would be a good idea to post your code and a schematic of your project ?

so you use the Ethernet library?

The easier you make it to read and copy your code the more likely it is that you will get help

Please follow the advice given in the link below when posting code , use code tags and post the code here

If you get errors when compiling please copy them from the IDE using the "Copy error messages" button and paste the clipboard here in code tags

here is my code:

#include <Ethernet.h>
#include <ArduinoModbus.h>

unsigned long HoldingResult, InputResult;
int  holding, holding1;
int ledpin = 11;
int ledpin1 = 9;
int inpin = A0;
int val = 0;
int Coils = 2-4; 
int Input;

EthernetServer EthServer(502);
ModbusTCPServer modbusTCPServer;

void setup() {


  pinMode(ledpin, OUTPUT);
  pinMode(ledpin1, OUTPUT);
  pinMode(inpin, INPUT);
  pinMode(29, OUTPUT);




 // byte mac[] = { 0xA8, 0x61, 0x0A, 0xAE, 0x8A, 0x08 };
  byte ip[] = { 192, 168, 1, 150 };
  byte subnet[] = { 255, 255, 255, 0 };


  Ethernet.begin(ip, subnet);
  EthServer.begin();
  modbusTCPServer.begin();


  HoldingResult = modbusTCPServer.configureHoldingRegisters(0, 5);
  InputResult   = modbusTCPServer.configureInputRegisters(0, 100);
//Coils         = modbusTCPServer.configureCoils(0x00, 3);
 






}

void loop() {

  EthernetClient client = EthServer.available();

  if (client.connected()) {
    modbusTCPServer.accept(client);

    modbusTCPServer.poll();
  }




  holding  = modbusTCPServer.holdingRegisterRead(0);
  holding1 = modbusTCPServer.holdingRegisterRead(1);

 
  
  {
    if (holding)
    {
      digitalWrite(ledpin, HIGH);
    }
    else
    {
      digitalWrite(ledpin, LOW);
    }
    {
   
    }
  }

/*
  {
    if (holding1)
    {
      digitalWrite(ledpin1, HIGH);
    }
    else
    {
      digitalWrite(ledpin1, LOW);
    }
    {
      lcd.setCursor(3, 0);
      lcd.print(holding1);
    }
  } */
  val = digitalRead(inpin);
  {
    if (val)
    {
     int inputRegisterWrite(2, 2);     
    }
    else
    {
    int inputRegisterWrite(2,2);
    }    
  }
  

  digitalWrite(29, HIGH); 
  delay(200);                       
  digitalWrite(29, LOW);      
  delay(200); 
}

What is this supposed to do ? And are you sure about declaring it like that ?

People will be asking to see schematics too , inputs with pull up/downs etc

Generally in this sort of case I would try a simple example to see if you can connect ok and that there isn’t a problem with the network . You could put a test in setup to check it’s connected ok ? Have you reserved this ip in your router so something else can’t grab it ?

Hello,

After a week of not using the arduino, i plugged it in and it suddenly worked like it should. but after 10 min it stopped working again, and cant seem to figure it out how to get it to work again.

the project will rest for now as it is not a priority for us. thanks for your awnsers.