I am having trouble with what should be a very simple task:
-listen on telnet port for incoming characters
-concatenate incoming characters to a string until the ";" character is typed
-print fully concatenated string
I can't figure out how I can concatenate to a string. All that prints is ";", no concat is taking place. Any help would be appreciated. See code below:
void loop() {
String printString;
// wait for a new client:
EthernetClient client = server.available();
// when the client sends the first byte, say hello:
if (client) {
if (!alreadyConnected) {
// clear out the input buffer:
client.flush();
Serial.println("We have a new client");
client.println("Hello, client!");
alreadyConnected = true;
}
if (client.available() > 0) {
// read the bytes incoming from the client:
char thisChar = client.read();
// echo the bytes back to the client:
server.write(thisChar);
// echo the bytes to the server as well:
printString = printString + thisChar;
}
}
if (printString.endsWith(";")) {
Serial.print(printString);
printString = "";//clear the string
}
}
You are reading only one character. The client send will come in a packet with possibly the entire message.
// change this
if (client.available() > 0) {
// to this
while (client.available() > 0) {
That code is more for a chat server. One client sends, and it echoes to all connected clients. Is that what you want? I have code in the playground that works more like a telnet server. It is a bit more complex though. The client doesn't need to send anything for the server to send a "hello" message to the client. Just a thought... http://playground.arduino.cc/Code/Telnet
Thanks for the fast reply. I am trying to read in a message until ";" is received. Then send the whole message to a LCD display and to a Text to Speech module (which needs an entire string sent in one shot.)
Changing to a While loop didn't seem to help....
-concatenate incoming characters to a string until the ";" character is typed
Below is some simple delimited serial capture code that should be adaptable to your telnet session code.
//zoomkat 3-5-12 simple delimited ',' string parce
//from serial port input (via serial monitor)
//and print result out serial port
// CR/LF could also be a delimiter
String readString;
void setup() {
Serial.begin(9600);
Serial.println("serial delimit test 1.0"); // so I can keep track of what is loaded
}
void loop() {
//expect a string like wer,qwe rty,123 456,hyre kjhg,
//or like hello world,who are you?,bye!,
if (Serial.available()) {
char c = Serial.read(); //gets one byte from serial buffer
if (c == ',') {
if (readString.length() >0) {
Serial.println(readString); //prints string to serial port out
//do stuff with the captured readString
readString=""; //clears variable for new input
}
}
else {
readString += c; //makes the string readString
}
}
}