annotate rfcal/cmu200/sertool.c @ 189:425f16556e69

doc/CMU200-notes started
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 23 Apr 2017 16:42:43 +0000
parents 219ae678b955
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
187
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * This module contains the main() function for fc-serscpi, a manual tool
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 * intended for learning how to control the CMU200 with SCPI commands
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 * over RS-232.
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 */
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <sys/types.h>
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <sys/errno.h>
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <stdio.h>
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include <stdlib.h>
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 #include <unistd.h>
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 #include <strings.h>
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 extern int errno;
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 int target_fd;
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 static void
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 safe_output(buf, cc)
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 u_char *buf;
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 {
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 int i, c;
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 for (i = 0; i < cc; i++) {
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 c = buf[i];
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 if (c == '\r' || c == '\n' || c == '\t' || c == '\b') {
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 putchar(c);
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 continue;
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 }
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 if (c & 0x80) {
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 putchar('M');
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 putchar('-');
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 c &= 0x7F;
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 }
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 if (c < 0x20) {
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 putchar('^');
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 putchar(c + '@');
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 } else if (c == 0x7F) {
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 putchar('^');
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 putchar('?');
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 } else
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 putchar(c);
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 }
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 fflush(stdout);
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 }
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 main(argc, argv)
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 char **argv;
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 {
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 char buf[BUFSIZ];
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 fd_set fds, fds1;
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 register int i, cc, max;
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 if (argc != 3) {
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 fprintf(stderr, "usage: %s ttyname baudrate\n", argv[0]);
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 exit(1);
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 }
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 open_target_serial(argv[1], argv[2]);
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 set_serial_nonblock(0);
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 FD_ZERO(&fds);
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 FD_SET(0, &fds);
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 FD_SET(target_fd, &fds);
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 max = target_fd + 1;
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 for (;;) {
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 bcopy(&fds, &fds1, sizeof(fd_set));
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 i = select(max, &fds1, NULL, NULL, NULL);
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 if (i < 0) {
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 if (errno == EINTR)
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 continue;
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 perror("select");
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 exit(1);
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 }
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 if (FD_ISSET(0, &fds1)) {
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 cc = read(0, buf, sizeof buf);
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 if (cc <= 0)
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 exit(0);
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77 write(target_fd, buf, cc);
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 }
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79 if (FD_ISSET(target_fd, &fds1)) {
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80 cc = read(target_fd, buf, sizeof buf);
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81 if (cc <= 0) {
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82 fprintf(stderr, "EOF/error on target tty\n");
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83 exit(1);
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
84 }
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
85 safe_output(buf, cc);
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
86 }
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
87 }
219ae678b955 fc-serscpi utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
88 }