Rev 137 | Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
65 | f9daq | 1 | #include <formatio.h> |
2 | #include <userint.h> |
||
3 | #include <ansi_c.h> |
||
4 | #include <utility.h> |
||
5 | #include <tcpsupp.h> |
||
6 | #include <cvirte.h> |
||
7 | |||
8 | #include "lecroy_tcp.h" |
||
9 | #include "wavejet_ui.h" |
||
10 | |||
11 | //#define SCOPE_IP "192.168.1.11" |
||
12 | #define MAX_TCP_CONNECT 5 /* time in secs to get a connection */ |
||
13 | #define MAX_TCP_READ 3 /* time in secs to wait for the DSO |
||
14 | to respond to a read request */ |
||
15 | #define MAX_ST 512 /* maximum message string. no picked from thin air */ |
||
16 | |||
17 | #define MAX_THREADS 10 |
||
18 | |||
19 | static int sockfd; |
||
20 | static int p1h, pID, rID, tfID; |
||
21 | static int poolHandle = 0; |
||
22 | |||
23 | |||
24 | |||
25 | int CVICALLBACK cb_timer (int panel, int control, int event, void *callbackData, |
||
26 | int eventData1, int eventData2) |
||
27 | { |
||
28 | QueueUserEvent (9000, p1h, P1_TIMER); |
||
29 | return (0); |
||
30 | } |
||
31 | /* |
||
32 | int CVICALLBACK daq_run(void *functionData) |
||
33 | { |
||
34 | return (0); |
||
35 | } |
||
36 | */ |
||
37 | |||
38 | int __stdcall WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, |
||
39 | LPSTR lpszCmdLine, int nCmdShow) |
||
40 | { |
||
41 | int i; |
||
42 | int status,connected,displen; |
||
43 | int refon; |
||
44 | int tmpindex,tmpint; |
||
45 | int p1_tra[5]={P1_TRA_1,P1_TRA_2,P1_TRA_3,P1_TRA_4,0}; |
||
46 | int p1_vdiv[5]={P1_VDIV_1,P1_VDIV_2,P1_VDIV_3,P1_VDIV_4,0}; |
||
47 | int p1_bwl[5]={P1_BWL_1,P1_BWL_2,P1_BWL_3,P1_BWL_4,0}; |
||
48 | float tmpfloat; |
||
49 | char ip_address[50],tmpstr[100]; |
||
50 | char outbuf[MAX_ST],inbuf[MAX_ST]; |
||
51 | char display[500000],imgfn[300],ftyp[10]; |
||
52 | |||
53 | FILE *imgfp; |
||
54 | |||
55 | connected=0; |
||
56 | |||
57 | if (InitCVIRTE (hInstance, 0, 0) == 0) |
||
58 | return -1; /* out of memory */ |
||
59 | |||
60 | SetSleepPolicy(VAL_SLEEP_MORE); |
||
61 | CmtNewThreadPool (MAX_THREADS, &poolHandle); |
||
62 | |||
63 | SetStdioWindowOptions (1000, 0, 0); |
||
64 | SetStdioWindowSize (150, 600); |
||
65 | SetStdioWindowPosition (100, 20); |
||
66 | |||
67 | if ((p1h = LoadPanel (0, "wavejet_ui.uir", P1)) < 0) return -1; |
||
68 | DisplayPanel (p1h); |
||
69 | |||
70 | |||
71 | // SetCtrlAttribute (p1h, P1_PLCH, ATTR_MAX_VALUE, NCH-1); |
||
72 | QueueUserEvent (1000, p1h, P1_IPF); |
||
73 | |||
74 | do { |
||
75 | GetUserEvent (1, &pID, &rID); |
||
76 | switch (rID) { |
||
77 | case P1_UPDATE: |
||
78 | if (!connected) break; |
||
79 | sprintf(outbuf,"TSCRN? BMP\n"); |
||
80 | LECROY_TCP_write(sockfd, outbuf); |
||
81 | LECROY_TCP_read(sockfd, display, sizeof(display), MAX_TCP_READ); |
||
82 | sscanf(display,"# %1d %8d",&status,&displen); |
||
83 | imgfp = fopen ("tmp.bmp", "wb"); |
||
84 | status = fwrite (&display[10], 1, displen, imgfp); |
||
85 | status = fclose (imgfp); |
||
86 | status = DisplayImageFile (p1h, P1_DISPLAY, "tmp.bmp"); |
||
87 | break; |
||
88 | case P1_DSAV: |
||
89 | if (!connected) break; |
||
90 | status = GetCtrlVal (p1h, P1_DSAV, tmpstr); |
||
91 | sprintf(ftyp,"*.%s",tmpstr); |
||
92 | StringLowerCase (ftyp); |
||
93 | status = FileSelectPopup ("", ftyp, ftyp,"Hard copy", VAL_SAVE_BUTTON, |
||
94 | 0, 1, 1, 1, imgfn); |
||
95 | if (status<1) break; |
||
96 | sprintf(outbuf,"TSCRN? %s\n",tmpstr); |
||
97 | LECROY_TCP_write(sockfd, outbuf); |
||
98 | LECROY_TCP_read(sockfd, display, sizeof(display), MAX_TCP_READ); |
||
99 | sscanf(display,"# %1d %8d",&status,&displen); |
||
100 | imgfp = fopen (imgfn, "wb"); |
||
101 | status = fwrite (&display[10], 1, displen, imgfp); |
||
102 | status = fclose (imgfp); |
||
103 | break; |
||
104 | case P1_GET_SETUP: |
||
105 | if (!connected) break; |
||
106 | break; |
||
107 | case P1_TIMER: |
||
108 | //ntics+=1; |
||
109 | GetCtrlVal (p1h, P1_REFRESH, &refon); |
||
110 | if (refon) QueueUserEvent (9000, p1h, P1_UPDATE); |
||
111 | break; |
||
112 | case P1_REFRESH: |
||
113 | break; |
||
114 | /* |
||
115 | case P1_DAQ: |
||
116 | GetCtrlVal (p1h, P1_DAQ, &daq_on); |
||
117 | if (daq_on) { |
||
118 | CmtScheduleThreadPoolFunction (poolHandle, daq_run, (void *)&dummy, &tfID); |
||
119 | } else { |
||
120 | CmtWaitForThreadPoolFunctionCompletion (poolHandle, tfID, |
||
121 | OPT_TP_PROCESS_EVENTS_WHILE_WAITING); |
||
122 | CmtReleaseThreadPoolFunctionID (poolHandle, tfID); |
||
123 | } |
||
124 | break; |
||
125 | */ |
||
126 | case P1_IPF: |
||
127 | status = GetCtrlVal (p1h, P1_IPF, ip_address); |
||
128 | status = SetCtrlVal (p1h, P1_IP, ip_address); |
||
129 | break; |
||
130 | case P1_CONNECT: |
||
131 | if (connected) { |
||
132 | strcpy(outbuf,"GTL\n"); |
||
133 | LECROY_TCP_write(sockfd, outbuf); |
||
134 | LECROY_TCP_disconnect(sockfd); |
||
135 | status = SetCtrlVal (p1h, P1_ID, ""); |
||
136 | connected=0; |
||
137 | }else{ |
||
138 | status = GetCtrlVal (p1h, P1_IP, ip_address); |
||
139 | sockfd=LECROY_TCP_connect(ip_address, MAX_TCP_CONNECT); |
||
140 | if (sockfd<0) { |
||
141 | //printf("\nCould not connect to the scope on IP: %s\n",ip_address); |
||
142 | status = SetCtrlVal (p1h, P1_ID, "Could not connect!"); |
||
143 | status = SetCtrlVal (p1h, P1_CONNECT, 0); |
||
144 | break; |
||
145 | }else{ |
||
146 | connected=1; |
||
147 | strcpy(outbuf,"*idn?\n"); |
||
148 | LECROY_TCP_write(sockfd, outbuf); |
||
149 | LECROY_TCP_read(sockfd, inbuf, sizeof(inbuf), MAX_TCP_READ); |
||
150 | status = SetCtrlVal (p1h, P1_ID, inbuf); |
||
151 | |||
152 | for (i=0;i<4;i++) { |
||
153 | sprintf(outbuf,"C%1d:TRA?\n",i+1); |
||
154 | LECROY_TCP_write(sockfd, outbuf); |
||
155 | LECROY_TCP_read(sockfd, inbuf, sizeof(inbuf), MAX_TCP_READ); |
||
156 | if (FindPattern (inbuf, 0, 2, "ON", 1, 0)) { |
||
157 | status = SetCtrlVal (p1h, p1_tra[i], 0); |
||
158 | }else{ |
||
159 | status = SetCtrlVal (p1h, p1_tra[i], 1); |
||
160 | } |
||
161 | |||
162 | sprintf(outbuf,"C%1d:VDIV?\n",i+1); |
||
163 | LECROY_TCP_write(sockfd, outbuf); |
||
164 | LECROY_TCP_read(sockfd, inbuf, sizeof(inbuf), MAX_TCP_READ); |
||
165 | sscanf(inbuf,"%g",&tmpfloat); |
||
166 | status = SetCtrlVal (p1h, p1_vdiv[i], tmpfloat); |
||
167 | |||
168 | sprintf(outbuf,"C%1d:BWL?\n",i+1); |
||
169 | LECROY_TCP_write(sockfd, outbuf); |
||
170 | LECROY_TCP_read(sockfd, inbuf, sizeof(inbuf), MAX_TCP_READ); |
||
171 | sscanf(inbuf,"%s",tmpstr); |
||
172 | status = SetCtrlVal (p1h, p1_bwl[i], tmpstr); |
||
173 | } |
||
174 | |||
175 | sprintf(outbuf,"PERS?\n"); |
||
176 | LECROY_TCP_write(sockfd, outbuf); |
||
177 | LECROY_TCP_read(sockfd, inbuf, sizeof(inbuf), MAX_TCP_READ); |
||
178 | sscanf(inbuf,"%s",tmpstr); |
||
179 | status = SetCtrlVal (p1h, P1_PERS, tmpstr); |
||
180 | |||
181 | sprintf(outbuf,"TSRC?\n"); |
||
182 | LECROY_TCP_write(sockfd, outbuf); |
||
183 | LECROY_TCP_read(sockfd, inbuf, sizeof(inbuf), MAX_TCP_READ); |
||
184 | sscanf(inbuf,"%s",tmpstr); |
||
185 | status = SetCtrlVal (p1h, P1_TSRC, tmpstr); |
||
186 | |||
187 | sprintf(outbuf,"TSLP?\n"); |
||
188 | LECROY_TCP_write(sockfd, outbuf); |
||
189 | LECROY_TCP_read(sockfd, inbuf, sizeof(inbuf), MAX_TCP_READ); |
||
190 | sscanf(inbuf,"%s",tmpstr); |
||
191 | status = SetCtrlVal (p1h, P1_TSLP, tmpstr); |
||
192 | } |
||
193 | } |
||
194 | break; |
||
195 | // vertical |
||
196 | case P1_TRA_1: |
||
197 | case P1_TRA_2: |
||
198 | case P1_TRA_3: |
||
199 | case P1_TRA_4: |
||
200 | for (i=0;i<4;i++) { |
||
201 | if (rID==p1_tra[i]) { |
||
202 | status = GetCtrlVal (p1h, p1_tra[i], &tmpint); |
||
203 | if (tmpint) { |
||
204 | sprintf(outbuf,"C%1d:TRA ON\n",i+1); |
||
205 | }else{ |
||
206 | sprintf(outbuf,"C%1d:TRA OFF\n",i+1); |
||
207 | } |
||
208 | LECROY_TCP_write(sockfd, outbuf); |
||
209 | } |
||
210 | } |
||
211 | break; |
||
212 | case P1_VDIV_1: |
||
213 | case P1_VDIV_2: |
||
214 | case P1_VDIV_3: |
||
215 | case P1_VDIV_4: |
||
216 | for (i=0;i<4;i++) { |
||
217 | if (rID==p1_vdiv[i]) { |
||
218 | status = GetCtrlVal (p1h, p1_vdiv[i], &tmpfloat); |
||
219 | sprintf(outbuf,"C%1d:VDIV %.3f\n",i+1,tmpfloat); |
||
220 | LECROY_TCP_write(sockfd, outbuf); |
||
221 | } |
||
222 | } |
||
223 | break; |
||
224 | case P1_BWL_1: |
||
225 | case P1_BWL_2: |
||
226 | case P1_BWL_3: |
||
227 | case P1_BWL_4: |
||
228 | for (i=0;i<4;i++) { |
||
229 | if (rID==p1_bwl[i]) { |
||
230 | status = GetCtrlVal (p1h, p1_bwl[i], tmpstr); |
||
231 | sprintf(outbuf,"C%1d:BWL %s\n",i+1,tmpstr); |
||
232 | LECROY_TCP_write(sockfd, outbuf); |
||
233 | } |
||
234 | } |
||
235 | break; |
||
236 | // horizontal |
||
237 | case P1_TDIV: |
||
238 | if (!connected) break; |
||
239 | status = GetCtrlVal (p1h, P1_TDIV, tmpstr); |
||
240 | sprintf(outbuf,"TDIV %s\n",tmpstr); |
||
241 | LECROY_TCP_write(sockfd, outbuf); |
||
242 | break; |
||
243 | case P1_PERS: |
||
244 | if (!connected) break; |
||
245 | status = GetCtrlVal (p1h, P1_PERS, tmpstr); |
||
246 | sprintf(outbuf,"PERS %s\n",tmpstr); |
||
247 | LECROY_TCP_write(sockfd, outbuf); |
||
248 | break; |
||
249 | // trigger |
||
250 | case P1_TLVL: |
||
251 | if (!connected) break; |
||
252 | status = GetCtrlVal (p1h, P1_TLVL, &tmpfloat); |
||
253 | sprintf(outbuf,"TLVL %f\n",tmpfloat); |
||
254 | LECROY_TCP_write(sockfd, outbuf); |
||
255 | break; |
||
256 | case P1_TRDL: |
||
257 | if (!connected) break; |
||
258 | status = GetCtrlVal (p1h, P1_TRDL, &tmpfloat); |
||
259 | sprintf(outbuf,"TRDL %.2E\n",tmpfloat); |
||
260 | LECROY_TCP_write(sockfd, outbuf); |
||
261 | break; |
||
262 | case P1_TSLP: |
||
263 | if (!connected) break; |
||
264 | status = GetCtrlVal (p1h, P1_TSLP, tmpstr); |
||
265 | sprintf(outbuf,"TSLP %s\n",tmpstr); |
||
266 | LECROY_TCP_write(sockfd, outbuf); |
||
267 | break; |
||
268 | case P1_TSRC: |
||
269 | if (!connected) break; |
||
270 | status = GetCtrlVal (p1h, P1_TSRC, tmpstr); |
||
271 | sprintf(outbuf,"TSRC %s\n",tmpstr); |
||
272 | LECROY_TCP_write(sockfd, outbuf); |
||
273 | break; |
||
274 | case P1_TRMD: |
||
275 | if (!connected) break; |
||
276 | status = GetCtrlVal (p1h, P1_TRMD, tmpstr); |
||
277 | sprintf(outbuf,"TRMD %s\n",tmpstr); |
||
278 | LECROY_TCP_write(sockfd, outbuf); |
||
279 | break; |
||
280 | case P1_TEST: |
||
281 | if (!connected) break; |
||
282 | strcpy(outbuf,"GTL\n"); |
||
283 | LECROY_TCP_write(sockfd, outbuf); |
||
284 | break; |
||
285 | //case P1_GET_SETUP: |
||
286 | // break; |
||
287 | default: |
||
288 | break; |
||
289 | } |
||
290 | |||
291 | } while ((rID != P1_EXIT)); |
||
292 | |||
293 | if (connected) { |
||
294 | strcpy(outbuf,"GTL\n"); |
||
295 | LECROY_TCP_write(sockfd, outbuf); |
||
296 | LECROY_TCP_disconnect(sockfd); |
||
297 | } |
||
298 | |||
299 | DiscardPanel (p1h); |
||
300 | CmtDiscardThreadPool (poolHandle); |
||
301 | |||
302 | return 0; |
||
303 | } |