#include <formatio.h>
#include <userint.h>
#include <ansi_c.h>
#include <utility.h>
#include <tcpsupp.h>
#include <cvirte.h>
#include "lecroy_tcp.h"
#ifndef _CVI_DEBUG_
# include "wavejet_uic.h"
#endif /* _CVI_DEBUG_ */
#include "wavejet_ui.h"
int ctrl_c=0;
//#define SCOPE_IP "192.168.1.11"
#define MAX_TCP_CONNECT 5 /* time in secs to get a connection */
#define MAX_TCP_READ 3 /* time in secs to wait for the DSO
to respond to a read request */
#define MAX_ST 512 /* maximum message string. no picked from thin air */
#define MAX_THREADS 10
static int sockfd;
static int p1h, pID, rID, tfID;
static int poolHandle = 0;
int CVICALLBACK cb_timer (int panel, int control, int event, void *callbackData,
int eventData1, int eventData2)
{
QueueUserEvent (9000, p1h, P1_TIMER);
return (0);
}
/*
int CVICALLBACK daq_run(void *functionData)
{
return (0);
}
*/
int CVICALLBACK acquire_waveforms(void *functionData) {
struct HDR {
int id;
int length;
int event;
};
struct HDR hdr;
int p1_tra[5]={P1_TRA_1,P1_TRA_2,P1_TRA_3,P1_TRA_4,0};
char outbuf[MAX_ST],inbuf[MAX_ST], tmpstr[100];
static char databuf[1000000];
int status, byte_count;
char filename[0xFF];
GetCtrlVal (p1h, P1_FNAME, filename);
ctrl_c=0;
LECROY_TCP_write(sockfd, "DTFORM BYTE\n");
LECROY_TCP_write(sockfd, "DTSTART 0\n");
LECROY_TCP_write(sockfd, "MLEN 1M\n");
LECROY_TCP_write(sockfd, "DTPOINTS 5000\n");
//LECROY_TCP_write(sockfd, "DTPOINTS 0\n");
FILE
*fp
= fopen (filename
, "wb");
int maxeve;
GetCtrlVal (p1h, P1_NEVE, &maxeve);
for (int neve=0;neve<maxeve;neve++){
SetCtrlVal (p1h, P1_CEVE, neve+1);
LECROY_TCP_write(sockfd, outbuf);
LECROY_TCP_read(sockfd, inbuf, sizeof(inbuf), MAX_TCP_READ);
hdr.event=neve;
for (int ch=0;ch<4;ch++){
int on;
GetCtrlVal (p1h, p1_tra[ch], &on);
if (on){
hdr.id=ch;
sprintf(outbuf
,"WAVESRC CH%d\n",ch
+1);
LECROY_TCP_write(sockfd, outbuf);
LECROY_TCP_write(sockfd, outbuf);
LECROY_TCP_read(sockfd, databuf, sizeof(databuf), MAX_TCP_READ);
sscanf(databuf
,"# %1d %8d",&status
,&byte_count
);
printf("[%d] wfmid=%d bytes=%d\n", neve
,status
, byte_count
);
if (byte_count<=0) continue;
hdr.length = byte_count+sizeof(hdr);
status
= fwrite (&hdr
, 1, sizeof(hdr
), fp
);
status
= fwrite (&databuf
[10], 1, byte_count
, fp
);
}
}
if (ctrl_c) break;
}
ctrl_c=0;
SetCtrlVal(p1h, P1_DTWFM, 0);
return 0;
}
int __stdcall WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpszCmdLine, int nCmdShow)
{
int i;
int status,connected,displen;
int refon;
int tmpindex,tmpint, dummy;
int p1_tra[5]={P1_TRA_1,P1_TRA_2,P1_TRA_3,P1_TRA_4,0};
int p1_vdiv[5]={P1_VDIV_1,P1_VDIV_2,P1_VDIV_3,P1_VDIV_4,0};
int p1_bwl[5]={P1_BWL_1,P1_BWL_2,P1_BWL_3,P1_BWL_4,0};
float tmpfloat;
char ip_address[50],tmpstr[100];
char outbuf[MAX_ST],inbuf[MAX_ST];
char display[500000],imgfn[300],ftyp[10];
int daqon=0;
FILE *imgfp;
connected=0;
if (InitCVIRTE (hInstance, 0, 0) == 0)
return -1; /* out of memory */
SetSleepPolicy(VAL_SLEEP_MORE);
CmtNewThreadPool (MAX_THREADS, &poolHandle);
SetStdioWindowOptions (1000, 0, 0);
SetStdioWindowSize (150, 600);
SetStdioWindowPosition (100, 20);
#ifdef _CVI_DEBUG_
if ((p1h = LoadPanel (0, "wavejet_ui.uir", P1)) < 0) return -1;
# else
p1h = BuildP1 (0);
#endif /* _CVI_DEBUG_ */
DisplayPanel (p1h);
// SetCtrlAttribute (p1h, P1_PLCH, ATTR_MAX_VALUE, NCH-1);
QueueUserEvent (1000, p1h, P1_IPF);
do {
GetUserEvent (1, &pID, &rID);
switch (rID) {
case P1_UPDATE:
if (!connected) break;
LECROY_TCP_write(sockfd, outbuf);
LECROY_TCP_read(sockfd, display, sizeof(display), MAX_TCP_READ);
sscanf(display
,"# %1d %8d",&status
,&displen
);
imgfp
= fopen ("tmp.bmp", "wb");
status
= fwrite (&display
[10], 1, displen
, imgfp
);
status = DisplayImageFile (p1h, P1_DISPLAY, "tmp.bmp");
break;
case P1_DSAV:
if (!connected) break;
status = GetCtrlVal (p1h, P1_DSAV, tmpstr);
StringLowerCase (ftyp);
status = FileSelectPopup ("", ftyp, ftyp,"Hard copy", VAL_SAVE_BUTTON,
0, 1, 1, 1, imgfn);
if (status<1) break;
sprintf(outbuf
,"TSCRN? %s\n",tmpstr
);
LECROY_TCP_write(sockfd, outbuf);
LECROY_TCP_read(sockfd, display, sizeof(display), MAX_TCP_READ);
sscanf(display
,"# %1d %8d",&status
,&displen
);
imgfp
= fopen (imgfn
, "wb");
status
= fwrite (&display
[10], 1, displen
, imgfp
);
break;
case P1_GET_SETUP:
if (!connected) break;
break;
case P1_TIMER:
//ntics+=1;
GetCtrlVal (p1h, P1_REFRESH, &refon);
if (refon) QueueUserEvent (9000, p1h, P1_UPDATE);
break;
case P1_REFRESH:
break;
/*
case P1_DAQ:
GetCtrlVal (p1h, P1_DAQ, &daq_on);
if (daq_on) {
CmtScheduleThreadPoolFunction (poolHandle, daq_run, (void *)&dummy, &tfID);
} else {
CmtWaitForThreadPoolFunctionCompletion (poolHandle, tfID,
OPT_TP_PROCESS_EVENTS_WHILE_WAITING);
CmtReleaseThreadPoolFunctionID (poolHandle, tfID);
}
break;
*/
case P1_IPF:
status = GetCtrlVal (p1h, P1_IPF, ip_address);
status = SetCtrlVal (p1h, P1_IP, ip_address);
break;
case P1_CONNECT:
if (connected) {
LECROY_TCP_write(sockfd, outbuf);
LECROY_TCP_disconnect(sockfd);
status = SetCtrlVal (p1h, P1_ID, "");
connected=0;
}else{
status = GetCtrlVal (p1h, P1_IP, ip_address);
sockfd=LECROY_TCP_connect(ip_address, MAX_TCP_CONNECT);
if (sockfd<0) {
//printf("\nCould not connect to the scope on IP: %s\n",ip_address);
status = SetCtrlVal (p1h, P1_ID, "Could not connect!");
status = SetCtrlVal (p1h, P1_CONNECT, 0);
break;
}else{
connected=1;
LECROY_TCP_write(sockfd, outbuf);
LECROY_TCP_read(sockfd, inbuf, sizeof(inbuf), MAX_TCP_READ);
status = SetCtrlVal (p1h, P1_ID, inbuf);
for (i=0;i<4;i++) {
LECROY_TCP_write(sockfd, outbuf);
LECROY_TCP_read(sockfd, inbuf, sizeof(inbuf), MAX_TCP_READ);
if (FindPattern (inbuf, 0, 2, "ON", 1, 0)) {
status = SetCtrlVal (p1h, p1_tra[i], 0);
}else{
status = SetCtrlVal (p1h, p1_tra[i], 1);
}
sprintf(outbuf
,"C%1d:VDIV?\n",i
+1);
LECROY_TCP_write(sockfd, outbuf);
LECROY_TCP_read(sockfd, inbuf, sizeof(inbuf), MAX_TCP_READ);
status = SetCtrlVal (p1h, p1_vdiv[i], tmpfloat);
LECROY_TCP_write(sockfd, outbuf);
LECROY_TCP_read(sockfd, inbuf, sizeof(inbuf), MAX_TCP_READ);
status = SetCtrlVal (p1h, p1_bwl[i], tmpstr);
}
LECROY_TCP_write(sockfd, outbuf);
LECROY_TCP_read(sockfd, inbuf, sizeof(inbuf), MAX_TCP_READ);
status = SetCtrlVal (p1h, P1_PERS, tmpstr);
LECROY_TCP_write(sockfd, outbuf);
LECROY_TCP_read(sockfd, inbuf, sizeof(inbuf), MAX_TCP_READ);
status = SetCtrlVal (p1h, P1_TSRC, tmpstr);
LECROY_TCP_write(sockfd, outbuf);
LECROY_TCP_read(sockfd, inbuf, sizeof(inbuf), MAX_TCP_READ);
status = SetCtrlVal (p1h, P1_TSLP, tmpstr);
}
}
break;
// vertical
case P1_TRA_1:
case P1_TRA_2:
case P1_TRA_3:
case P1_TRA_4:
for (i=0;i<4;i++) {
if (rID==p1_tra[i]) {
status = GetCtrlVal (p1h, p1_tra[i], &tmpint);
if (tmpint) {
sprintf(outbuf
,"C%1d:TRA ON\n",i
+1);
}else{
sprintf(outbuf
,"C%1d:TRA OFF\n",i
+1);
}
LECROY_TCP_write(sockfd, outbuf);
}
}
break;
case P1_VDIV_1:
case P1_VDIV_2:
case P1_VDIV_3:
case P1_VDIV_4:
for (i=0;i<4;i++) {
if (rID==p1_vdiv[i]) {
status = GetCtrlVal (p1h, p1_vdiv[i], &tmpfloat);
sprintf(outbuf
,"C%1d:VDIV %.3f\n",i
+1,tmpfloat
);
LECROY_TCP_write(sockfd, outbuf);
}
}
break;
case P1_BWL_1:
case P1_BWL_2:
case P1_BWL_3:
case P1_BWL_4:
for (i=0;i<4;i++) {
if (rID==p1_bwl[i]) {
status = GetCtrlVal (p1h, p1_bwl[i], tmpstr);
sprintf(outbuf
,"C%1d:BWL %s\n",i
+1,tmpstr
);
LECROY_TCP_write(sockfd, outbuf);
}
}
break;
// horizontal
case P1_TDIV:
if (!connected) break;
status = GetCtrlVal (p1h, P1_TDIV, tmpstr);
sprintf(outbuf
,"TDIV %s\n",tmpstr
);
LECROY_TCP_write(sockfd, outbuf);
break;
case P1_PERS:
if (!connected) break;
status = GetCtrlVal (p1h, P1_PERS, tmpstr);
sprintf(outbuf
,"PERS %s\n",tmpstr
);
LECROY_TCP_write(sockfd, outbuf);
break;
// trigger
case P1_TLVL:
if (!connected) break;
status = GetCtrlVal (p1h, P1_TLVL, &tmpfloat);
sprintf(outbuf
,"TLVL %f\n",tmpfloat
);
LECROY_TCP_write(sockfd, outbuf);
break;
case P1_TRDL:
if (!connected) break;
status = GetCtrlVal (p1h, P1_TRDL, &tmpfloat);
sprintf(outbuf
,"TRDL %.2E\n",tmpfloat
);
LECROY_TCP_write(sockfd, outbuf);
break;
case P1_TSLP:
if (!connected) break;
status = GetCtrlVal (p1h, P1_TSLP, tmpstr);
sprintf(outbuf
,"TSLP %s\n",tmpstr
);
LECROY_TCP_write(sockfd, outbuf);
break;
case P1_TSRC:
if (!connected) break;
status = GetCtrlVal (p1h, P1_TSRC, tmpstr);
sprintf(outbuf
,"TSRC %s\n",tmpstr
);
LECROY_TCP_write(sockfd, outbuf);
break;
case P1_TRMD:
if (!connected) break;
status = GetCtrlVal (p1h, P1_TRMD, tmpstr);
sprintf(outbuf
,"TRMD %s\n",tmpstr
);
LECROY_TCP_write(sockfd, outbuf);
break;
case P1_TEST:
if (!connected) break;
LECROY_TCP_write(sockfd, outbuf);
break;
case P1_DTWFM:
if (!connected) break;
GetCtrlVal(p1h, P1_DTWFM, &daqon);
if (daqon) {
CmtScheduleThreadPoolFunction (poolHandle, acquire_waveforms, (void *)&dummy, &tfID);
} else {
ctrl_c=1;
//CmtWaitForThreadPoolFunctionCompletion (poolHandle, tfID,
// OPT_TP_PROCESS_EVENTS_WHILE_WAITING);
//CmtReleaseThreadPoolFunctionID (poolHandle, tfID)
}
break;
default:
break;
}
} while ((rID != P1_EXIT));
if (connected) {
LECROY_TCP_write(sockfd, outbuf);
LECROY_TCP_disconnect(sockfd);
}
DiscardPanel (p1h);
CmtDiscardThreadPool (poolHandle);
return 0;
}