annotate cmu200/sercmd.c @ 11:aeffe53e110d

fc-cmu200d: freq-meas works
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 21 May 2017 19:22:11 +0000
parents bd62be88259d
children 99f753d4ccaf
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * This module contains the functions that send serial commands to the CMU200
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 * and collect the instrument's serial responses.
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 */
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 #include <sys/types.h>
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <ctype.h>
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <stdio.h>
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <stdlib.h>
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include <unistd.h>
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 #include <string.h>
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 #include <strings.h>
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 extern int target_fd;
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 char instrument_response[4096];
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 send_scpi_cmd(cmd)
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 char *cmd;
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 {
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 printf("Command to CMU: %s", cmd);
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 write(target_fd, cmd, strlen(cmd));
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 }
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 collect_instr_response()
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 {
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 char buf[BUFSIZ];
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 int cc, pos;
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 for (pos = 0; ; ) {
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 cc = read(target_fd, buf, sizeof buf);
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 if (cc <= 0) {
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 perror("error reading from serial port");
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 exit(1);
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 }
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 if (pos + cc > sizeof instrument_response) {
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 fprintf(stderr,
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 "error: response from CMU200 exceeds our buffer size\n");
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 exit(1);
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 }
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 bcopy(buf, instrument_response + pos, cc);
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 pos += cc;
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 if (instrument_response[pos-1] == '\n')
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 break;
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 }
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 instrument_response[pos-1] = '\0';
11
aeffe53e110d fc-cmu200d: freq-meas works
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
47 if (pos >= 2 && instrument_response[pos-2] == '\r')
aeffe53e110d fc-cmu200d: freq-meas works
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
48 instrument_response[pos-2] = '\0';
0
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 printf("Instrument response: %s\n", instrument_response);
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 }
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 collect_staropc_response()
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 {
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 collect_instr_response();
11
aeffe53e110d fc-cmu200d: freq-meas works
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
55 if (instrument_response[0] != '1' || instrument_response[1]) {
0
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 fprintf(stderr, "error: unexpected response to *OPC?\n");
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 exit(1);
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 }
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 }
11
aeffe53e110d fc-cmu200d: freq-meas works
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
60
aeffe53e110d fc-cmu200d: freq-meas works
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
61 parse_commasep_response(argv, expect_count)
aeffe53e110d fc-cmu200d: freq-meas works
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
62 char **argv;
aeffe53e110d fc-cmu200d: freq-meas works
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
63 {
aeffe53e110d fc-cmu200d: freq-meas works
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
64 char *cp, *sp;
aeffe53e110d fc-cmu200d: freq-meas works
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
65 int i;
aeffe53e110d fc-cmu200d: freq-meas works
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
66
aeffe53e110d fc-cmu200d: freq-meas works
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
67 cp = instrument_response;
aeffe53e110d fc-cmu200d: freq-meas works
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
68 for (i = 0; i < expect_count - 1; i++) {
aeffe53e110d fc-cmu200d: freq-meas works
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
69 argv[i] = cp;
aeffe53e110d fc-cmu200d: freq-meas works
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
70 sp = index(cp, ',');
aeffe53e110d fc-cmu200d: freq-meas works
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
71 if (!sp)
aeffe53e110d fc-cmu200d: freq-meas works
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
72 return(-1);
aeffe53e110d fc-cmu200d: freq-meas works
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
73 *sp = '\0';
aeffe53e110d fc-cmu200d: freq-meas works
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
74 cp = sp + 1;
aeffe53e110d fc-cmu200d: freq-meas works
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
75 }
aeffe53e110d fc-cmu200d: freq-meas works
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
76 argv[i] = cp;
aeffe53e110d fc-cmu200d: freq-meas works
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
77 if (index(cp, ','))
aeffe53e110d fc-cmu200d: freq-meas works
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
78 return(-1);
aeffe53e110d fc-cmu200d: freq-meas works
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
79 return(0);
aeffe53e110d fc-cmu200d: freq-meas works
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
80 }