C# Remoto control protocol 2 – GPIB

October 5, 2021

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.

This article will show you how to communicate with an external device in C # using a GPIB connection. GPIB is probably the oldest communication interface for measuring instruments, so it is probably installed in most measuring instruments. As a result, most people are still using GPIB.

1 About GPIB information

Is it the GPIB-USB-HS of Natinal Instruments (NI) that has become the de facto standard for GPIB as of 2018? Other famous companies include Keysight. People who wanted to program GPIB would have searched Google, but I’m surprised at the lack of information. The only site in Japanese that describes the C # and GPIB programs is this site from NI.( The information was updated. 2020.12)

NI-VISA .NET Library

So that you don’t have to look up the same thing over and over again, I’d like to explain in this blog as easy as possible to make your GPIB program the shortest distance possible.

2 Driver installation

This article assumes that the GPIB interface uses GPIB-USB-HS. You need to install the driver before using GPIB-USB-HS. If you have the original CD, you can use it.

If you don’t have one, download the driver NI-488.2 from NI’s HP.

NI-488.2 is not like NI-488.3. There is no mistake because there is only NI-488.2. I think it would be nice to have a simpler name.

Select the OS and version from the NI site and download. (The latest is 130MB)

After downloading, run NI-488.3.

There is a selection of functions to be installed during the installation.

At this time, be sure to install the latest version of .NET Framework.

Since you are deep in the hierarchy, you may pass by without noticing it.

Other than this, please install with the default settings.

3 Addition of class library

Start Visual Studio and open a new project. There is a category called Reference Settings in the Applications in Solutions in the Solution Explorer tree at the top right of the project.

To add a new reference, right-click on the References category and select Add Reference.

To use NI’s GPIB library, add NationalInstruments.Common and NationalInstruments.VisaNS to your project. The location of these two files differs depending on the driver and the version of windows, so search for them in windows.

4 Addition of code

Go to the code editor screen.

Since the lines of using ~ are lined up at the top of the file, add the following line at the bottom.

using National Instruments.VisaNS;

If there is no red line after writing the above line, there is probably no problem.

Next, go back to the designer and add a pitchTextBox and a button.

Double-click button1 to move to the code editor.

Add the following line under public partial class Form1: Form in the code editor.

private MessageBasedSession mbSession;

This is the class used for GPIB communication, and the above declares the class.

Next, write the following three lines in button1_Click etc.

mbSession=(MessageBasedSession)ResourceManager.GetLocalManager().Open("GPIB0::17::INSTR");

string responseString = mbSession.Query("*IDN?");

mbSession.Dispose();

The first line of the above code prepares (Opens) communication with an external device via GPIB.

In the above example, the GPIB address is 17, but change it according to the settings of the external device.

Next, send a command to the external device with mbsession.Query ().

In the above example, we sent the * IDN? Command.

This is a standard GPIB command that can be used with any device.

When the external device receives this command, it will return the device name and serial number.

The reply from the external device becomes an argument of mbSession.Query as it is and is assigned to responseString.

The last mbSession.Dispose () terminates the communication.

Normally, the execution part of communication is wrapped in try in case of sudden interruption. It looks like this when displayed through the programs up to this point.

using System;
 ・
 ・
using System.Windows.Forms;
using NationalInstruments.VisaNS;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private MessageBasedSession mbSession;
 
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                mbSession = (MessageBasedSession)ResourceManager.GetLocalManager().Open("GPIB0::17::INSTR");
                richTextBox1.Text = mbSession.Query("*IDN?");
                mbSession.Dispose();
            }
            catch
            {
                MessageBox.Show("Error");
            }
        }
    }
}

5 Execution

It’s finally done.

When you run this program, you can get information about external devices and firmware version.

Summary

In this article, I introduced how to communicate between C # and external devices via GPIB.

This time, as for the command of the device, I explained only the part that communicates with the external device in C #, so I have not been able to measure with the external device yet.

Next time, I will explain specific programming by limiting the models of external devices.

If you create a new project and create a program, you may get an error saying “Insufficient assembly reference."

This may be resolved by right-clicking on the application and selecting Properties and changing the framework within it from “.NET Framework 4 Client Profile" to “.NET Framework 4".