]> git.zerfleddert.de Git - rigol/blob - scope.c
display some state-informations on the web
[rigol] / scope.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <stdint.h>
5 #include <string.h>
6 #include <strings.h>
7
8 #include "scope.h"
9 #include "usbtmc.h"
10
11 /* Just USB for now... */
12 int sendscpi(struct scope* sc, char* cmd, unsigned char *resp, int resplen)
13 {
14 return usbtmc_sendscpi(sc, cmd, resp, resplen);
15 }
16
17 void closescope(struct scope* sc)
18 {
19 return usbtmc_close(sc);
20 }
21
22 void claimscope(struct scope* sc)
23 {
24 return usbtmc_claim(sc);
25 }
26
27 void releasescope(struct scope* sc)
28 {
29 /* Disable keylock, so the user doesn't have to press the 'force'-button */
30 sendscpi(sc, ":KEY:LOCK DISABLE",NULL,0);
31 return usbtmc_release(sc);
32 }
33
34 void resetscope(struct scope* sc)
35 {
36 return usbtmc_reset(sc);
37 }
38
39 struct scope* initscope(void)
40 {
41 struct scope *sc;
42
43 sc = usbtmc_initscope();
44
45 if (!sc) {
46 printf("No scope found.\n");
47 exit(EXIT_FAILURE);
48 }
49
50 claimscope(sc);
51 sendscpi(sc, "*IDN?", (unsigned char*)sc->idn, sizeof(sc->idn));
52 releasescope(sc);
53
54 printf("Scope found (%s)\n", sc->idn);
55
56 return sc;
57 }
58
59 char *scope_idn(struct scope *sc)
60 {
61 return sc->idn;
62 }
63
64 #define COPY_SCOPE_STRING(sc, cmd, dst) { \
65 char *buf; \
66 buf = scope_get_string(sc, cmd, sizeof(dst)); \
67 if (buf) { \
68 strcpy(dst, buf); \
69 free(buf); \
70 }\
71 }
72
73 char *scope_get_string(struct scope *sc, char *cmd, int maxlen)
74 {
75 unsigned char *buf;
76 int res;
77
78 buf = malloc(maxlen);
79 if (buf == NULL) {
80 perror("malloc(scope_get_strings)");
81 exit(EXIT_FAILURE);
82 }
83
84 res = sendscpi(sc, cmd, buf, maxlen);
85 if (res < 0) {
86 fprintf(stderr, "Command %s failed with %d\n", cmd, res);
87 free(buf);
88 return NULL;
89 }
90
91 buf[res] = 0;
92
93 return (char*)buf;
94 }
95
96 int scope_get_truth_value(struct scope *sc, char *cmd)
97 {
98 char buf[128];
99 int res;
100
101 bzero(buf, sizeof(buf));
102 res = sendscpi(sc, cmd, (unsigned char*)buf, sizeof(buf)-1);
103 if (res < 0) {
104 fprintf(stderr, "Command %s failed with %d\n", cmd, res);
105 return 0;
106 }
107
108 printf("%s %s\n", cmd, buf);
109
110 if (strcasecmp(buf, "on") == 0) {
111 return 1;
112 } else if (strcasecmp(buf, "enable") == 0) {
113 return 1;
114 }
115
116 return 0;
117 }
118
119 int scope_get_int(struct scope *sc, char *cmd)
120 {
121 char buf[128];
122 int res;
123
124 bzero(buf, sizeof(buf));
125 res = sendscpi(sc, cmd, (unsigned char*)buf, sizeof(buf)-1);
126 if (res < 0) {
127 fprintf(stderr, "Command %s failed with %d\n", cmd, res);
128 return 0;
129 }
130
131 return atoi(buf);
132 }
133
134 double scope_get_double(struct scope *sc, char*cmd)
135 {
136 char buf[128];
137 int res;
138 double ret;
139
140 bzero(buf, sizeof(buf));
141 res = sendscpi(sc, cmd, (unsigned char*)buf, sizeof(buf)-1);
142 if (res < 0) {
143 fprintf(stderr, "Command %s failed with %d\n", cmd, res);
144 return 0.0;
145 }
146
147 ret = strtod(buf, NULL);
148
149 return ret;
150 }
151
152 int update_scope_status(struct scope *sc)
153 {
154 bzero(&(sc->status), sizeof(sc->status));
155
156 COPY_SCOPE_STRING(sc, ":INFO:LANG?", sc->status.system.lang);
157
158 sc->status.system.counter_enabled = scope_get_truth_value(sc, ":COUN:ENAB?");
159 sc->status.system.beep_enabled = scope_get_truth_value(sc, ":BEEP:ENAB?");
160
161 sc->status.keyboard.key_lock = scope_get_truth_value(sc, ":KEY:LOCK?");
162
163 COPY_SCOPE_STRING(sc, ":ACQ:TYPE?", sc->status.acquire.type);
164 COPY_SCOPE_STRING(sc, ":ACQ:MODE?", sc->status.acquire.mode);
165
166 sc->status.acquire.averages = scope_get_int(sc, ":ACQ:AVER?");
167 sc->status.acquire.srate_chan1 = scope_get_double(sc, ":ACQ:SAMP? CHAN1");
168 sc->status.acquire.srate_chan2 = scope_get_double(sc, ":ACQ:SAMP? CHAN2");
169 sc->status.acquire.srate_digital = scope_get_double(sc, ":ACQ:SAMP? DIGITAL");
170
171 COPY_SCOPE_STRING(sc, ":TIM:MODE?", sc->status.timebase.mode);
172 sc->status.timebase.offset = scope_get_double(sc, ":TIM:OFFS?");
173 sc->status.timebase.delayed_offset = scope_get_double(sc, ":TIM:DEL:OFFS?");
174 sc->status.timebase.scale = scope_get_double(sc, ":TIM:SCAL?");
175 COPY_SCOPE_STRING(sc, ":TIM:FORM?", sc->status.timebase.format);
176
177 return 0;
178 }
Impressum, Datenschutz