Learn Knowledge Base

Knowledge Base

Instrumentation Programming with MATLAB

This knowledge base article introduces programming a Magna-Power programmable power product using MATLAB. MATLAB is a high-level programming environment renowned for its ease of use in numerical computation, visualization, and programming. Its robust toolboxes and functions make it an excellent choice for controlling, measuring, and plotting data from programmable instrumentation. Moreover, Magna-Power's extensive support for Standard Commands for Programmable Instrumentation (SCPI) ensures seamless integration with MATLAB through straightforward and intuitive commands.

Magna-Power products support a variety of communication interfaces, including RS-232, TCP/IP Ethernet, USB, RS-485, and IEEE-488 GPIB. Despite these different interfaces, the SCPI commands remain consistent for a particular product series, as documented in the respective product series' user manual. When creating a MATLAB program, the only difference between interfaces will be the settings for the device connection.

Communication Interfaces

USB, Serial, or RS-485 Connection

For USB, Serial, or RS-485 connections, MATLAB uses the built-in serialport function to create a serial connection to the instrument:

conn = serialport('COM4', 115200);

The serial baud rate for xGen products is 115200, while for non-xGen products, it is 19200. The port location is defined by your operating system. In Windows, this port can be found in the Device Manager.

Subsequent sending and receiving of commands over the serial connection will be as follows:

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

TCP/IP Ethernet Connection

For TCP/IP Ethernet connections, MATLAB uses the tcpclient function:

t = tcpclient('192.168.0.86', 50505);

Subsequent sending and receiving of commands over the TCP/IP Ethernet connection will be as follows:

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

IEEE-488 GPIB Connection

For IEEE-488 GPIB connections, MATLAB uses the gpib function along with the Instrument Control Toolbox:

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

Subsequent sending and receiving of commands over the IEEE-488 GPIB connection will be as follows:

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

In-Depth Examples

The following examples provide more detailed MATLAB programs using an xGen MagnaDC power supply. Programming a non-xGen MagnaDC programmable DC power supply in MATLAB will be nearly identical, with subtle changes to the SCPI commands as documented in the respective product series' user manual.

Example 1: Basic TCP/IP Ethernet Control

This basic example creates a TCP/IP Ethernet connection, sends some initialization commands, enables the DC output, raises the current level to 5 A, waits 20 seconds, and then shuts down.

% 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;

Example 2: Serial Connection with Current Set Points

This example creates a serial connection to the product, determines what product it is, and then sends a sequence of current commands with 20 seconds between each current level. This type of program can be expanded to cycle through voltage, power, and resistance values as well.

% 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;

Example 3: Discharging a Battery and Plotting Data

In this example, an xGen MagnaDC power supply is programmed to discharge a battery using set points and times read from a comma-separated values (.csv) file, measure the DC input using the product's high-accuracy measurement commands, and then provide a plot of the measured data versus time. This program could be further expanded to generate a PDF test report, integrating the measured data, plots, as well as information from other instruments.

% 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);

Note: Ensure that the CSV file example_profile.csv is properly formatted and located in the MATLAB working directory. The first row should be headers, and the data should start from the second row.

Conclusion

MATLAB provides a powerful environment for controlling Magna-Power programmable power products. With built-in functions for serial, TCP/IP, and GPIB communication, as well as extensive plotting capabilities, MATLAB simplifies the process of automating tests, collecting data, and visualizing results. By leveraging SCPI commands and MATLAB's features, users can develop sophisticated control programs tailored to their specific applications.

For detailed SCPI command sets and further customization, refer to the user manual of the specific Magna-Power product series you are using.

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