TCP Client problem!

Hello,
I am using enc28j60 and arduino uno as a tcp client/server app for the OMRON industrial camera. I got the client already working, BUT the problem is that if i keep on “sending packages from the camera” it works good, in a serial it just responds with new packages BUT if I stop sending the packages for a few seconds it just stops and I can not go back to receiving them. So I am asking how can I “handle” that process so even if I don’t touch anything for a second or minute etc. it is still “listening”.
@countrypaul

#include <SPI.h>
#include <UIPEthernet.h>

unsigned long lastConnectionTime = 0;             // last time you connected to the server, in milliseconds
const unsigned long postingInterval = 10L * 1000L; // delay between updates, in milliseconds
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress server(10, 5, 5, 10); // numeric IP for Google (no DNS)
IPAddress ip(10, 5, 5, 106);
EthernetClient client;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);

  

  // start the Ethernet connection:

  Ethernet.begin(mac, ip);

  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");

  // if you get a connection, report back via serial:
  if (client.connect(server, 9876)) {
    Serial.println("connected");

  }
  else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }

 
     Serial.println("1 lub 2");
}

void loop(){
if (client.available()){
   char c = client.read();
        Serial.print(c);

 
delay(100);}

    // if the server's disconnected, stop the client:
//    if (!client.connected()) {
//      Serial.println();
//      Serial.println("disconnecting.");
//      client.stop();
//  
//      // do nothing forevermore:
//      while (true);
//    }
}!

remove the delay and do timing with millis. the library needs frequent calls to handle the network

for new projects please use the EthernetENC library

Okay so it did kinda work but not excactly how i want to because it makes the whole process fluent? Like i need to have it in a way that :

  1. It gets connected
  2. If i choose to listen on that port i want it continous not like i will see on a screen that it gets connected disconected and all the time likes this.
    Later i also wanna add an option to send packages.
#include <SPI.h>
#include <UIPEthernet.h>

unsigned long lastConnectionTime = 0;             // last time you connected to the server, in milliseconds
const unsigned long postingInterval = 10L * 1000L; // delay between updates, in milliseconds
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress server(10, 5, 5, 10); // numeric IP for Google (no DNS)
IPAddress ip(10, 5, 5, 106);
EthernetClient client;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);

  

  // start the Ethernet connection:

  Ethernet.begin(mac, ip);

  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");

  // if you get a connection, report back via serial:
  if (client.connect(server, 9876)) {
    Serial.println("connected");

  }
  else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }

 
     Serial.println("1 lub 2");
}

void loop(){
if (client.available()){
   char c = client.read();
        Serial.print(c);}
        if (millis() - lastConnectionTime > postingInterval){loop();
        lastConnectionTime = millis();
        }

  }


You call the connect() method to server only once in setup function, so it’s normal this behaviour.
You should check in the loop() if the client is still connected and if not, connect again

if (!client.connected()) {
  client.connect(server, 9876);
  .....
}

Not convinced that these two lines are the best way of dealing with a timeout, you could end up just decending into a recursive loop. Might be simpler to reverse the logic and only call millis() if appropriate allowing the Loop function to simply be called again automatically.

Okay I did it ina way that it works constantly.
Now i want to choose (later on keyboard, now on serial)
if i press 1 - i read packages
if i press 2 - i send packages
And that part won’t work, I tried with cases and “if” and i don’t know what’s wrong.

#include <SPI.h>
#include <UIPEthernet.h>
String inString ="";

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress server(10, 5, 5, 10); // numeric IP for Google (no DNS)
IPAddress ip(10, 5, 5, 106);
EthernetClient client;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);



  // start the Ethernet connection:

  Ethernet.begin(mac, ip);

  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");

  // if you get a connection, report back via serial:
  if (client.connect(server, 9876)) {
    Serial.println("connected");

  }
  else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }


  Serial.println("Wcisnij 1 aby odczytywac pakiety");
  Serial.println("Wcisnij 2 aby wysylac pakiety");

 
  }


void loop() {

// Read serial input:
  if (Serial.available() > 0) {
    int inChar = Serial.read();
    if (isDigit(inChar)) {
      // convert the incoming byte to a char and add it to the string:
      inString += (char)inChar;
    }
    // if you get a newline, print the string, then the string's value:
    if (inChar == '\n') {
      Serial.print("Value:");
      Serial.println(inString.toInt());
      Serial.print("String: ");
      Serial.println(inString);
      int Wartosc = inString.toInt();
if (( inString == 1)){
  if (client.available())   {
    char c = client.read();
    Serial.print(c);
  }
  if (!client.connected()) {
    client.connect(server, 9876);}
}


if ( inString == 2){

 if (client.available())   {
    Serial.print("SERWER");
  }
  if (!client.connected()) {
    client.connect(server, 9876);}
}
     
      // clear the string for new input:
      inString = "";
    }
  }



 
 
}
  






I’m no that familiar with the String class but:

Are you sure you mean that, or should it be
if (Wartosc =1)
or possibly
if (inString == “1”)
or even
if (inString.toInt() == 1)

add Ethernet.maintain(); at the beginning of the loop() if you don’t call some other UIPEthernet or EthernetENC library function frequently

Okay i got it working to the point that i choose by typing:
a in serial i jump to “reading the packaged”
b i jump to sending packages
BUT i think that sending the packages does not work because i added also the line that will show the package that was sent (read from the server) and I think it doesn’t work.

#include <SPI.h>
#include <UIPEthernet.h>
char incomingByte=0;
char s=0;
int choice=0;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress server(10, 5, 5, 10); // numeric IP for Google (no DNS)
IPAddress ip(10, 5, 5, 106);
EthernetClient client;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
// start the Ethernet connection:
Ethernet.begin(mac, ip);
// give the Ethernet shield a second to initialize:
delay(1000);
Serial.println("connecting...");
// if you get a connection, report back via serial:
  if (client.connect(server, 9876)) {
    Serial.println("connected");
}
  else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
  Serial.println("Wcisnij a aby odczytywac pakiety");
  Serial.println("Wcisnij b aby wysylac pakiety");
  }


void loop() {

// Read serial input:
  if(Serial.available()>0)
  {
     incomingByte = Serial.read();
  }

  
switch (incomingByte) {
  
  case 'a':
  //Serial.print("Logi");
if (client.available())   {

char c = client.read();

Serial.print(c);
Serial.print("");
  }
  if (!client.connected()) {
    client.connect(server, 9876);}
break;

case 'b':
//Serial.print("Wpisz komendę");
 if (client.available())   {
    if(Serial.available()>0){
char s = 0;
s=Serial.read();
client.write(s);
client.print(s);


char c = client.read();
Serial.print(c);
  }}
  if (!client.connected()) {
    client.connect(server, 9876);}
    break;
}
}

 

Look at the “case b” part because that is the part that is responsible for sending the packages.

Can you auto-format the code in the IDE before posting, I am having difficulty in working out which brace is associated with which especially when there are several the same indented to the same level.

Also follow Juraj’s advice and add Ethernet.maintain(); at the beginning of the loop() . At worst it will take up one line of code but may resolve some timing problems. If the client is disconnected it may be that all four connections get used up without us knowing - and any further attempts may be fruitless - not entirely sure.

You may want to move the client.write and client.print to immediately after the client.connect block to maximise the chances teh client is connected.

Sorry, corrected code:

#include <SPI.h>
#include <UIPEthernet.h>
char incomingByte = 0;
char s = 0;
int choice = 0;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress server(10, 5, 5, 10); // numeric IP for Google (no DNS)
IPAddress ip(10, 5, 5, 106);
EthernetClient client;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  // start the Ethernet connection:
  Ethernet.begin(mac, ip);
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");
  // if you get a connection, report back via serial:
  if (client.connect(server, 9876)) {
    Serial.println("connected");
  }
  else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
  Serial.println("Wcisnij a aby odczytywac pakiety");
  Serial.println("Wcisnij b aby wysylac pakiety");
}


void loop() {

  // Read serial input:
  if (Serial.available() > 0)
  {
    incomingByte = Serial.read();
  }


  switch (incomingByte) {

    case 'a':
      //Serial.print("Logi");
      if (client.available())   {

        char c = client.read();

        Serial.print(c);
        Serial.print("");
      }
      if (!client.connected()) {
        client.connect(server, 9876);
      }
      break;

    case 'b':
      //Serial.print("Wpisz komendę");
      if (client.available())   {
        if (Serial.available() > 0) {
          char s = 0;
          s = Serial.read();
          client.write(s);
          client.print(s);


          char c = client.read();
          Serial.print(c);
        }
      }
      if (!client.connected()) {
        client.connect(server, 9876);
      }
      break;
  }
}



I do not know how to send packages/message
Serial input → Server
and display that also.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.