Hello,
As the subject implies, I have had some trouble communicating with my Arduino + Ethernet shield.
I have configured my Arduino to connect as a server in the below sketch, and I am sending it a string of characters over Telnet (using PuTTY). The sketch then saves that string and simply prints it straight back to the client.
/*
A simple Telnet server that receives text input
via Telnet and prints it back to the client.
Useful for testing string capture and comparison
methods.
*/
#include <SPI.h>
#include <Ethernet.h>
#define bufferSize 10
// declare global parameters
char incomingMessage[bufferSize];
EthernetClient client;
// Eddie's Ethernet shield MAC address
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0E, 0xA7, 0xAA };
/*
// Static IP addresses for connecting to Eddie's home ethernet network
IPAddress ip(192,168,254,200);
IPAddress dnServer(192,168,254,254);
IPAddress gateway(192,168,254,254);
IPAddress subnet(255,255,255,0);
*/
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server = EthernetServer(80);
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
// start the Ethernet connection and the server:
Ethernet.begin(mac);
server.begin();
Serial.print(F("Arduino is connected on IP address: "));
Serial.println(Ethernet.localIP());
}
void loop() {
// listen for incoming clients
client = server.available();
// if a client is present, collect string command sent
// and print to client
if (client) {
readDataFromClient();
// print out string command
client.println(incomingMessage);
}
}
// Checks for user input, and saves to incomingMessage[] global array
void readDataFromClient() {
// initialise array index counter
byte i = 0;
// record message
while (client.available() > 0 && i < bufferSize) {
// the below delay needed to ensure correct behaviour
//delay(10);
// read and store new byte
incomingMessage[i] = client.read();
// check if end of incoming message
if (incomingMessage[i] == '\r') {
// replace carriage return with null terminator
incomingMessage[i] = '\0';
// set array index counter to end
i = bufferSize;
// flush out any remaining control characters which would
// stay in client buffer until next loop
client.flush();
}
// increase array index counter
i++;
}
}
The sketch above works fine, but only because after a long debug session I realised that on some occasions the "record message" loop that starts on line 65 is cut short prematurely. I imagined it must be because the client.read() loop is going faster than the rate of arrival of the characters from my Telnet client, so I inserted a small delay in line 67 (commented out in the sketch above, just put it back in to stop seeing strange behaviour)
The funny thing is, this "strange behaviour" exhibited by the loop being exited prematurely only appears if I try to send commands of 1-3 characters in length. If I send a command of 4-9 characters in length then everything behaves fine, even without the delay.
And so I feel that -even though I appear to have solved the problem by inserting that small delay in line 67 - I'm not sure I actually understand the problem correctly, and so I can't know if this fix is going to work in all cases. Is there someone out there that knows exactly what is causing the behaviour described here, and can help me understand the best practice way to fix this?
Many thanks in advance for any help offered.
Ed