Go Down

Topic: EchoServer based on ChatServer, appending strings to output (Read 790 times) previous topic - next topic

RevDrMrFixIt

Hi,

I think I'm trying to implement something very simple, but I'm missing something very simple as well.

I'm trying to implement a vary basic terminal emulator with the Seeed Ethernet Shield v1.1 and an UNO.

The first objective is to append " +OK" to the input, which emulates the response of a very expensive DSP system that lives in a closet somewhere that I don't have access to all the time.  The point is to enable debugging of yet ANOTHER control system that hangs without the DSP present.

I started with the ChatServer example in the Tutorials.  I won't include the rest of the boilerplate because it seems well documented.

Code: [Select]

    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:
      Serial.write(thisChar);


I want to do something like
Code: [Select]

server.write(thisChar);
server.println(" +OK);


which looks like this in my terminal
hello
h +OK
e +OK
l +OK
l +OK
o +OK
+OK

+OK


I'm looking for something more like...


hello
hello +OK

I'm pretty rusty with my C, and I never really trained on C++, so I'm kind of lost.  I've read a number of examples of string functions to use, and I think what I need to do is read thisChar into a string, but I keep getting fouled up.  Is there an easy way to do this with char?

With kindest regards

SurferTim

Try something like this. It waits for a newline character to print the +OK.
Code: [Select]
if(thisChar == '\n') Serial.print(" +OK");
Serial.write(thisChar);


RevDrMrFixIt

Thanks!  I'm much closer now...


Hello, client!
test
test
+OK


FWIW, I used
Code: [Select]

if(thisChar == '\n') server.print(" +OK");

to write back to the telnet session instead of the serial monitor, but this definetly handles better than before.  I really need the +OK to be on the same line, though.  Any thoughts?

SurferTim

You should probably check for the return key also. They are normally sent as a pair.
Code: [Select]
if(thisChar == '\n') server.println(" +OK");
if(thisChar != '\r') server.print(thisChar);

This way the CR/LF is sent by the server.println() command.

RevDrMrFixIt

Awesome!


Hello, client!
hello
hello +OK


The server seems to be returning the cursor to the next line with no carriage return.

I'll need to to some more debugging, but this has got me on the right track, thank you so much!

I don't really understand why
Code: [Select]
      if(thisChar == '\n') server.print(" +OK");
      if(thisChar != '\r') server.print(thisChar);


puts the " +OK" after thisChar, since one comes after the other...

SurferTim

Quote
I don't really understand why
Code:
      if(thisChar == '\n') server.print(" +OK");
      if(thisChar != '\r') server.print(thisChar);

puts the " +OK" after thisChar, since one comes after the other...

It only prints " +OK" if thisChar is a newline. If thisChar is anything but a carriage return, it prints the character. Actually, it probably should have been like this:
Code: [Select]
      if(thisChar == '\n') server.println(" +OK");
      else if(thisChar != '\r') server.print(thisChar);

This way it doesn't print the "\n" either.

RevDrMrFixIt

solved with
Code: [Select]

if(thisChar == '\n') server.print(" +OK\r");


but I think I see why your else statement does the trick as well!

Thank you so very much, SurferTim, you just improved my life tremendously!


SurferTim

Glad I could help. You could do it with either print or println like this:
Code: [Select]
server.println(" +OK");
// equals
server.print(" +OK\r\n");


Go Up