Instrument Control (GPIB, Serial, VISA, IVI)

cancel
Showing results for 
Search instead for 
Did you mean: 

Anyone used C# and GPIB?

If anyone has an example, I would really appreciate it.
0 Kudos
Message 1 of 7
(11,078 Views)
The easiest way to use GPIB from managed environment is to use VISA COM library. NI-VISA 3.0 supports VISA COM. In your C# project, select "Add Reference" menu, then go for "COM" tab, and then select VISA COM type library (GlobMgr.DLL). Now an interop assembly for VISA COM will be generated and added to your project.

Here's the C# example for VISA COM:

-----

using VisaComLib;

private void button1_Click(object sender, System.EventArgs e)
{

ResourceManager rm;
IMessage session = null;
string strRd;

try
{
rm = new ResourceManager()

seesion = (IMessage)rm.Open(
"GPIB0::1::INSTR",
AccessMode.NO_LOCK,
0,
"" );

session.WriteString("*IDN?");
strRd = session.ReadString(256);
}
Catch( Ru
ntime.InteropServices.ComException exp)
{
MessageBox.Show(
exp.Message, "Error",
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation );
}
finally
{
session.Close();
}

}
Message 2 of 7
(11,076 Views)
Thanks for the help!

After not getting any replies for a couple days, I decided to attack the problem using the GPIB-32 dll.

I'm not sure what's better, using COM or DLL's functions directly, but my C# GPIB wrapper seems to be working out well so far.

In case someone wants to go that route, here's my wrapper class so far:

public class GPIB
{

[DllImport("GPIB-32.dll")]
public static extern int ibask(
int UnitDescriptor,
int Option,
System.IntPtr Value
);

[DllImport("GPIB-32.dll")]
public static extern int ibbna(
int UnitDescriptor,
[MarshalAs(UnmanagedType.LPStr)] char[] BName
);

[DllImport("GPIB-32.dll")]
public static extern int ibcac(
int UnitDescriptor,
int IsSynchronous
);

[DllImport("GPIB-32.dll")]
public static extern int ibclr(
int DeviceDescriptor
);

[DllImport("GPIB-32.dll")]
public static extern int ibcmd(
int UnitDescriptor,
byte[] CommandBuffer,
long Count
);

[DllImport("GPIB-32.dll")]
public static extern int ibcmda(
int UnitDescripor,
byte[] CommandBuffer,
long Count
);

[DllImport("GPIB-32.dll")]
public static extern int ibconfig(
int UnitDescriptor,
int Option,
int Value
);

[DllImport("GPIB-32.dll")]
public static extern int ibdev(
int BoardIndex,
int PrimaryAddress,
int SecondaryAddress,
int TimeOut,
int eot,
int eos
);

[DllImport("GPIB-32.dll")]
public static extern int ibdma(
int UnitDescriptor,
int EnableDMA
);

[DllImport("GPIB-32.dll")]
public static extern int ibeos(
int UnitDescriptor,
int EnableEOS
);

[DllImport("GPIB-32.dll")]
public static extern int ibeot(
int UnitDescriptor,
int EnableEOT
);

[DllImport("GPIB-32.dll")]
public static extern int ibfind(
[MarshalAs(UnmanagedType.LPStr)] char[] UnitName
);

[DllImport("GPIB-32.dll")]
public static extern int ibwrt(
int UnitDescriptor,
byte[] WriteBuffer,
long count
);

[DllImport("GPIB-32.dll")]
public static extern int ibrd(
int UnitDescriptor,
byte[] ReadBuffer,
long Count
);

public GPIB()
{
}

public static string DecodeGPIBStatus(int StatusWord)
{
string ES = "";
if((StatusWord & 1) != 0)
ES += "Device Clear State. ";
if((StatusWord & 2) != 0)
ES += "Device Trigger State. ";
if((StatusWord & 4) != 0)
ES += "Listener. ";
if((StatusWord & 😎 != 0)
ES += "Talker. ";
if((StatusWord & 0x10) != 0)
ES += "Attention is asserted. ";
if((StatusWord & 0x20) != 0)
ES += "Controller-In-Charge";
if((StatusWord & 0x40) != 0)
ES += "Remote State. ";
if((StatusWord & 0x80) != 0)
ES += "Lockout State. ";
if((StatusWord & 0x100) != 0)
ES += "I/O completed. ";
if((StatusWord & 0x800) != 0)
ES += "Device requesting service. ";
if((StatusWord & 0x1000) != 0)
ES += "SRQ interrupt received. ";
if((StatusWord & 0x2000) != 0)
ES += "End of EOS detected. ";
if((StatusWord & 0x4000) != 0)
ES += "Time limit exceeded. ";
if((StatusWord & 0x8000) != 0)
ES += "NI-488.2 error. ";

return ES;
}

public static int WriteString(int handle, string command)
{
char[] ch_command = command.ToCharArray();
byte[] cmd = new byte[ch_command.Length];
for(int i=0; i cmd[i] = (byte) command[i];

return GPIB.ibwrt(handle, cmd, cmd.Length);
}

public static string ReadGPIBBuffer(int handle, int size)
{
byte[] output = new byte[size];
char[] outchars = new char[size];
int response = ibrd(handle, output, size);
for(int i=0; i {
outchars[i] = (char) output[i];
if(outchars[i] == '\0')
break;
}

return new string(outchars);
}
}
0 Kudos
Message 3 of 7
(11,076 Views)
I'm running into the same thing.

A wrapper for gpib-32.dll works for the most.

But how do I access the status vars (i.e. ibsta, iberr, ibcnt, ibcntl)?

I can't seem to figure out how to declare these variables in my C# wrapper.
0 Kudos
Message 4 of 7
(10,989 Views)
National Instruments does offer two solutions for using C#, or any .NET language with NI-488.2. There is a simple NI-488.2 wrapper(available here), and a native .NET API which will include some more sophisticated .NET features(available here). One of these solutions should work out for your application.

Craig A
National Instruments Engineer
0 Kudos
Message 5 of 7
(10,968 Views)
*bonk* I should have known. I was under the impression that I needed to purchase MeasurementStudio.

Thank.

-Tim
0 Kudos
Message 6 of 7
(10,961 Views)

hi... I'm new in gpib and I tried to follow your suggestions here by making a gpib-32.dll wrapper.

I made my wrapper in C++, I have finished coding all the functions used by gpib-32.dll but I can't make it run.

I can load my new dll but can't really communicate with the other device, ibdev or ibfind returns EDVR error. 

 

Just want to know, has anyone really able to successfully make a wrapper?  thanks...

 

 

0 Kudos
Message 7 of 7
(9,443 Views)