Rev 312 |
Blame |
Compare with Previous |
Last modification |
View Log
| RSS feed
#include <userint.h>
#include "K2231A-ctrl.h"
#include <stdlib.h>
#include <stdio.h>
#include <windows.h>
#include <visa.h>
static int debug
= 0;
static ViStatus istat
;
static ViSession DeviceHandle
,RMHandle
;
char response
[0xFF]= {0};
int K2231A_SetDebug
(int x
){ debug
= x
;return 0;};
int K2231A_Write
(const char *format
, ...
)
{
char cmd
[0xFF];
va_list aptr
;
va_start(aptr
, format
);
vsprintf(cmd
, format
, aptr
);
va_end(aptr
);
char str
[0xFF];
sprintf(str
, "%s\r\n",cmd
);
int istat
= viPrintf
(DeviceHandle
, str
);
if (debug
) printf("K2231A_Write %s\n", cmd
);
return istat
;
}
const char * K2231A_Read
(const char *format
, ...
)
{
char cmd
[0xFF];
va_list aptr
;
va_start(aptr
, format
);
vsprintf(cmd
, format
, aptr
);
va_end(aptr
);
char str
[0xFF];
int istat
=0;
sprintf(str
, "%s\n",cmd
);
istat
= viPrintf
(DeviceHandle
, str
);
int nb
=0;
istat
= viRead
(DeviceHandle
, response
, 0xFF, &nb
);
if (nb
>0) response
[nb
-1]=0;
if (debug
) printf("K2231A_Read %s : %s\n", cmd
, response
);
if (istat
) return NULL
;
return response
;
}
int K2231A_QueryInt
(const char *format
, ...
)
{
char cmd
[0xFF];
va_list aptr
;
va_start(aptr
, format
);
vsprintf(cmd
, format
, aptr
);
va_end(aptr
);
char *p
= K2231A_Read
(cmd
);
if (p
!= NULL
) return atoi(p
);
return 0;
}
double K2231A_QueryDouble
(const char *format
, ...
) {
char cmd
[0xFF];
va_list aptr
;
va_start(aptr
, format
);
vsprintf(cmd
, format
, aptr
);
va_end(aptr
);
char *p
= K2231A_Read
(cmd
);
if (p
!= NULL
) return atof(p
);
return 0;
}
static int K2231A_Port
=5;
int K2231A_SetPort
(int x
){K2231A_Port
=x
; return x
;};
int K2231A_Initialize
()
{
istat
= viOpenDefaultRM
(&RMHandle
);
char cport
[0xFF];
sprintf(cport
,"ASRL%d::INSTR", K2231A_Port
);
if (RMHandle
) istat
= viOpen
(RMHandle
, cport
, VI_NULL
, VI_NULL
, &DeviceHandle
);
if (DeviceHandle
)
{
istat
= viSetAttribute
(DeviceHandle
, VI_ATTR_TERMCHAR
, '\n');
istat
= viSetAttribute
(DeviceHandle
, VI_ATTR_TERMCHAR_EN
, VI_TRUE
);
int baud
;
istat
= viGetAttribute
(DeviceHandle
, VI_ATTR_ASRL_BAUD
, &baud
);
printf("baudrate1 = %d\n", baud
);
istat
= viSetAttribute
(DeviceHandle
, VI_ATTR_TMO_VALUE
, 1000);
printf("\n");
}
else
{
MessagePopup
("Error","Cannot open handle");
}
return 0;
}
int K2231A_Test
()
{
char response
[0xFF]= {0};
int i
;
for ( i
=0; i
<255; i
++) response
[i
]=0;
K2231A_Read
("*IDN?");
K2231A_Read
("SYSTem:VERSion?");
K2231A_Read
("VOLT?");
K2231A_Read
("CURR?");
K2231A_Read
("MEAS:VOLT?");
K2231A_Read
("MEAS:CURR?");
K2231A_Read
("FETC:VOLT?");
K2231A_Read
("FETC:CURR?");
K2231A_Write
("SYSTem:BEEPer");
return TRUE
;
}
int K2231A_Close
()
{
K2231A_Write
("syst:loc");
return 0;
}
int K2231A_Open
()
{
K2231A_Initialize
();
K2231A_Write
("syst:rem");
return TRUE
;
}
int K2231A_RecallFromMemory
(int preset
){
return K2231A_Write
("*rcl %d",preset
);
}
double K2231A_GetSetCurrent
(){
return K2231A_QueryDouble
("CURR?");
}
double K2231A_GetSetVoltage
(){
return K2231A_QueryDouble
("VOLT?");
}
double K2231A_SetCurrent
(double x
){
return K2231A_Write
("CURR %f", x
);
}
double K2231A_SetVoltage
(double x
){
return K2231A_Write
("VOLT %f", x
);
}
double K2231A_GetCurrentMonitor
(){
return K2231A_QueryDouble
("MEAS:CURR?");
}
double K2231A_GetVoltageMonitor
(){
return K2231A_QueryDouble
("MEAS:VOLT?");
}
int K2231A_SelectChannel
(int ch
){
return K2231A_Write
( "INST CH%d",ch
);
}
int K2231A_GetOperationMode
(){
/*
//cv_cc = K2231A_QueryInt("Stat:ques:inst?");
sprintf(cmd, "Stat:ques:inst:isum%d?",ch+1);
*/
return K2231A_QueryInt
("Stat:ques:inst?");
}
int K2231A_SetSwitch
(int state
){
K2231A_Write
("Outp:enab %d",state
);
K2231A_Write
("Outp %d",state
);
return 0;
}
int K2231A_GetSwitch
(){
return K2231A_QueryInt
("CHAN:OUTP?");
}
void K2231A_ReadMonitorValues
(double * I
, double * V
, unsigned char * mode
, unsigned char *onoff
)
{
int iRet
;
char ch
=0;
double Voltage
;
double Current
;
char cv_cc
;
for (ch
=0; ch
<3; ch
++)
{
K2231A_SelectChannel
(ch
+1);
cv_cc
= K2231A_GetOperationMode
();
I
[ch
]= K2231A_GetCurrentMonitor
();
V
[ch
]= K2231A_GetVoltageMonitor
();
int onoff
=K2231A_QueryInt
("OUTP?");
if (debug
) printf("[%d] %g V %g A CVCC %d ONOFF %d\t",ch
, V
[ch
], I
[ch
], cv_cc
, onoff
);
}
return;
}
void K2231A_ReadSetValues
(double * I
, double * V
, unsigned char *onoff
)
{
int iRet
;
char ch
=0;
double Voltage
;
double Current
;
char cv_cc
;
for (ch
=0; ch
<3; ch
++)
{
K2231A_SelectChannel
(ch
+1);
cv_cc
= K2231A_GetOperationMode
();
I
[ch
]= K2231A_GetSetCurrent
();
V
[ch
]= K2231A_GetSetVoltage
();
int onoff
=K2231A_GetSwitch
();
if (debug
) printf("[%d] %g V %g A CVCC %d ONOFF %d\t",ch
, V
[ch
], I
[ch
], cv_cc
, onoff
);
}
return;
}
#ifdef MAIN
#include <ansi_c.h>
#include <utility.h>
#define RSTREG(a,x) (a&=(~(x)))
#define SETREG(a,x) (a|=x)
int gLogToFile
;
int vmon
[3]= {P1_VMON_1
,P1_VMON_2
,P1_VMON_3
};
int imon
[3]= {P1_IMON_1
,P1_IMON_2
,P1_IMON_3
};
int vset
[3]= {P1_U_1
,P1_U_2
,P1_U_3
};
int iset
[3]= {P1_I_1
,P1_I_2
,P1_I_3
};
int radiob
[3]= {P1_BOX_1
,P1_BOX_2
,P1_BOX_3
};
int cvcc
[3]= {P1_CVCC_1
,P1_CVCC_2
,P1_CVCC_3
};
int gMask
=0xF;
int pnl
;
FILE
*gFp
;
int main
(int argc
, char *argv
[])
{
int DeviceId
=0;
unsigned char MainOutput
=0, preset
=0;
double Voltage
=0, Current
=0, tinterval
=1;
char str
[0xFF];
int i
=0;
char response
[0xFF];
if (InitCVIRTE
(0, argv
, 0) == 0)
return -1; /* out of memory */
SetStdioPort
(CVI_STDIO_WINDOW
);
SetStdioWindowOptions
(1000, 1, 0);
SetStdioWindowSize
(150, 600);
SetStdioWindowPosition
(825, 20);
if ((pnl
= LoadPanel
(0, "K2231A-ctrl.uir", P1
)) < 0) return -1;
if (K2231A_Open
()== 0) MessagePopup
("Error","Cannot open USB device");
if (K2231A_Test
() == 0 )MessagePopup
("DLL error","Dll Error");
SetCtrlVal
(pnl
, P1_ONOFF
, MainOutput
);
GetCtrlVal
(pnl
, P1_TINTERVAL
, &tinterval
);
SetCtrlAttribute
(pnl
, P1_TIMER
, ATTR_INTERVAL
, tinterval
);
SetCtrlVal
(pnl
, P1_PRESET
, preset
);
SetTraceAttributeEx
(pnl
, P1_GRAPH_IMON
, 1, ATTR_TRACE_LG_TEXT
, "CH 30V");
SetTraceAttributeEx
(pnl
, P1_GRAPH_IMON
, 2, ATTR_TRACE_LG_TEXT
, "CH 30V");
SetTraceAttributeEx
(pnl
, P1_GRAPH_IMON
, 3, ATTR_TRACE_LG_TEXT
, "CH 5V");
SetTraceAttributeEx
(pnl
, P1_GRAPH_VMON
, 1, ATTR_TRACE_LG_TEXT
, "CH 30V");
SetTraceAttributeEx
(pnl
, P1_GRAPH_VMON
, 2, ATTR_TRACE_LG_TEXT
, "CH 30V");
SetTraceAttributeEx
(pnl
, P1_GRAPH_VMON
, 3, ATTR_TRACE_LG_TEXT
, "CH 5V");
int ison
;
for (unsigned char ch
=0; ch
<3; ch
++)
{
K2231A_SelectChannel
(ch
+1);
Voltage
=K2231A_GetSetVoltage
();
Current
=K2231A_GetSetCurrent
();
ison
=K2231A_GetSwitch
();
SetCtrlVal
(pnl
, vset
[ch
], Voltage
);
SetCtrlVal
(pnl
, iset
[ch
], Current
);
SetCtrlVal
(pnl
, radiob
[ch
], ison
);
if (ison
) SETREG
(gMask
,(1<<ch
));
else RSTREG
(gMask
,(1<<ch
));
}
DisplayPanel
(pnl
);
RunUserInterface
();
DiscardPanel
(pnl
);
K2231A_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
);
K2231A_SetSwitch
(state
);
break;
}
return 0;
}
int CVICALLBACK SetCB
(int panel
, int control
, int event
,
void *callbackData
, int eventData1
, int eventData2
)
{
switch (event
)
{
case EVENT_COMMIT
:
{
unsigned char preset
=0;
SetCtrlVal
(pnl
, P1_PRESET
, preset
);
for (unsigned char ch
= 0; ch
<3; ch
++)
{
//printf("ch %d %x\n", ch, gMask);
if (gMask
& (1<<ch
))
{
double Voltage
;
double Current
;
GetCtrlVal
(panel
, vset
[ch
], &Voltage
);
GetCtrlVal
(panel
, iset
[ch
], &Current
);
char cmd
[0xFF];
K2231A_SelectChannel
(ch
+1);
Voltage
=K2231A_GetVoltageMonitor
();
Current
=K2231A_GetCurrentMonitor
();
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
=0;
double Current
=0;
char cv_cc
=0;
switch (event
)
{
case EVENT_COMMIT
:
for (ch
= 0; ch
<3; ch
++)
{
if (gMask
& (1<<ch
))
{
K2231A_SelectChannel
(ch
+1);
Voltage
=K2231A_GetVoltageMonitor
();
Current
=K2231A_GetCurrentMonitor
();
cv_cc
= K2231A_GetOperationMode
();
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 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
)
{
SetCtrlAttribute
(panel
, P1_TIMER
, ATTR_ENABLED
, 1);
ResumeTimerCallbacks
();
}
else
{
SuspendTimerCallbacks
();
printf ("Disabling timer....\n");
}
break;
}
return 0;
}
int CVICALLBACK ExitCB
(int panel
, int control
, int event
,
void *callbackData
, int eventData1
, int eventData2
)
{
switch (event
)
{
case EVENT_COMMIT
:
QuitUserInterface
(0);
break;
}
return 0;
}
int CVICALLBACK SetMaskCB
(int panel
, int control
, int event
,
void *callbackData
, int eventData1
, int eventData2
)
{
int ison
;
switch (event
)
{
int i
=0;
case EVENT_COMMIT
:
GetCtrlVal
(panel
, control
, &ison
);
for (i
=0; i
<3; i
++) if (control
== radiob
[i
]) break;
if (ison
) SETREG
(gMask
,(1<<i
));
else RSTREG
(gMask
,(1<<i
));
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
[3]= {0,0,0};
double voltage
[3]= {0,0,0};
char cv_cc
=0;
case EVENT_TIMER_TICK
:
for (ch
= 0; ch
<3; ch
++)
{
if (gMask
& (1<<ch
))
{
K2231A_SelectChannel
(ch
+1);
voltage
[ch
] =K2231A_GetVoltageMonitor
();
current
[ch
] =K2231A_GetCurrentMonitor
();
cv_cc
= K2231A_GetOperationMode
();
int onoff
=K2231A_GetSwitch
();
SetCtrlVal
(panel
, vmon
[ch
], voltage
[ch
]);
SetCtrlVal
(panel
, imon
[ch
], current
[ch
]);
SetCtrlVal
(panel
, cvcc
[ch
], cv_cc
);
SetCtrlVal
(panel
, P1_ONOFFLED
, onoff
);
if (gLogToFile
)
{
if (!gFp
)
{
gFp
= fopen("pw18-1.8aq.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
[ch
], current
[ch
], cv_cc
);
}
}
}
PlotStripChart
(panel
, P1_GRAPH_IMON
, current
, 3, 0, 0, VAL_DOUBLE
);
PlotStripChart
(panel
, P1_GRAPH_VMON
, voltage
, 3, 0, 0, VAL_DOUBLE
);
break;
}
return 0;
}
int CVICALLBACK SetPresetCB
(int panel
, int control
, int event
,
void *callbackData
, int eventData1
, int eventData2
)
{
char cmd
[0xFF];
switch (event
)
{
case EVENT_COMMIT
:
{
unsigned char preset
;
double Voltage
, Current
;
GetCtrlVal
(panel
, control
, &preset
);
K2231A_RecallFromMemory
(preset
);
for (unsigned char ch
=0; ch
<3; ch
++)
{
int ison
;
K2231A_SelectChannel
(ch
+1);
Voltage
=K2231A_GetSetVoltage
();
Current
=K2231A_GetSetCurrent
();
ison
=K2231A_GetSwitch
();
SetCtrlVal
(pnl
, vset
[ch
], Voltage
);
SetCtrlVal
(pnl
, iset
[ch
], Current
);
GetCtrlVal
(pnl
, radiob
[ch
], &ison
);
if (ison
) SETREG
(gMask
,(1<<ch
));
else RSTREG
(gMask
,(1<<ch
));
}
break;
}
}
return 0;
}
int CVICALLBACK DebugCB
(int panel
, int control
, int event
,
void *callbackData
, int eventData1
, int eventData2
)
{
switch (event
)
{
case EVENT_COMMIT
:
GetCtrlVal
(panel
, control
, &debug
);
break;
}
return 0;
}
#endif MAIN