|
Instrument Control
One of the uses of spBasic is the control of Instruments
and Equipment using spBasic statements. Instrument control is used to
control laboratory equipment and is also used in test and measurement
(T&M) applications, where commands are sent to various instruments
to control equipment during testing. Test results are read by other instruments
such as digital multimeters and oscilloscopes and are recorded in an Excel
spreadsheet and plotted on Excel charts and graphs. Users of the instruments or equipment can then
write their own customized program from Excel spreadsheets to control
the instruments and read and create charts of any equipment output.
Example spBasic Add-Ins and 'Statement Text Files' for Instrument Control are included in the following directories in the free fully-functional download.
'C:\Program Files\spBasic\AddIns\Agilent\34401ADmm\AddIn\' - for the Agilent 34401A DMM and most other DMM's
'C:\Program Files\spBasic\AddIns\Agilent\E3600PwrSupplies\AddIn\' - for the Agilent E36xx Series power supplies and others
'C:\Program Files\spBasic\AddIns\Instrument\AddIn\' - for general Instrument Control
SpBasic can be used to control any piece of equipment as long as a PC
interface is available. SpBasic sends commands through the interface using Excel Add-Ins, either a general Instrument Add-In or one specifically written for a particular instrument. You also can write your own custom code using VBA to control the instrument, and add the custom VBA subroutines and functions to the spBasic language.
A standard way of controlling instruments is by using the Standard Commands for Programmable Instruments (SCPI) command set associated with each instrument. This command set uses ASCII commands sent to the instrument. An example is,
"VOLT:DC:RANG 10"
This command sets up a Digital MultiMeter (DMM) to measure DC volts with a range of 10 volts. To read the voltage from the meter, you would send the query command "READ?", which would return the measured voltage.
There are three basic ways you can send commands to instruments and read measurements,
- You can use the "Instrument" Excel Add-In provided with spBasic to send commands and return measurements using a 'SendIOCommand' and 'SendIOQuery' statements. See the "SpbInstrumentCore.xla" discussion below.
- You can use an Add-In customized for a particular instrument. This packages the commands and measurement queries more conveniently. See the "SpbAgilent34401A.xla" discussion below.
- You can write your own VBA subroutines and functions that use custom drivers provided by the instrument manufacturer, or write special VBA drivers for your own equipment. SpBasic statements can then call the VBA routines to interface with the instrument or equipment. For more information on writing custom spBasic statements, see the Custom Code page.
- The Instrument Add-In - "SpbInstrumentCore.xla"
Using the 'SendIOCommand' and 'SendIOQuery' statements
You can use the "Instrument" Excel Add-In provided with spBasic as a base for generating other add-ins, or you can use the add-in's 'SendIOCommand' and 'SendIOQuery' statements to send ASCII commands to any instrument. The spBasic program "IOCommandTest" below shows an example of the 'SendIO' statements using a RS232 serial interface to a Digital Multimeter (DMM).
You can copy the listing below and paste it into the spBasic Edit Form.
// ----- IOCommandTest -----
Inst_AddDevice "Voltage" //create a device named 'Voltage'
clear h:h // clear column 'H'
// -- configure the serial port for 9600 Baud --
Inst_ConfigSerialPort 9600,0,8,2,1,0,0,2,"SYST:REM",3
Inst_Initialize "asrl4::instr" // initialize the device for 'Com4' port
// -- set the DMM for DC, 10 volt range --
Inst_SendIOCommand "CONF:VOLT:DC 10, max"
// -- get 10 measurements with .1 sec between measurements --
for i = 1 to 10
h<<i>> = Inst_SendIOQuery("READ?") // put measurements in col 'H'
waitSec .1
next
End
|
A screen shot of the program in the Edit Form is,

- The Customized Add-In - "SpbAgilent34401A.xla"
An example of a customized Add-In is the add-in "SpbAgilent34401A.xla" for the Agilent 34401A DMM. This add-in is provided with spBasic along with the 'Instrument' add-in. The statements in this add-in are more convenient to use and provide expanded capability for this specific DMM. Since a lot of DMM's use the same standard commands, this add-in can also be used with other manufacturer's DMM's, and can also be modified through a 'Statement Text File' to fit any manufacturer's specific needs. The spBasic program "AgilentCommandTest" below shows an example of some simple statements using a GPIB interface to a Agilent 34401A DMM. This measures 50 points, .1 sec apart, and puts the results in Excel columns which are displayed on a Chart of Voltage vs. Time. You can copy the listing below and paste it into the spBasic Edit Form.
// ----- AgilentCommandTest -----
// -- Set up the meter --
a401_AddDevice "a401Dmm" // create a device
a401_Initialize "GPIB0::8::INSTR" // initialize the device to GPIB
a401_Reset
a401_ClearStatus
// ---- Immediate Trigger Example ----
clear measurements!d1:e50 // clear cells D1:E50 in worksheet 'measurements'
a401_ConfigDCVolts 10, .001 // configure to 10 V DC, .001 V resolution
a401_SetTrigSource "Imm" // set immediate trigger source
// -- read 50 points, .1 sec apart, put time in col D, measurements in col E
a401_ReadMultiPt 25, 0.1, 50, 0.1, 0, "measurements!D1", "measurements!E1"
End
|
A screen shot of the program in the Edit Form is,

List of spBasic Agilent 34401A Statements and Functions
The spBasic Help Form screen-shots below show the spBasic statements and functions included for the Agilent 34401A DMM. These can also be used for other DMM's that are compatable with the 34401A SCPI command set. If the DMM is not strictly compatable, the 'Statement Text File' associated with the add-in can be modified for the particular command set of the other DMM.





- Custom Code, using the 'Statement Text File'
You can modify the statements for the 'Instrument' or 'Agilent34401A' add-ins and create your own add-in by using a Statement Text File. To create code for your new instrument, you copy and rename the 'Instrument' or 'Agilent34401A' Statement Text File, modify the text file script, then create the add-in code using the spBasic 'Create Custom Code' command listed under the 'Tools' menu.
To create your customized Instrument Add-In,
- Create a Excel Add-In. See Custom Code - How to make an spBasic Add-In (.xla) file.
- Copy the 'Instrument' or 'Agilent34401A' Statement Text File from the "C:\Program Files\spBasic\AddIns\Instrument<or Agilent>\AddIn" directory. This file is named 'SpbInstrumentCoreText.txt' or 'SpbAgilent34401AText.txt'. Copy, move the file to another directory, and rename it. This is important, you want to keep the original file intact.
- Modify the file with the commands you need for the new instrument (see the instructions below).
- Create code for the new add-in by using the spBasic Edit Form "Tools -> Create Custom Code" menu item. See Custom Code - Creating the Code.
Modifying the Statement Text File for Instrument Control
The Statement Text File for Instrument Control is set up with SCPI strings used by the popular instruments and can be used to create Add-In's for spBasic. You can copy this file and use it to create a 'Statement Text File' for a new Instrument.
To create a 'Statement Text File' for your instrument, copy either the 'SpbInstrumentCoreText.txt' or 'SpbAgilent34401AText.txt' (whichever is more appropriate for your instrumrnt), move the file to another directory, and rename it. This is important, you want to keep the original file intact. Now you can edit the new file with the SCPI string commands for statements applicable to the instrument model you're using (see "How to Modifying the Statement Text File" below) . You can find the instrument SCPI commands in the instrument's User manual, usually under 'Remote Operation'. To add new Subs and Functions for the instrument, just copy an existing statement (such as the Sub 'Inst_Beep' or function 'Inst_GetdeviceID') and change the sub or function name, IO command string, and Help string, along with any other optional features. You must keep the core 'System' category commands such as 'AddDevice', 'ConfigSerialPort'(if applicable), 'Initialize', 'SelectActiveDevice', etc. to let you create, initialize, and select devices. A good rule of thumb is to keep all the 'Special Statement' defined by the 'Special Statement =' element. You can delete any statements or functions that are not applicable to your instrument. But remember, make a copy of this file first, don't use the original file distributed with the spBasic program.
After you've created the new 'Statement Text File', do a global Find and Replace using your text editor and replace the 'Inst_' prefix with a new short instrument prefix for your instrument (make sure you keep the '_' after the prefix, e.g., 'kg456_'). It's important that you change this prefix with a unique name for your instrument, since you can't have two Add-Ins with duplicate spBasic statements or functions.
When you're done editing the file to fit your instrument, use the file for creating the Add-In code by using the 'Tools -> Create Custom Code' menu item on the spBasic 'Edit Form'.
How to Modifying the Statement Text File - The File Elements
The Statement Text File has different elements which are used for different Add-In functions. The elements of the file, in order of appearance in the file, are given below. The examples shown are from the Agilent 34401A DMM spBasic Add-In. Look at an actual Statement Text File to see how these elements are used.
'Help Group Name = '
Example: Help Group Name = Agilent 34401A DMM
The Help Group Name element defines the name of the Help Group which appears in the 'Group' drop down list in the spBasic Help Form. The first 'Help Group Name =' line in a Statement Text File indicates the start of code generation, all 'Sub' and 'Function' statements before this line will be ignored. All statement Subs and Functions defined below the 'Help Group Name' will appear in this Help Group. If a new 'Help Group Name = ' line appears in the Statement Text File, a new Help Group will be created and all Subs and Functions appearing after that line will be included in the new Help Group.
NOTE: If a Help Group is deleted from a Statement Text File, and new Add-In code is created, you must exit and restart Excel before the Help Group is removed from the Help Form.
'Category Name = '
Example: Category Name = DC Voltage
The 'Category Name' element defines the Help category for a list of statements. The Category Name will appear before a list of statements in the Help window. If a new Category Name is defined, all statements appearing after that Category Name will appear under that Category until a new 'Category Name = ' line is defined.
'Sub Name = '
Example: Sub Name = a401_ConfigDCVolts Range, Resolution
The 'Sub Name' element creates an spBasic Statement with arguments in the form of,
Sub Name = <statement name> <argument 1>, <argument 2>, ... <argument n>
You can also use the 'optional' keyword to make the argument optional. In the statement,
Sub Name = a401_Initialize ResourceName, optional TerminationCharString
The argument 'TerminationCharString' is optional.
'Function Name = '
Example: Function Name = a401_SendIOQuery(QueryString, optional TimeOutSec)
The 'Function Name' element creates an Excel function with arguments in the form of,
Function Name = <function name>(<argument 1>, <argument 2>, ... <argument n>)
You can also use the 'optional' keyword to make the argument optional. In the statement,
Function Name = a401_SendIOQuery(QueryString, optional TimeOutSec)
The argument 'TimeOutSec' is optional. Note that parenthesis must be included around the arguments.
‘Help String =’
element. This creates Help text for the Sub or Function. See the statements in this file for Help text examples. *** NOTE: You must have a 'End Help' line at the end of each 'Help String' definition. ***
‘I/O Command String =’
Example: I/O Command String = CONF:VOLT:DC
The 'I/O Command String' element defines an Input/Output string to be sent to the device when executing the command. This is mainly used for devices controlled by SCPI commands e.g., ‘*RST’, ‘IDN?’, ‘INIT’, ‘Read?’, etc.
‘Special Statement =’
Example: Special Statement = AddDevice
The 'Special Statement' elements are special statements that are needed by the spBasic language and may be required to be included in the statement list, depending on the application. The Special Statement values must not be edited since the strings are used by spBasic to trigger operations needed by the statement.
‘Argument Type =’
Example: Argument Type = DeviceTriggerSource
The 'Argument Type' element defines a list of argument values which may be required for Statement or Function arguments. An example of this is a Trigger Source argument for a ‘SetTrigSource’ statement. For this example, the argument values can be ‘IMM’, ‘EXT’, or ‘BUS’. Any other value will cause an instrument error. The Argument Type definitions must come after all Sub or Function definitions.
An example of a statement definition which uses some of the elements defined above is shown here. The "Inst_SetTrigSource" statement sets the trigger source to Immediate, External, or Bus. This is a 'Special Statement' as defined above, has an argument of either ‘IMM’, ‘EXT’, or ‘BUS’, and has a SCPI command string of "TRIG:SOUR". The Help string that is defined appears in the spbasic Statement Help Form.
-----------------------------------------------------
Sub Name = Inst_SetTrigSource Imm_Ext_Bus
-----------------------------------------------------
====== Special SetTrigSource Statement ======
-- You can change the Function Name, but don't
change the number of arguments (1 argument) and
don't change the 'Special Statement' string below --
Special Statement = IO_SetTrigSource
=================================================
-- the line below defines the 'Imm_Ext_Bus' subroutine argument as the 'DeviceTriggerSource' type defined later --
Imm_Ext_Bus Argument Type = DeviceTriggerSource
I/O Command String = TRIG:SOUR
Help String =
This statement sets the trigger source. The trigger source determines the source of a trigger event that will cause a measurement to begin.
Parameters:
"Imm_Ext_Bus"
Trigger source input - select from the following Trigger sources,
"IMM" = SourceImmediate - The multimeter exits the Wait-For-Trigger state immediately after entering. It does not wait for a trigger of any kind.
"EXT" = SourceExternal - The multimeter exits the Wait-For-Trigger state when a trigger occurs on the external trigger input.
"BUS" = SourceSwTrigFunc - The multimeter exits the Wait-For-Trigger state when the Send Software Trigger function executes.
Immediate Triggering - In the immediate trigger mode (remote interface only), the trigger signal is always present. When you place the multimeter in the wait-for-trigger state, the trigger is issued immediately. This is the power-on trigger source for remote interface operation.
External Triggering - In the external trigger mode, the multimeter will accept a hardware trigger applied to the Ext Trig terminal. The multimeter takes one reading, or the specified number of readings (sample count), each time Ext Trig receives a low-true pulse.
Software Triggering - The software trigger mode is available only from the remote interface. This mode is similar to the single trigger mode from the front panel, but you trigger the multimeter by sending a bus trigger command.
Example:
Inst_SetTrigSource "IMM" //Sets the trigger source to immediate
End Help
|
This is an example of an Argument Type element called 'DeviceTriggerSource'.
***************************************************
Argument Type = DeviceTriggerSource
***************************************************
Value = IMM
Description = The device exits the Wait-For-Trigger state immediately after entering. It does not wait for a trigger of any kind.
I/O Command String = IMM
Value = EXT
Description = The device exits the Wait-For-Trigger state when a trigger occurs on the external trigger input.
I/O Command String = EXT
Value = BUS
Description = The device exits the Wait-For-Trigger state when the Send Software Trigger function executes.
I/O Command String = BUS
|
The text you put between "Help String =" and "End Help" appears in the Statement Help Form, which, for this statement, looks like,

‘Special Statement = Custom Code’
The 'Special Statement = Custom Code' element identifies a Sub or Function that uses ‘Custom Code’ that the user writes in VBA. When this command is used, you must write your own code for the Statement or Function. The Sub or Function lines are generated, but you must fill in your own VBA code to be executed. An example of this is given below.
The following lines define a custom code statement for a Statement Text File which will generate custom code for a subroutine named 'CustomCodeTest'. These lines are placed after a 'Help Group Name =' line which indicates the start of code generation.
-----------------------------------------------------
Sub Name = CustomCodeTest
-----------------------------------------------------
Special Statement = Custom Code
Help String =
This is a Custom Code test.
End Help
|
The 'Sub' below is a custom code routine with the proper error handler that's generated when you run the 'Tools -> Create Custom Code' menu item on the spBasic 'Edit Form'. The code is created in a workbook VBA module named 'Spb_CustomCode', which is automatically created if it doesn't already exist. The lines starting with 'MsgBox' and 'Err.Raise' were added by the user, as was the subroutine description under the 'Put subroutine description here' comment. If you run this example from the Edit Form, make sure the Edit Form is off to the side or the message box will appear behind the form and not be accessible. If this happens, just press 'Enter'. It's best create a Run button to run a program with a VBA message box or user form. You can also uncheck the Edit Form 'Keep on Top' checkbox.
Sub CustomCodeTest()
'---------------------------------------------------------------------------
' Put subroutine description here
' This example subroutine will display a message box and then raise an error which
' is displayed to the user by spBasic. You can use the 'Err.Raise' statement to display
' any error messages to the user, such as input argument out-of-range errors. An error will
' also be raised and displayed to the user if you have an execution error in your code.
'---------------------------------------------------------------------------
' -- if an error occurs, the ErrorHandler will display the error to the user --
On Error GoTo ErrorHandler
' ----- start code here -----
' *************** These lines were added by the user **********
MsgBox "An error will be raised when you press OK"
Err.Raise 5674, , "Custom code test error"
' ----- end code -----
Exit Sub
ErrorHandler:
Application.Run "SpBasicAddIn.xla!SpbCore_SetSubOrFunctionError", "CustomCodeTest", ""
End Sub
|
SpBasic Setup for Instrument Control
To set up spBasic for Instrumentation, you have to,
Install the spBasic Add-In's
See Downloads
- Install spBasic Add-In's for instructions on how to install the
add-in's.
Install VISA COM
An I/O library is software which communicates with an instrument
over USB, LAN, GPIB, RS-232 or some other physical medium. The VISA COM
I/O library is used for most apllications and is needed for spBasic. VISA
Libraries are written by equipment manufacturers to conform to a VISA
specification, and if you are already communicating to your instrument through the PC, chances are VISA COM is already installed. See Downloads - VISA
COM for information on how to install VISA COM.
Some manufacturer's instrument software drivers are not comparable with
other manufacturer's VISA implementation. Make sure all instruments are
operating properly with the installed VISA implementation on your computer.
If not, contact the instrument manufacturers to get VISA version compatability.
Example Programs
Example spBasic Add-Ins and 'Statement Text Files' for Instrument Control are included in the following directories in the free fully-functional download.
'C:\Program Files\spBasic\AddIns\Agilent\34401ADmm\AddIn\' - for the Agilent 34401A DMM and most other DMM's
'C:\Program Files\spBasic\AddIns\Agilent\E3600PwrSupplies\AddIn\' - for the Agilent E36xx Series power supplies and others
'C:\Program Files\spBasic\AddIns\Instrument\AddIn\' - for general Instrument Control
See Agilent 34401A Test Programs to view some spBasic programs used to test the Agilent 34401A DMM spBasic statements and functions.
|
Live Excel Help!
Free initial talk time, no charge until you're convinced that the expert can help you!
|