本知识库文章介绍了使用 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

 

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 文件格式正确并位于 MATLAB 工作目录中。第一行应为标题,数据应从第二行开始。example_profile.csv

结论

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

 

Logo

中国智能体开发者社区,聚焦智能体与大模型开发,提供前沿资讯、实用工具链、开源项目及行业案例。通过技术沙龙、开发者大赛等活动,促进经验交流与协作,助力开发者快速构建创新智能应用。

更多推荐