comparison rfcal/cmu200/sertool.c @ 187:219ae678b955

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