%% 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
1 回答
当然,除了
BytesAvailableFcn
之外,matlab还有datagramreceivedfcn在新的dagatrams上调用你的自定义函数,这是非阻塞而fread / fscanf阻塞(暂时) .关于matlab中的回调,请阅读events and cbs
Compillable standalone可能如下所示:
如果你想使用simulink块,请使用udpreceive,它具有非blokcking功能