Telnet capture/resend

Some basic telnet/client code attached from other example. The return outputs all the characters received after the ‘-’ is detected but I’m not sure how to capture specific lines within this data. Telnet data consists of 34 characters wide by 9 lines long (basic text). I just want to capture the line that begins with “RX” and send it out to another serial port…any help would be great!

Gus.

#include <Ethernet.h>
#include <SPI.h>


  byte ip[] = { 192, 168, 1, x };   //ip address (arduino)
  byte server[] = {192,168,1,x}; //ip Address of the server 
  byte subnet[] = { 255, 255, 255, 0 };
  byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

  Client client(server, 23); 

////////////////////////////////////////////////////

char inString[400]; // string for incoming serial data
int stringPos = 0; // string index counter
boolean startRead = false; 

void setup(){
  Ethernet.begin(mac, ip);
  Serial.begin(9600);
}

void loop(){
  String pageValue = connectAndRead(); //connect to the server and read the output

  Serial.println(pageValue); //print out the findings.

  delay(2000); //wait 2 seconds before connecting again
}

String connectAndRead(){
 //connect to the server

  Serial.println("connecting...");

  if (client.connect()) {
    Serial.println("connected");
    client.print("menu1");
    delay(10);
    client.print("menu2");
    delay(10);
    client.print("status");
 
    //Connected - Read the page
    return readPage(); // read the output

  }else{
    return "connection failed";
  }

}

String readPage(){
//read the page, and capture & return everything between '-' and '['

  stringPos = 0;
  memset( &inString, 0, 402); //clear inString memory

  while(true){

    if (client.available()) {
      char c = client.read();

      if (c == '-') { //'-' is our begining character
        startRead = true; //Ready to start reading 
      }else if(startRead){

        if(c != '['){ //'[' is our ending character
          inString[stringPos] = c;
          stringPos ++;
        }else{
          //got what we need - We can disconnect now
          startRead = false;
          client.stop();
          client.flush();
          Serial.println("disconnecting.");
          return inString;

        }

      }
    }

  }

}
String readPage(){
          return inString;

What is the purpose of having the function copy a global variable to a different type to return? Where is the advantage of String over the char array?

        if(c != '['){ //'[' is our ending character
          inString[stringPos] = c;
          stringPos ++;

Might be a good idea to check that there actually is room in the array, to prevent running off the end.

Telnet data consists of 34 characters wide by 9 lines long (basic text). I just want to capture the line that begins with "RX" and send it out to another serial port.

Some sample data would be good to look at.

Any book on C will cover all the string (lower case s) functions, like strstr (find the first occurrence of one string in another, like "RX" in inString). Once you know where the string in question starts, you can copy the data from that position to the end of the array to another array, and then put a NULL in that array where the data of interest ends (the CR/LF?). What you are left with then is the "RX line" which you do whatever with.

Thanks Paul, here is a sample of the data/text output.....I want to capture the rx state line and see if it is locked or unlocked. Al the tabs remain the same in the text data.

  TX level:                0.000000 dbm
  TX Freq:                500.000000 Khz
  PLL ID:                  4
  Channel number:     12
  TX   Cond:             OFF
  RX   Lock:             UNLOCKED
  Connection:           Idle Channel

I did something similar to capture some data in a web page. As the telnet data is probably captured one character at a time from the input buffer, you need to count the cr/lf that are encountered. In your sample data you would start capturing a string when the fifth lf is encountered and quit capturing when the sixth lf is encountered. Then you could use the .indexOf to test for a U in the captured string to determine locked or unlocked.