#include <ansi_c.h>
 
#include <utility.h>
 
#include <gpib.h>
 
#include "K617.h"
 
 
 
K617STATUS K617Stat;
 
static int gdev,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 (gdev,0);
 
 
 
}
 
 
 
void _VI_FUNC K617_open (int interface, int primary_addr, int secondary_addr,
 
                         int timeout)
 
{
 
/*
 
  gdev = OpenDev ("GPIB0", "");
 
  if (ibsta & ERR) GpibError("OpenDev Error");
 
  iret = ibpad (gdev, 0);
 
  if (ibsta & ERR) GpibError("OpenDev Error");
 
  iret = ibsad (gdev, NO_SAD);
 
  iret = ibtmo (gdev, T10s);
 
  iret = ibeot (gdev, 1);
 
  iret = ibeos (gdev, 0);
 
*/
 
  gdev = ibdev(interface,primary_addr,secondary_addr,timeout,1,0);
 
  if (ibsta & ERR) GpibError("OpenDev Error");
 
  Delay(GDELAY);   
 
  return;   
 
}
 
 
 
void _VI_FUNC K617_clear (void)
 
{
 
  iret = ibclr (gdev);                 /* Clear the device                        */
 
  if (ibsta & ERR) GpibError("OpenDev Error");
 
  Delay(GDELAY);   
 
  return;   
 
    
 
}
 
 
 
void _VI_FUNC K617_send (char *cmd, int len)
 
{
 
  iret = ibwrt (gdev, cmd, len);
 
  if (ibsta & ERR) GpibError("OpenDev Error");
 
  Delay(GDELAY);   
 
  return;  
 
}
 
 
 
int _VI_FUNC K617_receive (char *response, int maxbyt)
 
{
 
  iret = ibrd (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);
 
  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 (gdev);
 
  iret = ibonl(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 (0, 3, 0, 13);
 
/*
 
  K617_clear ();
 
  K617_current_mode (4);
 
  K617_zero_correct (1);
 
  K617_data_format (0);
 
  K617_trigger_mode (0);
 
  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("%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