当然,除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 具有非blokcking能力
一先进先出(FIFO)缓冲区接收数据。在每个步骤 步骤中,数据端口都会输出缓冲区中的请求值。在 非阻塞模式下,状态端口指示该块是否已收到 新数据。
如果您通过UDP进行通信,为什么不直接在单独的MATLAB实例中运行GUI? – Suever
@Suever因为我需要能够从gui访问我的simulink模型以及编写它的模型工作区 – Ortix92