#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 ("%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
(K617_Gdev
,0);
exit(1);
}
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];
len
=sprintf(cmd
,"G%0dX",mode
);
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];
len
=sprintf(cmd
,"F1R%0dX",range
);
// 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];
len
=sprintf(cmd
,"T%0dX",mode
);
K617_send
(cmd
, len
);
return;
}
void _VI_FUNC K617_reading_mode
(int mode
)
{
int len
;
char cmd
[100];
len
=sprintf(cmd
,"B%0dX",mode
);
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);
len
=sprintf(cmd
,"V%+0.2fX",value
);
// 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;
}
#ifdef K617_MAIN
#include <cvirte.h>
int __stdcall WinMain
(HINSTANCE hInstance
, HINSTANCE hPrevInstance
,
LPSTR lpszCmdLine
, int nCmdShow
)
{
int i
;
double value
;
char cres
[100];
if (InitCVIRTE
(hInstance
, 0, 0) == 0) return -1; /* out of memory */
K617_open
(1,5,3,0,13);
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
();
printf("%lg\n",value
);
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);
K617_close
();
return 0;
}
#endif