/I am trying to have my program reparse a string by reusing the pointers, I understand that free() is an import function to deallocate the memory of the ptrs but when I use free() it messes up the program, even more, when I compile it parses the string during the first iteration and first only after that it just prints NULL is it something I am doing with my dereferencing or Nulling of my token ptr or something, HELP./
#include <Adafruit_GPS.h>
#include <SoftwareSerial.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
[Mod edit swear word removed]
int i=0, a;
float wind, temperature;
static const int RXPin = 3, TXPin = 2; //Wind : UNO, yellow(Tx+):D3, Green(Tx-):D2
static const uint32_t GPSBaud = 4800;
char *tokp=NULL, *winddir=NULL, *ref=NULL, *windmagraw=NULL, *wunit, *error=NULL, *status=NULL, *checksum=NULL, *type=NULL, *temp=NULL, *tunit=NULL;
char tw[50], WNMEA[] = "$IIMWV,226.0,R,000.0,N,A *0B", TNMEA[] = "$WIXDR,C,022.0,C,,*52", WMID[x]="$IIMWV", TMID[ ]="$WIXDR";
//we need to make the wind and temp vars int* to be able to compare the quantities
// The serial connection to the GPS device
//SoftwareSerial ss(RXPin, TXPin);
//Adafruit_GPS GPS(&ss);
void setup()
{
Serial.begin(9600); //this is matched the CV7-V outputs at 4800 Baud Rate
// ss.begin(GPSBaud); //BIGG TIME SERIAL PORT FOR
}
void loop()
{
/*the function below is for wind magnitude and direction parsing*/
//SSNMEA = ss.read(); //this is what it will really be
//tokp = strtok(SSNMEA, ","); //this is what it will really be
tokp = NULL;
tokp = strtok(WNMEA, ",");
Serial.println(tokp);
// tokp = strtok(TNMEA, ",");
if(strcmp(tokp, WMID)==0){ //"$IIMWV"
winddir = strtok(NULL,",");
Serial.print("Wind Direction in Degrees (000) : ");
Serial.println(winddir);
winddir = NULL;
ref = strtok(NULL,",");
Serial.println(ref);
ref = NULL;
windmagraw = strtok(NULL,",");
Serial.print("Wind speed in Knots : ");
wind = atof(windmagraw); //any to float
Serial.println(windmagraw);
Serial.println(wind);
windmagraw = NULL;
wunit = strtok(NULL, ",");
Serial.print("Wind speed UNIT [Knots = N] : ");
Serial.println(wunit);
wunit = NULL;
error = strtok(NULL, ",");
Serial.println(error);
error = NULL;
status = strtok(NULL, ",");
Serial.println(status);
status = NULL;
checksum = strtok(NULL, ",");
Serial.println(checksum);
checksum = NULL;
}
else if(strcmp(tokp, TMID)==0){ //$WIXDR
type = strtok(NULL,",");
Serial.print("Type of Transducer (C=CV7-V) : ");
Serial.println(type);
type = NULL;
tokp = NULL;
temp = strtok(NULL,",");
Serial.print("The Temperature in DegC : ");
temperature = atof(temp); //any to flaot
Serial.println(temp); //print the string version
Serial.println(temperature); //print the float version that is in my temperature var
temp = NULL;
temperature = NULL;
tunit = strtok(NULL,",");
Serial.print("Unit of Temp (DegC) : ");
Serial.println(tunit);
tunit = NULL;
checksum = strtok(NULL, ",");
Serial.println(checksum);
checksum = NULL;
Serial.println(tokp);
tokp = NULL;
Serial.println(tokp);
free(tokp);
}
else
tokp = NULL;
delay(3000);
}