Sample Programs > Sample Code (Visual Basic 6.0)
Sample Programs
Sample Code (Visual Basic 6.0)
The samples given in this section assumes Microsoft Visual Basic 6.0 for the development platform and VISA COM Software for the I/O library.
For the VISA COM Software, "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 strIoResource varies for GPIB, RS232C, and USB.
For the GPIB, device address of 3 is assumed.
For the RS232C, the communication parameters, 19 200 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.
Dim rm As VisaComLib.ResourceManager
Dim session As VisaComLib.IMessage
Dim strIoResource As String
strIoResource = "GPIB0::3::INSTR" 'If you use GPIB
' strIoResource = "ASRL1::INSTR" 'If you use RS232
' strIoResource = "USB0::0x0B3E::0x1018::AB000001::INSTR" 'If you use USB
Set rm = CreateObject("VISA.GlobalRM")
'Set rm=CreateObject("AgilentRM.SRMCls") 'If you use Agilent VISA
Set session = rm.Open(strIoResource)
' Settings specific for ASRL (RS232) resource
If session.HardwareInterfaceType = INTF_ASRL Then
Dim rs As ISerial
Set rs = session
rs.BaudRate = 19200 'Baudrate 19200bps
rs.DataBits = 8 'Data 8bits
rs.StopBits = ASRL_STOP_TWO 'Stop 2bit
rs.Parity = ASRL_PAR_NONE 'Parity NONE
rs.FlowControl = ASRL_FLOW_XON_XOFF 'X-flow
rs.EndIn = ASRL_END_TERMCHAR 'Terminates with Termchar when reading
rs.EndOut = ASRL_END_TERMCHAR 'Add Termchar when writing
session.IOProtocol = PROT_4882_STRS
End If
' Settings specific for USB resource
If session.HardwareInterfaceType = 7 Then
session.WriteString("SYST:REN 1"); 'Make sure to Enable Remote
End If
' Other common settings
session.TerminationCharacter = 10
session.TerminationCharacterEnabled = True
session.Timeout = 10000
From this point, communication with the KFM2005/KFM2030 is carried out via the VisaComLib.IMessage interface (variable session).
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.
Dim adImpData(0 To 3) As Double
session.WriteString "MEAS:IMP:MAGN?;:FETC:IMP:RES?;REAC?;PHAS?"
Dim asImpData() As String
asImpData = Split(session.ReadString(256), ";", 4)
Dim ndx As Integer
For ndx = 0 To 3
adImpData(ndx) = CDbl(asImpData(ndx))
Next ndx
Dim dVoltData As Double
session.WriteString "MEAS:VOLT?"
dVoltData = CDbl(session.ReadString(256))
Dim dCurrData As Double
session.WriteString "MEAS:CURR?"
dCurrData = CDbl(session.ReadString(256))
session.Close
Sample code (setting the current and making the measurement)
In the next program example, the sperimpose 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.
session.WriteString "CONF:IMP"
session.WriteString "IMP:CURR:AC:LEV 165E-4;FREQ 1000"
session.WriteString "IMP:AVER:MOV:COUN 4"
Dim adImpData(0 To 3) As Double
session.WriteString "READ:IMP:MAGN?;:FETC:IMP:RES?;REAC?;PHAS?"
Dim asImpData() As String
asImpData = Split(session.ReadString(256), ";", 4)
Dim ndx As Integer
For ndx = 0 To 3
adImpData(ndx) = CDbl(asImpData(ndx))
Next ndx
session.Close
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 Split function of Visual Basic 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 WaitOnEvent function is used.
The voltage and current measurements are omitted.
session.WriteString "CONF:IMP"
session.WriteString "IMP:CURR:AC:LEV 165E-4;FREQ 1000"
session.WriteString "IMP:AVER:MOV:COUN 4"
session.WriteString "TRIG:SEQ1:COUN 5"
' Initiate measurement and wait for SRQ
session.WriteString "*ESE 1;*SRE 32;*CLS;INIT:SEQ1;*OPC"
Dim em As VisaComLib.IEventManager
Set em = session
em.EnableEvent EVENT_SERVICE_REQ, EVENT_QUEUE, 0
On Error Resume Next
Do
Dim ev As VisaComLib.IEvent
Set ev = em.WaitOnEvent(1000, EVENT_SERVICE_REQ, 0)
' wait for SRQ
' Do other job if you need...
Loop While ev Is Nothing
On Error GoTo 0
ev.Close
em.DisableEvent EVENT_SERVICE_REQ, EVENT_QUEUE, 0
' End of waiting for SRQ
Dim adImpMagn(0 To 4) As Double
Dim adImpRes(0 To 4) As Double
Dim adImpReac(0 To 4) As Double
Dim adImpPhas(0 To 4) As Double
Dim iNumData As Integer
iNumData = 5
Dim ndx As Integer
Dim asImpData() As String
session.WriteString "FETC:ARR:IMP:MAGN?"
asImpData = Split(session.ReadString(256), ",", iNumData)
For ndx = 0 To iNumData - 1
adImpMagn(ndx) = CDbl(asImpData(ndx))
Next ndx
session.WriteString "FETC:ARR:IMP:RES?"
asImpData = Split(session.ReadString(256), ",", iNumData)
For ndx = 0 To iNumData - 1
adImpRes(ndx) = CDbl(asImpData(ndx))
Next ndx
session.WriteString "FETC:ARR:IMP:REAC?"
asImpData = Split(session.ReadString(256), ",", iNumData)
For ndx = 0 To iNumData - 1
adImpReac(ndx) = CDbl(asImpData(ndx))
Next ndx
session.WriteString "FETC:ARR:IMP:PHAS?"
asImpData = Split(session.ReadString(256), ",", iNumData)
For ndx = 0 To iNumData - 1
adImpPhas(ndx) = CDbl(asImpData(ndx))
Next ndx
session.Close
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
session.WriteString "*CLS;INIT:SEQ1;*OPC"
Dim iEsr As Integer
Do
session.WriteString "*ESR?"
iEsr = CInt(session.ReadString(16))
'
' wait for OPC bit on *ESR register
' Do other job if you need...
Sleep 1000 ' Win32 API Sleep() function, you need declare it.
Loop While iEsr = 0
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.
session.WriteString "FUNC:ALL"
session.WriteString "CONF:IMP;VOLT;CURR"
session.WriteString "IMP:CURR:AC:LEV 165E-4;FREQ 0.1"
session.WriteString "TRIG:SEQ1:COUN 1;SOUR IMM"
session.WriteString "TRIG:SEQ2:COUN 16;SOUR TIM;TIM 3.0"
session.WriteString "*ESE 1;*SRE 32;*CLS;INIT;*OPC"
Dim em As VisaComLib.IEventManager
Set em = session
em.EnableEvent EVENT_SERVICE_REQ, EVENT_QUEUE, 0
On Error Resume Next
Do
Dim ev As VisaComLib.IEvent
Set ev = em.WaitOnEvent(1000, EVENT_SERVICE_REQ, 0)
' wait for SRQ
' Do other job if you need...
Loop While ev Is Nothing
On Error GoTo 0
ev.Close
em.DisableEvent EVENT_SERVICE_REQ, EVENT_QUEUE, 0
' End of waiting for SRQ
Dim dImpMagn As Double
Dim dImpRes As Double
Dim dImpReac As Double
Dim dImpPhas As Double
Dim asData() As String
session.WriteString "FETC:IMP:MAGN?;RES?;REAC?;PHAS?"
asData = Split(session.ReadString(256), ";", 4)
dImpMagn = CDbl(asData(0))
dImpRes = CDbl(asData(1))
dImpReac = CDbl(asData(2))
dImpPhas = CDbl(asData(3))
Dim adVolt(0 To 15) As Double
Dim adCurr(0 To 15) As Double
Dim iNumData As Integer
iNumData = 16
Dim ndx As Integer
session.WriteString "FETC:ARR:VOLT?"
asData = Split(session.ReadString(256), ",", iNumData)
For ndx = 0 To iNumData - 1
adVolt(ndx) = CDbl(asData(ndx))
Next ndx
session.WriteString "FETC:ARR:CURR?"
asData = Split(session.ReadString(256), ",", iNumData)
For ndx = 0 To iNumData - 1
adCurr(ndx) = CDbl(asData(ndx))
Next ndx
session.Close