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-