2013-10-13 45 views
0

我試圖讓一個覆盆子pi通過串口與arduino進行通信,硬件都設置好了,並且有一些通信正常。如果等於不匹配,但應該是。 Aduino的IDE串行

問題我已經傳遞的參數與if不匹配,否則如果表達式但真的應該返回什麼。

SerialCommand library處理什麼命令觸發什麼功能。我們只對這個例子中的sayHello()感興趣。 RaspberryPi發送「HELLO HELLO」,應該收到「YES HELLO」,但我總是會得到「NO HELLO」,表明在arg HELLO上沒有匹配。很明顯,收到的論據是迴應無論和我收到什麼應該匹配,你好,我在這個階段難住,有什麼建議嗎?

編輯:謝謝你在下面他的回答Thanushan維文評論,請使用以下替換

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

在下文功能arg是一個指針。因此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"); 
    } 
} 
+0

顯然不能,它不會編譯「rPi_serial.cpp:在函數 '空隙的sayHello()': rPi_serial.cpp:220:17:錯誤:ISO C++禁止指針之間的比較和整數[-fpermissive]「我編輯了我的問題,以包括一個鏈接到圖書館。如果那是真的,爲什麼我會收到HELLO而不是指針指向的地址? – dannix

+1

@dannix您收到HELLO,因爲arg的內容是HELLO,這就是println打印的內容。我認爲問題出在'if(arg ==「HELLO」)'。你可以試試'if(strcmp(「HELLO \ n」,arg)== 0)'。 – Thanushan

+0

修復它,我不知道爲什麼我需要使用strcmp而不是==,所以我必須閱讀。謝謝您的幫助。最受讚賞。 – dannix

相關問題