-
Notifications
You must be signed in to change notification settings - Fork 3
/
dump-serial.cpp
81 lines (65 loc) · 1.73 KB
/
dump-serial.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/timeb.h>
bool emit_timestamp = true;
void usage()
{
fprintf(stderr, "Usage:\n");
fprintf(stderr, "dump-serial serial-device-name baud-rate\n");
exit(1);
}
void show_progress()
{
static int progress=0;
char *progress_str = "|/-\\";
progress = (progress + 1) % strlen(progress_str);
fprintf(stderr, "%c%c", progress_str[progress], 8);
}
int main(int argc, char **argv)
{
timeb tb;
/*
ftime(&tb);
printf("%ld.%d,",(long)tb.time,(int)tb.millitm);
return 1;
*/
if (argc != 3) usage();
char *filename = argv[1];
int baudrate = atoi(argv[2]);
if (!baudrate) usage();
while (1) {
fprintf(stderr, "Capturing %s at %d baud\n", filename, baudrate);
int fd = open(filename, O_RDONLY | O_NONBLOCK);
FILE *in= fdopen(fd, "r");
if (!in) {
fprintf(stderr, "Can't open %s for reading\n", filename);
usage();
}
char buf[200];
sprintf(buf, "stty -f %s %d -crtscts", filename, baudrate);
system(buf);
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | ~O_NONBLOCK);
time_t t;
time(&t);
fprintf(stderr, "Capture starting at %s\n", ctime(&t));
bool beginning_of_line = true;
while (1) {
int c = getc(in);
if (c == EOF) break; // happens when there's an error reading, e.g. USB cable is unplugged
if (beginning_of_line) {
ftime(&tb);
printf("%ld.%d,",(long)tb.time,(int)tb.millitm);
}
putchar(c);
fflush(stdout);
beginning_of_line = (c == '\n');
if (beginning_of_line) show_progress();
}
time(&t);
fprintf(stderr, "Capture ending at %s\n", ctime(&t));
fclose(in);
}
}