annotate cmu200/sercmd.c @ 34:99f753d4ccaf

fc-cmu200d: check for errors on serial port write
author Mychaela Falconia <falcon@freecalypso.org>
date Tue, 23 May 2017 00:33:10 +0000
parents aeffe53e110d
children
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 {
34
99f753d4ccaf fc-cmu200d: check for errors on serial port write
Mychaela Falconia <falcon@freecalypso.org>
parents: 11
diff changeset
21 int cc, len;
99f753d4ccaf fc-cmu200d: check for errors on serial port write
Mychaela Falconia <falcon@freecalypso.org>
parents: 11
diff changeset
22
0
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 printf("Command to CMU: %s", cmd);
34
99f753d4ccaf fc-cmu200d: check for errors on serial port write
Mychaela Falconia <falcon@freecalypso.org>
parents: 11
diff changeset
24 len = strlen(cmd);
99f753d4ccaf fc-cmu200d: check for errors on serial port write
Mychaela Falconia <falcon@freecalypso.org>
parents: 11
diff changeset
25 cc = write(target_fd, cmd, len);
99f753d4ccaf fc-cmu200d: check for errors on serial port write
Mychaela Falconia <falcon@freecalypso.org>
parents: 11
diff changeset
26 if (cc != len) {
99f753d4ccaf fc-cmu200d: check for errors on serial port write
Mychaela Falconia <falcon@freecalypso.org>
parents: 11
diff changeset
27 perror("serial port write error");
99f753d4ccaf fc-cmu200d: check for errors on serial port write
Mychaela Falconia <falcon@freecalypso.org>
parents: 11
diff changeset
28 exit(1);
99f753d4ccaf fc-cmu200d: check for errors on serial port write
Mychaela Falconia <falcon@freecalypso.org>
parents: 11
diff changeset
29 }
0
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 }
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 collect_instr_response()
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 {
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 char buf[BUFSIZ];
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 int cc, pos;
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 for (pos = 0; ; ) {
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 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
39 if (cc <= 0) {
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 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
41 exit(1);
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 }
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 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
44 fprintf(stderr,
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 "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
46 exit(1);
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 }
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 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
49 pos += cc;
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 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
51 break;
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 }
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 instrument_response[pos-1] = '\0';
11
aeffe53e110d fc-cmu200d: freq-meas works
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
54 if (pos >= 2 && instrument_response[pos-2] == '\r')
aeffe53e110d fc-cmu200d: freq-meas works
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
55 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
56 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
57 }
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 collect_staropc_response()
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 {
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 collect_instr_response();
11
aeffe53e110d fc-cmu200d: freq-meas works
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
62 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
63 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
64 exit(1);
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 }
bd62be88259d initial import of rfcal code and docs from freecalypso-tools repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 }
11
aeffe53e110d fc-cmu200d: freq-meas works
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
67
aeffe53e110d fc-cmu200d: freq-meas works
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
68 parse_commasep_response(argv, expect_count)
aeffe53e110d fc-cmu200d: freq-meas works
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
69 char **argv;
aeffe53e110d fc-cmu200d: freq-meas works
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
70 {
aeffe53e110d fc-cmu200d: freq-meas works
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
71 char *cp, *sp;
aeffe53e110d fc-cmu200d: freq-meas works
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
72 int i;
aeffe53e110d fc-cmu200d: freq-meas works
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
73
aeffe53e110d fc-cmu200d: freq-meas works
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
74 cp = instrument_response;
aeffe53e110d fc-cmu200d: freq-meas works
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
75 for (i = 0; i < expect_count - 1; i++) {
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 sp = index(cp, ',');
aeffe53e110d fc-cmu200d: freq-meas works
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
78 if (!sp)
aeffe53e110d fc-cmu200d: freq-meas works
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
79 return(-1);
aeffe53e110d fc-cmu200d: freq-meas works
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
80 *sp = '\0';
aeffe53e110d fc-cmu200d: freq-meas works
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
81 cp = sp + 1;
aeffe53e110d fc-cmu200d: freq-meas works
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
82 }
aeffe53e110d fc-cmu200d: freq-meas works
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
83 argv[i] = cp;
aeffe53e110d fc-cmu200d: freq-meas works
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
84 if (index(cp, ','))
aeffe53e110d fc-cmu200d: freq-meas works
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
85 return(-1);
aeffe53e110d fc-cmu200d: freq-meas works
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
86 return(0);
aeffe53e110d fc-cmu200d: freq-meas works
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
87 }