我正在使用应用程序设计器(非常类似于但优于 GUIDE)创建一个 MATLAB GUI,我想用它来监控 中我的 simulink 模型的数据输出实时 .
换句话说,我有一个 simulink 模型和一个 GUI,它们都在同一个 MATLAB 实例中运行,我想通过 UDP 从 simulink 模型发送数据包,并在我的 GUI 中使用该数据来更新绘图。但是,我不知道如何在不阻塞的情况下从 UDP 数据包中读取数据。
有没有办法在收到数据包时绑定(bind)处理程序,以便我可以执行更新绘图/字段的函数?
最佳答案
当然,除了BytesAvailableFcn
matlab 有 datagramreceivedfcn在新的 dagatrams 上调用您的自定义函数,该函数是非阻塞的,而 fread/fscanf 正在阻塞(暂时)。关于 matlab 中的回调,请阅读 events and cbs
可编译的独立可能如下所示:
%% standalone main
%{
see docs/*
%}
function exitcode = main(port, remotePort)
% sanitize input
if(~exist('port','var') || ~exist('remotePort','var'))
disp(['args: port remotePort']);
exit(1);
end
if ischar(port)
port=str2num(port);
end
if ischar(remotePort)
remotePort=str2num(remotePort);
end
% create udp object
u = udp('127.0.0.1',remotePort, 'LocalPort', port);
% connect the UDP object to the host
fopen(u);
exitcode = 0;
% since we poll, suppress warning
warning('off','instrument:fscanf:unsuccessfulRead');
warning VERBOSE
% specify callback on datagram
while true
msgEnc= fscanf(u);
% timed out without datagram
if isempty(msgEnc)
continue
end
% do sth with msgEnc (which is a readable string)
fprintf(u, 'heard sth'); %answer
end
end
如果您想使用 simulink 模块,请使用 udpreceive
具有非阻塞能力
A First In First Out (FIFO) buffer receives the data. At every time step, the Data port outputs the requested values from the buffer. In a nonblocking mode, the Status port indicates if the block has received new data.
关于matlab - Matlab GUI中的非阻塞UDP接收器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43235503/