Knowledge Base

使用MATLAB进行仪器编程

本知识库文章介绍如何使用MATLAB对Magna-Power可编程电源产品进行编程。MATLAB是一种高级编程环境,以其在数值计算、可视化和编程方面的易用性而闻名。其强大的工具箱和函数使其成为控制、测量和绘制可编程仪器数据的理想选择。此外,Magna-Power对可编程仪器标准命令(SCPI)的全面支持,确保了通过简单直观的命令与MATLAB实现无缝集成。

Magna-Power产品支持多种通信接口,包括RS-232、TCP/IP以太网、USB、RS-485和IEEE-488 GPIB。尽管接口不同,但对于特定产品系列,SCPI命令保持一致,具体请参阅相应产品系列的用户手册。在创建MATLAB程序时,不同接口之间的唯一区别在于设备连接的设置。

通信接口

USB、串行或RS-485连接

对于USB、串行或RS-485连接,MATLAB使用内置的serialport函数创建与仪器的串行连接:

conn = serialport('COM4', 115200);

xGen产品的串行波特率为115200,非xGen产品的波特率为19200。端口位置由您的操作系统定义。在Windows中,可以在设备管理器中找到该端口。

后续通过串行连接发送和接收命令的方式如下:

writeline(conn, '*IDN?');
response = readline(conn);
disp(response);

TCP/IP以太网连接

对于TCP/IP以太网连接,MATLAB使用tcpclient函数:

t = tcpclient('192.168.0.86', 50505);

后续通过TCP/IP以太网连接发送和接收命令的方式如下:

writeline(t, '*IDN?');
response = read(t);
disp(char(response));

IEEE-488 GPIB连接

对于IEEE-488 GPIB连接,MATLAB使用gpib函数以及Instrument Control Toolbox:

g = gpib('NI', 0, 12);
fopen(g);

后续通过IEEE-488 GPIB连接发送和接收命令的方式如下:

fprintf(g, '*IDN?');
response = fscanf(g);
disp(response);

深入示例

以下示例提供了使用xGen MagnaDC电源的更详细的MATLAB程序。在MATLAB中对非xGen MagnaDC可编程直流电源进行编程几乎相同,仅需根据相应产品系列用户手册中的说明对SCPI命令进行细微调整。

示例1:基本TCP/IP以太网控制

此基本示例创建TCP/IP以太网连接,发送一些初始化命令,启用直流输出,将电流升至5 A,等待20秒,然后关闭。

% Create a TCP/IP connection to the instrument
t = tcpclient('192.168.0.86', 50505);

% Send SCPI command requesting the product to identify itself
writeline(t, '*IDN?');
% Receive the product's response and display it in the command window
response = read(t);
disp(char(response));

% Configure the MagnaDC for local control
writeline(t, 'CONF:SOUR 0');
% Set the DC output current to 0 A before enabling DC output
writeline(t, 'CURR 0');
% Enable the MagnaDC power supply output
writeline(t, 'OUTP:START');
% Set the DC output current to 5 A
writeline(t, 'CURR 5');

% Wait for 20 seconds
pause(20);

% Disable the DC output
writeline(t, 'OUTP:STOP');

% Clear the TCP/IP connection
clear t;

示例 2:带电流设定点的串行连接

本示例创建与产品的串行连接,识别产品型号,然后发送一系列电流命令,每个电流级别之间间隔 20 秒。此类程序可以扩展为循环遍历电压、功率和电阻值。

% Create a serial connection object with default baud rate for MagnaLOADs
conn = serialport('COM4', 115200);

% Send SCPI command requesting the product to identify itself
writeline(conn, '*IDN?');
% Receive the product's response and display it in the command window
response = readline(conn);
disp(response);

% Create an array of current set points
currSetPoints = [50, 100, 150, 250];

% Configure the MagnaDC power supply for local control
writeline(conn, 'CONF:SOUR 0');
% Enable the MagnaDC power supply output
writeline(conn, 'OUTP:START');

% Loop through each current set point
for currSetpoint = currSetPoints
    fprintf('Setting Current to %d A\n', currSetpoint);
    writeline(conn, sprintf('CURR %d', currSetpoint));
    pause(20);
end

% Disable the MagnaDC power supply output
writeline(conn, 'OUTP:STOP');

% Clear the serial connection
clear conn;

示例 3:电池放电与数据绘图

在本示例中,xGen MagnaDC 电源被编程为使用从逗号分隔值 (.csv) 文件读取的设定点和时间对电池进行放电,使用产品的高精度测量命令测量直流输入,然后提供测量数据与时间的关系图。该程序可以进一步扩展以生成 PDF 测试报告,整合测量数据、图表以及来自其他仪器的信息。

% Import data from CSV file
% The CSV file should have two columns:
% Column 1: Current set point in amperes
% Column 2: Time in seconds
data = readmatrix('example_profile.csv');

% Create a serial connection object with default baud rate for xGen products
conn = serialport('COM8', 115200);

% Configure the MagnaDC for local control
writeline(conn, 'CONF:SOUR 0');
% Enable the MagnaDC power supply output
writeline(conn, 'OUTP:START');

% Initialize arrays to store measurements
currents = [];
voltages = [];
times = [];
testStartTime = tic;

% Loop through each set point and duration
for i = 1:size(data, 1)
    currSetpoint = data(i, 1);
    duration = data(i, 2);
    
    % Set the current set point
    writeline(conn, sprintf('CURR %f', currSetpoint));
    
    % Record data for the specified duration
    stopTime = toc(testStartTime) + duration;
    while toc(testStartTime) < stopTime
        % Measure all DC output variables
        writeline(conn, 'MEAS:ALL?');
        response = readline(conn);
        measurements = strsplit(response, ',');
        
        % Store measurements
        currents(end+1) = str2double(measurements{1});
        voltages(end+1) = str2double(measurements{2});
        times(end+1) = toc(testStartTime);
        
        pause(0.5);
    end
end

% Disable the MagnaDC power supply output
writeline(conn, 'OUTP:STOP');

% Clear the serial connection
clear conn;

% Plot Current vs. Time
subplot(2,1,1);
plot(times, currents, 'r--');
ylabel('Output Current (A)');
title('I-V Profile');

% Plot Voltage vs. Time
subplot(2,1,2);
plot(times, voltages, 'b--');
xlabel('Time (s)');
ylabel('Output Voltage (V)');

% Display the plots
figure(gcf);

注意:请确保 CSV 文件 example_profile.csv 格式正确并位于 MATLAB 工作目录中。第一行应为标题行,数据应从第二行开始。

结论

MATLAB 为控制 Magna-Power 可编程电源产品提供了强大的环境。凭借内置的串行、TCP/IP 和 GPIB 通信功能以及丰富的绘图功能,MATLAB 简化了自动化测试、数据采集和结果可视化的过程。通过利用 SCPI 命令和 MATLAB 的功能,用户可以开发适合其特定应用的复杂控制程序。

如需了解详细的 SCPI 命令集和进一步的自定义选项,请参阅您所使用的 Magna-Power 产品系列的用户手册。

Originally published 十月 29, 2024

Stay connected and informed.

Subscribe to receive emails—no more than once per month—with new technical articles, product releases and factory updates from Magna-Power.

Have any questions or feedback?
We'd love to hear from you.
Contact us