我有一个使用Python设置的服务器,并且已经成功地与运行在Matlab脚本中的客户端 Build 了一个简单的通信协议 . 我需要在Simulink模型中运行此函数,以便测试我正在开发的一些控制器 . 由于UDP不支持代码生成,我一直在尝试将函数设置为外部函数,如下所示:
function z = fcn(u)
elevationMatrix = zeros(3,3);
coder.extrinsic('udp', 'fwrite', 'fopen');
% connect to the server
t = udp('localhost', 2002);
fopen(t);
% write a message
fwrite(t, 'This is a test message.');
% read the echo
bytes = fread(t, [t.BytesAvailable, 1], 'char');
%fit the data into a matrix
temp = reshape(bytes, [8 9]);
z = zeros(1,9);
for col = 1:9
bytepack=uint64(0);
for row = 1:8
temp(9-row, col)
bytepack = bitshift(temp(9 - row, col),8*(8-row));
z(col) = bitor(bytepack,z(col));
temp(row, col);
end;
end;
z = reshape(z, [3,3])';
% close the connection
fclose(t);
我收到一些我无法解决的错误;即,“尝试从'mxArray'中提取字段'BytesAvailable'”我猜是因为我需要以某种方式预定义t的大小 . 对于'bytes','temp'和'bytepack',我得到同样的东西 .
除非您能指出我可以通过内置的Simulink UDP块发送不同字符串的方式,否则我不想沿着那条路走下去,因为我将通过名称调用python服务器上的函数 .