Pages: [1]   Go Down
Author Topic: Wierd problem using Strings() ????  (Read 443 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Goodmorning,
i am happy to join your group and very happy to discover Arduino!

Unfortunately i am facing a problem that i can not solve by my own. I am giving you code below and explaining my issue.

Code:

void loop()
{
    // 0. CM initial state
    // 1. Read the client cmd
    // 2. Parse the cmd
    // 3. Fetch my cmd
    // 4. Execute and print
    // 5. Close
    // 6. Determine the color of the led, red or blue
 
  // Set the variables to zero
  // Set the cm led on
  cm_vars_led_initialState();


  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    boolean sentHeader = false;
    String readStr = String(30);
    boolean first = false;
   
    while(client.connected()){
       
      // Read incoming data from the client
      if(client.available())
      {
     
        // Take data from the client
        if(!sentHeader){
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/xml");
          client.println();
          sentHeader = true;
        }
       
        char c = client.read();
        Serial.print(c);
        Serial.flush();
       
        if(readStr.indexOf("GET") > 0){
          readStr = "";
        }else if(readStr.length() < 2 && ( readStr.indexOf(" ") != -1 || readStr.indexOf("/") != -1 ) ){
          readStr = "";
          first = true;
        }else if(first && c == ' ' ){
           String cmdStr = String(readStr);
           // Remove any empty spaces from the end of my command string
           cmdStr.trim();
           
           Serial.println("xml...");
 
           client.println("<?xml-stylesheet type=\"text/css\" href=\"data:text/css,");
           client.println("Response{display:block;  padding-top:15%;  text-align:center; background-color:%23336699; color:%23FFFFFF; font-family:Tahoma,Arial; font-size:40px;}");
           client.println("Info,Measurement{display:block; padding-top:15%; text-align:center; background-color:%23336699; color:%23ff9933; font-family:Tahoma,Arial; font-size:40px;}type{display:block; padding-bottom:5px; clear:right;}");
           client.println("value{color:%23FFFFFF;}");
           client.println("\"?>");
           client.println();
           
           
           Serial.print("command:");
           Serial.println(cmdStr);
           // Use the command string to obtain the command number
           int command = CmdFromStr(cmdStr);
           Serial.print("command num:");
           Serial.println(command);

           // switch using the command number and execute
           String result = ChooseCmdAndExecute(command);
 
           Serial.print("Results:");
           Serial.println(result);
           Serial.flush();
 
           //client.println(result);
           
           // Keep for debugging purposes
           //client.print("<Response>");
           //client.print(command);
           //client.println("</Response>");
           
           
           
           break;
        }
       
        readStr += c;
       
      } // client.available 
       
    }// while / client.connected() && client.available()
   
    delay(1);
    // Close the connection to the client because there is nothing else to read or write
    client.stop();
    client.flush();
  } // if(client)
 
   // Nodes led red or blue
   node_status_led_set();
   delay(1);
} // loop


Above you see the main loop functions. What i do is parse the http request and keep only the staff i need. After that i want to create a string, through another function and print it backto the client.


Quote
String command_12(){
  String ip_str = byte_ip_to_str(ip,4);
  String str ="<Info><type>firmvare<value> ";
  str += firmware_version;
  str += "</value></type><type>cm<value> ";
  str += cm_version;
  str += "</value></type><type>ip<value> ";
  str += ip_str;
  str += "</value></type><type>mac<value> ";
  str += mac_str;
  str += "</value></type></Info>";

  return str;
}

The above function creates a string with staff i want to send to the client. Ufortunately i can not seem to get any return from the function. Or the staff i get is garbage(!?).
I searched my code up and down and i can not find anything to explain the results. It seems that the String library is not working properly.

Any suggestions would be welcome as i am facing a deadend! 

Regards,
ioigoume
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

An info i forgot to mention. I am using :

- Arduino 1.0.3 ide
- Ardudino Board Model R3

Thanx
Logged

Offline Offline
Edison Member
*
Karma: 43
Posts: 1556
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Don't use the String library. The Arduino does not have enough ram (only 2kB) to do anything other than trivial things with Strings.
You will have to convert your code to use character string arrays (C-style null-terminated strings).

Pete
Logged

Where are the Nick Gammons of yesteryear?

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you for your quick reply!

Is there a possibility to use an sd card to extend the capabilities of the ram!

ioigoume
Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 108
Posts: 5144
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Is there a possibility to use an sd card to extend the capabilities of the ram!

No, an SD card is flash memory and not RAM. RAM is the working memory of the processor, it's reading and writing the RAM thousands of times in a second. Flash memory would wear out in a few seconds (limited write cycles).

The limited RAM is the "beginner" reason for not using the String class.

The implementation of the String class is done in a way suitable for PCs and other computers with lots of RAM and especially: memory management units. The ATmega328 of the Arduino has neither of it. Additionally there is a bug in the low level C library of the AVR utilities (in the free() routine) which makes the bad-for-Arduino programming of the String class even worse. This was the bit lengthy explanation of why not using the String class.

So: Don't use the String class! You can do everything with good old C strings (character arrays), just not as convenient.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanx again! Your feedback is very helpfull !!!  smiley
Logged

Pages: [1]   Go Up
Jump to: