English 中文(简体)
Arduino Sketch - Read Serial Bytes
原标题:Arduino Sketch - Reading Serial Bytes

我的Arduino有以下法典,它不断检查使用Wifly图书馆从TCP上空发送的序列指挥。

What the following code does is split a string like the following when sent over serial:

{power,tv}

因此,它确定了这些特性:

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

然后使用<代码>端Command(command, Value);,根据以下文件所列特性实施。

Keep in mind this works just fine using the Wifly library.

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] =   ;
            index = 0;
          }

        }

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

      }

    }

    value[index] =   ;

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

    sendCommand(command,value);
  }

}

不用使用WiFi I ve购买了一些Xbee模块。 它们基本上允许你寄出序列号。 唯一的问题是,我不敢肯定如何处理在座标上没有<条码>但(客户:联系()。 我没有使用<条码>,而是使用<条码>(Serial. Available()>的想法,这种思维将奏效,但出于某种原因,它并没有设定<条码>>> 值/代码>。

我收到<条码>command ,但我没有打上<条码>。

我也不清楚上述诉讼是否是我之后做什么的最佳方式,我知道,它只是以下列方式做功劳。

这里是我的新行程,其原因有:只有回归代码,而不是<条码>。

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] =   ;
          index = 0;
        }

      }

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

    }

    value[index] =   ;

    sendCommand(command,value);

  }

}

如果以下人员与新 lo合,我会非常高兴!

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

采用以下守则开展工作:

#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] =   ;
       started = true;
       ended = false;
    }
    else if(inChar == EOP)
    {
       ended = true;
       break;
    }
    else
    {
      if(index < 79)
      {
        inData[index] = inChar;
        index++;
        inData[index] =   ;
      }
    }
  }

  // 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] =   ;
  }
}
问题回答

我将改变类似结构:

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

那么,空气特性会大大放缓。





相关问题
How to "echo off" the serial port in java

I am using java comm library for serial port communication in my application. When I put some data on output stream, after sometime the same command is displayed on input stream of serial port. Do ...

Can someone please explain COMTIMEOUTS for me?

I have an application that has strict timing. Inter-character delay must be no more than is 0.15s The messages themselves are quite small. 10 - 50 chars I don t understand what ...

Unable to open serial port in .NET

I am trying to open COM1, but get a strange error every time I call SerialPort.Open(). The error is: The given port name does not start with COM/com or does not resolve to a valid serial port. ...

Serial programming: measuring time between characters

I am sending/receiving data over a serial line in Linux and I would like to find the delay between characters. Modbus uses a 3.5 character delay to detect message frame boundaries. If there is more ...

send string to serial

Buongiorno, I m trying to send a simple string to a serial port to command an instrument for noise measures. The strings are very easy: "M 1" = instrument on "M 2" = instrument off "M 3" = begin the ...

Java RS232 Comm on Vista-64 bit

We have a Java application which needs to communicate with a peripheral device over Virtual Serial COM port. We use the RS232 Java COMM API (javax.comm.properties, comm.jar, win32com.dll) to achieve ...

热门标签