Sample Programs > Sample Code (Visual C++ 6.0)
Sample Programs
Sample Code (Visual C++ 6.0)
The samples given in this section assumes Microsoft Visual C++ 6.0 for the development platform and VISA library (VISA C API) for the I/O library.
For the VISA library, "KI-VISA Library 2.2.x" can be downloaded from KIKUSUI Website, or NI-VISA by National Instruments (VER. 3.0 or later recommended) or Agilent VISA (Agilent I/O Library VER M01.00 or later recommended) by Agilent Technologies can be used.
Note USB functions cannot be used on older versions of VISA. In addition, USB functions cannot be used on Windows 95 or Windows NT 3.5x or 4.0.
Opening a VISA session and setting communication parameters
The next code is a common section to all sample programs that are introduced later. It must be executed before starting the communication with the KFM2005/KFM2030.
The format of the VISA resource string that is substituted in the variable pResourceName varies for GPIB, RS232C, and USB.
For the GPIB, device address of 3 is assumed.
For the RS232C, the communication parameters, 19200 bps, Data 8 bits, Stop 2 bits, Parity NONE, XFlow, and Ack-OFF are assumed. Set the KFM2005/KFM2030 interface to match these values.
For the USB, there are no interface parameters that need to be set on the KFM2005/KFM2030, but the USB VID (vendor ID), PID (product ID), and serial number must be specified explicitly in the VISA resource string. These values can be confirmed on the interface setup screen of the KFM2005/KFM2030.
The serial number used in the sample below is an example.
//
// Initiation of VISA
//
ViSession viRM;
ViSession vi;
ViStatus vs;
char* pResourceName = "GPIB0::3::INSTR"; //if you use GPIB
// char* pResourceName = "ASRL1::INSTR"; //if you use RS232
// char* pResourceName = "USB0::0x0B3E::0x1018::AB000001::INSTR"; //if you use USB
vs = viOpenDefaultRM( &viRM);
vs = viOpen( viRM, pResourceName, VI_NO_LOCK, 0, &vi);
// common settings
vs = viSetAttribute( vi, VI_ATTR_TMO_VALUE, 10000);
vs = viSetAttribute( vi, VI_ATTR_TERMCHAR, 0x10);
vs = viSetAttribute( vi, VI_ATTR_TERMCHAR_EN, 1);
vs = viSetAttribute( vi, VI_ATTR_WR_BUF_OPER_MODE, VI_FLUSH_ON_ACCESS);
vs = viSetAttribute( vi, VI_ATTR_RD_BUF_OPER_MODE, VI_FLUSH_ON_ACCESS);
short iIntfType;
vs = viGetAttribute( vi, VI_ATTR_INTF_TYPE, &iIntfType);
if( iIntfType == VI_INTF_ASRL) {
vs = viSetAttribute( vi, VI_ATTR_ASRL_BAUD, 19200);
vs = viSetAttribute( vi, VI_ATTR_ASRL_DATA_BITS, 8);
vs = viSetAttribute( vi, VI_ATTR_ASRL_PARITY, VI_ASRL_PAR_NONE);
vs = viSetAttribute( vi, VI_ATTR_ASRL_STOP_BITS, VI_ASRL_STOP_TWO);
vs = viSetAttribute( vi, VI_ATTR_ASRL_FLOW_CNTRL, VI_ASRL_FLOW_XON_XOFF);
vs = viSetAttribute( vi, VI_ATTR_ASRL_END_OUT, VI_ASRL_END_TERMCHAR);
vs = viSetAttribute( vi, VI_ATTR_ASRL_END_IN, VI_ASRL_END_TERMCHAR);
vs = viSetAttribute( vi, VI_ATTR_IO_PROT, VI_PROT_4882_STRS);
}
else if( iIntfType == VI_INTF_GPIB) {
}
else if( iIntfType == 7) {
vs = viPrintf( vi, "SYST:REN 1");
}
Sample code (the simplest measurement)
In the next example, the impedance, voltage, and current are measured and stored in real type variables.
Because the MEAS:<meter_fn>? query is used, detailed measurement conditions are not specified. The impedance measurement measures the absolute value, resistance, reactance, and phase, simultaneously. Thus, the FETC? query is used. The four types of impedance data are measured simultaneously.
double adImpData[4];
vs = viQueryf( vi,
"MEAS:IMP:MAGN?;:FETC:IMP:RES?;REAC?;PHAS?",
"%lf;%lf;%lf;%lf",
&adImpData[0],
&adImpData[1],
&adImpData[2],
&adImpData[3]
);
double dVoltData;
vs = viQueryf( vi, "MEAS:VOLT?", "%lf", &dVoltData);
double dCurrData;
vs = viQueryf( vi, "MEAS:CURR?", "%lf", &dCurrData);
vs = viClose( vi);
vs = viClose( viRM);
Sample code (setting the current and making the measurement)
In the next program example, the superimpose current (KFM2005)/ measuring AC current (KFM2030) is specified, and the impedance is measured.
The superimpose current, frequency, and moving average count are set to 16.5 mApp, 1 kHz, and 4, respectively. In this example, the READ? query is used instead of the MEAS:<meter_fn>? query. The voltage and current measurements are omitted.
In the case of KFM2030, replace the superimposed current 16.5 mApp with the measuring AC current 165 mApp.
vs = viPrintf( vi, "CONF:IMP");
vs = viPrintf( vi, "IMP:CURR:AC:LEV 165E-4;FREQ 1000");
vs = viPrintf( vi, "IMP:AVER:MOV:COUN 4");
double adImpData[4];
vs = viQueryf( vi,
"READ:IMP:MAGN?;:FETC:IMP:RES?;REAC?;PHAS?",
"%lf;%lf;%lf;%lf",
&adImpData[0],
&adImpData[1],
&adImpData[2],
&adImpData[3]
);
vs = viClose( vi);
vs = viClose( viRM);
Sample code (specifying the trigger count and making multiple measurements)
In the next program example, the impedance measurement is performed consecutively five times.
When the trigger count is large, more time is needed in making the measurements. Therefore, in this example, the *OPC command is used to generate a service request when the measurement is complete. The OPC bit (bit 0) of the standard event status enable register (ESE) and the ESB bit (bit 5) of the service request enable register (SRE) are set for this reason.
Because the measured data consists of comma-separated values, the array of VISA viScanf and viQueryf functions is used to retrieve the data and convert to real numbers.
The INIT command is used to start the measurement in this example. To wait for the service request to be generated, the viWaitOnEvent function is used.
The voltage and current measurements are omitted.
vs = viPrintf( vi, "CONF:IMP");
vs = viPrintf( vi, "IMP:CURR:AC:LEV 165E-4;FREQ 1000");
vs = viPrintf( vi, "IMP:AVER:MOV:COUN 4");
vs = viPrintf( vi, "TRIG:COUN 5");
//
// Initiate measurement and wait for SRQ
//
vs = viPrintf( vi, "*ESE 1;*SRE 32;*CLS;INIT;*OPC");
vs = viEnableEvent( vi, VI_EVENT_SERVICE_REQ, VI_QUEUE, 0);
ViEventType et;
ViEvent ev;
do {
vs = viWaitOnEvent( vi, VI_EVENT_SERVICE_REQ, 1000, &et, &ev);
// wait for SRQ.
// Do other job if you need...
} while( ev == VI_NULL);
vs = viClose(ev);
vs = viDisableEvent( vi, VI_EVENT_SERVICE_REQ, VI_QUEUE);
// End of waiting for SRQ
double adImpMagn[5];
double adImpRes[5];
double adImpReac[5];
double adImpPhas[5];
int iNumData = 5;
vs = viQueryf( vi, "FETC:IMP:MAGN?", "%,#lf", &iNumData, adImpMagn);
vs = viQueryf( vi, "FETC:IMP:RES?", "%,#lf", &iNumData, adImpRes);
vs = viQueryf( vi, "FETC:IMP:REAC?", "%,#lf", &iNumData, adImpReac);
vs = viQueryf( vi, "FETC:IMP:PHAS?", "%,#lf", &iNumData, adImpPhas);
vs = viClose( vi);
vs = viClose( viRM);
This code does not work on the RS232C, because the service request function is used. On the RS232C, replace the measurement start and SRQ wait sections of the code with the "OPC bit wait" procedure as in the example shown below.
//
// Initiate measurement and wait for OPC bit
//
vs = viPrintf( vi, "*CLS;INIT;*OPC");
int iEsr = 0;
do {
vs = viQueryf( vi, "*ESR?", "%d", &iEsr);
// wait for OPC bit on *ESR register
// Do other job if you need...
Sleep(1000);
} while( iEsr == 0);
// End of waiting for OPC bit
Sample code (simultaneous measurement of impedance, voltage, and current)
In the last example, the measurement functions of the KFM2005/KFM2030 are used to the fullest extent to measure all the data simultaneously.
In general, because impedance measurement takes time, the voltage and current can be measured numerous times during one impedance measurement. In this example, the trigger count of the voltage and current measurements is set to 16, and measurement is made at 3 second intervals.
vs = viPrintf( vi, "FUNC:ALL");
vs = viPrintf( vi, "CONF:IMP;VOLT;CURR");
vs = viPrintf( vi, "IMP:CURR:AC:LEV 165E-4;FREQ 0.1");
vs = viPrintf( vi, "TRIG:SEQ1:COUN 1;SOUR IMM");
vs = viPrintf( vi, "TRIG:SEQ2:COUN 16;SOUR TIM;TIM 3.0");
vs = viPrintf( vi, "*ESE 1;*SRE 32;*CLS;INIT;*OPC");
vs = viEnableEvent( vi, VI_EVENT_SERVICE_REQ, VI_QUEUE, 0);
ViEventType et;
ViEvent ev;
do {
vs = viWaitOnEvent( vi, VI_EVENT_SERVICE_REQ, 1000, &et, &ev);
// wait for SRQ.
// Do other job if you need...
} while( ev == VI_NULL);
vs = viClose(ev);
vs = viDisableEvent( vi, VI_EVENT_SERVICE_REQ, VI_QUEUE);
double dImpMagn;
double dImpRes;
double dImpReac;
double dImpPhas;
vs = viQueryf( vi,
"FETC:IMP:MAGN?;RES?;REAC?;PHAS?",
"%lf;%lf;%lf;%lf",
&dImpMagn,
&dImpRes,
&dImpReac,
&dImpPhas
);
double adVolt[16];
double adCurr[16];
int iNumData = 16;
vs = viQueryf( vi, "FETC:VOLT?", "%,#lf", &iNumData, adVolt);
vs = viQueryf( vi, "FETC:CURR?", "%,#lf", &iNumData, adCurr);
vs = viClose( vi);
vs = viClose( viRM);