Serial debugging not behaving right way

Well the following is my code that reads from sd card (which it does pretty well accurately)

#include <SD.h>

File myFile;
char input[200], ip_add[16], dns[16], port[5], ssid[32], pass[50];
String ip_string, dns_string, ssid_string, pass_string, port_string;
int count=0, initial=0, counter=0;
boolean run=false;

void setup()
{
  Serial.begin(9600);
 // Serial.print("Initializing SD card...");
  // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  // Note that even if it's not used as the CS pin, the hardware SS pin 
  // (10 on most Arduino boards, 53 on the Mega) must be left as an output 
  // or the SD library functions will not work. 
   pinMode(10, OUTPUT);
   
  if (!SD.begin(10)) {
    Serial.println("initialization failed!");
    return;
  }
  //Serial.println("initialization done.");  
  // Open the file for reading:
  myFile = SD.open("settings.txt", FILE_READ);
  if (myFile) {
    Serial.println("settings.txt:");
    count = 0;
    // read from the file until there's nothing else in it:
    while (myFile.available()) {
    	//Serial.write(myFile.read());
        input[count++] = myFile.read(); 
    }
    Serial.print(input); 
    for (int i=5; i < 201; i++){
      if (input[i] == '='){
        initial = i+1;
        counter++;
      }
      if (input[i] == ','){
            int set = i;
           if (counter == 1){
            for (int a=initial;a < set; a++ ){
            ip_add[a-initial] = input[a];
            }
           }
           if (counter == 2){
            for (int a=initial;a < set; a++ ){
            dns[a-initial] = input[a];
            }
           }
           if (counter == 3){
            for (int a=initial;a < set; a++ ){
            port[a-initial] = input[a];
            }
           }
           if (counter == 4){
            for (int a=initial;a < set; a++ ){
            ssid[a-initial] = input[a];
            }
           }
           if (counter == 5){
            for (int a=initial;a < set; a++ ){
            pass[a-initial] = input[a];
            }
            counter == 0;
           }
            //initial = 0;
            //set = 0;
        }
    }
    Serial.println("IP: ");
    Serial.println(ip_add);
    Serial.println("DNS: ");
    Serial.println(dns);
    Serial.println("Port: ");
    Serial.println(port);
    Serial.println("SSID: ");
    Serial.println(ssid);
    Serial.println("PASS: ");
    Serial.println(pass);
    ip_string = ip_add;
    dns_string = dns;
    port_string = port;
    ssid_string = ssid;
    pass_string = pass;
    run = true;
    // close the file:
    myFile.close();
  } else {
  	// if the file didn't open, print an error:
    Serial.println("error opening settings.txt");
  }
}

void loop()
{
 if (run == true) {
    Serial.print("$$");
    delay(200);
    Serial.print("set ip address " + ip_string);
    Serial.print("\r");
    delay(200);
    Serial.print("set ip nm " + dns_string);
    Serial.print("\r");
    delay(200);
    Serial.print('set ip localport' + port_string);
    Serial.print("\r");
    delay(200);
    Serial.print("set wlan ssid " + ssid_string);
    Serial.print("\r");
    delay(200);
    Serial.print("set wlan phrase " + pass_string);
    Serial.print("\r");
    delay(200);
    Serial.print("save");
    Serial.print("\r");
    delay(200);
    Serial.print("exit");
    Serial.print("\r");
    delay(200);
    run = false; 
 }
}

and then after extracting some info some serial statments are made which just work till:

$$set Jp (and not ip whereas it should be ip)address 192.168.0.155

All I can see is:

Serial.print("set ip address " + ip_string);

There must be some sort of global overload for the '+', not a problem though, I can suggest dumping the string library however. There are many threads pointing out its 'not so greatness'. There is a few on the topic of bugs in 'free' causing errors in the string library also.

Each one of the calls to the global operator '+' most probably creates a temporary string object, so 'free' is called many times.

#include <SD.h>

512+ bytes of SRAM gone...

char input[200], ip_add[16], dns[16], port[5], ssid[32], pass[50];

329 more bytes gone...

String ip_string, dns_string, ssid_string, pass_string, port_string;

Resources pissed away like you have terrabytes of memory...

    // read from the file until there's nothing else in it:
    while (myFile.available()) {
    	//Serial.write(myFile.read());
        input[count++] = myFile.read(); 
    }

Whether there is room in the array, or not...

Don't bother with NULL terminating anything.

    for (int i=5; i < 201; i++){
      if (input[i] == '='){

This will try to access the 201st element of the 200 in the array.

            for (int a=initial;a < set; a++ ){
            ip_add[a-initial] = input[a];
            }

Whether the data fits, or not...

            for (int a=initial;a < set; a++ ){
            dns[a-initial] = input[a];
            }

Ditto.

    ip_string = ip_add;
    dns_string = dns;
    port_string = port;
    ssid_string = ssid;
    pass_string = pass;

Why? There is no bloody reason for this!

How much memory do you think you have available while this code is running?

    Serial.print("set ip address " + ip_string);
    Serial.print("\r");

That's not doing quite what you're expecting I think... Ignoring all the other glaring errors around memory management etc...

"\r" is "carriage return" - that is, return the cursor to the start of the current line. The Aruino serial console doesn't handle that at all.

What you are wanting is "\n", which is 'Go to a new line'. better still, use the "Serial.println()" function:

    Serial.println("set ip address " + ip_string);

And while you're at it, ditch the String library :stuck_out_tongue: Better to split the command into two:

    Serial.print("set ip address ");
    Serial.println(ip_string);

That avoids a whole heap of unpleasantness.

There must be some sort of global overload for the '+'

Nope. The String class overloads the + operator to perform concatenation. It is overloaded for a String on the left and for a String on the right.

PaulS:

There must be some sort of global overload for the '+'

Nope. The String class overloads the + operator to perform concatenation. It is overloaded for a String on the left and for a String on the right.

Actually yes.

The type on the left determines the operator owner, and as const char* literal is not a user defined type, you cannot specify a member based operator for it. It can only be globally overloaded, which it is:

friend StringSumHelper & operator + (const StringSumHelper &lhs, const String &rhs);

This test works OK:

String ip_string = "foo";

void setup ()
  {
  Serial.begin (115200);
  Serial.println ("set ip address " + ip_string);
  }  // end of setup

void loop () { }

Output:

set ip address foo

@OP: Please note that, at present, the String library has bugs as discussed here and here.

In particular, the dynamic memory allocation used by the String class may fail and cause random crashes.

I recommend reworking your code to manage without String. Use C-style strings instead (strcpy, strcat, strcmp, etc.).

Alternatively, install the fix described here:
http://arduino.cc/forum/index.php/topic,145765