#include <userint.h>
#include "K2231A-ctrl.h"
#include <stdlib.h>
#include <stdio.h>
#include <windows.h>
#include <visa.h>
//#include <K2231A.h>
static ViStatus istat;
static ViSession RedPHandle,RMHandle;
int K2231A_DeviceId=0;
int K2231A_Initialize()
{
return 0;
}
int K2231A_Test()
{
char str[0xFF];
printf("K2231A_ModelNameQ %s\n",str
);
return TRUE;
}
int K2231A_Close()
{
return 0;
}
int K2231A_Open()
{
K2231A_Initialize();
return TRUE;
}
int K2231A_OpenHandle(char * dev, char * usb ){
K2231A_DeviceId = 0;
return K2231A_DeviceId;
}
/****************TOKOVI (potek, kako se spreminjajo tokovi s casom)****************/
void K2231A_ReadCurrent(double * I , double * V, unsigned char * mode, unsigned char *onoff){
int iRet;
char ch=0;
double Voltage;
double Current;
char cv_cc;
//iRet = K2231A_TimeOut(K2231A_DeviceId, 1);
//iRet = K2231A_Refresh(K2231A_DeviceId);
//iRet = K2231A_MainOutput(K2231A_DeviceId, Output);
//iRet = K2231A_MainOutputQ(K2231A_DeviceId, &Output);
for (ch=0;ch<3;ch++){
//iRet = K2231A_MoniDataQ(K2231A_DeviceId, ch+1, &Voltage, &Current, &cv_cc);
//if (debug) printf("[%d] %g V %g A CVCC %d\t",ch, Voltage, Current, cv_cc );
I[ch]= Current; //s tem definiras kaj sta I in V.
V[ch]= Voltage;
}
//if (debug) printf("\n");
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, preset;
double Voltage, Current, tinterval;
char str[0xFF];
if (InitCVIRTE (0, argv, 0) == 0)
return -1; /* out of memory */
SetStdioPort (CVI_STDIO_WINDOW);
SetStdioWindowOptions (1000, 0, 0);
SetStdioWindowSize (150, 600);
SetStdioWindowPosition (825, 20);
istat = viOpenDefaultRM (&RMHandle);
if (RMHandle) istat = viOpen (RMHandle, "TCPIP0::178.172.43.91::5000::SOCKET", VI_NULL, VI_NULL, &RedPHandle);
if (RedPHandle){
istat = viSetAttribute (RedPHandle, VI_ATTR_TCPIP_KEEPALIVE, VI_TRUE);
istat = viSetAttribute (RedPHandle, VI_ATTR_TCPIP_NODELAY, VI_TRUE);
istat = viSetAttribute (RedPHandle, VI_ATTR_TERMCHAR, '\n');
istat = viSetAttribute (RedPHandle, VI_ATTR_TERMCHAR_EN, VI_TRUE);
istat = viSetAttribute (RedPHandle, VI_ATTR_TMO_VALUE, 1000);
}
if ((pnl = LoadPanel (0, "K2231A-ctrl.uir", P1)) < 0) return -1;
if (K2231A_Open()== 0) MessagePopup("Error","Cannot open USB device");
//DeviceId = K2231A_OpenHandle ("PW-A","USB:1:1");
if (K2231A_Test() == 0 )MessagePopup("DLL error","Dll Error");
if (DeviceId < 0) MessagePopup("Error","Not Connected");
printf("TMI device ID %d\n",K2231A_DeviceId
);
//K2231A_MainOutputQ(K2231A_DeviceId, &MainOutput);
SetCtrlVal(pnl, P1_ONOFF, MainOutput);
GetCtrlVal(pnl, P1_TINTERVAL, &tinterval);
SetCtrlAttribute (pnl, P1_TIMER, ATTR_INTERVAL, tinterval);
//K2231A_PresetQ(K2231A_DeviceId, &preset);
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");
for (unsigned char ch=0;ch<4;ch++){
int ison;
//K2231A_VoltageQ(K2231A_DeviceId, ch+1, preset, &Voltage);
//K2231A_CurrentQ(K2231A_DeviceId, ch+1, preset, &Current);
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));
}
DisplayPanel (pnl);
RunUserInterface ();
DiscardPanel (pnl);
K2231A_Close();
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_MainOutput(K2231A_DeviceId, 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;
//K2231A_PresetQ(K2231A_DeviceId, &preset);
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);
//printf("->ch %d VSet %g Iset %g\n", ch, Voltage, Current);
//K2231A_Voltage(K2231A_DeviceId, ch+1, preset, Voltage);
//K2231A_VoltageQ(K2231A_DeviceId, ch+1, preset, &Voltage);
//K2231A_Current(K2231A_DeviceId, ch+1, preset, Current);
//K2231A_CurrentQ(K2231A_DeviceId, ch+1, preset, &Current);
//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;
switch (event)
{
case EVENT_COMMIT:
for (ch = 0; ch<3;ch++){
if (gMask & (1<<ch)){
//iRet = K2231A_TimeOut(K2231A_DeviceId, 1);
//iRet = K2231A_Refresh(K2231A_DeviceId);
//iRet = K2231A_MoniDataQ(K2231A_DeviceId, ch+1, &Voltage, &Current, &cv_cc);
//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){
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];
double voltage[3];
char cv_cc;
case EVENT_TIMER_TICK:
for (ch = 0; ch<3;ch++){
if (gMask & (1<<ch)){
//K2231A_TimeOut(K2231A_DeviceId, 1);
//K2231A_Refresh(K2231A_DeviceId);
//K2231A_MoniDataQ(K2231A_DeviceId, ch+1, &voltage[ch], ¤t[ch], &cv_cc);
SetCtrlVal(panel, vmon[ch], voltage[ch]);
SetCtrlVal(panel, imon[ch], current[ch]);
SetCtrlVal(panel, cvcc[ch], cv_cc);
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) {
switch (event) {
case EVENT_COMMIT:{
unsigned char preset;
double Voltage, Current;
GetCtrlVal(panel, control, &preset);
//K2231A_Preset(K2231A_DeviceId, preset);
for (unsigned char ch=0;ch<3;ch++){
int ison;
//K2231A_VoltageQ(K2231A_DeviceId, ch+1, preset, &Voltage);
//K2231A_CurrentQ(K2231A_DeviceId, ch+1, preset, &Current);
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;
}
#endif MAIN