Pages: [1]   Go Down
Author Topic: EchoServer based on ChatServer, appending strings to output  (Read 708 times)
0 Members and 1 Guest are viewing this topic.
San Francisco
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
    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:
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
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 144
Posts: 5986
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

San Francisco
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks!  I'm much closer now...


Hello, client!
test
test
 +OK


FWIW, I used
Code:
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?
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 144
Posts: 5986
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You should probably check for the return key also. They are normally sent as a pair.
Code:
if(thisChar == '\n') server.println(" +OK");
if(thisChar != '\r') server.print(thisChar);
This way the CR/LF is sent by the server.println() command.
Logged

San Francisco
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
      if(thisChar == '\n') server.print(" +OK");
      if(thisChar != '\r') server.print(thisChar);

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

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 144
Posts: 5986
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
      if(thisChar == '\n') server.println(" +OK");
      else if(thisChar != '\r') server.print(thisChar);
This way it doesn't print the "\n" either.
Logged

San Francisco
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

solved with
Code:
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!

Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 144
Posts: 5986
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: