首页 文章

Matlab GUI中的非阻塞UDP接收器

提问于
浏览
1

我正在使用应用程序设计器创建一个MATLAB GUI(非常类似于GUIDE),我想用它来监控我在simulink模型中的数据输出 real time .

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

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

1 回答

  • 0

    当然,除了 BytesAvailableFcn 之外,matlab还有datagramreceivedfcn在新的dagatrams上调用你的自定义函数,这是非阻塞而fread / fscanf阻塞(暂时) .

    关于matlab中的回调,请阅读events and cbs

    Compillable standalone可能如下所示:

    %% 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,它具有非blokcking功能

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

相关问题