#include <ansi_c.h>
#include <utility.h>
#include <gpib.h>
#include "prologix.h"
#include "K617.h"
K617STATUS K617Stat;
static int K617_Interface,K617_Port,K617_Gdev;
static int iret;
void GpibError(char *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");
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
);
/* Call ibonl to take the device and interface offline */
ibonl (K617_Gdev,0);
}
void _VI_FUNC K617_open (int interface, int port, int primary_addr,
int secondary_addr, int timeout)
{
char cmd[100];
/*
K617_Gdev = OpenDev ("GPIB0", "");
if (ibsta & ERR) GpibError("OpenDev Error");
iret = ibpad (K617_Gdev, 0);
if (ibsta & ERR) GpibError("OpenDev Error");
iret = ibsad (K617_Gdev, NO_SAD);
iret = ibtmo (K617_Gdev, T10s);
iret = ibeot (K617_Gdev, 1);
iret = ibeos (K617_Gdev, 0);
*/
K617_Interface=interface;
K617_Port=port;
switch (K617_Interface){
case 1:
PROLOGIX_Open (K617_Port);
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:
K617_Gdev = ibdev(K617_Port,primary_addr,secondary_addr,timeout,1,0);
if (ibsta & ERR) GpibError("OpenDev Error");
}
Delay(GDELAY);
return;
}
void _VI_FUNC K617_clear (void)
{
switch (K617_Interface){
case 1:
PROLOGIX_Send("++clr");
break;
default:
iret = ibclr (K617_Gdev); // Clear the device
if (ibsta & ERR) GpibError("OpenDev Error");
}
// Delay(GDELAY);
Delay(2.);
return;
}
void _VI_FUNC K617_send (char *cmd, int len)
{
switch (K617_Interface){
case 1:
PROLOGIX_Send(cmd);
break;
default:
iret = ibwrt (K617_Gdev, cmd, len);
if (ibsta & ERR) GpibError("OpenDev Error");
}
Delay(GDELAY);
return;
}
int _VI_FUNC K617_receive (char *response, int maxbyt)
{
switch (K617_Interface){
case 1:
PROLOGIX_Send("++read eoi");
return PROLOGIX_Receive (response,maxbyt);
break;
default:
iret = ibrd (K617_Gdev, response, maxbyt);
if (ibsta & ERR) GpibError("OpenDev Error");
response[ibcntl]=0;
return ibcntl;
}
}
void _VI_FUNC K617_status (void)
{
int len;
char cres[100];
K617_send ("U0X", 3);
len = K617_receive (cres, 90);
sscanf(cres
," 617%1d%2d%1d%1d%1d%1d%1d%1d%1d%1d%1d%2d%1d%2c",
&K617Stat.function,&K617Stat.range,&K617Stat.zero_check,
&K617Stat.zero_correct,&K617Stat.suppress,&K617Stat.trigger,
&K617Stat.vsource_operate,&K617Stat.read_mode,
&K617Stat.data_prefix,&K617Stat.display,
&K617Stat.data_store,&K617Stat.srq,&K617Stat.eoi,K617Stat.terminator);
// printf("617FRRCZNTOBGDQMMKYY\n");
// printf("%s\n",cres);
// K617_send ("X", 1);
return;
}
void _VI_FUNC K617_data_format (int mode)
{
int len;
char cmd[100];
K617_send (cmd, len);
return;
}
double _VI_FUNC K617_get (char *prefix, int *loc)
{
int len;
double value;
char cres[100];
K617_send ("X", 1);
len = K617_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 K617_current_mode (int range)
{
int len;
char cmd[100];
// printf("%d, %s\n",len,cmd);
K617_send (cmd, len);
Delay(1);
return;
}
void _VI_FUNC K617_zero_correct (int zcorrect)
{
K617_send ("C0X", 3);
K617_send ("Z0X", 3);
if (zcorrect) {
K617_send ("C1X", 3);
Delay(2);
K617_send ("Z1X", 3);
K617_send ("C0X", 3);
}
return;
}
void _VI_FUNC K617_trigger_mode (int mode)
{
int len;
char cmd[100];
K617_send (cmd, len);
return;
}
void _VI_FUNC K617_reading_mode (int mode)
{
int len;
char cmd[100];
K617_send (cmd, len);
return;
}
void _VI_FUNC K617_vsource_set (float value)
{
int len;
char cmd[100];
value
=0.05*floor((value
+0.025)/0.05);
// printf("%d, %s\n",len,cmd);
K617_send (cmd, len);
return;
}
double _VI_FUNC K617_vsource_get (void)
{
double value;
K617_reading_mode (4);
value = K617_get (NULL, NULL);
K617_reading_mode (0);
return value;
}
void _VI_FUNC K617_vsource_operate (int operate)
{
if (operate)
K617_send ("O1X", 3);
else
K617_send ("O0X", 3);
return;
}
void _VI_FUNC K617_close (void)
{
// iret = CloseDev (K617_Gdev);
switch (K617_Interface){
case 1:
PROLOGIX_Close();
break;
default:
iret = ibonl(K617_Gdev, 0);// Take the device offline
if (ibsta & ERR) GpibError("OpenDev Error");
}
return;
}
void _VI_FUNC K617_test (void)
{
int i;
double value;
char cres[100];
K617_clear ();
K617_current_mode (4);
K617_zero_correct (1);
K617_data_format (0);
K617_trigger_mode (1);
K617_vsource_set (-100.);
K617_vsource_operate (1);
K617_status();
value=K617_get(cres,&i);
printf("%s, %lg, %d\n",cres
,value
,i
);
value=K617_vsource_get();
value=K617_get(cres,&i);
printf("%s, %lg, %d\n",cres
,value
,i
);
value=K617_get(cres,&i);
printf("%s, %lg, %d\n",cres
,value
,i
);
value=K617_get(cres,&i);
printf("%s, %lg, %d\n",cres
,value
,i
);
value=K617_get(cres,&i);
printf("%s, %lg, %d\n",cres
,value
,i
);
Delay(5);
K617_vsource_operate (0);
}
#ifdef K617_MAIN
#include <cvirte.h>
#include <userint.h>
#include "K617-ctrl.h"
int pnl; 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;
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);
if ((pnl = LoadPanel (0, "K617-ctrl.uir", P1)) < 0) return -1;
K617_open (1,4,3,0,13);
SetCtrlVal(pnl, P1_ONOFF, 0);
GetCtrlVal(pnl, P1_TINTERVAL, &tinterval);
SetCtrlAttribute (pnl, P1_TIMER, ATTR_INTERVAL, tinterval);
for (unsigned char ch=0;ch<1;ch++){
int ison,i;
Voltage =K617_vsource_get();
Current =K617_get(cres,&i);
SetCtrlVal(pnl, vset[ch], Voltage);
SetCtrlVal(pnl, iset[ch], Current);
GetCtrlVal(pnl, radiob[ch], &ison);
gMask = ison;
}
DisplayPanel (pnl);
RunUserInterface ();
DiscardPanel (pnl);
K617_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);
K617_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
);
K617_vsource_set (Voltage);
Voltage=K617_vsource_get();
// TMI_Current(TMI_DeviceId, ch+1, Pre_1, Current);
Current=K617_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 =K617_vsource_get();
Current =K617_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 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)
{
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 =K617_vsource_get();
current[ch] =K617_get(cres,&i);
if (gLogToFile){
if (!gFp) {
gFp
= fopen("K617.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:
K617_test();
break;
}
return 0;
}
#endif