String handling

I have a possibly two-part question...

I'm new to C++ and embedded computing, but certainly not new to computer hardware, software, etc... I'm trying to make the Arduino handle serial commands (like, multi-byte serial strings, terminated by ENTER). The command structure I want has every command prefixed with the password, like "pass enable something" ("pass" is the password, followed by the command which enables something).

So I found arduino.cc/en/Tutorial/TextString this String library (forum won't let me post the live URL). I extracted the zip file, and put it (contrary to what the instructions suggest, but it seems to be the only place that worked) in (arduino 0016 folder)\hardware\libraries. When the IDE started, there were some compiler warnings, just as the instructions said there would be. In my code (below), I check for Serial.available() > 0, and if so, I read it into char SerialInByte. Then I have a Switch around SerialInByte, if it's DEC 13 (\n, or newline), I run a function called ProcessCmd, otherwise I append SerialInByte to the end of a string buffer using the append method of that String library. Just for grins, I blink Pin13 so I can see if my code is still running (for reasons that will momentarily become obvious). All that part works.

Here's my

#include <WString.h>


String SerialBuffer = String(30);
String CmdPassword = String(4); 

long PrevMillis = 0;
int LEDstate = LOW;

void setup() {
  Serial.begin(9600);
  
  pinMode(13, OUTPUT);
  
  CmdPassword.append("pass");
  
  Serial.print("Ready\r\n");
}



void loop() {
  // Look for serial data...
  char SerialInByte;
  if (Serial.available() > 0) {
    SerialInByte = Serial.read();

    Serial.print(SerialInByte);
    
    // Look at this byte -- if it's ENTER, process it, else stick it in the buffer...
    switch (SerialInByte) {
      case 13: //newline
        Serial.print("\r\n");
        ProcessCmd();
        break;
      default:
        SerialBuffer.append(SerialInByte); 
    }
  }
  
  // Blinky light, so we know it's still alive...
  if (millis() - PrevMillis > 300) {
    PrevMillis = millis();
    if (LEDstate == HIGH)
      LEDstate = LOW;
    else
      LEDstate = HIGH;
    digitalWrite(13, LEDstate);
  }
}

void ProcessCmd() {
  Serial.print("in ProcessCmd...\r\n");
  
  int cmdlen = SerialBuffer.length();  // # of chars in the entire command buffer
  String thispass = SerialBuffer.substring(0,4);  // Password is always the first 4 bytes
  
  // Check pass...
  if ( cmdlen >= 4 ) {
    if ( CmdPassword.equals(thispass) ){
      Serial.print("CMD:");
      Serial.print(SerialBuffer);
      Serial.print("\r\n");
    } else {
      Serial.print("Bad Password:");
      Serial.print(SerialBuffer);
      Serial.print("\r\n");
    }
  } else {
    Serial.print("Bad command:");
    Serial.print(SerialBuffer);
    Serial.print("\r\n");
  }
  
  // Clear the buffer...
  SerialBuffer = 0;
}

The part that doesn't work is ProcessCmd. It gets as far as the Serial.print saying its running, and then (depending on what commands I put in), either the blinky-light stops blinking, or it resets (and prints "Ready" again). I've narrowed it down to the substring method, but I'm also having a problem with the equals method.

So the first question is, what am i doing wrong, if anything? The second question is, how could I do this differently without using this String class (which seems to be old).

Thank you,

-Matt-

There is a memory leak in the TextString library that you will need to patch.

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1241618944/5

Huh -- well, I'm using the same string all the time, emptying it each time the command is handled, so I'm not sure where memory is leaking, but I believe you...

For this among other reasons, I think I might be better off ditching that WString class, and either finding another one, or doing it the hard way...

What are the chances that this library would compile and run on the Arduino? Boost.Regex - 1.39.0

I can't help you with that other library, but I can tell you the TextString library is very nice once patched.

A full RegEx library would probably be a bit heavy for Arduino, and probably overkill for what you've described.