Step-by-Step Examples C#
Add Reference (Install Nuget Package)
To perform an automated measurement, add an Automation Interface Reference first. This first step is independent of the .NET or COM* compatible programming language you use.
In the following, we explain how to add a Bode Automation Interface Reference using C#.
First a reference is needed. Add therefore the reference to your project to access the OmicronLab.VectorNetworkAnalysis.AutomationInterface.dll.
To install the OmicronLab.VectorNetworkAnalysis.AutomationInterface NuGet package in Visual Studio 2012 or higher, run the following command in the Package Manager Console
PM> Install-Package OmicronLab.VectorNetworkAnalysis.AutomationInterface
The Bode Automation Interface References for other .NET or COM compatible languages, such as MatLab, Python, Excel, Java, and more, can be found in the Add Reference. section.
Note
Using COM is not recommended for new developments. We recommend using the SCPI interface.
After successfully adding the reference, execute the following code to gain access to the device and start a measurement. See the following chapters for the C# step-by-step example.
Step-by-Step example to perform a simple measurement
This chapter describes how to perform a simple step-by-step measurement using C#. After adding the Bode Automation Reference, proceed with the following steps.
Defining the usings serves as the starting point. A using directive allows to use types defined in a namespace without specifying the fully qualified namespace of that type.
//Include the usings
using OmicronLab.VectorNetworkAnalysis.AutomationInterface;
using OmicronLab.VectorNetworkAnalysis.AutomationInterface.Interfaces;
using OmicronLab.VectorNetworkAnalysis.AutomationInterface.Interfaces.Measurements;
using OmicronLab.VectorNetworkAnalysis.AutomationInterface.Enumerations;
using OmicronLab.VectorNetworkAnalysis.AutomationInterface.DataTypes;
Afterwards, create the Bode100AutomationInterface object. This is a precondition for using the Bode Automation Interface.
//Create and initialize a bodeAutomationInterface object.
BodeAutomationInterface automationInterface = new BodeAutomation();
After the initialization, search for free devices and connect to an available device.
Note
It is impossible to influence the ordering of the devices found since this is related to the device drivers.
//Search for free devices and connect to the first available device.
BodeDevice bode = auto.Connect();
Auto-connect searches for free devices and connects automatically to the first available device. Alternatively, search for free devices using the ScanForFreeDevices () function. Similarly, the ScanForAttachedDevices () function allows to find all devices attached to the PC, even if they are busy with another measurement. When using these device searching functions, it is required to manually connect to one of the available devices. Therefore, use the ConnectWithSerialNumber(string SerialNumber) function.
//Find all free devices connected to the PC.
string[] devices = automationInterface.ScanForFreeDevices();
//Connect to the second device found.
BodeDevice bode = automationInterface.ConnectWithSerialNumber(devices[0]);
Note
Do not execute the search and connect to a device code directly in the GUI thread.
Connecting to a Bode 100 for the first time can take up to 1.5 minutes.
Bode 100 will then execute the internal device calibration.
When the connection is successful, start to define a measurement type. There are three Measurement Categories available. In this example, a S21 Measurement from the category Transmission is selected. The selected Transmission measurement measures S21 using the internal reference.
//Create a S21 Measurement
S21Measurement s21Measurement = bode.Transmission.CreateS21Measurement();
From the measurement type object, configure the measurement sweep now. Therefore, define its frequency range. This involves defining the Start Frequency, Stop Frequency, Number of Points to measure, and the scale of the frequency points (Linear or Logarithmic). The parameter restrictions are in the Definitions section.
//Configure Frequency Points, StartFrequency 1kHz, StopFrequency 22MHz, 201 Points, Logarithmic scale
s21Measurement.ConfigureSweep(1000, 22000000, 201, SweepMode.Logarithmic);
The next step is defining all necessary measurement settings.
Note
Every measurement configures default values, such as Termination, Resistance, and Attenuation. For the exact values and more information, see the Measurement Modes & Default Values section.
//Set the source level of the output to 0dBm
s21Measurement.SetSourceLevel(0, LevelUnit.dBm);
//Set the maximum receiver bandwidth to 1kHz
s21Measurement.ReceiverBandwidth = ReceiverBandwidth.kHz1;
//Set the Attenuation level of Channel one and two to 10dB
s21Measurement.Attenuation.Channel1 = Attenuator.dB10;
s21Measurement.Attenuation.Channel2 = Attenuator.dB10;
//Set the Termination impedance of Channel 2 to 50Ohm
s21Measurement.TerminationChannel2 = Termination.Ohm50;
After configuring the measurement, execute it. The ExecuteMeasurement function will return the Execution State as a result.
//Execute the measurement
ExecutionState state = s21Measurement.ExecuteMeasurement();
For error handling, we recommend implementing an execution state controlling using the following code.
//Check if execution state is correct.
if (state != ExecutionState.Ok)
{
//An error happened
// If the measurement is aborted here don’t forget to shutdown the Bode
device.Shutdown();
//...
return;
}
After checking the state, get (read) the results. Different results types are available. See the Result Types section for more information on the other result types and how to get results in general.
//Get the results in a form of an e.g. array either as:
//Frequencies (measurement points) and measured Magnitude result values in dB and measured Phase result values in °
double[] frequencies = s21Measurement.Results.MeasurementFrequencies;
double[] magnitudes = s21Measurement.Results.Magnitude(MagnitudeUnit.dB);
double[] phase = s21Measurement.Results.Phase(AngleUnit.Degree);
If the device is no longer required, always shut down the device using the ShutDown() function.
//This is the most important part. Always disconnect and shut down the Bode 100 device to release it for future connection
device.ShutDown();
S21 Measurement Example in C#
In following example shows the full code from the step-by-step example in C#.
//Include the usings
using OmicronLab.VectorNetworkAnalysis.AutomationInterface;
using OmicronLab.VectorNetworkAnalysis.AutomationInterface.Interfaces;
using OmicronLab.VectorNetworkAnalysis.AutomationInterface.Interfaces.Measurements;
using OmicronLab.VectorNetworkAnalysis.AutomationInterface.Enumerations;
using OmicronLab.VectorNetworkAnalysis.AutomationInterface.DataTypes;
//Create and initialize a bodeAutomationInterface object.
BodeAutomationInterface auto = new BodeAutomation();
//Search for free devices and connect to the first available device.
BodeDevice bode = auto.Connect();
//From the connected device you can now start with defining measurement type.
//In this example, a S21 Measurement from the category Transmission is selected.
S21Measurement s21Measurement = bode.Transmission.CreateS21Measurement();
//From the measurement type object you can now configure measurement sweep
////Configure Frequency Points, StartFrequency 1kHz, StopFrequency 22MHz, 201 Points, Logarithmic scale
s21Measurement.ConfigureSweep(1000, 22000000, 201, SweepMode.Logarithmic);
//The next step is defining all necessary measurement settings.
//Set the source level of the output to 0dBm
s21Measurement.SetSourceLevel(0, LevelUnit.dBm);
//Set the maximum receiver bandwidth to 1kHz
s21Measurement.ReceiverBandwidth = ReceiverBandwidth.kHz1;
//Set the Attenuation level of Channel one and two to 10dB
s21Measurement.Attenuation.Channel1 = Attenuator.dB10;
s21Measurement.Attenuation.Channel2 = Attenuator.dB10;
//Set the Termination impedance of Channel 2 to 50Ohm
s21Measurement.TerminationChannel2 = Termination.Ohm50;
//Execute the measurement
ExecutionState state = s21Measurement.ExecuteMeasurement();
//For error handling, we recommend implementing an execution state controlling using the following code.
//Control loop about the executed measurement
if(state != ExecutionState.Ok)
{
//Error
//Handle error
//shutdown Bode 100 if you abort the measurement.
//bode.Shutdown();
return;
}
//Get the results in a form of an e.g. array either as:
//Frequencies (measurement points) and measured Magnitude result values in dB and measured Phase result values in °
double[] frequencies = s21Measurement.Results.MeasurementFrequencies;
double[] magnitudes = s21Measurement.Results.Magnitude(MagnitudeUnit.dB);
double[] phase = s21Measurement.Results.Phase(AngleUnit.Degree);
//This is the most important part. Always disconnect and shut down the Bode 100 device to release it for future connection
device.ShutDown();
Note
The Bode Automation Interface References for other .NET or COM* compatible languages as MatLab, Python, Excel, Java and more languages can be found in the Add Reference section. Using COM is not recommended for new developments. We recommend using the SCPI interface.