#include <tcpsupp.h>
#include <formatio.h>
#include <userint.h>
#include <ansi_c.h>
#include <utility.h>
#include <gpib.h>
#include "toolbox.h"
#include "prologix.h"
#include "K6517.h"
K6517STATUS K6517Stat
;
static int K6517_Interface
,K6517_Port
,K6517_Gdev
;
static int iret
;
static int stop
;
static int debugCode
;
int _VI_FUNC K6517_status_print
(void);
void GpibError
(char *msg
) {
printf ("%s\n", msg
);
printf ("ibsta = &H%x <", ibsta
);
if (ibsta
& ERR
) printf (" ERR");
if (ibsta
& TIMO
) printf (" TIMO");
if (ibsta
& END
) printf (" END");
if (ibsta
& SRQI
) printf (" SRQI");
if (ibsta
& RQS
) printf (" RQS");
if (ibsta
& CMPL
) printf (" CMPL");
if (ibsta
& LOK
) printf (" LOK");
if (ibsta
& REM
) printf (" REM");
if (ibsta
& CIC
) printf (" CIC");
if (ibsta
& ATN
) printf (" ATN");
if (ibsta
& TACS
) printf (" TACS");
if (ibsta
& LACS
) printf (" LACS");
if (ibsta
& DTAS
) printf (" DTAS");
if (ibsta
& DCAS
) printf (" DCAS");
printf (" >\n");
printf ("iberr = %d", iberr
);
if (iberr
== EDVR
) printf (" EDVR <DOS Error>\n");
if (iberr
== ECIC
) printf (" ECIC <Not Controller-In-Charge>\n");
if (iberr
== ENOL
) printf (" ENOL <No Listener>\n");
if (iberr
== EADR
) printf (" EADR <Address error>\n");
if (iberr
== EARG
) printf (" EARG <Invalid argument>\n");
if (iberr
== ESAC
) printf (" ESAC <Not System Controller>\n");
if (iberr
== EABO
) printf (" EABO <Operation aborted>\n");
if (iberr
== ENEB
) printf (" ENEB <No GPIB board>\n");
if (iberr
== EOIP
) printf (" EOIP <Async I/O in progress>\n");
if (iberr
== ECAP
) printf (" ECAP <No capability>\n");
if (iberr
== EFSO
) printf (" EFSO <File system error>\n");
if (iberr
== EBUS
) printf (" EBUS <Command error>\n");
if (iberr
== ESTB
) printf (" ESTB <Status byte lost>\n");
if (iberr
== ESRQ
) printf (" ESRQ <SRQ stuck on>\n");
if (iberr
== ETAB
) printf (" ETAB <Table Overflow>\n");
printf ("ibcntl = %ld\n", ibcntl
);
printf ("\n");
/* Call ibonl to take the device and interface offline */
ibonl
(K6517_Gdev
,0);
exit(1);
}
void _VI_FUNC K6517_open
(int interface
, int port
, int primary_addr
,
int secondary_addr
, int timeout
)
{
char cmd
[100];
int status
;
/*
K6517_Gdev = OpenDev ("GPIB0", "");
if (ibsta & ERR) GpibError("OpenDev Error");
iret = ibpad (K6517_Gdev, 0);
if (ibsta & ERR) GpibError("OpenDev Error");
iret = ibsad (K6517_Gdev, NO_SAD);
iret = ibtmo (K6517_Gdev, T10s);
iret = ibeot (K6517_Gdev, 1);
iret = ibeos (K6517_Gdev, 0);
*/
K6517_Interface
=interface
;
K6517_Port
=port
;
switch (K6517_Interface
){
case 1:
status
= PROLOGIX_Open
(K6517_Port
);
printf("K6517_open %d\n", status
);
sprintf(cmd
,"++addr %0d",primary_addr
);
PROLOGIX_Send
(cmd
);
PROLOGIX_Send
("++auto 0");
PROLOGIX_Send
("++eoi 1");
PROLOGIX_Send
("++eot_enable 0");
PROLOGIX_Send
("++read_tmo_ms 1000");
break;
default:
K6517_Gdev
= ibdev
(K6517_Port
,primary_addr
,secondary_addr
,timeout
,1,0);
if (ibsta
& ERR
) GpibError
("OpenDev Error");
}
printf("Interface %d\n", interface
);
K6517_send
("*RST",0);
K6517_send
("*IDN?", 5);
char cres
[100];
cres
[0]= 0;
int len
= K6517_receive
(cres
, 50);
printf("[%d] %s\n", len
, cres
);
Delay
(GDELAY
);
return;
}
void _VI_FUNC K6517_clear
(void)
{
switch (K6517_Interface
){
case 1:
PROLOGIX_Send
("++clr");
break;
default:
iret
= ibclr
(K6517_Gdev
); // Clear the device
if (ibsta
& ERR
) GpibError
("OpenDev Error");
}
// Delay(GDELAY);
Delay
(2.
);
return;
}
void _VI_FUNC K6517_send
(char *cmd
, int len
)
{
if (debugCode
) printf("-> %s\n", cmd
);
if (!len
) len
= strlen(cmd
);
switch (K6517_Interface
){
case 1:
PROLOGIX_Send
(cmd
);
break;
default:
iret
= ibwrt
(K6517_Gdev
, cmd
, len
);
if (ibsta
& ERR
) GpibError
("OpenDev Error");
}
Delay
(GDELAY
);
return;
}
int _VI_FUNC K6517_receive
(char *response
, int maxbyt
)
{
int ierr
=0;
switch (K6517_Interface
){
case 1:
PROLOGIX_Send
("++read eoi");
ierr
= PROLOGIX_Receive
(response
,maxbyt
);
if (debugCode
) printf("<---Read [%d] %s\n", ierr
, response
);
return ierr
;
break;
default:
iret
= ibrd
(K6517_Gdev
, response
, maxbyt
);
if (ibsta
& ERR
) GpibError
("OpenDev Error");
response
[ibcntl
]=0;
return ibcntl
;
}
}
void _VI_FUNC K6517_status
(void)
{
int len
;
char cres
[100];
K6517_send
("U0X", 3);
len
= K6517_receive
(cres
, 90);
sscanf(cres
," 617%1d%2d%1d%1d%1d%1d%1d%1d%1d%1d%1d%2d%1d%2c",
&K6517Stat.
function,&K6517Stat.
range,&K6517Stat.
zero_check,
&K6517Stat.
zero_correct,&K6517Stat.
suppress,&K6517Stat.
trigger,
&K6517Stat.
vsource_operate,&K6517Stat.
read_mode,
&K6517Stat.
data_prefix,&K6517Stat.
display,
&K6517Stat.
data_store,&K6517Stat.
srq,&K6517Stat.
eoi,K6517Stat.
terminator);
printf("617FRRCZNTOBGDQMMKYY\n");
printf("%s\n",cres
);
// K6517_send ("X", 1);
return;
}
void _VI_FUNC K6517_data_format
(int mode
)
{
int len
;
char cmd
[100];
len
=sprintf(cmd
,"G%0dX",mode
);
K6517_send
(cmd
, len
);
return;
}
int _VI_FUNC K6517_EventStatus
()
{
K6517_send
(":STATUS:MEAS:EVEN?",0);
char cres
[100];
cres
[0]= 0;
int len
= K6517_receive
(cres
, 50);
if (len
>0)
return atoi(cres
);
else
return -1;
}
double _VI_FUNC K6517_get
(char *prefix
, int *loc
)
{
double value
;
char cres
[100];
K6517_send
("SENS:FUNC 'curr'",0);
// K6517_send (":SENS:CURR:RANG:UPP 21e-3",0);
// K6517_send ("SYST:ZCH 0",0);
K6517_send
("READ?",0);
cres
[0]= 0;
int len
= K6517_receive
(cres
, 50);
if (debugCode
) printf("current >%d, %s\n",len
,cres
);
return atof(cres
);
/*
K6517_send ("X", 1);
len = K6517_receive (cres, 50);
if (loc) {
*loc=-1;
sscanf(cres,"%4c%lg,%d",prefix, &value, loc);
prefix[4]=0;
} else {
sscanf(cres,"%*4c%lg,%*d",&value);
}
return value;
*/
}
void _VI_FUNC K6517_current_mode
(double range
)
{
int len
;
char cmd
[100];
if (range
> 0) {
len
=sprintf(cmd
,":SENS:CURR:RANG:AUTO OFF");
len
=sprintf(cmd
,":SENS:CURR:RANG:UPP %g",range
);
}
else len
=sprintf(cmd
,":SENS:CURR:RANG:AUTO ON");
// printf("%d, %s\n",len,cmd);
K6517_send
(cmd
, len
);
Delay
(1);
K6517_send
("SYST:ZCH 0",0);
return;
}
void _VI_FUNC K6517_zero_correct
(int zcorrect
)
{
K6517_send
("C0X", 3);
K6517_send
("Z0X", 3);
if (zcorrect
) {
K6517_send
("C1X", 3);
Delay
(2);
K6517_send
("Z1X", 3);
K6517_send
("C0X", 3);
}
return;
}
void _VI_FUNC K6517_trigger_mode
(int mode
)
{
int len
;
char cmd
[100];
//len=sprintf(cmd,"T%0dX",mode);
//K6517_send (cmd, len);
return;
}
void _VI_FUNC K6517_reading_mode
(int mode
)
{
int len
;
char cmd
[100];
//len=sprintf(cmd,"B%0dX",mode);
//K6517_send (cmd, len);
return;
}
void _VI_FUNC K6517_vsource_set
(float value
)
{
int len
;
char cmd
[100];
len
=sprintf(cmd
,":SOURCE:VOLTAGE %f",value
);
K6517_send
(cmd
, len
);
len
=sprintf(cmd
,":SOURCE:VOLTAGE:MCONNECT ON");
K6517_send
(cmd
, len
);
return;
}
double _VI_FUNC K6517_vsource_get
(void)
{
double value
;
/*
K6517_send ("SENS:FUNC 'volt'",0);
K6517_send ("SENS:VOLT:RANG:AUTO ON",0);
K6517_send ("SYST:ZCH 0",0);
K6517_send ("READ?",0);
*/
K6517_send
(":MEASure:VOLTage?",0);
char cres
[100];
cres
[0]= 0;
int len
= K6517_receive
(cres
, 50);
return atof(cres
);
}
void _VI_FUNC K6517_vsource_operate
(int operate
)
{
if (operate
){
// K6517_send (":SOURCE:VOLTAGE:MCONNECT 1", 0);
// K6517_send (":SENS:VOLTAGE:MANual:VSOurce:OPERate 1", 0);
//K6517_send (":MCONNECT 1", 0);
K6517_send
(":OUTP ON", 0);
// K6517_send (":SOURCE:VOLTAGE:RANGE 99", 0);
// K6517_send (":SOURCE:VOLTAGE 22", 0);
} else {
//K6517_send (":SOURCE:VOLTAGE 0", 0);
// K6517_send (":SENSe:VOLTAGE:MANual:VSOurce:OPERate 0", 0);
// K6517_send (":SOURCE:VOLTAGE:MCONNECT 0", 0);
K6517_send
(":OUTP OFF", 0);
//K6517_send (":MCONNECT 0", 0);
}
K6517_send
(":STATUS:OPER?",0);
char cres
[100];
cres
[0]= 0;
int len
= K6517_receive
(cres
, 50);
// return atoi(cres);
}
void _VI_FUNC K6517_close
(void)
{
// iret = CloseDev (K6517_Gdev);
switch (K6517_Interface
){
case 1:
PROLOGIX_Close
();
break;
default:
iret
= ibonl
(K6517_Gdev
, 0);// Take the device offline
if (ibsta
& ERR
) GpibError
("OpenDev Error");
}
return;
}
void _VI_FUNC K6517_test
(void)
{
int i
;
double value
;
char cres
[100];
K6517_clear
();
K6517_current_mode
(0);
K6517_zero_correct
(1);
K6517_data_format
(0);
K6517_trigger_mode
(1);
K6517_vsource_set
(-100.
);
K6517_vsource_operate
(1);
K6517_status
();
value
=K6517_get
(cres
,&i
);
printf("%s, %lg, %d\n",cres
,value
,i
);
value
=K6517_vsource_get
();
printf("%lg\n",value
);
value
=K6517_get
(cres
,&i
);
printf("%s, %lg, %d\n",cres
,value
,i
);
value
=K6517_get
(cres
,&i
);
printf("%s, %lg, %d\n",cres
,value
,i
);
value
=K6517_get
(cres
,&i
);
printf("%s, %lg, %d\n",cres
,value
,i
);
value
=K6517_get
(cres
,&i
);
printf("%s, %lg, %d\n",cres
,value
,i
);
Delay
(5);
K6517_vsource_operate
(0);
}
#ifdef K6517_MAIN
#include <cvirte.h>
#include <userint.h>
#include "K6517-ctrl.h"
int pnl
; int gLogToFile
;
int pn2
;
int gLogToFile
;
int vmon
[4]={P1_VMON_1
};
int imon
[4]={P1_IMON_1
};
int vset
[4]={P1_U_1
};
int iset
[4]={P1_I_1
};
int radiob
[4]={P1_BOX_1
};
int cvcc
[4]={P1_CVCC_1
};
const unsigned char Pre_1
= 1;
int gMask
=0xF;
int pnl
;
FILE
*gFp
;
static int tfID
;
static int rID
;
static CmtThreadPoolHandle poolHandle
= 0;
#define MAX_THREADS 10
/*---------------------------------------------------------------------------*/
/* Macros */
/*---------------------------------------------------------------------------*/
#define tcpChk(f) if ((g_TCPError=(f)) < 0) {ReportTCPError();}
/*---------------------------------------------------------------------------*/
/* Internal function prototypes */
/*---------------------------------------------------------------------------*/
int CVICALLBACK ClientTCPCB
(unsigned handle
, int event
, int error
,
void *callbackData
);
static void ReportTCPError
(void);
/*---------------------------------------------------------------------------*/
/* Module-globals */
/*---------------------------------------------------------------------------*/
static unsigned int g_hconversation
;
static int g_hmainPanel
;
static int g_connected
= 0;
static int g_TCPError
= 0;
/*---------------------------------------------------------------------------*/
/* Report TCP Errors if any */
/*---------------------------------------------------------------------------*/
static void ReportTCPError
(void)
{
if (g_TCPError
< 0)
{
char messageBuffer
[1024];
sprintf(messageBuffer
,
"TCP library error message: %s\nSystem error message: %s",
GetTCPErrorString
(g_TCPError
), GetTCPSystemErrorString
());
MessagePopup
("Error", messageBuffer
);
g_TCPError
= 0;
}
}
/*---------------------------------------------------------------------------*/
int CVICALLBACK DisconnectCB
(int panel
, int control
, int event
,
void *callbackData
, int eventData1
, int eventData2
) {
switch (event
) {
case EVENT_COMMIT
:
if (g_connected
)
DisconnectFromTCPServer
(g_hconversation
);
g_hconversation
= 0;
g_connected
= 0;
break;
}
return 0;
}
int CVICALLBACK ConnectCB
(int panel
, int control
, int event
, void *callbackData
, int eventData1
, int eventData2
) {
int portNum
=10000;
char tempBuf
[512];
sprintf(tempBuf
,"localhost");
switch (event
) {
case EVENT_COMMIT
:
if (g_connected
) return 0;
if (ConnectToTCPServer
(&g_hconversation
, portNum
, tempBuf
, ClientTCPCB
, NULL
, 5000) < 0)
MessagePopup
("TCP Client", "Connection to server failed !");
else
{
SetWaitCursor
(0);
g_connected
= 1;
/* We are successfully connected -- gather info */
if (GetTCPHostAddr
(tempBuf
, 256) >= 0) printf("%s\n" ,tempBuf
);
if (GetTCPHostName
(tempBuf
, 256) >= 0) printf("%s\n" ,tempBuf
);
tcpChk
(GetTCPPeerAddr
(g_hconversation
, tempBuf
, 256));
printf("%s\n" ,tempBuf
);
tcpChk
(GetTCPPeerName
(g_hconversation
, tempBuf
, 256));
printf("%s\n" ,tempBuf
);
}
break;
}
return 0;
}
int CVICALLBACK ClientTCPCB
(unsigned handle
, int event
, int error
, void *callbackData
)
{
char receiveBuf
[256] = {0};
ssize_t dataSize
= sizeof (receiveBuf
) - 1;
switch (event
)
{
case TCP_DATAREADY
:
if ((dataSize
= ClientTCPRead
(g_hconversation
, receiveBuf
,
dataSize
, 1000))
< 0)
{
printf( "Receive Error\n");
}
else
{
receiveBuf
[dataSize
] = '\0';
//printf("%s", receiveBuf);
float temp
= 0;
float humidity
= 0;
float dt
=0;
float tdiff
=0;
int t0
= 0;
sscanf(receiveBuf
, "%d%f%f%f%f", &t0
,&humidity
,&temp
, &tdiff
, &dt
);
SetCtrlVal
(pn2
,P2_TMON
, temp
);
SetCtrlVal
(pn2
,P2_HUMIDITY
, humidity
);
if (fabs(tdiff
)<0.2 && fabs(dt
) < 0.05) SetCtrlVal
(pn2
,P2_LED
, 1);
else SetCtrlVal
(pn2
,P2_LED
, 0);
}
break;
case TCP_DISCONNECT
:
MessagePopup
("TCP Client", "Server has closed connection!");
g_connected
= 0;
break;
}
return 0;
}
void SetDimming
(int state
) {
SetCtrlAttribute
(pnl
, P2_IVSCAN
, ATTR_DIMMED
, state
);
SetCtrlAttribute
(pnl
, P2_EXIT
, ATTR_DIMMED
, state
);
SetCtrlAttribute
(pnl
, P2_STOP
, ATTR_DIMMED
, !state
);
}
void CVICALLBACK EndOfThread
( CmtThreadPoolHandle poolhandle
,
CmtThreadFunctionID functionID
, unsigned int event
,
int value
, void *callbackData
) {
SetDimming
(0);
printf("End of Thread \n");
return ;
}
int ivscan
(void *arg
);
int TScan
() {
double temp
;
int n
=0;
int led
;
GetNumTableRows
(pn2
, P2_TABLE
, &n
);
stop
= 0;
for(int i
=0; i
<n
; i
++) {
if (stop
) break;
GetTableCellVal
(pn2
, P2_TABLE
, MakePoint
(1,i
+1), &temp
);
if (temp
>100) break;
char transmitBuf
[512]= {0};
sprintf(transmitBuf
, "1 %f\n", temp
);
if (g_connected
)
if ( ClientTCPWrite
(g_hconversation
, transmitBuf
, strlen (transmitBuf
), 1000) < 0) printf("Transmit Error\n"); else printf("%s\n", transmitBuf
);
SetCtrlVal
(pn2
,P2_LED
, 0);
do {
time_t t0
= time(NULL
);
SetCtrlVal
(pn2
,P2_TIME
, ctime(&t0
));
Delay
(1);
GetCtrlVal
(pn2
,P2_LED
, &led
);
if (led
) {
for (int k
=0;k
<10;k
++){
Delay
(1);
if (stop
) break;
}
GetCtrlVal
(pn2
,P2_LED
, &led
);
}
if (stop
) break;
} while (!led
);
if (stop
) break;
int itemp
= (int) temp
;
ivscan
(&itemp
);
printf("%d %f\n",i
,temp
);
}
return 0;
}
int CVICALLBACK TScanCB
(int panel
, int control
, int event
,
void *callbackData
, int eventData1
, int eventData2
) {
switch (event
) {
case EVENT_COMMIT
:{
ThreadFunctionPtr mythread
= TScan
;
CmtScheduleThreadPoolFunctionAdv
(poolHandle
, mythread
, &rID
,
DEFAULT_THREAD_PRIORITY
,
EndOfThread
,
EVENT_TP_THREAD_FUNCTION_END
,
NULL
, RUN_IN_SCHEDULED_THREAD
,
&tfID
);
}
break;
}
return 0;
}
int CVICALLBACK TimerOnOffCB
(int panel
, int control
, int event
,
void *callbackData
, int eventData1
, int eventData2
)
{
int state
;
switch (event
)
{
case EVENT_COMMIT
:
GetCtrlVal
(panel
, control
, &state
);
if (state
){
ResumeTimerCallbacks
();
} else {
SuspendTimerCallbacks
();
printf ("Disabling timer....\n");
}
break;
}
return 0;
}
// reads the run number from the first line in the file
int GetRunNumberFromFile
(char *fname
) {
char line
[MAX_PATHNAME_LEN
];
int ndim
= MAX_PATHNAME_LEN
;
int current_run
= -1;
FILE
*fp
= NULL
;
ssize_t size
;
if ( GetFileInfo
(fname
,&size
) ) fp
= fopen(fname
,"r");
if (fp
) {
if (fgets(line
,ndim
,fp
)!= NULL
) current_run
= atoi(line
);
fclose(fp
);
}
return current_run
;
}
int IncreaseRunNumberInFile
(char *fname
) {
int current_run
= GetRunNumberFromFile
(fname
);
FILE
*fp
= fopen(fname
,"w");
if (fp
) {
fprintf(fp
,"%d", current_run
+1 );
fclose(fp
);
}
return current_run
+1;
}
int __stdcall WinMain
(HINSTANCE hInstance
, HINSTANCE hPrevInstance
,
LPSTR lpszCmdLine
, int nCmdShow
)
{
char cres
[100];
double Voltage
, Current
, tinterval
;
if (InitCVIRTE
(hInstance
, 0, 0) == 0) return -1; /* out of memory */
SetStdioPort
(CVI_STDIO_WINDOW
);
SetSleepPolicy
(VAL_SLEEP_MORE
);
CmtNewThreadPool
(MAX_THREADS
, &poolHandle
);
if ((pnl
= LoadPanel
(0, "K6517-ctrl.uir", P1
)) < 0) return -1;
if ((pn2
= LoadPanel
(0, "K6517-ctrl.uir", P2
)) < 0) return -1;
DisableBreakOnLibraryErrors
();
K6517_open
(1,4,3,0,13);
SetCtrlVal
(pnl
, P1_ONOFF
, 0);
GetCtrlVal
(pnl
, P1_TINTERVAL
, &tinterval
);
SetCtrlAttribute
(pnl
, P1_TIMER
, ATTR_INTERVAL
, tinterval
);
TimerOnOffCB
(pnl
, P1_TIMERON
, EVENT_COMMIT
,NULL
,0,0);
for (unsigned char ch
=0;ch
<1;ch
++){
int ison
,i
;
Voltage
=K6517_vsource_get
();
Current
=K6517_get
(cres
,&i
);
SetCtrlVal
(pnl
, vset
[ch
], Voltage
);
SetCtrlVal
(pnl
, iset
[ch
], Current
);
GetCtrlVal
(pnl
, radiob
[ch
], &ison
);
gMask
= ison
;
}
DisplayPanel
(pnl
);
DisplayPanel
(pn2
);
RunUserInterface
();
CmtDiscardThreadPool
(poolHandle
);
if (g_connected
)
DisconnectFromTCPServer
(g_hconversation
);
DiscardPanel
(pn2
);
DiscardPanel
(pnl
);
K6517_close
();
if (gFp
) fclose(gFp
);
return 0;
}
int CVICALLBACK SwitchOnOffCB
(int panel
, int control
, int event
,
void *callbackData
, int eventData1
, int eventData2
)
{
unsigned char state
;
switch (event
)
{
case EVENT_COMMIT
:
GetCtrlVal
(panel
, control
, &state
);
K6517_vsource_operate
(state
);
break;
}
return 0;
}
int CVICALLBACK SetCB
(int panel
, int control
, int event
,
void *callbackData
, int eventData1
, int eventData2
)
{
char cres
[100];
int i
;
switch (event
)
{
case EVENT_COMMIT
:
for (unsigned char ch
= 0; ch
<1;ch
++){
//printf("ch %d %x\n", ch, gMask);
if (gMask
){
double Voltage
;
double Current
;
GetCtrlVal
(panel
, vset
[ch
], &Voltage
);
GetCtrlVal
(panel
, iset
[ch
], &Current
);
printf("->ch %d VSet %g Iset %g\n", ch
, Voltage
, Current
);
K6517_vsource_set
(Voltage
);
Voltage
=K6517_vsource_get
();
// TMI_Current(TMI_DeviceId, ch+1, Pre_1, Current);
Current
=K6517_get
(cres
,&i
);
printf("<-ch %d VSet %g Iset %g\n", ch
, Voltage
, Current
);
}
}
break;
}
return 0;
}
int CVICALLBACK ReadCB
(int panel
, int control
, int event
,
void *callbackData
, int eventData1
, int eventData2
)
{
int iRet
;
char ch
=0;
double Voltage
;
double Current
;
char cv_cc
;
char cres
[100];
switch (event
)
{
case EVENT_COMMIT
:
for (ch
= 0; ch
<1;ch
++){
if (gMask
){
int ison
,i
;
Voltage
=K6517_vsource_get
();
Current
=K6517_get
(cres
,&i
);
cv_cc
= 1;
printf("ch %d VSet %g Iset %g\n", ch
, Voltage
, Current
);
SetCtrlVal
(panel
, vmon
[ch
], Voltage
);
SetCtrlVal
(panel
, imon
[ch
], Current
);
SetCtrlVal
(panel
, cvcc
[ch
], cv_cc
);
}
}
break;
}
return 0;
}
int CVICALLBACK SetIntervalCB
(int panel
, int control
, int event
,
void *callbackData
, int eventData1
, int eventData2
) {
double tinterval
;
switch (event
) {
case EVENT_COMMIT
:
GetCtrlVal
(panel
, control
, &tinterval
);
SetCtrlAttribute
(panel
, P1_TIMER
, ATTR_INTERVAL
, tinterval
);
break;
}
return 0;
}
int CVICALLBACK ExitCB
(int panel
, int control
, int event
,
void *callbackData
, int eventData1
, int eventData2
)
{
switch (event
)
{
case EVENT_COMMIT
:
K6517_send
(":OUTP OFF", 0);
QuitUserInterface
(0);
break;
}
return 0;
}
int CVICALLBACK SetMaskCB
(int panel
, int control
, int event
,
void *callbackData
, int eventData1
, int eventData2
)
{ int ison
;
switch (event
)
{
case EVENT_COMMIT
:
GetCtrlVal
(panel
, control
, &ison
);
for (int i
=0;i
<1;i
++) if (control
== radiob
[i
]) break;
gMask
= ison
;
break;
}
return 0;
}
int CVICALLBACK LogToFileCB
(int panel
, int control
, int event
,
void *callbackData
, int eventData1
, int eventData2
)
{
switch (event
)
{
case EVENT_COMMIT
:
GetCtrlVal
(panel
, control
, &gLogToFile
);
break;
}
return 0;
}
int CVICALLBACK TimerCB
(int panel
, int control
, int event
,
void *callbackData
, int eventData1
, int eventData2
)
{
switch (event
)
{
unsigned char ch
;
double current
[4];
double voltage
;
char cv_cc
= 0;
char cres
[100];
int i
=0;
case EVENT_TIMER_TICK
:
for (ch
= 0; ch
<1;ch
++){
if (gMask
){
voltage
=K6517_vsource_get
();
current
[ch
] =K6517_get
(cres
,&i
);
if (gLogToFile
){
if (!gFp
) {
gFp
= fopen("K6517.log","w");
fprintf(gFp
,"#time\tch\tU\tI\tCV_CC\n");
}
fprintf(gFp
,"%ul\t%d\t%g\t%g\t%d\n", time(NULL
), ch
+1, voltage
, current
[ch
], cv_cc
);
}
}
}
PlotStripChart
(panel
, P1_GRAPH
, current
, 1, 0, 0, VAL_DOUBLE
);
PlotStripChart
(panel
, P1_GRAPH_1
, &voltage
, 1, 0, 0, VAL_DOUBLE
);
break;
}
return 0;
}
int CVICALLBACK TestCB
(int panel
, int control
, int event
,
void *callbackData
, int eventData1
, int eventData2
) {
switch (event
) {
case EVENT_COMMIT
:
K6517_test
();
break;
}
return 0;
}
int ivscan
(void *arg
) {
int *iarg
= (int *) arg
;
int nsteps
;
double umin
, umax
, u
;
double *xpoints
;
double *ypoints
;
static int plivhandle
=0;
double current
;
double voltage
;
char cv_cc
= 0;
char cres
[100];
char fileName
[100];
char fname
[100];
char path
[100];
double range
;
int k
=0;
int state
;
int overflow
;
char ovf
[10];
FILE
*fp
;
time_t mtime
;
ssize_t size
;
int repeatscan
= 1;
while (repeatscan
){
K6517_send
(":OUTP ON", 0);
SetCtrlVal
(pnl
, P1_TIMERON
, 0);
GetCtrlVal
(pn2
, P2_FILENAME
, fileName
);
GetCtrlVal
(pn2
, P2_PATH
, path
);
TimerOnOffCB
(pnl
, P1_TIMERON
, EVENT_COMMIT
, NULL
, 0, 0);
GetCtrlVal
(pn2
, P2_REPEAT
, &repeatscan
);
if (repeatscan
) {
int month
, day
, year
, hours
,minutes
,seconds
;
GetSystemDate
(&month
,&day
,&year
);
GetSystemTime
(&hours
, &minutes
, &seconds
);
sprintf(fname
,"%s/%s.%d_%d_%d_%d_%d.dat", path
, fileName
,year
,month
,day
,hours
,minutes
);
fp
= fopen(fname
,"w");
} else {
if (*iarg
>=100) sprintf(fname
, "%s/%s.dat", path
, fileName
);
else sprintf(fname
, "%s/%s_T%d.dat", path
, fileName
,*iarg
);
if ( !GetFileInfo
(fname
,&size
) || strstr("test",fileName
)!=NULL
)
fp
= fopen(fname
,"w");
else {
sprintf(cres
, "File %s exist\n Remove it first",fname
);
MessagePopup
("Info", cres
);
return 0;
}
}
time(&mtime
);
printf("#%s %s\n",DateStr
(), TimeStr
());
if (fp
) fprintf(fp
, "#%s %s\n",DateStr
(), TimeStr
());
// K6517_current_mode (range);
stop
= 0;
LogScaleCB
(pn2
, P2_LOGSCALE
, EVENT_COMMIT
,NULL
, 0, 0);
int voltageMax
[4]={P2_UMAX_1
,P2_UMAX_2
,P2_UMAX_3
,P2_UMAX_4
};
int voltageMin
[4]={P2_UMIN_1
,P2_UMIN_2
,P2_UMIN_3
,P2_UMIN_4
};
int numberOfSteps
[4]={P2_NSTEPS_1
,P2_NSTEPS_2
,P2_NSTEPS_3
,P2_NSTEPS_4
};
int selectRange
[4]={P2_RANGE_1
,P2_RANGE_2
,P2_RANGE_3
,P2_RANGE_4
};
int intervals
[4]={P2_SLCT_1
,P2_SLCT_2
,P2_SLCT_3
,P2_SLCT_4
};
int nrOfInt
;
int nrOfSteps
;
int N
=0;
int K
=0;
for (int j
=0;j
<4;j
++){
GetCtrlVal
(pn2
, intervals
[j
], &nrOfInt
);
N
= N
+ nrOfInt
;
GetCtrlVal
(pn2
, numberOfSteps
[j
], &nrOfSteps
);
K
= K
+ nrOfSteps
;
}
xpoints
= (double *) malloc ((K
-N
+2)*sizeof(double)*4);
ypoints
= (double *) malloc ((K
-N
+2)*sizeof(double)*4);
int tockaK
=0;
double t1
,t2
;
GetCtrlVal
(pn2
, P2_DELAY
, &t1
);
GetCtrlVal
(pn2
, P2_DELAY2
, &t2
);
for (int i
=0;i
<N
;i
++){
GetCtrlVal
(pn2
, voltageMin
[i
], &umin
);
GetCtrlVal
(pn2
, voltageMax
[i
], &umax
);
GetCtrlVal
(pn2
, numberOfSteps
[i
], &nsteps
);
GetCtrlVal
(pn2
, selectRange
[i
], &range
);
K6517_current_mode
(range
);
for (int n
=0;n
<nsteps
+1;n
++){
if (i
>0) {
if(n
==0) n
++;
};
u
= umin
+ n
*(umax
-umin
)/nsteps
;
if (n
==0) {
K6517_vsource_set
(u
);
Delay
(1);
K6517_get
(cres
,&k
);
K6517_vsource_set
(u
);
Delay
(1);
K6517_get
(cres
,&k
);
}
K6517_vsource_set
(u
);
current
=K6517_get
(cres
,&k
);
if (n
==0) Delay
(t1
);
Delay
(t2
);
// voltage =K6517_vsource_get();
current
=K6517_get
(cres
,&k
);
int status
= K6517_EventStatus
();
SetCtrlVal
(pn2
,P2_STATUS
, status
);
SetCtrlVal
(pn2
,P2_OVERFLOW
, status
& 0x1);
// if (current > range) printf("Overflow +\n");
// if (current < -range) printf("Overflow -\n");
if (status
& 0x1){
if (range
>0) range
*=10;
K6517_current_mode
(range
);
Delay
(1);
current
=K6517_get
(cres
,&k
);
status
= K6517_EventStatus
();
SetCtrlVal
(pn2
,P2_STATUS
, status
);
SetCtrlVal
(pn2
,P2_OVERFLOW
, status
& 0x1);
}
voltage
= u
;
xpoints
[tockaK
]= voltage
;
ypoints
[tockaK
]= current
;
SetCtrlVal
(pn2
, P2_VMON
, voltage
);
SetCtrlVal
(pn2
, P2_VCUR
, current
);
double temperature
=0;
double humidity
=0;
GetCtrlVal
(pn2
, P2_TMON
, &temperature
);
GetCtrlVal
(pn2
, P2_HUMIDITY
, &humidity
);
if (fp
) fprintf(fp
, "%d %g %g %g %g %g %d\n",tockaK
, u
, voltage
,current
, humidity
, temperature
, status
);
if (debugCode
) printf("n=%d u=%g voltage=%g current=%g\n",tockaK
, u
, voltage
,current
);
if (plivhandle
) DeleteGraphPlot
(pn2
, P2_GRAPHIV
, plivhandle
, VAL_IMMEDIATE_DRAW
);
plivhandle
= PlotXY
(pn2
, P2_GRAPHIV
, xpoints
, ypoints
, tockaK
+1, VAL_DOUBLE
, VAL_DOUBLE
, VAL_CONNECTED_POINTS
, VAL_BOLD_X
, VAL_SOLID
, 1, VAL_RED
);
ProcessSystemEvents
();
tockaK
++;
if (stop
) break;
}
if (stop
) break;
}
u
= 0;
K6517_vsource_set
(u
);
SetCtrlVal
(pn2
, P2_VMON
, u
);
SetCtrlVal
(pnl
, P1_ONOFF
, 0);
K6517_vsource_operate
(0);
fclose(fp
);
free(xpoints
);
free(ypoints
);
if (stop
) break;
if (repeatscan
){
double period
;
GetCtrlVal
(pn2
,P2_PERIOD
,&period
);
while (period
>0){
Delay
(1);
period
--;
SetCtrlVal
(pn2
,P2_NEXTSCAN
, period
);
if (stop
) {
SetCtrlVal
(pn2
,P2_NEXTSCAN
, 0);
return 0;
}
}
}
}
return 0;
}
int CVICALLBACK IVSCANCB
(int panel
, int control
, int event
,
void *callbackData
, int eventData1
, int eventData2
) {
rID
=100;
switch (event
) {
case EVENT_COMMIT
:{
ThreadFunctionPtr mythread
= ivscan
;
CmtScheduleThreadPoolFunctionAdv
(poolHandle
, mythread
, &rID
,
DEFAULT_THREAD_PRIORITY
,
EndOfThread
,
EVENT_TP_THREAD_FUNCTION_END
,
NULL
, RUN_IN_SCHEDULED_THREAD
,
&tfID
);
}
break;
}
return 0;
}
int CVICALLBACK SETVOLTCB
(int panel
, int control
, int event
,
void *callbackData
, int eventData1
, int eventData2
) {
double voltage
;
switch (event
) {
case EVENT_COMMIT
:
GetCtrlVal
(panel
, P2_VOLT
, &voltage
);
K6517_vsource_set
(voltage
);
SetCtrlVal
(panel
, P2_VMON
, voltage
);
break;
}
return 0;
}
int CVICALLBACK StopCB
(int panel
, int control
, int event
,
void *callbackData
, int eventData1
, int eventData2
) {
switch (event
) {
case EVENT_COMMIT
:
stop
= 1;
break;
}
return 0;
}
int CVICALLBACK LogScaleCB
(int panel
, int control
, int event
,
void *callbackData
, int eventData1
, int eventData2
) {
int selected
;
switch (event
) {
case EVENT_COMMIT
:
GetCtrlVal
(panel
, control
, &selected
);
if(selected
) SetCtrlAttribute
(panel
, P2_GRAPHIV
,ATTR_YMAP_MODE
, VAL_LOG
);
else SetCtrlAttribute
(panel
, P2_GRAPHIV
,ATTR_YMAP_MODE
, VAL_LINEAR
);
break;
}
return 0;
}
//****************************
int EscapeString
(const char *i
, char *o
) {
int iptr
=0, optr
= 0;
// walk though the input string
for (iptr
= 0; iptr
< strlen(i
); iptr
++, optr
++) {
// search for
if ( (i
[iptr
]=='\\') ) {
printf("Escape String %d %d \n",i
[iptr
] , i
[iptr
]==92 ) ;
//sa02Printf("Escape String %d %d \n",i[iptr] , ( (i[iptr] == 92) || (i[iptr] == 42) )) ;
o
[optr
] = i
[iptr
];
optr
++;
}
o
[optr
] = i
[iptr
];
}
o
[optr
] = '\0';
return 0;
}
int CVICALLBACK DrawCB
(int panel
, int control
, int event
,
void *callbackData
, int eventData1
, int eventData2
) {
switch (event
) {
case EVENT_COMMIT
:{
char fileName
[100];
char fname
[100];
char path
[100];
GetCtrlVal
(panel
, P2_FILENAME
, fileName
);
GetCtrlVal
(panel
, P2_PATH
, path
);
sprintf(fname
, "%s/%s.dat", path
, fileName
);
int status
;
char name
[MAX_PATHNAME_LEN
];
// char dfile[MAX_PATHNAME_LEN];
// char efile[MAX_PATHNAME_LEN];
// status = FileSelectPopup ("dat", "*.dat", ".dat",
// "Izberi datoteko s podatki",
// VAL_LOAD_BUTTON, 0, 0, 1, 0, efile);
// EscapeString(efile,dfile);
sprintf(name
,"C:/root/bin/root.exe IUdraw.cxx(\\\"%s\\\")", fname
);
// sprintf(name ,"C:/root/bin/root.exe IUdraw.cxx");
printf("%s\n",name
);
LaunchExecutable
(name
);
break;
}
}
return 0;
}
int CVICALLBACK Print
(int panel
, int control
, int event
,
void *callbackData
, int eventData1
, int eventData2
) {
char dfile
[MAX_PATHNAME_LEN
];
char name
[MAX_PATHNAME_LEN
];
int h2
=0;
switch (event
) {
case EVENT_COMMIT
: {
int hours
, minutes
, seconds
;
int month
, day
, year
;
int id
=-1;
//int bitmap = 0;
GetSystemDate
(&month
,&day
,&year
);
GetSystemTime
(&hours
, &minutes
, &seconds
);
sprintf(name
,"IV");
sprintf(dfile
,"%d_%d_%d_%d_%d_%s.bmp",year
,month
,day
,hours
,minutes
, name
);
SaveCtrlDisplayToFile
(panel
,P2_GRAPHIV
, 0, -1, -1, dfile
);
printf("bmp image saved as %s\n", dfile
);
//PrintCtrl (p1h, P1_GRAPH2D, "sa02_CVI", 1, 1);
}
break;
}
return 0;
}
int CVICALLBACK DebugCB
(int panel
, int control
, int event
,
void *callbackData
, int eventData1
, int eventData2
) {
int selected
;
switch (event
) {
case EVENT_COMMIT
:
GetCtrlVal
(panel
, control
, &selected
);
if(selected
) debugCode
= 1;
else debugCode
= 0;
break;
}
return 0;
}
int CVICALLBACK Exit2CB
(int panel
, int control
, int event
,
void *callbackData
, int eventData1
, int eventData2
) {
switch (event
) {
case EVENT_COMMIT
:
K6517_send
(":OUTP OFF", 0);
QuitUserInterface
(0);
break;
}
return 0;
}
#endif