【问题标题】:Non blocking UDP receiver in Matlab GUIMatlab GUI中的非阻塞UDP接收器
【发布时间】:2017-08-31 07:44:33
【问题描述】:

我正在使用应用程序设计器(与 GUIDE 非常相似但优于 GUIDE)创建一个 MATLAB GUI,我想用它来实时监控我的 simulink 模型的数据输出。

换句话说,我有一个 simulink 模型和一个 GUI,它们都在同一个 MATLAB 实例中运行,我想从 simulink 模型通过 UDP 发送数据包,并在我的 GUI 中使用该数据来更新绘图。但是,我不知道如何在不阻塞的情况下从 UDP 数据包中读取数据。

有没有办法在收到数据包时绑定处理程序,以便我可以执行函数来更新绘图/字段?

【问题讨论】:

  • 如果您通过 UDP 进行通信,为什么不在单独的 MATLAB 实例中运行 GUI?
  • @Suever 因为我需要能够从 gui 访问我的 simulink 模型以及编写它的模型工作区

标签: matlab sockets user-interface udp simulink


【解决方案1】:

当然,除了BytesAvailableFcn matlab 有datagramreceivedfcn 可以在新数据上调用您的自定义函数,它是非阻塞的,而 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 具有非阻塞能力

先进先出 (FIFO) 缓冲区接收数据。每时每刻 步骤,数据端口从缓冲区输出请求的值。在一个 非阻塞模式,状态端口指示块是否已收到 新数据。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-12
    • 2015-06-18
    • 2011-05-03
    • 2022-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多