Reading Ethernet

Hi,
I want to analyze the characters received per pair.

#include <SPI.h>
#include <Ethernet.h>

char c;
char c2;

byte mac = {0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02};

EthernetServer server(2324);

void setup()
{
Serial.begin(9600);
while (!Serial);

if (Ethernet.begin(mac) == 0) {
Serial.println(“Failed to configure Ethernet using DHCP”);
while (true);
}
Serial.print("IP : ");
Serial.print(Ethernet.localIP());
Serial.println();

server.begin();
}

void loop()
{
EthernetClient client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
c = client.read();
if (c >= ‘0’ && c <= ‘z’) {
Serial.print("C = ");
Serial.println(c);
c2 = client.read();
Serial.print("C2 = ");
Serial.println(c2);
}
}
}
}
}

(I do the test “if (c >= ‘0’ && c <= ‘z’)” because at the beginning of TCP transmission I have unprintable strange characters, I don’t know why…)

With this code and the string “test”, the console displays :

IP : 192.168.1.47
C = t
C2 = 
C = e
C2 = 
C = s
C2 = 
C = t
C2 = ÿ

While I should have this :

IP : 192.168.1.47
C = t
C2 = e
C = s
C2 = t
         if (client.available()) {

The available() method is not a boolean function. It returns the number of characters available to be read. This test will evaluate to true if there is at least one character. If so, you then read 2, one of which may not have arrived yet.

Not a good idea.

         if (client.available() >= 2) {

I get it, this is not good :

IP : 192.168.1.47
C = t
C2 = 
C = e
C2 = 
C = s
C2 =

Post your modified code please, inside code tags, not quotes.

Stupid question...

Where does this TCP string come from? Are you sure you're not receiving a carriage return or something like that from the other side?

Is there another terminal program that you can use, so you can see the ASCII code instead of it's representation to clear that up?

Can you post the entire received message as well?

Try with that code :

void loop()
{
   EthernetClient client = server.available();
   if (client) {
      while (client.connected()) {
         if (client.available()>0) {
            c = client.read();
            if (c >= '0' && c <= 'z') {
               static bool flag=false;
               if (flag==false)
                Serial.print("C = ");
               else 
                Serial.print("C2 = ");
                Serial.println(c);
                flag = !flag;
               }
             
            }
         }
      }
   }
}

Try with that code

I don't think that the idea as to print a different prefix in front of the characters. It was to store the characters in different variables. That code does not do that.

Of course, some simple debug statements in the original code would tell where the problem is. For instance, does the if statement ever evaluate to true? I know that it should, but the question is does it?