Serial port Arduino草图-读取串行字节

Serial port Arduino草图-读取串行字节,serial-port,arduino,xbee,Serial Port,Arduino,Xbee,我的Arduino上有以下代码,它使用Wifly库不断检查通过TCP发送的串行命令 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

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

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);
  }

}
下面的代码在通过串行方式发送时会拆分一个字符串,如下所示:

{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())
认为这会起作用,但出于某种原因,它没有设置
属性

我得到了
命令
,但没有得到

此外,我不确定上面的循环是否是实现我所追求的目标的最佳方式,我所知道的是,它的工作方式很好。:)

这是我的新循环,由于某些原因,它只返回
命令
,而不返回

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

我将改变结构,类似于:

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

串行字符的接收速度比循环慢得多。

使用以下代码使其正常工作:

#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';
  }
}
#定义SOP'{'
#定义EOP'}'
bool start=false;
布尔结束=假;
char-inData[80];
字节索引;
无效设置()
{
Serial.begin(9600);
//其他东西。。。
}
void循环()
{
//尽可能快地读取所有可用的串行数据
while(Serial.available()>0)
{
char inChar=Serial.read();
如果(英寸=SOP)
{
指数=0;
inData[索引]='\0';
开始=真;
结束=假;
}
否则如果(英寸==EOP)
{
结束=真;
打破
}
其他的
{
如果(指数<79)
{
inData[指数]=英寸;
索引++;
inData[索引]='\0';
}
}
}
//我们在这里也不是因为所有待处理的序列号
//数据已被读取,或者因为
//包裹标记器到了,是哪一个?
如果(开始和结束)
{
//数据包结束标记已到达。请处理数据包
char*cmd=strtok(inData,“,”);
if(cmd)
{
char*val=strtok(空,“”,“”);
if(val)
{
sendCommand(cmd,val);
}
} 
//为下一个数据包重置
开始=错误;
结束=假;
指数=0;
inData[索引]='\0';
}
}