Go Down

Topic: trying to split a char (Read 1 time) previous topic - next topic


I am working with a GPS using a software serial port and putting each character into a char array. I need to split all the data such as latitude, longitude, date and time and put them into variables so I can parse the data individually. I am using the strtok() function to separate the comma delimited data. From there I run a loop to separate the tokens and populate the variables. This works great and I get all the data properly. Now I am trying to work with the date variable which is stored as a char but I cant figure out how.The date comes in format HhMmSs (Ex. 062317) and  I need to split it into int hours, int minutes, int seconds. Can I split a char like this? If not how can I grab the data differently in order to split it?Here is code for how I am getting the data for the date variable.

Code: [Select]

char date;
char delimiters[] = ",";
char* valPosition;

void GPRMC(){
  valPosition = strtok(GPRMCC, delimiters);
  i = 0;
  while(valPosition != NULL || i < 11){
    valPosition = strtok(NULL, delimiters);
      date = *valPosition;
      Serial.print(F("date = "));


Here's a tip:

If you are trying to use GPS with arduino, consider using the TinyGPS library. It has a reasonably small footprint and seems relatively easy to use. I am going to use it when I get my GPS hooked up with my autopilot software. Should make things much easier than having to manually parse the data string.

Side note: For some reason, the TinyGPS website is sometimes down... or it might be slow loading.

RadarProject - Arduino Autopilot for RC http://radarproject.wordpress.com/


I appreciate that. I have used both tinyGPS library and the sketch from the playground and they both work well. I am trying to write my own in order to learn as well as develop some features for my project that would impossible without a lot of modification to the library.


Dec 11, 2012, 09:59 am Last Edit: Dec 11, 2012, 10:09 am by guix Reason: 1
Hello, an easy solution is to use sscanf
Code: [Select]

uint8_t h, m, s;
sscanf( date, "%2hhu%2hhu%2hhu", &h, &m, &s );

//or if you really need int..
int h, m, s;
sscanf( date, "%2d%2d%2d", &h, &m, &s );

Another solution..
Code: [Select]

  h = ((date[0] - '0') * 10) + (date[1] - '0'),
  m = ((date[2] - '0') * 10) + (date[3] - '0'),
  s = ((date[4] - '0') * 10) + (date[5] - '0');


I would avoid sscanf(): like printf(), it is one of those first things they teach you not to do in embedded programming 101.

The 2nd approach is what I would use, probably through a macro to structure it a little bit more.

Go Up