Search code examples
serial-portarduinoxbee

Arduino Sketch - Reading Serial Bytes


I have the following code on my Arduino that constantly checks for a serial command that's sent over TCP using a Wifly library.

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

{power,tv}

It sets these properties accordingly:

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

It then executes certain methods using sendCommand(command, value); based on the properties set in the loop below.

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

        }

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

      }

    }

    value[index] = '\0';

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

    sendCommand(command,value);
  }

}

Instead of using WiFi I've purchased some Xbee modules. They basically allow you to send serial bytes as well. The only problem is that I'm not quite sure how to handle the looping considering there's no while(client.connected()) anymore. Instead of that I've used while(Serial.available()) thinking that will work, but it doesn't set the value property for some reason.

I get command but I don't get value.

Also I'm not sure whether the loop above is the best way of doing what I'm after, all I know is that it works just fine the way it is. :)

Here is my new loop, which only returns command and not value for some reason:

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

  }

}

If the following works with the new loop, I'll be very happy!

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

Solution

  • Got it working by using the following code:

    #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';
      }
    }