使用VB.NET进行仪器编程
本知识库文章介绍了如何使用VB.NET编程语言对Magna-Power可编程电源产品进行编程。VB.NET是一种集成在Microsoft .NET Framework中的通用语言,以其在开发Windows应用程序方面的简洁性和易用性而著称。其强大的功能使其成为创建可编程仪器控制程序、进行测量甚至绘制图表的理想选择。此外,Magna-Power对可编程仪器标准命令(SCPI)的广泛支持意味着公司的产品可以在VB.NET中通过简单直观的命令轻松控制。
Magna-Power的产品支持多种通信接口,包括RS-232、TCP/IP以太网、USB、RS-485和IEEE-488 GPIB。尽管接口不同,但对于特定产品系列,SCPI命令是相同的。SCPI命令记录在相应产品系列的用户手册中。在创建VB.NET程序时,不同接口之间的唯一区别在于设备连接的设置。
通信接口
USB、串口或RS-485连接
对于USB、串口或RS-485连接,VB.NET使用System.IO.Ports.SerialPort类来创建与仪器的串行连接:
Dim conn As New IO.Ports.SerialPort("COM4", 115200)
conn.Open()
xGen产品的串口波特率为115200,而非xGen产品的串口波特率为19200。端口名称由您的操作系统定义。在Windows中,可以在设备管理器中找到该端口。
后续通过串行连接发送和接收命令的方式如下:
conn.WriteLine("*IDN?")
Dim response As String = conn.ReadLine()
Console.WriteLine(response)
TCP/IP以太网连接
对于TCP/IP以太网连接,VB.NET使用System.Net.Sockets.TcpClient类:
Dim client As New Net.Sockets.TcpClient("192.168.0.86", 50505)
Dim stream As Net.Sockets.NetworkStream = client.GetStream()
Dim writer As New IO.StreamWriter(stream)
Dim reader As New IO.StreamReader(stream)
writer.AutoFlush = True
后续通过TCP/IP以太网连接发送和接收命令的方式如下:
writer.WriteLine("*IDN?")
Dim response As String = reader.ReadLine()
Console.WriteLine(response)
IEEE-488 GPIB连接
对于IEEE-488 GPIB连接,您可以使用GPIB硬件制造商提供的库,例如National Instruments的NI-488.2或Keysight的IO Libraries Suite。在VB.NET中,您可以使用VISA.NET库与GPIB仪器进行通信。
使用VISA.NET的示例:
首先,确保您已安装VISA.NET库,并在项目中添加对Ivi.Visa的引用。
Imports Ivi.Visa
Dim rm As IResourceManager = GlobalResourceManager.Instance
Dim session As IMessageBasedSession = CType(rm.Open("GPIB0::12::INSTR"), IMessageBasedSession)
' Subsequent sending and receiving of commands over IEEE-488 GPIB connection:
session.FormattedIO.WriteLine("*IDN?")
Dim response As String = session.FormattedIO.ReadLine()
Console.WriteLine(response)
深入示例
以下示例提供了使用xGen MagnaDC电源的更详细的VB.NET程序。使用VB.NET对非xGen MagnaDC可编程直流电源进行编程几乎相同,只需根据相应产品系列用户手册中记录的SCPI命令进行细微调整。
示例1:基本TCP/IP以太网控制
此基本示例创建TCP/IP以太网连接,发送一些初始化命令,启用直流输出,将电流升至5 A,等待20秒,然后关闭。
Imports System.Net.Sockets
Imports System.IO
Imports System.Threading
Module Program
Sub Main()
' Create a TCP/IP client and connect to the instrument
Dim client As New TcpClient("192.168.0.86", 50505)
Dim stream As NetworkStream = client.GetStream()
Dim writer As New StreamWriter(stream)
Dim reader As New StreamReader(stream)
writer.AutoFlush = True
' Send SCPI command requesting the product to identify itself
writer.WriteLine("*IDN?")
' Receive the product's response and display it in the console
Dim response As String = reader.ReadLine()
Console.WriteLine(response)
' Configure the MagnaDC for local control
writer.WriteLine("CONF:SOUR 0")
' Set the DC output current to 0 A before enabling DC output
writer.WriteLine("CURR 0")
' Enable the MagnaDC power supply output
writer.WriteLine("OUTP:START")
' Set the DC output current to 5 A
writer.WriteLine("CURR 5")
' Wait 20 seconds
Thread.Sleep(20000)
' Disable the DC output
writer.WriteLine("OUTP:STOP")
' Close the communication channel to the product
writer.Close()
reader.Close()
client.Close()
End Sub
End Module
示例2:带电流设定点的串口连接
此示例创建与产品的串行连接,确定产品型号,然后发送一系列电流命令,每个电流级别之间间隔20秒。此类程序可以扩展为循环设置电压、功率和电阻值。
Imports System.IO.Ports
Imports System.Threading
Module Program
Sub Main()
' Create a serial connection object with default baud rate for MagnaLOADs
Dim conn As New SerialPort("COM4", 115200)
conn.Open()
' Send SCPI command requesting the product to identify itself
conn.WriteLine("*IDN?")
' Receive the product's response and display it in the console
Dim response As String = conn.ReadLine()
Console.WriteLine(response)
' Create an array of current set points
Dim currSetPoints() As Integer = {50, 100, 150, 250}
' Configure the MagnaDC power supply for local control
conn.WriteLine("CONF:SOUR 0")
' Enable the MagnaDC power supply output
conn.WriteLine("OUTP:START")
' Loop through each current set point
For Each currSetpoint In currSetPoints
Console.WriteLine($"Setting Current to {currSetpoint} A")
conn.WriteLine($"CURR {currSetpoint}")
Thread.Sleep(20000) ' Wait 20 seconds
Next
' Disable the MagnaDC power supply output
conn.WriteLine("OUTP:STOP")
' Close the communication channel to the product
conn.Close()
End Sub
End Module
示例 3:放电电池并绘制数据
在本示例中,xGen MagnaDC 电源被编程为使用从逗号分隔值 (.csv) 文件中读取的设定点和时间对电池进行放电,利用产品的高精度测量命令测量直流输入,然后提供测量数据与时间的关系图。该程序可进一步扩展以生成 PDF 测试报告,整合测量数据、图表以及其他仪器的信息。
要在 VB.NET 中处理 CSV 文件和绘图,您可以使用内置类和 System.Windows.Forms.DataVisualization.Charting 命名空间进行图表绘制。
步骤:
- 创建一个 Windows 窗体应用程序。
- 从工具箱中将
Chart控件添加到窗体。 - 在窗体的代码隐藏文件中使用以下代码。
Imports System.IO.Ports
Imports System.Threading
Imports System.IO
Imports System.Windows.Forms.DataVisualization.Charting
Public Class Form1
Private conn As SerialPort
Private outputSamples As New List(Of OutputSample)()
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' Initialize the serial connection with the default baud rate for xGen products
conn = New SerialPort("COM8", 115200)
conn.Open()
' Configure the MagnaDC for local control
conn.WriteLine("CONF:SOUR 0")
' Enable the MagnaDC power supply output
conn.WriteLine("OUTP:START")
' Read the CSV file containing current set points and durations
Dim inputSamples As List(Of InputSample) = ReadCsvFile("example_profile.csv")
' Record the start time
Dim testStartTime As DateTime = DateTime.Now
' Loop through each input sample
For Each sample In inputSamples
' Set the current set point
conn.WriteLine($"CURR {sample.CurrentSetPoint}")
' Calculate the stop time
Dim stopTime As DateTime = DateTime.Now.AddSeconds(sample.Duration)
' Collect measurements until the stop time
While DateTime.Now < stopTime
' Request measurements from the device
conn.WriteLine("MEAS:ALL?")
' Read the response
Dim response As String = conn.ReadLine()
Dim measurements() As String = response.Split(","c)
' Parse and store the measurements
If measurements.Length >= 3 Then
Dim curr As Double = Double.Parse(measurements(0))
Dim volt As Double = Double.Parse(measurements(1))
Dim timeElapsed As Double = (DateTime.Now - testStartTime).TotalSeconds
outputSamples.Add(New OutputSample With {
.Current = curr,
.Voltage = volt,
.Time = timeElapsed
})
End If
Thread.Sleep(500) ' Wait for 0.5 seconds
End While
Next
' Disable the MagnaDC power supply output
conn.WriteLine("OUTP:STOP")
' Close the communication channel to the product
conn.Close()
' Plot the data
PlotData()
End Sub
Private Function ReadCsvFile(filePath As String) As List(Of InputSample)
Dim samples As New List(Of InputSample)()
Using reader As New StreamReader(filePath)
' Skip the header line
reader.ReadLine()
While Not reader.EndOfStream
Dim line As String = reader.ReadLine()
Dim values() As String = line.Split(","c)
If values.Length >= 2 Then
samples.Add(New InputSample With {
.CurrentSetPoint = Double.Parse(values(0)),
.Duration = Double.Parse(values(1))
})
End If
End While
End Using
Return samples
End Function
Private Sub PlotData()
' Configure the chart control
Chart1.Series.Clear()
Chart1.ChartAreas.Clear()
Chart1.ChartAreas.Add(New ChartArea("MainArea"))
' Create series for current and voltage
Dim currentSeries As New Series("Current")
currentSeries.ChartType = SeriesChartType.Line
currentSeries.XValueType = ChartValueType.Double
currentSeries.YValueType = ChartValueType.Double
Dim voltageSeries As New Series("Voltage")
voltageSeries.ChartType = SeriesChartType.Line
voltageSeries.XValueType = ChartValueType.Double
voltageSeries.YValueType = ChartValueType.Double
' Add data points to the series
For Each sample In outputSamples
currentSeries.Points.AddXY(sample.Time, sample.Current)
voltageSeries.Points.AddXY(sample.Time, sample.Voltage)
Next
' Add series to the chart
Chart1.Series.Add(currentSeries)
Chart1.Series.Add(voltageSeries)
' Set chart titles and labels
Chart1.ChartAreas("MainArea").AxisX.Title = "Time (s)"
Chart1.ChartAreas("MainArea").AxisY.Title = "Current (A)"
Chart1.ChartAreas("MainArea").AxisY2.Title = "Voltage (V)"
Chart1.ChartAreas("MainArea").AxisY2.Enabled = AxisEnabled.True
' Associate voltage series with secondary Y-axis
voltageSeries.YAxisType = AxisType.Secondary
End Sub
' Classes to hold input and output samples
Private Class InputSample
Public Property CurrentSetPoint As Double
Public Property Duration As Double
End Class
Private Class OutputSample
Public Property Current As Double
Public Property Voltage As Double
Public Property Time As Double
End Class
End Class
注意事项:
- 将
"COM8"替换为您系统上相应的串口。 - 确保 CSV 文件
example_profile.csv格式正确并位于应用程序目录中。 - CSV 文件应包含两列:
CurrentSetPoint和Duration,并带有标题行。
example_profile.csv 示例:
CurrentSetPoint,Duration
50,10
100,15
150,20
200,25
结论
VB.NET 为控制 Magna-Power 可编程电源产品提供了强大且用户友好的环境。借助用于串口和 TCP/IP 通信的内置类,以及通过制造商库实现的 GPIB 支持,VB.NET 简化了自动化测试、数据采集和结果可视化的流程。通过利用 SCPI 命令和 VB.NET 的功能,用户可以开发针对其特定应用的复杂控制程序。
有关详细的 SCPI 命令集和进一步的自定义信息,请参阅您所使用的特定 Magna-Power 产品系列的用户手册。