fix off by one
[rigol] / scope.c
CommitLineData
713be7a4
MG
1#include <stdio.h>
2#include <stdlib.h>
3#include <unistd.h>
7df76f49 4#include <stdint.h>
78fb0984
MG
5#include <string.h>
6#include <strings.h>
713be7a4 7
713be7a4 8#include "scope.h"
7906b395 9#include "usbtmc.h"
713be7a4
MG
10
11/* Just USB for now... */
12int sendscpi(struct scope* sc, char* cmd, unsigned char *resp, int resplen)
13{
7906b395 14 return usbtmc_sendscpi(sc, cmd, resp, resplen);
713be7a4
MG
15}
16
17void closescope(struct scope* sc)
18{
7906b395 19 return usbtmc_close(sc);
713be7a4
MG
20}
21
22void claimscope(struct scope* sc)
23{
7906b395 24 return usbtmc_claim(sc);
713be7a4
MG
25}
26
27void releasescope(struct scope* sc)
28{
10ca7ea8 29 /* Disable keylock, so the user doesn't have to press the 'force'-button */
713be7a4 30 sendscpi(sc, ":KEY:LOCK DISABLE",NULL,0);
7906b395 31 return usbtmc_release(sc);
713be7a4
MG
32}
33
10ca7ea8
MG
34void resetscope(struct scope* sc)
35{
36 return usbtmc_reset(sc);
37}
38
713be7a4
MG
39struct scope* initscope(void)
40{
713be7a4
MG
41 struct scope *sc;
42
7906b395 43 sc = usbtmc_initscope();
713be7a4 44
7906b395 45 if (!sc) {
2999345d
MG
46 printf("No scope found.\n");
47 exit(EXIT_FAILURE);
48 }
713be7a4 49
713be7a4
MG
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
59char *scope_idn(struct scope *sc)
60{
61 return sc->idn;
62}
78fb0984
MG
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
73char *scope_get_string(struct scope *sc, char *cmd, int maxlen)
74{
75 unsigned char *buf;
76 int res;
77
759a1836 78 buf = malloc(maxlen+1);
78fb0984
MG
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
96int 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
119int 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
134double 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
152int 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