GPIB programmng in C# for 34401A Digital Multi Meter.

2020年9月9日

The 34401A is a standard digital multimeter from Keysight Technology. In this article, I will explain GPIB programming to control 34401A with C #.

I have compiled a set of C# programs to use for instrument control, so if you haven’t seen it yet, please check.

In this post, I used “*IDN?” for the measurement command. This *IDN? Command is a standard GPIB command and returns instrument information.

This command can be used on most devices. This time I will show you how to use the commands specific to the 34401A.

Command for 34401A

When programming in C#, use the “MEAS” command. This command performs setting, measurement, and return of results.

In addition to the MEAS command, the 34401 has highly versatile commands such as “CONF → READ?” and “CONF → INIT → FETC?” which have the same function, but the MEAS command can do the same thing.

Write the voltage, current, resistance and direct current or alternating current behind the MEAS.

• DC voltage measurement – MEAS: VOLT: DC?
• AC current measurement – MEAS: CURR: AC?
• Resistance measurement – MEAS: RES?

If you want an answer, add “?”. And write the command in mbSession.query (” “). For example, to display the result of DC voltage measurement in a rich text box

richTextBox1.Text = mbSession.Query("MEAS:VOLT:DC?");


Sample code

As an example, I made sample code of 34401A GPIB control. It is written on the assumption that the driver of NI is installed.

Example 1 Measure the voltage when the button is pressed

The sample below is a program that measures when buttonStart is pressed. Place buttonStart and richTextBox1 on the designer screen.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NationalInstruments.VisaNS;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private MessageBasedSession mbSession;

public Form1()
{
InitializeComponent();
}

private void buttonStart_Click(object sender, EventArgs e)
{
try
{
mbSession = (MessageBasedSession)ResourceManager.GetLocalManager().Open("GPIB0::20::INSTR");
richTextBox1.Text = mbSession.Query("MEAS:VOLT:DC?") + "n";
mbSession.Dispose();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}



Example 2 Measure voltage every fixed time

The following example uses a timer to repeat the measurement every 2000 milliseconds. When the start button is pressed, it stops at the stop button.

Please place timer1 on the designer screen.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NationalInstruments.VisaNS;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private MessageBasedSession mbSession;

public Form1()
{
InitializeComponent();
}

private void buttonStart_Click(object sender, EventArgs e)
{
timer1.Interval = 2000;
timer1.Enabled = true;
}

private void buttonStop_Click(object sender, EventArgs e)
{
timer1.Enabled = false;
}

private void timer1_Tick(object sender, EventArgs e)
{
try
{
mbSession = (MessageBasedSession)ResourceManager.GetLocalManager().Open("GPIB0::20::INSTR");
richTextBox1.Text += mbSession.Query("MEAS:VOLT:DC?");
mbSession.Dispose();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}


I think that the result is displayed in richTextBox1 as follows.

Summary

Once you understand the above code, you can create a large program just by adding some features or changing the layout of the display.