Code:
#include <stdio.h>
#include <windows.h>
#include <string.h>
void main()
{
//*****************************************SAMPLE STRING INPUT FROM COM PORT**********************
char streaminput[] = "#RAWWAASFRAMEA,COM1,9,68.0,SATTIME,1268,126458.000,00000000,58e4,1522,22,133,62,566345634563462323c000,22*d4567cde";
//************************************REMOVE THE DECLARED VALUE ABOVE BEFORE USE************
double a, GPSsec, c;
int i, GPSweek, k, l, m, decodPRN, decodmsgtype, p;
char COMport[5], decodmsg[100], decodCRC[20];
if ( sscanf(streaminput, "#RAWWAASFRAMEA,%4[^,],%d,%lf,SATTIME,%d,%lf,%d,%lf,%d,%d,%d,%d,%99[^,],%d*%19[^\n]",
COMport, &i, &a, &GPSweek, &GPSsec, &k, &c, &l, &m, &decodPRN, &decodmsgtype, decodmsg, &p, decodCRC) == 14 )
//****************************************CHECK TO SEE THAT WE HAVE THE CORRECT VALUE****************************************
printf("GPSsec = %g\n", GPSsec);
printf("GPSweek = %d\ndecodPRN = %d\ndecodmsgtype = %d\n" , GPSweek, decodPRN, decodmsgtype);
printf("COMport = %s\n", COMport);
printf("decodmsg = %s\n", decodmsg);
printf("decodCRC = %s\n", decodCRC);
//************************************************REMOVE ALL ABOVE BEFORE USE************************************************
char stringPRN[4]; //conversion from decodPRN for final string "decoded"
char stringmsgtype[5]; //conversion from decodmsgtype for final string "decoded"
char buffPRN[5]; //string for PRN **needed for naming for folders**
char buffyear[20]; //string for year |
char buffday[20]; //string for day | These will be split from GPS time conversion value " TIME"
char buffhour[20]; //string for day |
char decoded[250] = "\0"; //FINAL OUTPUT STRING, TERMINATED BY \0
int hour = 2; //*************************************
int day = 10; //*********FOR TEST PURPOSE************
int year = 2004; //*********REMOVE BEFORE USE***********
char time[9] = "09 42 07"; //*************************************
char date[12] = "05 07 05"; //*************************************
//************************************Checking and setting of directories********************************************
sprintf(buffPRN, "c:\\temp\\PRN%3d", decodPRN);
SetCurrentDirectory(buffPRN); //sets directory to current PRN
if(SetCurrentDirectory(buffPRN) == NULL)
{ CreateDirectory(buffPRN, NULL);
printf("\n\n\nNew PRN detected and folder created\n\n\n");
}
//******Checks to see if year exists and creates new directory if statement if false*********\\
sprintf(buffyear, "c:\\temp\\PRN%3d\\Y%4d", decodPRN, year);
SetCurrentDirectory(buffyear); //sets directory to current year
if (SetCurrentDirectory(buffyear) == NULL)
{ CreateDirectory(buffyear, NULL);
printf("\n\n\nNew Year detected and folder created\n\n\n");
}
//******Checks to see if day exists and creates new directory if statement if false*********\\
sprintf(buffday, "c:\\temp\\PRN%3d\\Y%4d\\d%3d", decodPRN, year, day);
SetCurrentDirectory(buffday); //sets directory to current day
if (SetCurrentDirectory(buffday) == NULL)
{ CreateDirectory(buffday, NULL);
printf("\n\n\nNew Day detected and folder created\n\n\n");
}
sprintf(buffhour, "c:\\temp\\PRN%3d\\Y%4d\\d%3d\\h%2d.ems", decodPRN, year, day, hour);
//*****************************Conversion of Ints to Chars for the final output string************************
sprintf(stringPRN, "%d", decodPRN);
sprintf(stringmsgtype, "%d", decodmsgtype);
//sprintf(stringtime, "%d", time); *********** NOT NEEDED YET (activate before use) ************
//***************************Joining of all appropriate strings to construct final string************************
memset(decoded,0,250);
strcat(decoded, stringPRN);
strcat(decoded, " ");
strcat(decoded, time);
strcat(decoded, " ");
strcat(decoded, date);
strcat(decoded, " ");
strcat(decoded, stringmsgtype);
strcat(decoded, " ");
strcat(decoded, decodmsg);
strcat(decoded, " ");
strcat(decoded, decodCRC);
printf("\n\nString = %s\n\n", decoded);
{
//*************This portion writes the formatted data to the appropriate ems file************
FILE *fp = fopen(buffhour, "a+b");
{
//*****************Writes contents of decoded final data to the appropriate hour file********
fprintf ( fp, "\n"); // write to a new line
fputs ( decoded, fp );
printf("String Written in file\n\n");
//******************Closes the file, flushes all buffers and waits for next cycle*************************
fclose ( fp );
fflush(NULL);
printf("All processes successful\n\n");
}
}
}