用C#进行仪器控制系列——Keysight 直流电源
本文将介绍通过C#控制Keysight E3647A双路输出直流电源。
被控对象介绍
Keysight E3647A台式电源是Keysight基础型双路输出直流电源,属于E364XA系列电源中的一个。该电源标配有GPIB和RS232通讯接口,很适合用于实现自动化的需求。E3647A 60 W 双路输出台式电源提供了高低两个电压输出范围,可以灵活选择。 如果选择低电压输出范围,那么电源能够输出更大电流。
该电源的主要参数如下:
1)额定输出(0 °C 至 40 °C),双路输出
- 输出范围 1:0 至 35 V / 0.8 A
- 输出范围 2:0 至 60 V / 0.5 A
2)编程准确度(25 °C ± 5 °C),± (输出的 % + 偏置)
- 电压:< 0.05% + 10 mV(Keysight E3646A、E3647A、E3648A、E3649A 的输出 2:< 0.1% + 25 mV)
- 电流:< 0.2% + 10 mA
3)纹波和噪声(20 Hz 至 20 MHz)
- 正常模式电压:< 8 mVpp / 1 mVrms
- 正常模式电流:< 4 mArms
4)回读准确度(25 °C ± 5 °C),± (输出的 % + 偏置)
- 电压:< 0.05% + 5 mV(E3646A、E3647A、E3648A、E3649A 的输出 2:< 0.1% + 25 mV)
- 电流:< 0.15% + 5 mA(E3646A、E3647A、E3648A、E3649A 的输出 2:< 0.15% + 10 mA)
控制要求
本文主要实现以下功能:
1)对Keysight 3647A的2个输出通道的输出电压、输出电流进行设置;
2)控制电源输出2路电源信号;
3)对电源的输出电压和输出电流进行回读,并显示在控制台窗口中。
程序代码实现
1)首先创建E3647A控制台程序;
2)添加对VISA组件的引用
3)编写C#控制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using Ivi.Visa.Interop;
namespace Keysight_E364XA
{
internal class Program
{
static void Main(string[] args)
{
ResourceManager rm;
FormattedIO488 instrument;
String result;
try
{
// Get a handle to the default resource manager
rm = new ResourceManager();
// Create an IO formatter
instrument = new FormattedIO488();
// Open a connection to the instrument and bind it to the formatter
instrument.IO = (IMessage)rm.Open("GPIB0::5::INSTR");
// Once we're connected, make sure we close things correctly
try
{
// Send the *IDN? query to the instrument...
instrument.WriteString("*IDN?", true);
// and read back the response.
result = instrument.ReadString();
Console.WriteLine("Instrument ID is: " + result);
// Channel 1 parameters set
instrument.WriteString("INSTrument:SELect OUTPut1");
instrument.WriteString("SOURce:VOLTage:RANGe LOW");
instrument.WriteString("SOURce:VOLTage:LEVel:IMMediate:AMPLitude 5");
instrument.WriteString("SOURce:CURRent:LEVel:IMMediate:AMPLitude 0.5");
// Channel 2 parameters set
instrument.WriteString("INSTrument:SELect OUTPut2");
instrument.WriteString("SOURce:VOLTage:RANGe HIGH");
instrument.WriteString("SOURce:VOLTage:LEVel:IMMediate:AMPLitude 24");
instrument.WriteString("SOURce:CURRent:LEVel:IMMediate:AMPLitude 0.5");
// Output ON
instrument.WriteString("OUTPut:STATe 1");
// Delay(1);
// Channel 1 readback
instrument.WriteString("INSTrument:SELect OUTPut1");
//instrument.WriteString("MEASure:SCALar:VOLTage:DC?");
instrument.WriteString("MEASure:SCALar:VOLTage:DC?");
string Response = instrument.ReadString();
Console.WriteLine("CH1 Voltage Readback: {0}", Response);
instrument.WriteString("MEASure:SCALar:CURRent:DC?");
Response = instrument.ReadString();
Console.WriteLine("CH1 Current Readback: {0}", Response);
// Channel 2 readback
instrument.WriteString("INSTrument:SELect OUTPut2");
instrument.WriteString("MEASure:SCALar:VOLTage:DC?");
Response = instrument.ReadString();
Console.WriteLine("CH2 Voltage Readback: {0}", Response);
instrument.WriteString("MEASure:SCALar:CURRent:DC?");
Response = instrument.ReadString();
Console.WriteLine("CH2 Current Readback: {0}", Response);
// Output OFF
instrument.WriteString("OUTPut:STATe 0");
}
finally
{
// Close the connection to the instrument
instrument.IO.Close();
// and free the reference to the session.
instrument.IO = null;
}
}
catch (Exception e)
{
Console.Error.WriteLine("Error occurred: " + e.Message);
}
Console.WriteLine("Press any key to quit.");
Console.ReadKey(true);
}
static bool Delay(int delayTime)
{
DateTime now = DateTime.Now;
int s;
do
{
TimeSpan spand = DateTime.Now - now;
s = spand.Seconds;
}
while (s < delayTime);
return true;
}
}
}
注:以上代码是基于GPIB通信接口来实现PC机与仪器之间的通讯的,若采用RS232接口,只需要对建立连接的代码稍作修改即可。
程序执行结果验证
执行以上C#控制台程序,得到如下所示的结果,证明程序能够正常执行,实现了对Keysight E3647A双路输出直流电源的控制。
后续优化思路
后续会将本程序改造为C# Winform 桌面应用程序,可通过UI界面实现对仪器参数的设置、输出使能控制、输出数据回读等功能,预期界面如下图所示(届时将会融合很多之前文章介绍过的相关功能,比如自动获取硬件端口号、创建新线程等)。