代码之家  ›  专栏  ›  技术社区  ›  dannix

if equals不匹配,但应该匹配。Aduino IDE串行

  •  0
  • dannix  · 技术社区  · 11 年前

    我正试图让一个树莓派通过串行与arduino通信,硬件已经全部设置好,还有一些通信可以。

    我遇到的问题是,传递的参数与if,else-if表达式不匹配,但实际上应该偏离返回的内容。

    SerialCommand library 处理哪些命令触发哪些功能。对于这个例子,我们只对sayHello()感兴趣。RaspberryPi发送“HELLO HELLO”,它应该会收到“YES HELLO”但我总是收到“NO HELLO”提示在arg HELLO上没有匹配。很明显,不管怎样,收到的论点都会得到回应,我收到的应该是匹配的,你好,所以我在这个阶段被难住了,有什么建议吗?

    编辑:感谢Thanushan Balakrishnan在下面的回答中的评论,替换

    if(arg == "HELLO")
    

    通过以下操作修复了问题。

    if(strcmp("HELLO", arg) == 0)
    

    _

    // Demo Code for SerialCommand Library
    // Steven Cogswell
    // May 2011
    
    
    // temp & Humidity
    #include <DHT22.h>
    #define DHT22_PIN 12
    // Setup a DHT22 instance
    DHT22 myDHT22(DHT22_PIN);
    
    // tank water level
    int sensorValue = 0;
    int constrainedValue = 0;
    int tankLevel = 0;
    #define TANK_SENSOR 0
    #define TANK_EMPTY 0
    #define TANK_FULL 1023
    
    
    /*RelayBrd */
    //Pin connected to latch pin (ST_CP) of 74HC595
    const int latchPin = 4;
    //Pin connected to clock pin (SH_CP) of 74HC595
    const int clockPin = 3;
    ////Pin connected to Data in (DS) of 74HC595
    const int dataPin = 2;
    boolean thisState = LOW;
    
    
    #include <SerialCommand.h>
    #define arduinoLED 13   // Arduino LED on board
    
    SerialCommand sCmd;     // The demo SerialCommand object
    
    void setup() {
      pinMode(arduinoLED, OUTPUT);      // Configure the onboard LED for output
      digitalWrite(arduinoLED, LOW);    // default to LED off
    
      //mySerial.begin(9600);
      //mySerial.println("Ready");
    
      Serial.begin(115200);
    
      // Setup callbacks for SerialCommand commands
      sCmd.addCommand("ON",   LED_on);          // Turns LED on
      sCmd.addCommand("OFF",  LED_off);         // Turns LED off
    
      sCmd.addCommand("getenv",   getEnv);
      sCmd.addCommand("relaybrd",   relayBrd);
      sCmd.addCommand("gettanklevel",   getTankLevel);
      sCmd.addCommand("setlouver",   setLouver);
      sCmd.addCommand("HELLO", sayHello);        // Echos the string argument back
      sCmd.addCommand("P",     processCommand);  // Converts two arguments to integers and echos them back
      sCmd.setDefaultHandler(unrecognized);      // Handler for command that isn't matched  (says "What?")
      Serial.println("Ready");
    }
    
    void loop() {
      sCmd.readSerial();     // We don't do much, just process serial commands
    
     // if (mySerial.available())
     //   Serial.write(mySerial.read());
     // if (Serial.available())
     //   mySerial.write(Serial.read());
    
    }
    
    
    
    void relayBrd(){
    
      char *arg;
      char *arg1;
      arg = sCmd.next();
      arg1 = sCmd.next();
    
    
      if (arg != NULL) {
        //int num = atol(arg);
        if (arg1 != NULL) {
           int state = atol(arg1);
           if (arg == "fan") {
              //do something when var equals 1
              registerWrite(1, state);
    
           }else if(arg == "water"){
              //do something when var equals 2
              registerWrite(2, state);
    
          }else if(arg == "mister"){
              //do something when var equals 2
              registerWrite(3, state);
    
          }else if(arg == "heater"){
              //do something when var equals 2
              registerWrite(4, state);
    
          }else{ 
              // if nothing else matches, do the default
              Serial.print("ERR got:");Serial.print(arg);Serial.println(":");
          }
        }else{
            Serial.println("ERR Relay state missing 1/0");
        }  
    
      }else{
        Serial.println("ERR Relay argument missing fan/water/heater/mister");
      }
    
    
    
    
    }
    
    // This method sends bits to the shift register:
    
    void registerWrite(int whichPin, int whichState) {
    
         Serial.print("Relay ");Serial.print(whichPin);Serial.print(" set to ");Serial.println(whichState);
    // the bits you want to send
      byte bitsToSend = 0;
    
      // turn off the output so the pins don't light up
      // while you're shifting bits:
      digitalWrite(latchPin, LOW);
    
      // turn on the next highest bit in bitsToSend:
      bitWrite(bitsToSend, whichPin, whichState);
    
      // shift the bits out:
      shiftOut(dataPin, clockPin, MSBFIRST, bitsToSend);
    
        // turn on the output so the LEDs can light up:
      digitalWrite(latchPin, HIGH);
    
    }
    
    void getEnv(){
      Serial.println("26 degC");
       DHT22_ERROR_t errorCode;
     Serial.print("Requesting data...");
      errorCode = myDHT22.readData();
      switch(errorCode)
      {
        case DHT_ERROR_NONE:
          Serial.print("Got Data ");
          Serial.print(myDHT22.getTemperatureC());
          Serial.print("C ");
          Serial.print(myDHT22.getHumidity());
          Serial.println("%");
          break;
        default:
        Serial.print("ERR DHT22 ");
        Serial.println(errorCode);
      }
    
    }
    
    
    
    
    void getTankLevel(){
      Serial.println("78% water level");
      sensorValue = analogRead( TANK_SENSOR );
      constrainedValue = constrain( sensorValue, TANK_EMPTY, TANK_FULL );
      tankLevel = map( constrainedValue, TANK_EMPTY, TANK_FULL, 0, 100 );
    
    }
    
    void setLouver() {
      char *arg;
    
    
      arg = sCmd.next();
      if (arg != NULL) {
        if(arg == "OPEN"){
             Serial.println("Louver OPEN");
        }else if(arg == "CLOSE"){
              Serial.println("Louver CLOSED");
        }else{
        Serial.print(arg);Serial.println(" not known");
        }
      }else{
        Serial.println("Louver command missing OPEN/CLOSE");
      }
    }  
    
    void LED_on() {
      Serial.println("LED on");
      digitalWrite(arduinoLED, HIGH);
    }
    
    void LED_off() {
      Serial.println("LED off");
      digitalWrite(arduinoLED, LOW);
    }
    
    void sayHello() {
      char *arg;
      arg = sCmd.next();    // Get the next argument from the SerialCommand object buffer
    
      if (arg != NULL) {    // As long as it existed, take it
        if (arg == "HELLO") {    // As long as it existed, take it
        Serial.print("YES ");
        Serial.println(arg);
        }else{
           Serial.print("NO ");
        Serial.println(arg);
        }
      }
      else {
        Serial.println("Hello Pi");
      }
    }
    
    
    void processCommand() {
      int aNumber;
      char *arg;
    
      Serial.println("We're in processCommand");
      arg = sCmd.next();
      if (arg != NULL) {
        aNumber = atoi(arg);    // Converts a char string to an integer
        Serial.print("First argument was: ");
        Serial.println(aNumber);
      }
      else {
        Serial.println("No arguments");
      }
    
      arg = sCmd.next();
      if (arg != NULL) {
        aNumber = atol(arg);
        Serial.print("Second argument was: ");
        Serial.println(aNumber);
      }
      else {
        Serial.println("No second argument");
      }
    }
    
    // This gets set as the default handler, and gets called when no other command matches.
    void unrecognized(const char *command) {
      Serial.println("What?");
    }
    
    1 回复  |  直到 11 年前
        1
  •  1
  •   Thanushan    11 年前

    在以下功能中 arg 是一个指针。所以 参数 具有保存“HELLO”的存储器的地址。所以你应该检查一下 *arg == "HELLO"

    void sayHello()
    {
       char *arg;
       arg = sCmd.next();    // Get the next argument from the SerialCommand object buffer
    
       if (arg != NULL) {    // As long as it existed, take it
          if (strcmp("HELLO\n", arg) == 0) {         <-------------------------- HERE
             Serial.print("YES ");
             Serial.println(arg);
          }else{
             Serial.print("NO ");
             Serial.println(arg);
          }
       }
       else {
          Serial.println("Hello Pi");
       }
    }