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