annotate rfcal/cmu200/sercmd.c @ 234:4cee70d429bf

CHANGES: fc-fsio format change documented
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 12 Aug 2017 17:37:02 +0000
parents 31d43f0e469a
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
194
31d43f0e469a fc-cmu200d skeleton started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
31d43f0e469a fc-cmu200d skeleton started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * This module contains the functions that send serial commands to the CMU200
31d43f0e469a fc-cmu200d skeleton started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 * and collect the instrument's serial responses.
31d43f0e469a fc-cmu200d skeleton started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 */
31d43f0e469a fc-cmu200d skeleton started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5
31d43f0e469a fc-cmu200d skeleton started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 #include <sys/types.h>
31d43f0e469a fc-cmu200d skeleton started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <ctype.h>
31d43f0e469a fc-cmu200d skeleton started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <stdio.h>
31d43f0e469a fc-cmu200d skeleton started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <stdlib.h>
31d43f0e469a fc-cmu200d skeleton started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include <unistd.h>
31d43f0e469a fc-cmu200d skeleton started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 #include <string.h>
31d43f0e469a fc-cmu200d skeleton started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 #include <strings.h>
31d43f0e469a fc-cmu200d skeleton started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13
31d43f0e469a fc-cmu200d skeleton started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 extern int target_fd;
31d43f0e469a fc-cmu200d skeleton started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15
31d43f0e469a fc-cmu200d skeleton started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 char instrument_response[4096];
31d43f0e469a fc-cmu200d skeleton started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17
31d43f0e469a fc-cmu200d skeleton started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 send_scpi_cmd(cmd)
31d43f0e469a fc-cmu200d skeleton started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 char *cmd;
31d43f0e469a fc-cmu200d skeleton started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 {
31d43f0e469a fc-cmu200d skeleton started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 printf("Command to CMU: %s", cmd);
31d43f0e469a fc-cmu200d skeleton started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 write(target_fd, cmd, strlen(cmd));
31d43f0e469a fc-cmu200d skeleton started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 }
31d43f0e469a fc-cmu200d skeleton started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24
31d43f0e469a fc-cmu200d skeleton started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 collect_instr_response()
31d43f0e469a fc-cmu200d skeleton started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 {
31d43f0e469a fc-cmu200d skeleton started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 char buf[BUFSIZ];
31d43f0e469a fc-cmu200d skeleton started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 int cc, pos;
31d43f0e469a fc-cmu200d skeleton started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29
31d43f0e469a fc-cmu200d skeleton started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 for (pos = 0; ; ) {
31d43f0e469a fc-cmu200d skeleton started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 cc = read(target_fd, buf, sizeof buf);
31d43f0e469a fc-cmu200d skeleton started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 if (cc <= 0) {
31d43f0e469a fc-cmu200d skeleton started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 perror("error reading from serial port");
31d43f0e469a fc-cmu200d skeleton started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 exit(1);
31d43f0e469a fc-cmu200d skeleton started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 }
31d43f0e469a fc-cmu200d skeleton started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 if (pos + cc > sizeof instrument_response) {
31d43f0e469a fc-cmu200d skeleton started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 fprintf(stderr,
31d43f0e469a fc-cmu200d skeleton started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 "error: response from CMU200 exceeds our buffer size\n");
31d43f0e469a fc-cmu200d skeleton started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 exit(1);
31d43f0e469a fc-cmu200d skeleton started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 }
31d43f0e469a fc-cmu200d skeleton started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 bcopy(buf, instrument_response + pos, cc);
31d43f0e469a fc-cmu200d skeleton started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 pos += cc;
31d43f0e469a fc-cmu200d skeleton started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 if (instrument_response[pos-1] == '\n')
31d43f0e469a fc-cmu200d skeleton started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 break;
31d43f0e469a fc-cmu200d skeleton started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 }
31d43f0e469a fc-cmu200d skeleton started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 instrument_response[pos-1] = '\0';
31d43f0e469a fc-cmu200d skeleton started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 printf("Instrument response: %s\n", instrument_response);
31d43f0e469a fc-cmu200d skeleton started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 }
31d43f0e469a fc-cmu200d skeleton started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49
31d43f0e469a fc-cmu200d skeleton started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 collect_staropc_response()
31d43f0e469a fc-cmu200d skeleton started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 {
31d43f0e469a fc-cmu200d skeleton started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 collect_instr_response();
31d43f0e469a fc-cmu200d skeleton started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 if (instrument_response[0] != '1' ||
31d43f0e469a fc-cmu200d skeleton started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 instrument_response[1] && !isspace(instrument_response[1])) {
31d43f0e469a fc-cmu200d skeleton started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 fprintf(stderr, "error: unexpected response to *OPC?\n");
31d43f0e469a fc-cmu200d skeleton started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 exit(1);
31d43f0e469a fc-cmu200d skeleton started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 }
31d43f0e469a fc-cmu200d skeleton started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 }