Go Down

Topic: Looping through serial bytes for Xbee communication. (Read 318 times) previous topic - next topic

fulvio

Jan 13, 2012, 12:35 pm Last Edit: Jan 13, 2012, 01:34 pm by fulvio Reason: 1
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:

Code: [Select]
{power,tv}

It sets these properties accordingly:

Code: [Select]

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


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

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

Code: [Select]

   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.

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:

Code: [Select]

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


Code: [Select]

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


PaulS

Ethernet/wifi data arrives using SPI, which is much faster than serial data. So, the Arduino does not run out of data to read. In the serial data case, it does.

You could use something like this to read the data:
Code: [Select]

#define SOP '<'
#define EOP '>'

bool started = false;
bool ended = false;

char inData[80];
byte index;

void setup()
{
   Serial.begin(57600);
   // 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

    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';
  }
}

Change the SOP and EOP values to match your start and end markers. Where it says "Process the packet", use strtok() to extract the tokens and copy them to your arrays.

fulvio

#2
Jan 14, 2012, 05:56 am Last Edit: Jan 14, 2012, 06:27 am by fulvio Reason: 1

Change the SOP and EOP values to match your start and end markers. Where it says "Process the packet", use strtok() to extract the tokens and copy them to your arrays.


Not sure what you mean about using strtok() to extract tokens. What will inData contain exactly?

Am I on the right track with this? I'm not entirely sure about strtok() and how to use it properly for what I need.

Code: [Select]

// The end of packet marker arrived. Process the packet
char *p = inData;
char *str;

while ((str = strtok_r(p, ",", &p))) {
 sendCommand(str[0], str[1]); // error: invalid conversion from 'char' to 'char*'
}


This is my existing method and how I intend to use it:

Code: [Select]

void sendCommand(char *command, char *value) {
 if (strcmp(command,"power") == 0)
   power(value);
}

PaulS

Quote
Am I on the right track with this? I'm not entirely sure about strtok() and how to use it properly for what I need.

Close, but no.

I suggested strtok(). You used strtok_r(). The strtok_r version is the thread-safe, re-entrant version. Why you think you need that on a single-threaded processor escapes me.

The strtok() version is much simpler, too.
Code: [Select]

char *cmd = strtok(inData, ",");
if(cmd)
{
   char *val = strtok(NULL, ",");
   if(val)
   {
      sendCommand(cmd, val);
   }
}

Go Up