View unanswered posts | View active topics It is currently Fri Oct 24, 2014 9:23 pm






Reply to topic  [ 7 posts ] 
Splitting a string in Robot C 
Author Message
Rookie

Joined: Thu Jan 30, 2014 9:28 am
Posts: 8
Post Splitting a string in Robot C
I was trying to get a GPS receiver to work with the cortex. The GPS outputs coordinates in a format similar to: "$GPGLL,4027.027912,N,08704.857070,W,18432.00,A,D*7A"

The data(longitude and latitude) is split up by commas. I was trying to split this string into different sections (after each comma) and apply the sections to different variables.

Does anyone know how to do this in Robot C?


Thu Jan 30, 2014 9:42 am
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3227
Location: Rotterdam, The Netherlands
Post Re: Splitting a string in Robot C
I have some token parsing here: https://github.com/botbench/rdpartyrobo ... ind-wifi.h, check towards the bottom :)

= Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Thu Jan 30, 2014 1:09 pm
Profile WWW
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3227
Location: Rotterdam, The Netherlands
Post Re: Splitting a string in Robot C
I managed to find some old code I had:

Edit, seems I blogged about it a while back: http://botbench.com/blog/2013/01/03/ple ... or-robotc/

Code:
#define STRTOK_MAX_TOKEN_SIZE 20
#define STRTOK_MAX_BUFFER_SIZE 50

/**
 * Tokenise an array of chars, using a seperator
 * @param buffer pointer to buffer we're parsing
 * @param token pointer to buffer to hold the tokens as we find them
 * @param seperator the seperator used between tokens
 * @return true if there are still tokens left, false if we're done
 */
bool strtok(char *buffer, char *token, char *seperator)
{
  int pos = StringFind(buffer, seperator);
  char t_buff[STRTOK_MAX_BUFFER_SIZE];

  // Make sure we zero out the buffer and token
  memset(token, 0, STRTOK_MAX_TOKEN_SIZE);
  memset(&t_buff[0], 0, STRTOK_MAX_BUFFER_SIZE);

  // Looks like we found a seperator
  if (pos >= 0)
  {
    // Copy the first token into the token buffer, only if the token is
    // not an empty one
    if (pos > 0)
      memcpy(token, buffer, pos);
    // Now copy characters -after- the seperator into the temp buffer
    memcpy(&t_buff[0], buffer+(pos+1), strlen(buffer) - pos);
    // Zero out the real buffer
    memset(buffer, 0, strlen(buffer) + 1);
    // Copy the temp buffer, which now only contains everything after the previous
    // token into the buffer for the next round.
    memcpy(buffer, &t_buff[0], strlen(&t_buff[0]));
    return true;
  }
  // We found no seperator but the buffer still contains a string
  // This can happen when there is no trailing seperator
  else if(strlen(buffer) > 0)
  {
    // Copy the token into the token buffer
    memcpy(token, buffer, strlen(buffer));
    // Zero out the remainder of the buffer
    memset(buffer, 0, strlen(buffer) + 1);
    return true;
  }
  return false;
}

task main()
{
  // For output purposes only
  int counter = 0;

  // Buffer we'd like to parse
  char *buff = "1234:5678::12:";

  // Array and associated pointer to hold tokens
  char tokArr[STRTOK_MAX_TOKEN_SIZE];
  char *tokPtr = &tokArr;

  // The character used to seperate the tokens
  char *sep = ":";


  writeDebugStreamLine("buff: %s", buff);
  writeDebugStreamLine("sep: \"%s\"", sep);

  // Go through buff until we're done and print out all the tokens
  // as we find them
  while (true)
  {
    if (strtok(buff, tokPtr, sep))
      writeDebugStreamLine("tok[%d]: %s", counter++, tokPtr);
    else
      return;
    // This is to keep writeDebugStreamLine from tripping up when
    // there's too much output at once
    EndTimeSlice();
  }
}



This should do what you want.

= Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Fri Jan 31, 2014 3:53 am
Profile WWW
Rookie

Joined: Thu Jan 30, 2014 9:28 am
Posts: 8
Post Re: Splitting a string in Robot C
Thanks for the reply. I'm not very experienced in programming, so I'm lost while looking at the code you posted. It's full of functions that I don't understand. If it wouldn't be a problem, could you explain how I could integrate the code for what I'm doing?


Fri Jan 31, 2014 9:44 am
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3227
Location: Rotterdam, The Netherlands
Post Re: Splitting a string in Robot C
No worries. I modified the main task part (not the strtok function, that's generic) to munge on the NMEA string. So take the above program and replace the task main part with this stuff. I have changed the separator to a comma "," instead of colon as it was in my original code.

Code:
task main()
{
  // For output purposes only
  int counter = 0;

  // Buffer we'd like to parse
  char *buff = "$GPGLL,4027.027912,N,08704.857070,W,18432.00,A,D*7A";

  // Array and associated pointer to hold tokens
  char tokArr[STRTOK_MAX_TOKEN_SIZE];
  char *tokPtr = &tokArr;

  // The character used to seperate the tokens
  char *sep = ",";


  writeDebugStreamLine("buff: %s", buff);
  writeDebugStreamLine("sep: \"%s\"", sep);

  // Go through buff until we're done and print out all the tokens
  // as we find them
  while (true)
  {
    if (strtok(buff, tokPtr, sep))
      writeDebugStreamLine("tok[%d]: %s", counter++, tokPtr);
    else
      return;
    // This is to keep writeDebugStreamLine from tripping up when
    // there's too much output at once
    EndTimeSlice();
  }
}


When you run this, it will write the following in the DebugStream window:

Code:
buff: $GPGLL,4027.027912,N,08704.857070,W,18432.00,A,D*7A
sep: ","
tok[0]: $GPGLL
tok[1]: 4027.027912
tok[2]: N
tok[3]: 08704.857070
tok[4]: W
tok[5]: 18432.00
tok[6]: A
tok[7]: D*7A


So now your GPS string has been split up, just like you wanted it to be. If you have more questions, let me know.

= Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Fri Jan 31, 2014 11:02 am
Profile WWW
Rookie

Joined: Thu Jan 30, 2014 9:28 am
Posts: 8
Post Re: Splitting a string in Robot C
Thanks! Is there a way I could store the tokens in the debug stream as a string or integer like "gpslat"?


Mon Feb 03, 2014 9:15 am
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3227
Location: Rotterdam, The Netherlands
Post Re: Splitting a string in Robot C
Sure! Right now they're all null terminated char arrays, so they're like strings. If you know if a token is supposed to be a float (like the latitude), you can simply use atof() to convert to a float and atoi to convert to an int.

= Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Mon Feb 03, 2014 10:48 am
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 7 posts ] 

Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.