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 = ÿ
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.
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?