1

我的 Arduino 上有以下代码,它不断检查使用 Wifly 库通过 TCP 发送的串行命令。

以下代码的作用是在通过串行发送时拆分如下所示的字符串:

{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!
}
4

2 回答 2

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';
  }
}
于 2012-01-14T13:30:16.590 回答
-2

我会改变类似于以下的结构:

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

串行字符的接收速度明显慢于循环。

于 2012-01-13T17:47:20.260 回答