What are you looking for?
Handling Data with Instruments
This tutorial describes how to read data from an Keysight instrument or send data to an Keysight instrument. The most common type of data is a string representation of a number. However, this tutorial will show examples for both string and double representations of the data.
Note: Be sure that you connect to your instrument before testing your code. Instructions for connecting to an instrument can be found in the first three tutorials. All examples require Keysight VISA COM installed on your PC.
Reading Strings
The following code will display a string in a text box as sent by a DMM:
Dim strData As String instrument.WriteString "Meas?" strData = instrument.ReadString() Text1.Text = strData
The command WriteString tells the instrument to see if there is a measurement to be recorded. The command ReadString writes the reply of the Meas? query as a string into strData. The data is written to a text box that you create.
Writing Strings
If you have a string to begin with, writing a string to an instrument is straightforward. The following code writes a string to the instrument display:
instrument.WriteString ":syst:beep;:disp:text 'LOVE AGILENT'"
Note that in this case, the string must be in between single quotes in order for the instrument to recognize the string. Alternately, you may set the display text to the expression in a text box for more flexibility. The ReadString and WriteString commands are used with IEEE488 ASCII strings and they must follow the 488.2 ASCII string rules. For example:
instrument.WriteString ":syst:beep;:disp:text " & "'" & Text2.Text & "'"
Reading Numbers
Converting the string representation into number format (i.e. double, single, integer or long) is relatively simple. Change the type of the data variable to the type of preference. For example:
Dim dblData As Double Dim strReply As String instrument.WriteString "Meas?" strReply = instrument.ReadString() dblData = Val(strReply) Text3.Text = dblData
Notice that we use the Val method with the ReadString command. This is needed in order to stay consistent with a computer's geographic setting. Depending on your geographical location and computer language preferences, different countries have different data syntax. The Val command translates a string into a double consistent with the instrument use of a decimal radix. Consult later tutorials for more information concerning this localization issue.
Writing Numbers
Use Str$ (string_name) to convert a double to a string.
instrument.WriteString ":syst:beep;:disp:text " & "'" & Str$(14.2) & "'"
Similar to ReadString and WriteString, there are also ReadNumber and WriteNumber commands. The following code writes 0.09 to the null register of a DMM, reads back that number and displays it in a textbox.
Dim dblNum As Double Dim offset As Double offset = 0.09 With instrument .WriteString "CALC:FUNC null" .WriteString "CALC:STAT on" .WriteString "CALC:NULL:OFFS ", False .WriteNumber offset, VisaComLib.IEEEASCIIType.ASCIIType_BSTR .WriteString "CALC:NULL:OFFS?" dblNum = .ReadNumber() End With Text4.Text = dblNum
In this example, we wrote the double offset to the instrument using the WriteNumber command. Before writing the number to the DMM, the DMM should be set up to ensure proper execution. The .WriteString "CALC:FUNC null" and .WriteString "CALC:STAT on" lines set up the DMM by enabling the math function. Both of these lines are executed in memory.
The most interesting line in the code, .WriteString "CALC:NULL:OFFS ", False, writes a null value to the null register that reserves space to write the number offset. The second argument in WriteString indicates whether or not to flush the first argument's register. By not flushing the register, we are able to use WriteNumber to add offset as a string to the previous command.
Finally, the .WriteString "CALC:NULL:OFFS?" code checks to see if there is a value in the null register, and the ReadNumber command reads that value. The code should display 0.09 in a text box. Consult your instrument's user guide for more information about operation commands.