Go Down

Topic: Pointer is not getting compared correctly (Read 294 times) previous topic - next topic


I pass Commands to Ardiuno via serial, The format which is being used is PARM1/PARM2/PARM3 which is split by ardiuno into 3 parameters (this part is done correctly)

Even if i pass the following command STW/LED/1 or WL/0/0, it still returns command incorrect.

Code: [Select]

void setup() {
Serial.begin(9600);
Serial.flush();
}

void loop () {
char cmd[15];
int i=0;

if (Serial.available()>0) {
  for (i=0;i<15;i++) {
    cmd[i]=Serial.read();
    cmd[i+1]='\0';
    delay(50);
  }

  char* fxn=strtok(cmd,"/\0");
 
  char* obj=strtok(NULL,"/\0");
  int state=atoi(strtok(NULL,"/\0"));
 

 
  if (fxn=="STW") {
  Serial.println("STW");
  }
  else if (fxn=="WL") {
  Serial.println("WL");
  }
  else
  {
  Serial.println("Invalid Command");
  }
 
}




}

AWOL

#1
Mar 27, 2013, 12:37 pm Last Edit: Mar 27, 2013, 12:40 pm by AWOL Reason: 1
Code: [Select]
if (fxn=="STW") {
Did you mean
Code: [Select]
if (strcmp (fxn,"STW") == 0) { perhaps?


(Edit: The pointers are being compared correctly, and because they cannot be the same, the comparison returns false)
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Thanks a lot!! it worked with
Code: [Select]
strcmp (fxn,"STW") == 0

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy