Instrument Control (GPIB, Serial, VISA, IVI)

cancel
Showing results for 
Search instead for 
Did you mean: 

Catch error when creating new device in .NET

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

0 Kudos
Message 1 of 5
(4,894 Views)

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

 

0 Kudos
Message 2 of 5
(4,855 Views)

Thank you, Curt.  I will try this as soon as I can -- I'm off onto higher-priority projects for the moment.

 

Gerrit

0 Kudos
Message 3 of 5
(4,837 Views)

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.

0 Kudos
Message 4 of 5
(4,820 Views)

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;
            }
        }

 

FindDevice.png

 

 

Curt

0 Kudos
Message 5 of 5
(4,816 Views)