matlab - Matlab GUI中的非阻塞UDP接收器

标签 matlab sockets user-interface udp simulink

我正在使用应用程序设计器(非常类似于但优于 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/

相关文章:

matlab - 避免中断 Matlab GUI 中的回调函数

matlab - Matlab 中两点之间的距离

database - Matlab 到 Microsoft Access 数据库 2010 日期和时间

Java Netty - 根据输入消息将结果作为 StringEncoder 或 ObjectEncoder 发送到客户端?

java - 设置UI内容仅添加最后一行

r - 在 Matlab 中实现的两样本 Kolmogorov-Smirnov 检验 (kstest2) 实现不当?

c# - 为什么通过TCP发送后字节序颠倒了

python - 为什么 python 在套接字对象的实例化中也接受 `proto` 时,却用 SOCK_STREAM 或 SOCK_DGRAM 指定 IPv4 或 IPv6?

user-interface - 如何设计用于构建条件表达式的 UI?

python - gtk:检测点击 TreeView 中的单元格