2012-01-13 58 views
1

我在我的Arduino上有以下代碼,它不斷檢查通過TCP使用Wifly庫發送的串行命令。Arduino素描 - 閱讀串行字節

什麼下面的代碼不被分裂像字符串以下時通過串行發送:

{power,tv}

它設置這些屬性相應地:

char command[32]; 
char value[32]; 

然後,它執行使用sendCommand(command, value);某些方法基於下面循環中設置的屬性。

請記住,這工作得很好,使用Wifly庫。

void loop() { 
    Client client = server.available(); 

    if (client) { 

    boolean start_data = false; 
    boolean next = false; 

    char command[32]; 
    char value[32]; 
    int index = 0; 

    while (client.connected()) { 

     if (client.available()) { 
     char c = client.read(); 
     Serial.print(c); 

     if (c == '}') { 
      break; 
     } 

     if(start_data == true) { 

      if(c != ',') { 

      if(next) 
       value[index] = c; 
      else 
       command[index] = c; 

      index++; 
      } else { 
      next = true; 
      command[index] = '\0'; 
      index = 0; 
      } 

     } 

     if (c == '{') { 
      start_data = true; 
     } 

     } 

    } 

    value[index] = '\0'; 

    client.flush(); 
    client.stop(); 

    sendCommand(command,value); 
    } 

} 

而不是使用WiFi我已經購買了一些Xbee模塊。他們基本上允許你發送串行字節。唯一的問題是,我不太清楚如何處理循環考慮到沒有while(client.connected())了。而不是我曾使用while(Serial.available())認爲這將工作,但它不會因某種原因設置value屬性。

我得到command但我沒有得到value

此外,我不確定上面的循環是否是做我以後的最佳方式,我所知道的是,它的工作方式很好。 :)

這是我的新的循環,只返回command,而不是value出於某種原因:

void loop() { 

    // if there are bytes waiting on the serial port 
    if (Serial.available()) { 
    boolean start_data = false; 
    boolean next = false; 

    char command[32]; 
    char value[32]; 
    int index = 0; 

    while (Serial.available()) { 
     char c = Serial.read(); 
     Serial.print(c); 

     if (c == '}') { 
     break; 
     } 

     if(start_data == true) { 
     if(c != ',') { 

      if(next) 
      value[index] = c; 
      else 
      command[index] = c; 

      index++; 
     } else { 
      next = true; 
      command[index] = '\0'; 
      index = 0; 
     } 

     } 

     if (c == '{') { 
     start_data = true; 
     } 

    } 

    value[index] = '\0'; 

    sendCommand(command,value); 

    } 

} 

如果新的循環以下工作,我會很高興!

void sendCommand(char *command, char *value) { 
// do something wonderful with command and value! 
} 

回答

1

得到它通過使用下面的代碼工作:

#define SOP '{' 
#define EOP '}' 

bool started = false; 
bool ended = false; 

char inData[80]; 
byte index; 

void setup() 
{ 
    Serial.begin(9600); 
    // Other stuff... 
} 

void loop() 
{ 
    // Read all serial data available, as fast as possible 
    while(Serial.available() > 0) 
    { 
    char inChar = Serial.read(); 
    if(inChar == SOP) 
    { 
     index = 0; 
     inData[index] = '\0'; 
     started = true; 
     ended = false; 
    } 
    else if(inChar == EOP) 
    { 
     ended = true; 
     break; 
    } 
    else 
    { 
     if(index < 79) 
     { 
     inData[index] = inChar; 
     index++; 
     inData[index] = '\0'; 
     } 
    } 
    } 

    // We are here either because all pending serial 
    // data has been read OR because an end of 
    // packet marker arrived. Which is it? 
    if(started && ended) 
    { 
    // The end of packet marker arrived. Process the packet 
    char *cmd = strtok(inData, ","); 
    if(cmd) 
    { 
     char *val = strtok(NULL, ","); 
     if(val) 
     { 
      sendCommand(cmd, val); 
     } 
    } 

    // Reset for the next packet 
    started = false; 
    ended = false; 
    index = 0; 
    inData[index] = '\0'; 
    } 
} 
-2

我會改變的結構相似:

while(c != '}') { 
    if (Serial.available()) { 
      . 
      . 
      . 
    } 
} 

串行字符被接收顯著慢然後循環。