Instrument Control (GPIB, Serial, VISA, IVI)

cancel
Showing results for 
Search instead for 
Did you mean: 

Is there a VISA driver for vb.net yet?

I actually haven't tried to use the events yet. But looking in the help index for callback function showed :

using System;
using System.Runtime.InteropServices;

public delegate bool CallBack(int hwnd, int lParam);

public class EnumReportApp {

[DllImport("user32")]
public static extern int EnumWindows(CallBack x, int y);

public static void Main()
{
CallBack myCallBack = new CallBack(EnumReportApp.Report);
EnumWindows(myCallBack, 0);
}

public static bool Report(int hwnd, int lParam) {
Console.Write("Window handle is ");
Console.WriteLine(hwnd);
return true;
}
}

Looks pretty straight forward. Just declare a new instance of viEventHandler passing your function to th
e constructor. Then pass the instance of viEventHandler to viInstallHandler.

Also for the viStatusDesc you should have been able to declare it this way:

[DllImport("visa32.dll")] public static extern
System.Int32 viStatusDesc (System.UInt32 vi, System.UInt32 status,[MarshalAs(UnmanagedType.VBByRefStr)] ref System.String desc);
0 Kudos
Message 11 of 17
(3,394 Views)
Thank you so much for this example. I will try it out immidiately. I tried looking through the help for an example like this but to no avail. I should have used "callback" in the search criteria as you did. This looks pretty straight forward now that I see it in black and white but as I tried doing it on my own, for some reason it didn't seem that intuitive. Thanks again. Yes the viStatusDesc works that way as well, I just preffer the byte array method because that is what I am used to.
0 Kudos
Message 12 of 17
(3,394 Views)
saikey,
I just can't seem to get the viEnableEvent function to work correctly. It returns a VI_ERROR_SYTEM_ERROR. I have a feeling that it has to do with the original function requiring a void* data type. Do you have any ideas.

Thanks again for your help.
0 Kudos
Message 13 of 17
(3,394 Views)
Change the return type for the delegate to System.Int32.
Make sure that your event handler is declared static.
Should work fine now.

uint defRM = 0, vi = 0;
int stat = 0;
VISA.viOpenDefaultRM(ref defRM);
VISA.viOpen(defRM, "GPIB0::23::INSTR", 0, 2000, ref vi);
viEventHandler SRQ = new viEventHandler(Form1.fSRQ);
VISA.viInstallHandler(vi, VISA.VI_EVENT_SERVICE_REQ, SRQ, 0);
stat = VISA.viEnableEvent(vi, VISA.VI_EVENT_SERVICE_REQ, VISA.VI_HNDLR, 0);

switch(stat) {
case VISA.VI_ERROR_INV_OBJECT:
listBox1.Items.Add("INV_OBJECT");
break;
case VISA.VI_ERROR_INV_EVENT:
listBox1.Items.Add("INV_EVENT");
break;
case VISA.VI_ERROR_INV_MECH:
listBox1.Items.Add("
INV_MECH");
break;
case VISA.VI_ERROR_INV_CONTEXT:
listBox1.Items.Add("INV_CONTEXT");
break;
case VISA.VI_ERROR_HNDLR_NINSTALLED:
listBox1.Items.Add("HNDLR_NINSTALLED");
break;
case VISA.VI_ERROR_NSUP_MECH:
listBox1.Items.Add("NSUP_MECH");
break;
case VISA.VI_SUCCESS:
listBox1.Items.Add("SUCCESS");
break;
default:
listBox1.Items.Add("UNK");
break;
}

VISA.viClose(vi);
VISA.viClose(defRM);
}

public static System.Int32 fSRQ(System.UInt32 vi, System.UInt32 eventType, System.UInt32 viEvent, System.UInt32 userHandle) {
return VISA.VI_SUCCESS;
}

this returns VI_SUCCESS on viEnableEvent.
don't know if it really gets the event or not.

Thanks for your help with the debugging.
0 Kudos
Message 14 of 17
(3,394 Views)
Excellent postings here saikey

Exactly the jump start I was looking for.
===========================
Mark Eckdahl - Software Manager at Blue Max Systems

LabVIEW developer for 20 years

0 Kudos
Message 15 of 17
(3,394 Views)
I 2nd that!!

Thanks,
Ken M/A-COM
0 Kudos
Message 16 of 17
(3,394 Views)
Thanks for files. It was big help
0 Kudos
Message 17 of 17
(3,665 Views)