Home › Forums › HTBasic Forum › GPIBNI, VISA, 34401A and *OPC? failing › Reply To: GPIBNI, VISA, 34401A and *OPC? failing
April 19, 2025 at 10:59 am
#9486
I reimplemented this in C# using the NI VISA libraries and the code works as expected:
using Ivi.Visa;
using NationalInstruments.Visa;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.ConstrainedExecution;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace SRQTest
{
internal class Program
{
public static GpibSession gpibSession;
public static NationalInstruments.Visa.ResourceManager resManager;
public static int gpibIntAddress = 14; // 34401A
public static string gpibAddress = string.Format("GPIB0::{0}::INSTR", gpibIntAddress);
public static SemaphoreSlim srqWait = new SemaphoreSlim(0, 1); // use a semaphore to wait for the SRQ events
static void Main(string[] args)
{
// Setup the GPIB connection via the ResourceManager
resManager = new NationalInstruments.Visa.ResourceManager();
// Create a GPIB session for the specified address
gpibSession = (GpibSession)resManager.Open(gpibAddress);
gpibSession.TimeoutMilliseconds = 8000; // Set the timeout to be 4s
gpibSession.TerminationCharacterEnabled = true;
gpibSession.Clear(); // Clear the session
gpibSession.ServiceRequest += SRQHandler;
SendCommand("*RST"); // Reset the DMM
SendCommand("*CLS"); // Clear the DMM status registers
SendCommand("*ESE 1"); // Enable "operation complete" bit to set "standard event" bit in status byte
SendCommand("*SRE 32"); // Enable "standard event" bit in status byte to pull the IEEE-488 SRQ line
var opcResult = QueryString("*OPC?"); // Assure synchronization
Console.WriteLine($"OPC Result: {opcResult}"); // Print the result of the OPC query
SendCommand("CONF:VOLT:DC 10"); // Set DMM to 10 volt DC range
SendCommand("VOLT:DC:NPLC 10"); // Set the integration time to 10 PLCs
SendCommand("TRIG:COUN 100"); // DMM will accept 100 triggers
SendCommand("INIT"); // Place DMM in "wait-for-trigger" state
SendCommand("*OPC"); // Set "operation complete" bit in standard event
// Wait for the SRQ event
srqWait.Wait(); // Wait for the SRQ event to be signaled
// Read the data
string response = QueryString("FETCH?");
Console.WriteLine($"1. Response: {response}");
// Clear the DMM status registers
SendCommand("*CLS");
}
public static void SRQHandler(object sender, Ivi.Visa.VisaEventArgs e)
{
// Read the Status Byte
var gbs = (GpibSession)sender;
StatusByteFlags sb = gbs.ReadStatusByte();
Console.WriteLine($"1. SRQHandler - Status Byte: {sb}");
gpibSession.DiscardEvents(EventType.ServiceRequest);
SendCommand("*CLS");
srqWait.Release();
}
static private void SendCommand(string command)
{
gpibSession.FormattedIO.WriteLine(command);
}
static private string ReadResponse()
{
return gpibSession.FormattedIO.ReadLine();
}
static private string QueryString(string command)
{
SendCommand(command);
return (ReadResponse());
}
}
}
Looking at the NI IO Trace Utility, I can see that the HTBasic program differs in that it doesn’t add a 0A to the end of the *OPC? string – Adding END to the OUTPUT statement fixes that and I can continue on but I now run into a problem on line 270 with HT Basic giving an Error 170 “I/O operation not allowed”:
270 ENABLE INTR Hpib;Mask ! Enable SRQ to interrupt the program
If I swap to the GPIBNI driver then the program works successfully.
TonyG