#region 引用
using System;
using System.IO.Ports;
using System.Threading;
using Ivi.Visa.Interop;
#endregion
namespace calibrationCmd
{
public sealed class Hp34461A
{
#region Fields
private string strIPorUSB = "TCPIP0::192.168.0.101";//Example string for LAN
//string strIPorUSB = "USB0::0x2A8D::0x1301::MY57231522"; //Example string for USB
//string strIPorUSB = "USB0::0x2A8D::0x1401::MY57231522"; //Example string for USB
public byte CurrentMode { get; private set; }
#endregion
#region Constructor
/// 初始化 类的新实例。
public Hp34461A( )
{
CurrentMode = 0xff;
}
#endregion
#region Methods
public void SetPort(string port)
{
strIPorUSB = port;
}
///
///
///
///
public byte Mode()
{
return CurrentMode;
}
///
///
///
/// 1: VOLT:DC, 2: CURR:DC
///
public bool SetMode(byte mode)
{
if (CurrentMode == mode)
return true;
CurrentMode = mode;
return true;
}
///
///
///
///
public void SetRemote()
{
Thread.Sleep(500);
}
///
///
///
///
public void SetLocal()
{
Thread.Sleep(500);
}
public string Call(bool ret, string format, params object[] args)
{
string retval = string.Empty;
if (format == null)
throw new ArgumentNullException("format");
ResourceManager rm = new ResourceManager(); //Open up a new resource manager
FormattedIO488 myDmm = new FormattedIO488(); //Open a new Formatted IO 488 session
myDmm.IO = (IMessage)rm.Open(strIPorUSB, AccessMode.NO_LOCK, 2000, ""); //Open up a handle to the DMM with a 2 second timeout
myDmm.IO.Timeout = 3000; //You can also set your timeout by doing this command, sets to 3 seconds
myDmm.WriteString("*CLS");
Thread.Sleep(25);
myDmm.WriteString(string.Format(format, args));
Thread.Sleep(25);
retval = ret ? myDmm.ReadString() : string.Empty;
myDmm.IO.Close();
return retval;
}
public double Read(int delay = 500)
{
if (delay > 0)
Thread.Sleep(delay);
return double.Parse(Call(true, "Read?"));
}
public double ReadVoltage()
{
double fDCVResult = 0;
ResourceManager rm = new ResourceManager(); //Open up a new resource manager
FormattedIO488 myDmm = new FormattedIO488(); //Open a new Formatted IO 488 session
try
{
myDmm.IO = (IMessage)rm.Open(strIPorUSB, AccessMode.NO_LOCK, 2000, ""); //Open up a handle to the DMM with a 2 second timeout
myDmm.IO.Timeout = 3000; //You can also set your timeout by doing this command, sets to 3 seconds
myDmm.IO.Clear(); //Send a device clear first to stop any measurements in process
myDmm.WriteString("*RST", true); //Reset the device
myDmm.WriteString("*IDN?", true); //Get the IDN string
string IDN = myDmm.ReadString();
Console.WriteLine(IDN); //report the DMM's identity
//Configure for DCV 100V range, 100uV resolution
myDmm.WriteString("CONF:VOLT:DC 100, 0.0001", true);
myDmm.WriteString("READ?", true);
string DCVResult = myDmm.ReadString();
Console.WriteLine("DCV Reading = " + DCVResult); //report the DCV reading
CheckDMMError(myDmm); //Check if the DMM has any errors
myDmm.IO.Close();
double.TryParse(DCVResult, out fDCVResult);
}
catch(Exception ex)
{
Console.WriteLine(ex.Message+"\n"+ ex.StackTrace); //report the DMM's identity
}
return fDCVResult;
}
public double ReadCurrent()
{
double fDCIResult = 0;
ResourceManager rm = new ResourceManager(); //Open up a new resource manager
FormattedIO488 myDmm = new FormattedIO488(); //Open a new Formatted IO 488 session
myDmm.IO = (IMessage)rm.Open(strIPorUSB, AccessMode.NO_LOCK, 2000, ""); //Open up a handle to the DMM with a 2 second timeout
myDmm.IO.Timeout = 3000; //You can also set your timeout by doing this command, sets to 3 seconds
myDmm.IO.Clear(); //Send a device clear first to stop any measurements in process
myDmm.WriteString("*RST", true); //Reset the device
myDmm.WriteString("*IDN?", true); //Get the IDN string
string IDN = myDmm.ReadString();
Console.WriteLine(IDN); //report the DMM's identity
//Configure for DCI 3A range, 100uA resolution
myDmm.WriteString("CONF:CURR:DC 3, 0.0001", true);
myDmm.WriteString("READ?", true);
string DCIResult = myDmm.ReadString();
Console.WriteLine("DCI Reading = " + DCIResult); //report the DCI reading
CheckDMMError(myDmm); //Check if the DMM has any errors
myDmm.IO.Close();
double.TryParse(DCIResult, out fDCIResult);
return fDCIResult;
}
public void Write(string command, params object[] args)
{
Call(false, command, args);
}
public string Error()
{
string strError = string.Empty;
ResourceManager rm = new ResourceManager(); //Open up a new resource manager
FormattedIO488 myDmm = new FormattedIO488(); //Open a new Formatted IO 488 session
myDmm.IO = (IMessage)rm.Open(strIPorUSB, AccessMode.NO_LOCK, 2000, ""); //Open up a handle to the DMM with a 2 second timeout
myDmm.IO.Timeout = 3000; //You can also set your timeout by doing this command, sets to 3 seconds
myDmm.WriteString("SYST:ERR?");
Thread.Sleep(25);
strError = myDmm.ReadString();
myDmm.IO.Close();
return strError;
}
public void CheckDMMError(FormattedIO488 myDmm)
{
myDmm.WriteString("SYST:ERR?", true);
string errStr = myDmm.ReadString();
if (errStr.Contains("No error")) //If no error, then return
return;
//If there is an error, read out all of the errors and return them in an exception
else
{
string errStr2 = "";
do
{
myDmm.WriteString("SYST:ERR?", true);
errStr2 = myDmm.ReadString();
if (!errStr2.Contains("No error")) errStr = errStr + "\n" + errStr2;
} while (!errStr2.Contains("No error"));
throw new Exception("Exception: Encountered system error(s)\n" + errStr);
}
}
#endregion
}
}