首页 > 解决方案 > 使用matlab中的fprinf函数改变电源电压和电流制作GUI

问题描述

我试图将电压和电流作为输入变量来改变电源中的电压和电流。我想让它变成 GUI,以便用户输入电压和电流,这将改变电源中的值。作为试验,我使用:

voltage = 1;
current = 1;
fprintf('APPL %d , %d', voltage, current);

它在打印时起作用

APPL 1 , 1

更改电源的原始代码是:

    %% HighV Voltage Power Supply
%Instrument Connection

% Find a serial port object.
% Find a tcpip object.
obj1 = instrfind('Type', 'tcpip', 'RemoteHost', '192.168.0.111', 'RemotePort', 2268, 'Tag', '');

% Create the tcpip object if it does not exist
% otherwise use the object that was found.
if isempty(obj1)
    obj1 = tcpip('192.168.0.111', 2268);
else
    fclose(obj1);
    obj1 = obj1(1);
end

%% Disconnect and Clean Up

% Disconnect from instrument object, obj1.
fclose(obj1);

%% Instrument Connection

% Connect to instrument object, obj1.
fopen(obj1);

%% Instrument Configuration and Control
% Communicating with instrument object, obj1.
fprintf(obj1, 'APPL 2,1.5'); %setting voltage and current syntax: 'APPL voltage,current'

fprintf(obj1, 'DISPlay:MENU:NAME 3'); %To display changed voltage and current on the screen panel on the high voltage supply

该代码在电源上的电压和电流变为 2v 和 1.5A 时起作用。但是使用之前的测试,我将代码更改为:

%% HighV Voltage Power Supply
%Instrument Connection

% Find a serial port object.
% Find a tcpip object.
obj1 = instrfind('Type', 'tcpip', 'RemoteHost', '192.168.0.111', 'RemotePort', 2268, 'Tag', '');

% Create the tcpip object if it does not exist
% otherwise use the object that was found.
if isempty(obj1)
    obj1 = tcpip('192.168.0.111', 2268);
else
    fclose(obj1);
    obj1 = obj1(1);
end

%% Disconnect and Clean Up

% Disconnect from instrument object, obj1.
fclose(obj1);

%% Instrument Connection

% Connect to instrument object, obj1.
fopen(obj1);

%% Instrument Configuration and Control
% Communicating with instrument object, obj1.

voltage = 1;
current = 1;
fprintf(obj1, 'APPL %d , %d', voltage, current);

fprintf(obj1, 'DISPlay:MENU:NAME 3'); %To display changed voltage and current on the screen panel on the high voltage supply

此代码给出以下错误消息:

Error using icinterface/fprintf (line 124)
MODE must be either 'sync' or 'async'.

Error in HighVoltageSupply (line 32)
fprintf(obj1, 'APPL %d , %d', voltage, current);

我怎样才能解决这个问题?我希望能够将电压和电流作为输入。

非常感谢你提前

标签: matlab

解决方案


您的问题源于 Matlab 将相同的函数fprintf用于两个不同的目的。第一个示例中的一个fprintf('APPL %d , %d', voltage, current);是将字符串写入文件(或在本例中为屏幕)。第二个fprintf(obj1, 'APPL 2,1.5');是向仪器发送命令。

在您的错误中,您基本上是在尝试将两者混合使用,这是不允许的。然后你可以做的是首先创建一个字符串,例如,,sprintf然后将此字符串作为命令传递。

s = sprintf('APPL %d , %d', voltage, current);
fprintf(obj1, s);

或者,在一行中

fprintf(obj1, sprintf('APPL %d , %d', voltage, current));

推荐阅读