Simulink UDP does not work, but Matlab code does

조회 수: 1 (최근 30일)
Albin
Albin 2015년 6월 15일
답변: Gao Yang 2020년 2월 5일
Hi! I can get a piece of code to run in my Matlab script, but not when I use the corresponding Simulink block.
I am using UDP as a way of piping live streams from Simulink to python (Blender, 3D software) In matlab I am simply using
u = udp(host,port);
fopen(u);
fwrite(u, data, 'double');
fclose(u)
and I am sending a simple array of numbers. This comes through nicely. However, using Simulink 'UDP Send', I get the wrong numbers (instead of 3.14 i get something to the power of negative 300, etc.). My first thought is that this is owing to a different data type being used, but there is no way to set 'double' in the Simulink block, and it is my understanding that Simulink uses 'double' as its standard data type anyway.
Is there a way to change the data type, or could my problem lie somewhere else?
The Python end of the solution looks like
data = self.request[0]
socket = self.request[1]
numOfValues = int(len(data) / 8)
info=struct.unpack('>' + 'd' * numOfValues, data)

답변 (2개)

Albin
Albin 2015년 6월 15일
편집: Albin 2015년 6월 15일
Ok, so it turns out Matlab does one bit order and Simulink another. This can be fixed from the python end. Still no idea of how to fix it from Matlab though.
I simply changed the corresponding Python code to
unp = struct.unpack(numOfValues*'d', data)
And now it works in Simulink, but not in Matlab. Good enough for me!

Gao Yang
Gao Yang 2020년 2월 5일
Hello,sir! I met the same problem. I code the UDP server code in python. But do you know how to output the data UDP server received to simulink? And how to parse this data in real time in Python's UDP code? Very thanks, I am looking forward to your reply.

카테고리

Help CenterFile Exchange에서 String에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by