Arduino Sketch - 读取序列字节

时间:2012-01-13 11:00:50

标签: serial-port arduino xbee

我的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!
}

2 个答案:

答案 0 :(得分: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';
  }
}

答案 1 :(得分:-2)

我会改变类似的结构:

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

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