Help with serial communication

I am making a basic environmental capture device and writing a basic command line interface so I can quickly get readings. However this is totally not working the way I expect. Here is my code:

int incomingByte = 0;
char buffer[100];
int pos=0;

void setup() 
{
    Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}

void loop() 
{
        // send data only when you receive data:
        if (Serial.available() > 1) 
        {
                // read the incoming byte:
                incomingByte = Serial.read();  
               

                // say what you got:
                Serial.print("\r\nI received: ");
                Serial.print(incomingByte,DEC);
                
                if(incomingByte==13) //enter pressed
                {
                    pos=0;
                    
                    //process command
                    if(buffer=="help")CMD_help();
                    else if(buffer=="astates")CMD_astates();
                    else if(buffer=="dstates")CMD_dstates();
                    else if(buffer=="allstates")CMD_allstates();
                    else if(buffer!="")
                    {
                      Serial.print("Unknown command: \"");
                      Serial.print(buffer);
                      Serial.print("\", type help for list of valid commands");
                    }
                    
                    Serial.flush();
                    
                    pos=0;
                    Serial.print("\r\n\r\n#");  
                }
                else
                {                
                  buffer[pos]=incomingByte;
                  buffer[pos+1]=0;
                  pos++; 
                }
               
        }
}






void CMD_help()
{
    Serial.print("-------------------------------------------------------------------------------\r\n");  
    Serial.print("HAL9000 / EnviroState 1.0.0 [updated June 15 2012]  -  www.iceteks.com\r\n");
    Serial.print("-------------------------------------------------------------------------------\r\n");
    Serial.print("help...........................................................show this screen\r\n");
    Serial.print("astates......................................................show analog states\r\n");
    Serial.print("dstates.....................................................show digital states\r\n");
    Serial.print("allstates..................................show both analong and digital states\r\n");
}





void CMD_astates()
{
  

}

void CMD_dstates()
{
  

}


void CMD_allstates()
{
  CMD_astates();
  CMD_dstates();
}

None of the if statements seem to be working. It always says it's an invalid command even though the text matches.

Am I missing something here? I've written serial/tcp programs before and used a similar way to capture the data into a buffer then process it, but for some reason this is not working out at all. I'm very new to this so maybe there's something I should be doing? When it's comparing is it comparing the ENTIRE array or is it smart enough to know to only compare all the way to \0 and NOT include the \0 in the compare?

                    if(buffer=="help")CMD_help();

You better read up on C strings. This is comparing two pointers, not what they are pointing to.

Better would be:

                    if (strcmp (buffer, "help") == 0) CMD_help ();

Ahhh I see, I'm used to working with std strings in C++, so I have to use strcmp like with the old style strings. I think I got it now. Thanks.

There is a String class, and you can install std::string, however with limited memory you are probably better off getting static char arrays to work.

Good to know, but yeah I'll stick to standard strings. So is this actually true c++ or just a syntax that is similar? Like can I actually use OOP and such? I probably would not write programs complex enough for that mind you, but always good to know it's available.

Here's my final app, think this will work out ok for my needs:

#include <EEPROM.h>

int incomingByte = 0;
char buffer[100];
int pos=0;
int deviceid;

void setup() 
{
    //want all digital pins to be digital inputs, no PWM needed for this app
    pinMode(2, INPUT);
    pinMode(3, INPUT);
    pinMode(4, INPUT);
    pinMode(5, INPUT);
    pinMode(6, INPUT);
    pinMode(7, INPUT);
    pinMode(8, INPUT);
    pinMode(9, INPUT);
    pinMode(10, INPUT);
    pinMode(11, INPUT);
    pinMode(12, INPUT);
    
    //notes: cannot use pins 0,1 and 13 as they are reserved. 
    
        
    deviceid=EEPROM.read(0);    
  
    Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}

void loop() 
{
        // send data only when you receive data:
        if (Serial.available() > 0) 
        {
                // read the incoming byte:
                incomingByte = Serial.read();  
                
               
                //filter unwanted chars:                
                if(incomingByte<13 || (incomingByte>14 && incomingByte<32) || incomingByte>126)return;
                
                
                
                //echo back what we typed:                
                Serial.print((char)incomingByte);
               
                //if enter pressed or reached end of buffer, process buffer            
                if(incomingByte==13 || pos>=99) 
                {
                    pos=0;
                    
                    Serial.print("\r\n");
                    
                    //process command
                    if(strcmp(buffer,"help")==0)CMD_help();
                    else if(strcmp(buffer,"id")==0)CMD_id();
                    else if(strcmp(buffer,"setid")==0)CMD_setid();
                    else if(strcmp(buffer,"astates")==0)CMD_astates();
                    else if(strcmp(buffer,"dstates")==0)CMD_dstates();
                    else if(strcmp(buffer,"allstates")==0)CMD_allstates();
                    else if(strcmp(buffer,"")!=0)
                    {
                      Serial.print("Unknown command: \"");
                      Serial.print(buffer);
                      Serial.print("\", type help for list of valid commands");
                    }
                    
                    Serial.flush();
                    
                    pos=0;
                    Serial.print("\r\n# ");  
                }
                else
                {                
                  buffer[pos]=incomingByte;
                  buffer[pos+1]=0;
                  pos++; 
                }
               
        }
}






void CMD_help()
{
    Serial.print("\r\n\r\n-------------------------------------------------------------------------------\r\n");  
    Serial.print("HAL9000 / EnviroState 1.0.0 [updated June 15 2012]  -  www.iceteks.com\r\n");
    Serial.print("-------------------------------------------------------------------------------\r\n");
    Serial.print("help             Show this screen\r\n");
    Serial.print("id               Show device id\r\n");
    Serial.print("setid            Set device id\r\n");
    Serial.print("astates          Show analog states\r\n");
    Serial.print("dstates          Show digital states\r\n");
    Serial.print("allstates        Show both analong and digital states\r\n");
}



void CMD_id()
{
  Serial.print(deviceid,DEC);
}


void CMD_setid()
{
  Serial.print("Cur id:");
  Serial.print(deviceid,DEC);
  Serial.print("\r\nnew id:");  

  //wait for data to be available
  while(Serial.available() == 0);
  
  deviceid = Serial.read(); 
  
  EEPROM.write(0,deviceid);
  
  Serial.print("\r\nID changed\r\n");
}


void CMD_astates()
{
  Serial.println(analogRead(A0));
  Serial.println(analogRead(A1));
  Serial.println(analogRead(A2));
  Serial.println(analogRead(A3));
  Serial.println(analogRead(A4));
  Serial.println(analogRead(A5));
}

void CMD_dstates()
{
  Serial.println(digitalRead(2));
  Serial.println(digitalRead(3));
  Serial.println(digitalRead(4));
  Serial.println(digitalRead(5));
  Serial.println(digitalRead(6));
  Serial.println(digitalRead(7));
  Serial.println(digitalRead(8));
  Serial.println(digitalRead(9));
  Serial.println(digitalRead(10));
  Serial.println(digitalRead(11));
  Serial.println(digitalRead(12));
}


void CMD_allstates()
{
  CMD_astates();
  CMD_dstates();
}

I'll be getting some scrap telco frame jumper cable tomorrow at work and then I can start connecting the inputs to my terminal block on the makeshift chassis that I made, and I'll have myself a nice little environmental data acquisition device. :stuck_out_tongue: This is working out quite nicely. Glad I heard of Arduino!

RedSquirrel:
So is this actually true c++ or just a syntax that is similar?

It's C++.

I think I may have mentioned that in the "sticky":

http://arduino.cc/forum/index.php/topic,97455.0.html