C# Remoto control protocol 3 – USB

December 23, 2020

This website summarizes how to write communication programs necessary for measuring instrument control for those who have a general understanding of C # introduction.

In addition to this article, there are the following articles.

In this article, I will show you how to make a USB connection in C # with a Keysight instrument.

USB has become the most major interface now. If you want to communicate via USB connection, you don’t need to buy anything. However, programming USB with C # is not easy.

This is because USB is not originally an interface for communication with measuring instruments, so there are a lot of methods of controlling external devices using USB.

Under these circumstances, Keysight (formerly Agilent (formerly HP)), a leading manufacturer of measuring instruments, equips most new products with a USB port, providing an easy way to control the USB connection.

Instead, GPIB has become less aggressively developed.

In this article, we’ll limit ourselves to Agilent devices and show you how to control external devices via a USB connection.

1 Installation of USB library suite

In order to control Keysight’s devices from a windows computer, it is necessary to install the Keysight IO library suite provided by Keysight.

The IO Library Suite is included with the instrument or can be downloaded from Keysight’s website.


The IO Library Suite contains drivers for almost all Keysight USB-enabled devices.


It should be , but it seems that some libraries are omitted in the minor version of the IO library suite.


Before installing the IO library suite, it seems necessary to check if it is compatible with your device.


Once you have the installation file, run it to install. The default settings seem to be okay, but the installation will take some time. Let’s wait with heart.

2 USB connection via C #

The sample code is installed when you install the IO Library Suite.

Depending on the version, I was under C: \ Users \ Public \ Documentation National Instruments.

You can find out the procedure by digging inside this project.

In addition, there was an explanation of USB and C # connection in the material of Keysight.

Please also refer to this.

Place rich text boxes and buttons on the form as you would a GPIB communication program.

2-1 Addition of reference

First, add a reference from Solution Explorer at the top right of the screen.

Right-click on the reference item and select Add Reference …

The referenced file is Ivi.Visa.Interop.dll.

C:\Windows\assembly\GAC_64\Ivi.Visa.Interop\5.9.0.0__a128c98f1d7717c1\

2-2 Addition of namespace

Add the following line to the end of the group of using ~ at the beginning of the file.

using Ivi.Visa.Interop;

Once this is done, add the following line immediately after the {in the public partial class Form1: Form just below.

private ResourceManager ioMgr = new ResourceManager ();
private FormattedIO488 instrument = null;
string usbAdd = "USB0 :: 2399 :: 36999 :: MY59999999 :: 0 :: INSTR";

The spell behind usbAdd is to be displayed on the USB device itself, so read the manual, display it, and copy it. It depends on the device.

Then double-click the button in the designer to create button1_Click and move the cursor there. Write the following line inside {}.

instrument.IO = (IMessage)ioMgr.Open(usbAdd);
instrument.WriteString("*IDN?");
richTextBox1.Text = instrument.ReadString();
instrument.IO.Close();

As for the code above, the first line is preparing for communication.

The second line sends the * IDN? Command to the external device. This is a command to inquire about the external device ID.

The third line displays the reply from the external device in the rich text box.

Communication ends on the 4th line.

Normally, try / catch is used to prepare for communication errors.

The whole thing looks like this:

using System;
using System.Windows.Forms;
using Ivi.Visa.Interop;

namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
private ResourceManager ioMgr = new ResourceManager();
private FormattedIO488 instrument = null;
string usbAdd = "USB0::2399::36999::MY59999999::0::INSTR";

public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
try
{
instrument.IO = (IMessage)ioMgr.Open(usbAdd);
instrument.WriteString("*IDN");
richTextBox1.Text = instrument.ReadString();
instrument.IO.Close();
}
catch
{
MessageBox.Show("error");
}
}
}
}

Did the ID of the external device appear in the rich text box when executed?

3 Summary

In this article, I introduced how to communicate with an external device connected via USB in C #.

Same as GPIB, but the command uses only * IDN ?. Other commands will be introduced again.

Programing

Posted by naozane