matlab - 使用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 将相同的函数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));
推荐阅读
- android - 关于 settings.gradle.ktl 的 Kotlin DSL 未解析参考
- java - 获取错误的firebase规则。当我可以首先显示时,我的设置配置文件活动会在我更改“.write”时显示:true 它没有显示
- tox - 在另一个命令中使用命令的输出
- angular - 如何以角度调试传单地图事件?
- json - 如何在 Talend 中使用 API 调用执行增量数据更新(批量 Json 文件)过程
- c# - 没有 UI 的 IdentityServer4 Windows 身份验证
- java - 重新发送失败的请求
- python-2.7 - 如何模拟 aws athena 功能。?
- python - 如何在python中用条件填充空值?
- firebase - 无法访问 Firebase 函数中自动填充的环境变量