⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 powersupplysimple.cs

📁 gpib编程之简单控制可编程电源的程序,对与工业测试应用极广
💻 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>
	/*
	* This application demonstrates how to read from and write to the
	* Tektronix PS2520G Programmable Power Supply.
	*
	* This sample application is comprised of three basic parts:
	*
	* 1. Initialization
	* 2. Main Body
	* 3. Cleanup
	*
	* The Initialization portion consists of getting a handle to a
	* device and then clearing the device.
	*
	* In the Main Body, this application queries a device for its
	* identification code by issuing the '*IDN?' command. Many
	* instruments respond to this command with an identification string.
	* Note, 488.2 compliant devices are required to respond to this
	* command.
	*
	* The last step takes the device offline.
	*/
	/// </summary>
	class PowerSupplySimple
	{
		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 const int ARRAYSIZE = 1024;					// size of ReadBuffer
		private static int Dev;								// a reference to an instrument on the bus
		private static string ValueStr;						// holds the string returned from instrument
		private const int BDINDEX = 0;					    // board Index
		private const int PRIMARY_ADDR_OF_PPS = 1;          // pimary address of device
		private const int NO_SECONDARY_ADDR = 0;            // secondary address of device
		private static int TIMEOUT;                         // tmeout value
		private const int EOTMODE = 1;                      // enable the END message
		private const int EOSMODE = 0;                      // disable the EOS mode		
		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
				TIMEOUT = c.T10s;            // Set timeout to 10 seconds	
			
				/*
				 * The application brings the power supply online using ibdev. A
				 * device handle, Dev, is returned and is used in all subsequent
				 * calls to the device.
				 */
				Dev = li.ibdev(BDINDEX, PRIMARY_ADDR_OF_PPS, NO_SECONDARY_ADDR, TIMEOUT, EOTMODE, EOSMODE);
				if ((li.ibsta & c.ERR)!=0)              // throw an error
					throw new System.Exception("Unable to open device" + System.Environment.NewLine +
						"ibsta: " + li.ibsta + System.Environment.NewLine +
						"ERR: " + c.ERR);
   
				/*
				 * Clear the internal or device functions of the device.  If the
				 * error bit ERR is set in ibsta, call GPIBCleanup with an error
				 * message.
				 */
				li.ibclr (Dev);
				if ((li.ibsta & c.ERR)!=0)              // throw an error
					throw new System.Exception("Unable to clear device");
   
				/*
				 * The application issues the '*IDN?' command to the power supply.
				 */
				li.ibwrt (Dev, "*idn?", 5);
				if ((li.ibsta & c.ERR)!=0)                           // throw an error
					throw new System.Exception("Unable to write to power supply");
            
				/*
				 * The application reads the identification code in the form of an
				 * ASCII string from the power supply into the ValueStr variable.
				 */
				li.ibrd (Dev, out ValueStr, ARRAYSIZE);
				if ((li.ibsta & c.ERR)!=0)                              // throw an error
					throw new System.Exception("Unable to read data from power supply");   

				Console.WriteLine("Data Read: " + ValueStr);            // Print out string returned from power supply
			}
			// 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 (Dev, 0);
				li.ibonl (BDINDEX, 0);
				li.Dispose();
			}

			
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -