using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; 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 GPIBcontrl { public partial class Form1 : Form { 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 static int ValueInt; //holds the number returned from instrument private const int BDINDEX = 0; // board Index private const int PRIMARY_ADDR_OF_PPS =5; // 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 public Form1() { InitializeComponent(); } private void button2_Click(object sender, EventArgs e) { /* * 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 MessageBox.Show("Unable to open device"); /* * 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 MessageBox.Show("unable to clear device"); /* * The application issues the '*IDN?' command to the power supply. */ /* li.ibwrt(Dev, "VOLT 5",6); li.ibwrt(Dev, "CURR 5", 6); li.ibwrt(Dev, "SENS:FUNC 'CURR' ", 20);*/ li.ibwrt(Dev, "*IDN?\n", 6); li.ibwrt(Dev, "READ?", 5); if ((li.ibsta & c.ERR) != 0) // throw an error MessageBox.Show("unable to write device"); } private void button1_Click(object sender, EventArgs e) { li.ibrd(Dev, out ValueStr, ARRAYSIZE); if ((li.ibsta & c.ERR) != 0) // throw an error MessageBox.Show("Can not read the data"); textBox1.Text= ValueStr; } } }
private const int PRIMARY_ADDR_OF_PPS =5; // pimary address of device 仪器上的主地址设置对了没有啊?
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
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 GPIBcontrl
{
public partial class Form1 : Form
{
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 static int ValueInt; //holds the number returned from instrument
private const int BDINDEX = 0; // board Index
private const int PRIMARY_ADDR_OF_PPS =5; // 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 public Form1()
{
InitializeComponent();
} private void button2_Click(object sender, EventArgs e)
{
/*
* 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
MessageBox.Show("Unable to open device"); /*
* 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
MessageBox.Show("unable to clear device"); /*
* The application issues the '*IDN?' command to the power supply.
*/
/* li.ibwrt(Dev, "VOLT 5",6);
li.ibwrt(Dev, "CURR 5", 6);
li.ibwrt(Dev, "SENS:FUNC 'CURR' ", 20);*/
li.ibwrt(Dev, "*IDN?\n", 6);
li.ibwrt(Dev, "READ?", 5);
if ((li.ibsta & c.ERR) != 0) // throw an error
MessageBox.Show("unable to write device"); }
private void button1_Click(object sender, EventArgs e)
{
li.ibrd(Dev, out ValueStr, ARRAYSIZE);
if ((li.ibsta & c.ERR) != 0) // throw an error
MessageBox.Show("Can not read the data");
textBox1.Text= ValueStr;
}
}
}
仪器上的主地址设置对了没有啊?