//tell DGPS to start or stop logging // args: [start,stop] filename // KM July 2003 // version 1.1 #include #include #include #include #include #include #define BYTE unsigned char #define SERIALPORT "/dev/ttyS0" #define LOGFILE "/home/km/incoming/gps.log" // GLOBAL variables for simplicity int serialport; // serial port FILE *logfd; // log file char mesg[512]; // somewhere to make logfile strings! //---------------------- HELPERS------------------------- BYTE getbyte () { BYTE byte; read (serialport, &byte, 1); return byte; } // write a byte BYTE putbyte(BYTE b) { return(write(serialport, &b, 1)); } // write a string int putstring(char *s) { int l = strlen(s); //log(s); if ( write(serialport, s, l) != l) return(-1); else return(0); } // print message to log file void logfile(char *str) { fprintf(logfd, str); fflush(logfd); } // print error message to log file and exit erexit(char *str) { fprintf(logfd, str); // exit is a bit severe? exit(1); } //-------------------- INITIALISE----------------- // open serial port for raw byte transfers // open log file initialise() { struct termios term; // open log file for appending logfd = fopen(LOGFILE, "a"); if( logfd == NULL) erexit("failed to open log file!"); // now the serial port serialport = open (SERIALPORT, O_RDWR | O_NOCTTY ); if (serialport == -1) erexit ("can not open serial port!\n"); if( tcgetattr(serialport, &term) == -1) erexit("getattr failed!"); // set BAUD rate cfsetospeed(&term, B38400); cfsetispeed(&term, B38400); cfmakeraw(&term); // Set the serial port mode NOW if( tcsetattr(serialport, TCSANOW, &term) == -1) erexit("setattr failed!"); logfile("openned GPS port OK\n"); } //-------------------- MAIN------------------------ main (int argc, char **argv) { int n; BYTE byte; char command[512]; initialise(); if( !strcmp(argv[1], "-start")){ // set the filename sprintf(command, "create,%s\n",argv[2]); logfile(command); putstring(command); // start logging sprintf(command, "em,/cur/log,def\n"); logfile(command); putstring(command); } else if(!strcmp(argv[1],"-stop")){ // stop logging sprintf(command, "dm,/cur/log\n"); putstring(command); logfile(command); } fclose(logfd); close(serialport); }