#include #include #include #define BUFFSIZE 256 #define NQCCOMMAND "nqc" // make sure nqc is in path #define TRUE 1 #define FALSE 0 int sendNQCCommand(char *command, char *outfile) { char buff[BUFFSIZE]; sprintf(buff, "%s %s > %s", NQCCOMMAND, command, outfile); printf("Executing: %s\n", buff); return system(buff); } /* sendNQCCommand() */ char *getNQCResult(char *infile, char *result, int resultsize) { FILE *fp; char *cp; *result = '\0'; fp = fopen(infile,"r"); if(!fp) { printf("Unable to open file %s for result\n", infile); exit(1); } if(! feof(fp)) { fgets(result,resultsize,fp); cp = strchr(result,'\n'); if(cp) *cp = '\0'; } fclose(fp); return result; } /* getNQCResult() */ int waitForDatalog(char *tempfile) { int getlog; char result[BUFFSIZE]; sendNQCCommand("-raw 120000", tempfile); getNQCResult(tempfile, result, BUFFSIZE); if(strlen(result)) { printf("Result was %s\n", result); sscanf(result, "%d", &getlog); if(getlog == 4) { remove(tempfile); return FALSE; } else if( getlog == 0) { sendNQCCommand("-raw 1400020100", tempfile); } else if( getlog == 2) { sendNQCCommand("-raw 1400020300", tempfile); } else if( getlog == 1 || getlog == 3) { printf("RCX hasn't replied to ping.\n"); } } return TRUE; // keep waiting } /* waitForDatalog() */ #define LOGSTART 32000 void retrieveDatalog(char *tempfile, char *datalog) { char buff[BUFFSIZE]; int logstart; sendNQCCommand("-raw 1400020500", tempfile); // tell RCX we're grabbing log do { sendNQCCommand("-datalog", tempfile); getNQCResult(tempfile, buff, BUFFSIZE); printf("retrieveDatalog() - Result is %s\n", buff); if(strlen(buff)) { printf("Retrived datalog - result was %s\n", buff); sscanf(buff, "%d", &logstart); if(logstart == LOGSTART) // change this { sprintf(buff, "cat %s >> %s", tempfile, datalog); printf("Executing: %s\n", buff); system(buff); } else { printf("Unknown start for Datalog - %s\n", buff); } } } while( logstart != LOGSTART); remove(tempfile); } /* retrieveDatalog() */ void resetDatalog(char *tempfile) { char buff[BUFFSIZE]; sendNQCCommand("-raw 1400020600", tempfile); getNQCResult(tempfile, buff, BUFFSIZE); if(strlen(buff)) { printf("Reset datalog - result was %s\n", buff); } remove(tempfile); } /* resetDatalog() */ main() { char tempfile[BUFFSIZE], datalog[BUFFSIZE]; sprintf(tempfile, "__temp.%d", getpid()); sprintf(datalog,"datalog.%d", getpid()); while(1) { while(waitForDatalog(tempfile)) sleep(1); retrieveDatalog(tempfile, datalog); resetDatalog(tempfile); } }