Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

MessageBasedSession

I am using the following code to query the available resources on a pc.

// Find all the available resources on this machine.
string filter = "?*";
string[] resources = ResourceManager.GetLocalManager().FindResources(filter);
int NumberOfResources = resources.Length;

// Create the number of Sessions for this machine.
Session[] AvailableSessions = new Session[ NumberOfResources ];

// Now instantiate a session for each type found.
for( i = 0; i < NumberOfResources; i++ )
{
AvailableSessions[ i ] = new Session( resources[ i ] );
}


MessageBasedSession mbs = (MessageBasedSession)ResourceManager.GetLocalManager().Open(AvailableSessions[X].ResourceName);

SerialSession ss = mbs as SerialSession;
GpibSession gpibs = mbs as GpibSession;
UsbSession usbs = mbs as UsbSession;
TcpipSession Tcpips = mbs as TcpipSession;

where
AvailableSessions[0] is ASRL1::INSTR
AvailableSessions[1] is ASRL10::INSTR
AvailableSessions[2] is GPIB0::INSTR

which seems to work fine except when X in the above statement is 2. When it is I get an invalid cast exception thrown and I do not understand why. Can anyone help?
Thanks
Barry
0 Kudos
Message 1 of 2
(6,528 Views)
Barry,
from your previous posts, I believe what you're trying to do is get a collection of heterogenous Visa Sessions and then be able to select which one of them you want to use. the code you posted ishe filter actually creating two sessions for each resource descriptor you find - once by calling new Session and once by calling GetLocalManager().Open.
I do not recommend calling either the Session or MessageBasedSession constructors directly. (or RegisterBasedSession, though that's not relevant to your code) These are base classes the encapsulate shared functionality of different leaf class session types like GpibSession, SerialSession, etc. In your case, the best thing to do is call ResourceManager.GetLocalManager(),Open() with the resource descriptors returned from FindResources.
Have you stepped through your code to see where the exception in occurring and what the ResourceName is when it happens? The filter you're using to search for devices is going to return EVERYTHING hooked up to your system - are you certain that you're only getting the devices in that list of resource names? For insance, if one of the resource names returned is for a GpibInterface (your board, as compared to the devices hooked up to it), you would get an Invalid Cast Exception when you tried to cast to MessageBasedSession, because GpibInterface does not derive from MessageBasedSession.
So, two recommendations for your code:
1) Simplify your session creation so you're not creating two sessions for every resource descriptor
2) Reduce the number of resource descriptors we get back to just what you're interested in.

// Find all the available resources that we're interested in on this machine
// This will give us the gpib, serial, tcpip, and usb instrument resources.
string filter = "(GPIB|ASRL|TCPIP|USB)?*INSTR";
string[] resources = ResourceManager.GetLocalManager().FindResources(filter);
int NumberOfResources = resources.Length;

// Create the number of Sessions for this machine.
MessageBasedSession[] AvailableSessions = new Session[ NumberOfResources ];

// Now instantiate a session for each type found.
foreach (string resource in resources)
{
AvailableSessions[ i ] = (MessageBasedSession)ResourceManager.GetLocalManager().Open(resource);
}

Also, remember when you cast a reference using the "as" operator that it will return null if the cast fails. So, in your code like
SerialSession s = AvailableSession[i] as SerialSession
you then need to check whether g is null before using it:
if (s != null)
{
s.BaudRate = 9200;
//other serial-only operations.
}

OK, that was covered a lot of different topics. So, to directly answer your original question:
You are probably getting and InvalidCastException from the code

MessageBasedSession mbs = (MessageBasedSession)ResourceManager.GetLocalManager().Open(AvailableSessions[X].ResourceName);

because it is likely that you are creating sessions for visa resources like gpib boards that are not message based session devices. Checking the list of resource names returned from FindResources can confirm this suspicion.

Glenn Burnside
Message 2 of 2
(6,509 Views)