// // comlog.exe // // Generic RS-232 serial port data logging tool. // Line time-stamping and file checkpoint features. // // 07-Jul-2001 tvb // #include #include #include #include #include "comlib.h" #include "loglib.h" void TimeStamp(int mjd, int utc); int Debug; #define DEBUG if (Debug) char Usage[] = "\n" "Usage: comlog [options] port [baud] [log]\n" "\n" "Options:\n" " /port:# - Set COM port\n" " /baud:# - Set baud rate\n" " /even - Even parity\n" " /odd - Odd parity\n" " /flow - Use hardware flow control\n" " /mjd - Use MJD time stamps\n" " /utc - Use UTC time stamps\n" " /log:f - Specify output log file\n" " /help - This help message\n" "\n" "Example:\n" " comlog 3\n" " comlog 1 19200\n" " comlog 7 9600 hp.log\n" " comlog /baud:2400 /odd /log:fts4065b.log\n" "\n"; struct { int Port; int Baud; int Parity; int Flow; int Mjd; int Utc; char *LogPath; } Options; void GetOptions (int argc, char *argv[]) { if (argc == 1) { fprintf(stderr, "%s", Usage); exit(1); } // // Set defaults. // Options.Baud = 9600; Options.Parity = NOPARITY; // // Parse options. // while (argc > 1 && argv[1][0] == '/') { if (strncmp(argv[1], "/port:", 6) == 0) { Options.Port = atoi(&argv[1][6]); } else if (strncmp(argv[1], "/baud:", 6) == 0) { Options.Baud = atoi(&argv[1][6]); } else if (strcmp(argv[1], "/even") == 0) { Options.Parity = EVENPARITY; } else if (strcmp(argv[1], "/odd") == 0) { Options.Parity = ODDPARITY; } else if (strcmp(argv[1], "/flow") == 0) { Options.Flow = TRUE; } else if (strcmp(argv[1], "/mjd") == 0) { Options.Mjd = TRUE; } else if (strcmp(argv[1], "/utc") == 0) { Options.Utc = TRUE; } else if (strncmp(argv[1], "/log:", 5) == 0) { Options.LogPath = &argv[1][5]; } else if (strcmp(argv[1], "/debug") == 0) { Debug = TRUE; } else if (strcmp(argv[1], "/help") == 0) { fprintf(stderr, "%s", Usage); exit(0); } else { fprintf(stderr, "Unknown option: %s\n", argv[1]); exit(1); } argc -= 1; argv += 1; } // // Parse arguments. // if (argc > 1) { Options.Port = atoi(argv[1]); } if (argc > 2) { Options.Baud = atoi(argv[2]); } if (argc > 3) { Options.LogPath = argv[3]; } if (Options.Port == 0) { fprintf(stderr, "Comm port number required.\n"); fprintf(stderr, "%s", Usage); exit(1); } return; } #define IS_NEWLINE(c) ( (c) == '\n' || (c) == '\r' ) VOID _cdecl main (int argc, char *argv[]) { int NewLine; GetOptions(argc, argv); CommOpen(Options.Port, Options.Baud, Options.Parity, Options.Flow); LogOpen(Options.LogPath); DEBUG CommShowStatus(); NewLine = TRUE; do { char Buffer[1000]; char Byte; int Count; int Index; // // Read from comm port and write to log file. // At the beginning of each line add optional time stamps. // Also flush file to disk if necessary. // Count = CommRead(Buffer, sizeof Buffer); DEBUG printf("[%d]", Count); for (Index = 0; Index < Count; Index += 1) { Byte = Buffer[Index]; if (NewLine && !IS_NEWLINE(Byte)) { LogSync(); TimeStamp(Options.Mjd, Options.Utc); } NewLine = IS_NEWLINE(Byte); } LogWrite(Buffer, Count); } while (TRUE); LogClose(); CommClose(); } // // Prepend MJD and/or UTC time stamp to line. // void TimeStamp(int mjd, int utc) { char Buffer[100]; time_t Time; struct tm *Tm; long DateToMjd (long Year, long Month, long Day); time(&Time); Tm = gmtime(&Time); if (mjd) { double Mjd; long Seconds; Mjd = DateToMjd(Tm->tm_year + 1900, Tm->tm_mon + 1, Tm->tm_mday); Seconds = (((Tm->tm_hour * 60) + Tm->tm_min) * 60) + Tm->tm_sec; Mjd += Seconds / 86400.0; sprintf(Buffer, "%.5lf ", Mjd); LogWrite(Buffer, strlen(Buffer)); } if (utc) { sprintf(Buffer, "%.4d-%.2d-%.2d %.2d:%.2d:%.2d ", Tm->tm_year + 1900, Tm->tm_mon + 1, Tm->tm_mday, Tm->tm_hour, Tm->tm_min, Tm->tm_sec); LogWrite(Buffer, strlen(Buffer)); } return; } // // Return Modified Julian Day (MJD) given // calendar year, month (1-12), and day (1-31). // - Valid for Gregorian dates from 17-Nov-1858. // - Adapted from sci.astro FAQ. // long DateToMjd (long Year, long Month, long Day) { return 367 * Year - 7 * (Year + (Month + 9) / 12) / 4 - 3 * ((Year + (Month - 9) / 7) / 100 + 1) / 4 + 275 * Month / 9 + Day + 1721028 - 2400000; } // Or use a makefile... #include "comlib.c" #include "loglib.c"