Rev 137 | Details | Compare with Previous | 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" |
||
137 | f9daq | 9 | #ifndef _CVI_DEBUG_ |
10 | # include "wavejet_uic.h" |
||
11 | #endif /* _CVI_DEBUG_ */ |
||
65 | f9daq | 12 | #include "wavejet_ui.h" |
13 | |||
323 | f9daq | 14 | int ctrl_c=0; |
65 | f9daq | 15 | //#define SCOPE_IP "192.168.1.11" |
16 | #define MAX_TCP_CONNECT 5 /* time in secs to get a connection */ |
||
17 | #define MAX_TCP_READ 3 /* time in secs to wait for the DSO |
||
18 | to respond to a read request */ |
||
19 | #define MAX_ST 512 /* maximum message string. no picked from thin air */ |
||
20 | |||
21 | #define MAX_THREADS 10 |
||
22 | |||
23 | static int sockfd; |
||
24 | static int p1h, pID, rID, tfID; |
||
25 | static int poolHandle = 0; |
||
26 | |||
27 | |||
28 | |||
29 | int CVICALLBACK cb_timer (int panel, int control, int event, void *callbackData, |
||
30 | int eventData1, int eventData2) |
||
31 | { |
||
32 | QueueUserEvent (9000, p1h, P1_TIMER); |
||
33 | return (0); |
||
34 | } |
||
35 | /* |
||
36 | int CVICALLBACK daq_run(void *functionData) |
||
37 | { |
||
38 | return (0); |
||
39 | } |
||
40 | */ |
||
41 | |||
323 | f9daq | 42 | int CVICALLBACK acquire_waveforms(void *functionData) { |
43 | struct HDR { |
||
44 | int id; |
||
45 | int length; |
||
46 | int event; |
||
47 | }; |
||
48 | |||
49 | struct HDR hdr; |
||
50 | int p1_tra[5]={P1_TRA_1,P1_TRA_2,P1_TRA_3,P1_TRA_4,0}; |
||
51 | char outbuf[MAX_ST],inbuf[MAX_ST], tmpstr[100]; |
||
52 | static char databuf[1000000]; |
||
53 | int status, byte_count; |
||
54 | char filename[0xFF]; |
||
55 | |||
56 | GetCtrlVal (p1h, P1_FNAME, filename); |
||
57 | ctrl_c=0; |
||
58 | |||
59 | LECROY_TCP_write(sockfd, "DTFORM BYTE\n"); |
||
60 | LECROY_TCP_write(sockfd, "DTSTART 0\n"); |
||
61 | LECROY_TCP_write(sockfd, "MLEN 1M\n"); |
||
62 | LECROY_TCP_write(sockfd, "DTPOINTS 5000\n"); |
||
63 | //LECROY_TCP_write(sockfd, "DTPOINTS 0\n"); |
||
64 | FILE *fp = fopen (filename, "wb"); |
||
65 | |||
66 | int maxeve; |
||
67 | GetCtrlVal (p1h, P1_NEVE, &maxeve); |
||
68 | for (int neve=0;neve<maxeve;neve++){ |
||
69 | SetCtrlVal (p1h, P1_CEVE, neve+1); |
||
70 | sprintf(outbuf,"WSGL?\n"); |
||
71 | LECROY_TCP_write(sockfd, outbuf); |
||
72 | LECROY_TCP_read(sockfd, inbuf, sizeof(inbuf), MAX_TCP_READ); |
||
73 | sscanf(inbuf,"%s",tmpstr); |
||
74 | |||
75 | hdr.event=neve; |
||
76 | for (int ch=0;ch<4;ch++){ |
||
77 | int on; |
||
78 | GetCtrlVal (p1h, p1_tra[ch], &on); |
||
79 | |||
80 | if (on){ |
||
81 | hdr.id=ch; |
||
82 | sprintf(outbuf,"WAVESRC CH%d\n",ch+1); |
||
83 | LECROY_TCP_write(sockfd, outbuf); |
||
84 | |||
85 | sprintf(outbuf,"DTWAVE?\n"); |
||
86 | LECROY_TCP_write(sockfd, outbuf); |
||
87 | LECROY_TCP_read(sockfd, databuf, sizeof(databuf), MAX_TCP_READ); |
||
88 | sscanf(databuf,"# %1d %8d",&status,&byte_count); |
||
89 | printf("[%d] wfmid=%d bytes=%d\n", neve,status, byte_count); |
||
90 | if (byte_count<=0) continue; |
||
91 | hdr.length = byte_count+sizeof(hdr); |
||
92 | status = fwrite (&hdr, 1, sizeof(hdr), fp); |
||
93 | status = fwrite (&databuf[10], 1, byte_count, fp); |
||
94 | |||
95 | } |
||
96 | } |
||
97 | if (ctrl_c) break; |
||
98 | } |
||
99 | ctrl_c=0; |
||
100 | status = fclose (fp); |
||
101 | SetCtrlVal(p1h, P1_DTWFM, 0); |
||
102 | return 0; |
||
103 | } |
||
104 | |||
65 | f9daq | 105 | int __stdcall WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, |
106 | LPSTR lpszCmdLine, int nCmdShow) |
||
107 | { |
||
108 | int i; |
||
109 | int status,connected,displen; |
||
110 | int refon; |
||
323 | f9daq | 111 | int tmpindex,tmpint, dummy; |
65 | f9daq | 112 | int p1_tra[5]={P1_TRA_1,P1_TRA_2,P1_TRA_3,P1_TRA_4,0}; |
113 | int p1_vdiv[5]={P1_VDIV_1,P1_VDIV_2,P1_VDIV_3,P1_VDIV_4,0}; |
||
114 | int p1_bwl[5]={P1_BWL_1,P1_BWL_2,P1_BWL_3,P1_BWL_4,0}; |
||
115 | float tmpfloat; |
||
116 | char ip_address[50],tmpstr[100]; |
||
117 | char outbuf[MAX_ST],inbuf[MAX_ST]; |
||
118 | char display[500000],imgfn[300],ftyp[10]; |
||
323 | f9daq | 119 | int daqon=0; |
65 | f9daq | 120 | |
121 | FILE *imgfp; |
||
122 | |||
123 | connected=0; |
||
124 | |||
125 | if (InitCVIRTE (hInstance, 0, 0) == 0) |
||
126 | return -1; /* out of memory */ |
||
127 | |||
128 | SetSleepPolicy(VAL_SLEEP_MORE); |
||
129 | CmtNewThreadPool (MAX_THREADS, &poolHandle); |
||
130 | |||
131 | SetStdioWindowOptions (1000, 0, 0); |
||
132 | SetStdioWindowSize (150, 600); |
||
133 | SetStdioWindowPosition (100, 20); |
||
134 | |||
137 | f9daq | 135 | #ifdef _CVI_DEBUG_ |
65 | f9daq | 136 | if ((p1h = LoadPanel (0, "wavejet_ui.uir", P1)) < 0) return -1; |
137 | f9daq | 137 | # else |
138 | p1h = BuildP1 (0); |
||
139 | #endif /* _CVI_DEBUG_ */ |
||
65 | f9daq | 140 | DisplayPanel (p1h); |
141 | |||
142 | |||
143 | // SetCtrlAttribute (p1h, P1_PLCH, ATTR_MAX_VALUE, NCH-1); |
||
144 | QueueUserEvent (1000, p1h, P1_IPF); |
||
145 | |||
146 | do { |
||
147 | GetUserEvent (1, &pID, &rID); |
||
148 | switch (rID) { |
||
149 | case P1_UPDATE: |
||
150 | if (!connected) break; |
||
151 | sprintf(outbuf,"TSCRN? BMP\n"); |
||
152 | LECROY_TCP_write(sockfd, outbuf); |
||
153 | LECROY_TCP_read(sockfd, display, sizeof(display), MAX_TCP_READ); |
||
154 | sscanf(display,"# %1d %8d",&status,&displen); |
||
155 | imgfp = fopen ("tmp.bmp", "wb"); |
||
156 | status = fwrite (&display[10], 1, displen, imgfp); |
||
157 | status = fclose (imgfp); |
||
158 | status = DisplayImageFile (p1h, P1_DISPLAY, "tmp.bmp"); |
||
159 | break; |
||
160 | case P1_DSAV: |
||
161 | if (!connected) break; |
||
162 | status = GetCtrlVal (p1h, P1_DSAV, tmpstr); |
||
163 | sprintf(ftyp,"*.%s",tmpstr); |
||
164 | StringLowerCase (ftyp); |
||
165 | status = FileSelectPopup ("", ftyp, ftyp,"Hard copy", VAL_SAVE_BUTTON, |
||
166 | 0, 1, 1, 1, imgfn); |
||
167 | if (status<1) break; |
||
168 | sprintf(outbuf,"TSCRN? %s\n",tmpstr); |
||
169 | LECROY_TCP_write(sockfd, outbuf); |
||
170 | LECROY_TCP_read(sockfd, display, sizeof(display), MAX_TCP_READ); |
||
171 | sscanf(display,"# %1d %8d",&status,&displen); |
||
172 | imgfp = fopen (imgfn, "wb"); |
||
173 | status = fwrite (&display[10], 1, displen, imgfp); |
||
174 | status = fclose (imgfp); |
||
175 | break; |
||
176 | case P1_GET_SETUP: |
||
177 | if (!connected) break; |
||
178 | break; |
||
179 | case P1_TIMER: |
||
180 | //ntics+=1; |
||
181 | GetCtrlVal (p1h, P1_REFRESH, &refon); |
||
182 | if (refon) QueueUserEvent (9000, p1h, P1_UPDATE); |
||
183 | break; |
||
184 | case P1_REFRESH: |
||
185 | break; |
||
186 | /* |
||
187 | case P1_DAQ: |
||
188 | GetCtrlVal (p1h, P1_DAQ, &daq_on); |
||
189 | if (daq_on) { |
||
190 | CmtScheduleThreadPoolFunction (poolHandle, daq_run, (void *)&dummy, &tfID); |
||
191 | } else { |
||
192 | CmtWaitForThreadPoolFunctionCompletion (poolHandle, tfID, |
||
193 | OPT_TP_PROCESS_EVENTS_WHILE_WAITING); |
||
194 | CmtReleaseThreadPoolFunctionID (poolHandle, tfID); |
||
195 | } |
||
196 | break; |
||
197 | */ |
||
198 | case P1_IPF: |
||
199 | status = GetCtrlVal (p1h, P1_IPF, ip_address); |
||
200 | status = SetCtrlVal (p1h, P1_IP, ip_address); |
||
201 | break; |
||
202 | case P1_CONNECT: |
||
203 | if (connected) { |
||
204 | strcpy(outbuf,"GTL\n"); |
||
205 | LECROY_TCP_write(sockfd, outbuf); |
||
206 | LECROY_TCP_disconnect(sockfd); |
||
207 | status = SetCtrlVal (p1h, P1_ID, ""); |
||
208 | connected=0; |
||
209 | }else{ |
||
210 | status = GetCtrlVal (p1h, P1_IP, ip_address); |
||
211 | sockfd=LECROY_TCP_connect(ip_address, MAX_TCP_CONNECT); |
||
212 | if (sockfd<0) { |
||
213 | //printf("\nCould not connect to the scope on IP: %s\n",ip_address); |
||
214 | status = SetCtrlVal (p1h, P1_ID, "Could not connect!"); |
||
215 | status = SetCtrlVal (p1h, P1_CONNECT, 0); |
||
216 | break; |
||
217 | }else{ |
||
218 | connected=1; |
||
219 | strcpy(outbuf,"*idn?\n"); |
||
220 | LECROY_TCP_write(sockfd, outbuf); |
||
221 | LECROY_TCP_read(sockfd, inbuf, sizeof(inbuf), MAX_TCP_READ); |
||
222 | status = SetCtrlVal (p1h, P1_ID, inbuf); |
||
223 | |||
224 | for (i=0;i<4;i++) { |
||
225 | sprintf(outbuf,"C%1d:TRA?\n",i+1); |
||
226 | LECROY_TCP_write(sockfd, outbuf); |
||
227 | LECROY_TCP_read(sockfd, inbuf, sizeof(inbuf), MAX_TCP_READ); |
||
228 | if (FindPattern (inbuf, 0, 2, "ON", 1, 0)) { |
||
229 | status = SetCtrlVal (p1h, p1_tra[i], 0); |
||
230 | }else{ |
||
231 | status = SetCtrlVal (p1h, p1_tra[i], 1); |
||
232 | } |
||
233 | |||
234 | sprintf(outbuf,"C%1d:VDIV?\n",i+1); |
||
235 | LECROY_TCP_write(sockfd, outbuf); |
||
236 | LECROY_TCP_read(sockfd, inbuf, sizeof(inbuf), MAX_TCP_READ); |
||
237 | sscanf(inbuf,"%g",&tmpfloat); |
||
238 | status = SetCtrlVal (p1h, p1_vdiv[i], tmpfloat); |
||
239 | |||
240 | sprintf(outbuf,"C%1d:BWL?\n",i+1); |
||
241 | LECROY_TCP_write(sockfd, outbuf); |
||
242 | LECROY_TCP_read(sockfd, inbuf, sizeof(inbuf), MAX_TCP_READ); |
||
243 | sscanf(inbuf,"%s",tmpstr); |
||
244 | status = SetCtrlVal (p1h, p1_bwl[i], tmpstr); |
||
245 | } |
||
246 | |||
247 | sprintf(outbuf,"PERS?\n"); |
||
248 | LECROY_TCP_write(sockfd, outbuf); |
||
249 | LECROY_TCP_read(sockfd, inbuf, sizeof(inbuf), MAX_TCP_READ); |
||
250 | sscanf(inbuf,"%s",tmpstr); |
||
251 | status = SetCtrlVal (p1h, P1_PERS, tmpstr); |
||
252 | |||
253 | sprintf(outbuf,"TSRC?\n"); |
||
254 | LECROY_TCP_write(sockfd, outbuf); |
||
255 | LECROY_TCP_read(sockfd, inbuf, sizeof(inbuf), MAX_TCP_READ); |
||
256 | sscanf(inbuf,"%s",tmpstr); |
||
257 | status = SetCtrlVal (p1h, P1_TSRC, tmpstr); |
||
258 | |||
259 | sprintf(outbuf,"TSLP?\n"); |
||
260 | LECROY_TCP_write(sockfd, outbuf); |
||
261 | LECROY_TCP_read(sockfd, inbuf, sizeof(inbuf), MAX_TCP_READ); |
||
262 | sscanf(inbuf,"%s",tmpstr); |
||
263 | status = SetCtrlVal (p1h, P1_TSLP, tmpstr); |
||
264 | } |
||
265 | } |
||
266 | break; |
||
267 | // vertical |
||
268 | case P1_TRA_1: |
||
269 | case P1_TRA_2: |
||
270 | case P1_TRA_3: |
||
271 | case P1_TRA_4: |
||
272 | for (i=0;i<4;i++) { |
||
273 | if (rID==p1_tra[i]) { |
||
274 | status = GetCtrlVal (p1h, p1_tra[i], &tmpint); |
||
275 | if (tmpint) { |
||
276 | sprintf(outbuf,"C%1d:TRA ON\n",i+1); |
||
277 | }else{ |
||
278 | sprintf(outbuf,"C%1d:TRA OFF\n",i+1); |
||
279 | } |
||
280 | LECROY_TCP_write(sockfd, outbuf); |
||
281 | } |
||
282 | } |
||
283 | break; |
||
284 | case P1_VDIV_1: |
||
285 | case P1_VDIV_2: |
||
286 | case P1_VDIV_3: |
||
287 | case P1_VDIV_4: |
||
288 | for (i=0;i<4;i++) { |
||
289 | if (rID==p1_vdiv[i]) { |
||
290 | status = GetCtrlVal (p1h, p1_vdiv[i], &tmpfloat); |
||
291 | sprintf(outbuf,"C%1d:VDIV %.3f\n",i+1,tmpfloat); |
||
292 | LECROY_TCP_write(sockfd, outbuf); |
||
293 | } |
||
294 | } |
||
295 | break; |
||
296 | case P1_BWL_1: |
||
297 | case P1_BWL_2: |
||
298 | case P1_BWL_3: |
||
299 | case P1_BWL_4: |
||
300 | for (i=0;i<4;i++) { |
||
301 | if (rID==p1_bwl[i]) { |
||
302 | status = GetCtrlVal (p1h, p1_bwl[i], tmpstr); |
||
303 | sprintf(outbuf,"C%1d:BWL %s\n",i+1,tmpstr); |
||
304 | LECROY_TCP_write(sockfd, outbuf); |
||
305 | } |
||
306 | } |
||
307 | break; |
||
308 | // horizontal |
||
309 | case P1_TDIV: |
||
310 | if (!connected) break; |
||
311 | status = GetCtrlVal (p1h, P1_TDIV, tmpstr); |
||
312 | sprintf(outbuf,"TDIV %s\n",tmpstr); |
||
313 | LECROY_TCP_write(sockfd, outbuf); |
||
314 | break; |
||
315 | case P1_PERS: |
||
316 | if (!connected) break; |
||
317 | status = GetCtrlVal (p1h, P1_PERS, tmpstr); |
||
318 | sprintf(outbuf,"PERS %s\n",tmpstr); |
||
319 | LECROY_TCP_write(sockfd, outbuf); |
||
320 | break; |
||
321 | // trigger |
||
322 | case P1_TLVL: |
||
323 | if (!connected) break; |
||
324 | status = GetCtrlVal (p1h, P1_TLVL, &tmpfloat); |
||
325 | sprintf(outbuf,"TLVL %f\n",tmpfloat); |
||
326 | LECROY_TCP_write(sockfd, outbuf); |
||
327 | break; |
||
328 | case P1_TRDL: |
||
329 | if (!connected) break; |
||
330 | status = GetCtrlVal (p1h, P1_TRDL, &tmpfloat); |
||
331 | sprintf(outbuf,"TRDL %.2E\n",tmpfloat); |
||
332 | LECROY_TCP_write(sockfd, outbuf); |
||
333 | break; |
||
334 | case P1_TSLP: |
||
335 | if (!connected) break; |
||
336 | status = GetCtrlVal (p1h, P1_TSLP, tmpstr); |
||
337 | sprintf(outbuf,"TSLP %s\n",tmpstr); |
||
338 | LECROY_TCP_write(sockfd, outbuf); |
||
339 | break; |
||
340 | case P1_TSRC: |
||
341 | if (!connected) break; |
||
342 | status = GetCtrlVal (p1h, P1_TSRC, tmpstr); |
||
343 | sprintf(outbuf,"TSRC %s\n",tmpstr); |
||
344 | LECROY_TCP_write(sockfd, outbuf); |
||
345 | break; |
||
346 | case P1_TRMD: |
||
347 | if (!connected) break; |
||
348 | status = GetCtrlVal (p1h, P1_TRMD, tmpstr); |
||
349 | sprintf(outbuf,"TRMD %s\n",tmpstr); |
||
350 | LECROY_TCP_write(sockfd, outbuf); |
||
351 | break; |
||
352 | case P1_TEST: |
||
353 | if (!connected) break; |
||
354 | strcpy(outbuf,"GTL\n"); |
||
355 | LECROY_TCP_write(sockfd, outbuf); |
||
356 | break; |
||
323 | f9daq | 357 | case P1_DTWFM: |
358 | if (!connected) break; |
||
359 | GetCtrlVal(p1h, P1_DTWFM, &daqon); |
||
360 | if (daqon) { |
||
361 | CmtScheduleThreadPoolFunction (poolHandle, acquire_waveforms, (void *)&dummy, &tfID); |
||
362 | } else { |
||
363 | ctrl_c=1; |
||
364 | |||
365 | //CmtWaitForThreadPoolFunctionCompletion (poolHandle, tfID, |
||
366 | // OPT_TP_PROCESS_EVENTS_WHILE_WAITING); |
||
367 | //CmtReleaseThreadPoolFunctionID (poolHandle, tfID) |
||
368 | |||
369 | } |
||
370 | |||
371 | |||
372 | |||
373 | break; |
||
65 | f9daq | 374 | default: |
375 | break; |
||
376 | } |
||
377 | |||
378 | } while ((rID != P1_EXIT)); |
||
379 | |||
380 | if (connected) { |
||
381 | strcpy(outbuf,"GTL\n"); |
||
382 | LECROY_TCP_write(sockfd, outbuf); |
||
383 | LECROY_TCP_disconnect(sockfd); |
||
384 | } |
||
385 | |||
386 | DiscardPanel (p1h); |
||
387 | CmtDiscardThreadPool (poolHandle); |
||
388 | |||
389 | return 0; |
||
390 | } |