Programming in C# – GPIB interface and instruments.

2020年1月15日

GPIB is the oldest communication interface among instruments, so it is probably equipped with the largest number of instruments.

Therefore, most people still use GPIB. This article shows you how to communicate with an external device using GPIB connection in C#.

About GPIB

The most used GPIB interface is the GPIB-USB-HS from Natinal Instruments (NI). There are other interfaces from Keysight Technology, but I can not find much information.

The amount of information about programming in GPIB is small, and programming is difficult. In this blog, I would like to explain how to make a GPIB program as easy as possible.

Installation of driver

In this article, GPIB interface is assumed to use GPIB-USB-HS.

It is necessary to install the driver before using GPIB-USB-HS. If you have a purchased CD, you can use that CD.

If you do not have it, download the NI-488.2 driver from the NI company’s HP.

Download the OS and version from the NI site. Once downloaded, run NI-488.3.


During the installation, you have a choice of features to install. At this time, be sure to install .NET Framework 4 or later. .NET Framework is difficult to find in the list, you may not be able to install it without noticing it.

Please install with default setting except this.

Add class library

Start Visual Studio C# and open a new project.
At the top right of the project in “Solution” in “Solution Explorer tree” there is a category called “Reference Settings”.

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

To use the NI GPIB library, add NationalInstruments.Common and NationalInstruments.VisaNS to your project. These two files are located in different places depending on the driver and the version of windows, so please search in windows.

Add code

Move to the code editor screen.

Since the line of using … is lined up at the top of the file, add the following line at the bottom.

using NationalInstruments.VisaNS;

Next we go back to the designer and add ritchTextBox and button.

Double-click button1 to move to the code editor. Add the following line under the public partial class Form1: Form in the code editor.

private MessageBasedSession mbSession;

This is a class used for GPIB communication, and declares the class. Next, write the following 3 lines in button1_Click.

mbSession=(MessageBasedSession)ResourceManager.GetLocalManager().Open("GPIB0::17::INSTR");
string responseString = mbSession.Query("*IDN?");
mbSession.Dispose();

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

In the above example, the GPIB address is 17 but change 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 can be used commonly for all GPIB standard commands.

When the external device receives this command, the device name and serial number will be returned.

The reply from the external device is assigned to the argument of mbSession.Query.

Communication is terminated with the mbSession.Dispose ().

Normally, the execution part of the communication is bound by try in preparation for sudden interruption. It will look like this when viewed through the program so far.

Use “try” to prepare for a sudden communication outage.

The program so far is as follows.

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("通信エラー");
            }
        }
    }
}

Summary

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

This time, I explained only the part that communicates with the external device in C#, for the time being the command of the device.

Next time, I will explain the specific programming by limiting the model of the external device.