📜 ⬆️ ⬇️

Controlling Tektronix oscilloscopes from Visual Studio

You rarely encounter such tasks, but if this happens, it is very pleasant to read a comprehensive article that will help you quickly start productive work, rather than breaking your head all day with the questions “What to download?”, “Where to find?”, “How does it work at all?” . I have a similar task in the development of an automated test bench for boards. In the process of solving it, a number of problems arose, and there is not enough information on the Internet on this subject due to the narrowness of the problem. This article describes the main points in as much detail as possible and for getting started quickly with Tektronix oscilloscopes from Visual Studio.

First, you need to download and install the TekVISA driver (you will need to register, otherwise you will not be able to download the necessary drivers).

After installing the TekVISA driver, you need to go to the C: \ Windows \ assembly directory, this is where the TekVISANet.dll library is needed to work . Next, add the library to the project (Project> Add Reference ...> Browse ...) and ... that's it!

And it would seem that everything is so simple! However, half of the working day was spent in order to understand where this miracle library was saved. After all, following the voice of reason, you expect to find it in completely different directories, for example, C: \ Program Files (x86) \ IVI Foundation or C: \ Program Files (x86) \ Tektronix.
')
Interact with the oscilloscope as easy as possible. For example, the following piece of code allows you to connect to a device and read its identification data:

using System; using System.Collections; using TekVISANet; namespace TekVISAExample { class Program { static void Main(string[] args) { string response; ArrayList resources = new ArrayList(); VISA tekVISA = new VISA(); tekVISA.FindResources("?*", out resources); Console.WriteLine("RESOURCES:"); foreach (string s in resources) Console.WriteLine(s); tekVISA.Open(resources[0].ToString()); tekVISA.Write("*IDN?"); bool status = tekVISA.Read(out response); if (status) Console.WriteLine("IDN (device information): {0}", response); Console.ReadKey(); } } } 

OUTPUT:
RESOURCES:
USB0 :: 0x0699 :: 0x0376 :: C011053 :: INSTR
ASRL7 :: INSTR
ASRL8 :: INSTR
IDN (device information):
TEKTRONIX, MSO2012, C011053, CF: 91.1CT FV: v1.52 DPO2COMP: V1.00

To communicate with the device you need to use the SCPI (Standard Commands for Programmable Instruments) commands. You can get acquainted with them here: sdphca.ucsd.edu/Lab_Equip_Manuals/SCPI-99.pdf . However, all devices have different settings and functionality, so you need to find a set of SCPI commands that apply specifically to your particular device. As a rule, they can be found in the Programmer Manual device.

You can also download the OpenChoice Desktop free application for Tektronix oscilloscopes. After downloading and installing, you need to connect the device to the computer (USB), start the application, go to the Get & Send Settings tab and click the Get Settings button. All available commands for setting up the device with their current parameters will appear on the screen.

Example 1. Measuring the RMS value from the second channel:

 using System; using System.Collections; using TekVISANet; namespace TekVISAExample { class Program { private const string OSCILLOSCOPE = "USB0::0x0699::0x0376::C011053::INSTR"; static void Main(string[] args) { VISA tekVISA = new VISA(); ArrayList resources = new ArrayList(); tekVISA.FindResources("?*", out resources); bool opened = false; if (resources.Contains(OSCILLOSCOPE)) opened = tekVISA.Open(OSCILLOSCOPE); if (opened) { ///  2 tekVISA.Write("CH2:COUPLING AC"); tekVISA.Write("CH2:SCALE 20.000E-3"); tekVISA.Write("CH2:BANDWIDTH 20.000E+6"); ///   ? tekVISA.Write("MEASUREMENT:MEAS1:TYPE RMS"); tekVISA.Write("MEASUREMENT:MEAS1:SOURCE1 CH2"); tekVISA.Write("MEASUREMENT:MEAS1:STATE ON"); ///    RMS tekVISA.Write("MEASUREMENT:MEAS1:VALUE?"); string response = ""; ///  tekVISA.Read(out response); Console.WriteLine("RMS CH2 VALUE: {0}", response); } Console.ReadKey(); } } 

Example 2. Obtaining a waveform in the form of points saved in a .csv file for further use:

 using System; using System.Linq; using System.Collections; using System.Globalization; using TekVISANet; using System.Threading; namespace TekVISAExample { class Program { private const string OSCILLOSCOPE = "USB::0x0699::0x0376::C011053::INSTR"; static void Main(string[] args) { Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US"); VISA tekVISA = new VISA(); ArrayList resources = new ArrayList(); tekVISA.FindResources("?*", out resources); bool opened = false; if (resources.Contains(OSCILLOSCOPE)) opened = tekVISA.Open(OSCILLOSCOPE); if (opened) { NumberStyles styles = NumberStyles.AllowExponent | NumberStyles.Number; string response = ""; tekVISA.Write("DATA:SOU CH2"); tekVISA.Write("DATA:WIDTH 1"); tekVISA.Write("DATA:ENC ASCII"); tekVISA.Write("DATA:STOP 5208"); tekVISA.Query("WFMPRE:YMULT?", out response); response = response.Replace(":WFMPRE:YMULT ", ""); float ymult = float.Parse(response, styles); tekVISA.Query("WFMPRE:YZERO?", out response); response = response.Replace(":WFMPRE:YZERO ", ""); float yzero = float.Parse(response, styles); tekVISA.Query("WFMPRE:YOFF?", out response); response = response.Replace(":WFMPRE:YOFF ", ""); float yoff = float.Parse(response, styles); tekVISA.Query("WFMPRE:XINCR?", out response); response = response.Replace(":WFMPRE:XINCR ", ""); float xincr = float.Parse(response, styles); tekVISA.Write("CURVE?"); tekVISA.Read(out response); response = response.Replace(":CURVE ", ""); sbyte[] rawwave = response.Split(',').Select(n => Convert.ToSByte(n)).ToArray(); float[] wave = new float[rawwave.Count()]; for (int j = 0; j < rawwave.Count(); j++) wave[j] = (rawwave[j] - yoff) * ymult + yzero; System.IO.StreamWriter file = new System.IO.StreamWriter("waveform.csv"); file.WriteLine("V,S"); for (int j = 0; j < wave.Count(); j++) { float timepoint = j * xincr; file.WriteLine(wave[j].ToString() + "," + timepoint.ToString()); } file.Close(); tekVISA.Close(); } Console.ReadKey(); } } } 

Source: https://habr.com/ru/post/271641/


All Articles