Pages: [1]   Go Down
Author Topic: Looping through serial bytes for Xbee communication.  (Read 300 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
{power,tv}

It sets these properties accordingly:

Code:
char command[32];
char value[32];

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

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

Code:
   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. smiley

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

Code:
   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:
   void sendCommand(char *command, char *value) {
     // do something wonderful with command and value!
    }
« Last Edit: January 13, 2012, 07:34:53 am by fulvio » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 631
Posts: 50111
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
// 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:
void sendCommand(char *command, char *value) {
  if (strcmp(command,"power") == 0)
    power(value);
}
« Last Edit: January 14, 2012, 12:27:09 am by fulvio » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 631
Posts: 50111
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
char *cmd = strtok(inData, ",");
if(cmd)
{
   char *val = strtok(NULL, ",");
   if(val)
   {
      sendCommand(cmd, val);
   }
}
Logged

Pages: [1]   Go Up
Jump to: