/* serial.c * * Written by Andrew Poelsrta * February 11, 2010 * * This code is public domain. */ #include #include #include #include int main(int argc, char **argv) { FILE *fh; int ch = '\n'; /* Start with newline so first line will have timestamp */ if(argc != 2) { puts("You need to supply the serial port!"); return EXIT_FAILURE; } fh = fopen(argv[1], "rb"); if(fh == NULL) { puts("Failed to open serial port!"); return EXIT_FAILURE; } do { unsigned char c = ch; if(c == '\n') { time_t t = time(NULL); struct tm *st = localtime(&t); printf("\n%02d:%02d:%02d: ", st->tm_hour, st->tm_min, st->tm_sec); fflush(stdout); } else if(c == '\r') { /* ignore this character */ } else if(isprint(c)) { printf("%c", c); fflush(stdout); } else { printf("[%02X]", c); fflush(stdout); } } while((ch = fgetc(fh)) != EOF); puts(""); fclose(fh); return 0; }