03-29-2014 08:08 PM
I have a .NET Windows application running fine, using C# in Visual Studio with the NI 3.1.2 drivers. This requires 'namespace NationalInstruments.NI4882'. I create a new device using
device = new Device(boardID, (byte)gpibAddressNumericUpDown.Value);
to access the GPIB device specified in gpibAddressNumericUpDown.Value, and I'm able to read and write from it just fine.
Now how do I catch the exception if I put the wrong address in gpibAddressNumericUpDown.Value? I've tried
try {
device = new Device(boardID, (byte)gpibAddressNumericUpDown.Value);
}
catch {
}
and I don't seem to ever enter the 'catch' block. Object Browser for the NI Device object says something about "exceptions", but I don't know how to check for them or if that's what I want.
Thanks for any help!
Gerrit
03-31-2014 11:23 AM
Hi,
It won't throw an error since all you're doing is just creating a new device.
Try using the FindDevice function in the 488 library, Try soemthing like this, I think it should work
private Board m_board; // test if device is online before initializtion to bus public bool FindDevice(byte address) { bool retval = false; try { m_board = new Board(0); Address checkAddress = new Address(address); retval = m_board.FindListener(checkAddress); return retval; } catch (NationalInstruments.NI4882.GpibException gpibex) { Trace.WriteLine("FindDevice GpibException: " + gpibex.Message); //strError = gpibex.Message; return false; } catch (Exception ex) { Trace.WriteLine("FindDevice: " + ex.Message.ToString()); return false; } }
or try using the VisaNS library.
Use the resource manager to find out which devices are connected to the bus, then parse out the address(es) from the string(s) that are returned
string[] resources = ResourceManager.GetLocalManager().FindResources("GPIB?*INSTR")
This will return an array of strings in this form, "GPIB0::16::INSTR". 0 is the board number, 16 is the device address.
Hope this helps
Curt
04-01-2014 10:25 AM
Thank you, Curt. I will try this as soon as I can -- I'm off onto higher-priority projects for the moment.
Gerrit
04-02-2014 10:31 AM
I got a chance to try this yesterday, and found that m_board.FindListener(checkAddress) always returns 'false'. I tried other GPIB addresses from known-good instruments with known addresses, and also the instrument which I've been successfully reading and writing. I will experiment some more, shortly I hope.
04-02-2014 11:28 AM
Hi,
I tried the FindListener function and it worked for me. I have a scope at address 16 on my machine
private void button1_Click(object sender, EventArgs e) { FindDevice(Convert.ToByte(this.primaryAddressNumericUpDown.Value )); } private void FindDevice(byte addr) { try { bool retval = false; m_board = new Board(0); Address checkAddress = new Address(addr); retval = m_board.FindListener(checkAddress); this.stringReadTextBox.AppendText( primaryAddressNumericUpDown.Value.ToString() + " returned " + retval.ToString() + "\r\n"); } catch (Exception) { throw; } }
Curt