📄 findinstruments.cs
字号:
using System;
using System.Runtime.InteropServices; // Used for _getch() to monitor user inputs
using NationalInstruments.NI4882; // This namespace must be included to reference the .NET language interface
namespace NI_C_SHARP
{
/// <summary>
/* Filename - Findinstruments.c
*
* This sample application initializes the bus and the GPIB interface
* board so that the GPIB board is Controller-In-Charge (CIC). It
* then proceeds to find all the instruments on the GPIB bus and
* print the PAD (Primary Address) and SAD (Secondary Address) of
* each instrument.
*
*/
/// </summary>
class FindInstruments
{
private static LangInt li; // declare an instance of the .NET language interface functions
private static GpibConstants c; // declare an instance of the .NET language interface constants
private static int Num_Listeners; // Number of instruments on GPIB
private static int PAD; // Primary address
private static int SAD; // Secondary address
private static int loop; // Loop counter
private const int board = 0; // GPIB board number
private static short[] Instruments; // Array of primary addresses
private static short[] Result; // Array of listen addresses
private static string[] ErrorMnemonic = {"EDVR", "ECIC", "ENOL", "EADR", "EARG", // Error codes
"ESAC", "EABO", "ENEB", "EDMA", "",
"EOIP", "ECAP", "EFSO", "", "EBUS",
"ESTB", "ESRQ", "", "", "", "ETAB"};
[DllImport("msvcrt.dll")]
public static extern char _getch(); //Silently inputs a user keystroke
[STAThread]
static void Main(string[] args)
{
// try this block of code until an exception is thrown
try
{
/*
* Allocate memory space for the classes and arrays
*/
li = new LangInt(); // Contains all GPIB functions
c = new GpibConstants(); // Contains all GPIB global constants
Instruments = new short[32]; // An array containing all instrument addresses (1-32)
Result = new short[31]; // An array that holds the addresses of instruments found on the bus
/*
* Your board needs to be the Controller-In-Charge in order to find
* all instrument on the GPIB. To accomplish this, the function
* SendIFC is called. If the error bit ERR is set in ibsta, call
* GPIBCleanup with an error message.
*/
li.SendIFC(board);
if ((li.ibsta & c.ERR)!=0)
throw new System.Exception("Unable to open board"); // throw an error
/*
* Create an array containing all valid GPIB primary addresses,
* except for primary address 0. Your GPIB interface board is at
* address 0 by default. This array (Instruments) will be given to
* the function FindLstn to find all instruments.
*/
for (loop = 0; loop < 30; loop++)
Instruments[loop] = (short)(loop + 1);
Instruments[30] = c.NOADDR;
/*
* Print message to tell user that the program is searching for all
* active listeners. Find all of the listeners on the bus. Store
* the listen addresses in the array Result. Note, the instruments
* must be powered on and connected with a GPIB cable in order for
* FindLstn to detect them.If the error bit ERR is set in ibsta, throw
* an error message.
*/
Console.Write("Finding all listeners on the bus..." + System.Environment.NewLine);
li.FindLstn(board, Instruments, Result, 31);
if ((li.ibsta & c.ERR)!=0)
throw new System.Exception("Unable to issue FindLstn call"); // throw an error
/*
* ibcntl contains the actual number of addresses stored in the
* Result array. Assign the value of ibcntl to the variable
* Num_Listeners. Print the number of listeners found.
*/
Num_Listeners = li.ibcntl;
Console.Write("Number of Instruments found = " + Num_Listeners + System.Environment.NewLine);
/*
* The Result array contains the addresses of all listening devices
* found by FindLstn. Use the constant NOADDR, as defined in
* GPIBConstants
*/
Result[Num_Listeners] = c.NOADDR;
/*
* Print out each instrument's PAD and SAD, one at a time.
*
* Establish a FOR loop to print out the information. The variable
* LOOP will serve as a counter for the FOR loop and as the index
* to the array Result.
*/
for (loop = 0; loop < Num_Listeners; loop++)
{
/*
* The low byte of the instrument address is the primary
* address. Assign the variable PAD the primary address of the
* instrument. The macro GetPAD, defined in GPIBConstants, returns
* the low byte of the instrument address.
*/
PAD = c.GetPAD((byte)Result[loop]);
/*
* The high byte of the instrument address is the secondary
* address. Assign the variable SAD the primary address of the
* instrument. The macro GetSAD, defined in NI488.H, returns
* the high byte of the instrument address.
*/
SAD = c.GetSAD((byte)Result[loop]);
if (SAD == c.NO_SAD)
Console.WriteLine("The instrument at Result[{0}]: PAD = {1} SAD = NONE", loop, PAD);
else
Console.WriteLine("The instrument at Result[{0}]: PAD = {1} SAD = {2}", loop, PAD, SAD);
Console.WriteLine(System.Environment.NewLine);
}
}
// Execute this block when an exception is thrown
catch (System.Exception caught)
{
//Print out error messages
Console.Write(System.Environment.NewLine +
"Error: " + caught.Message + System.Environment.NewLine + //error Name
"ibsta = " + li.ibsta + System.Environment.NewLine + //ibsta
"iberr = " + li.iberr + System.Environment.NewLine + //iberr
ErrorMnemonic[li.iberr] + System.Environment.NewLine); //error code
}
// Always execute this block to ensure that the GPIB board is taken offline
finally
{
//Allow the user to see the output before exiting.
Console.Write(System.Environment.NewLine + "Hit Any Key to exit" + System.Environment.NewLine);
_getch();
//Take the board offline.
Console.Write("Taking board offline" + System.Environment.NewLine);
li.ibonl (board, 0);
li.Dispose();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -