Simulink UDP does not work, but Matlab code does

1 view (last 30 days)
Albin
Albin on 15 Jun 2015
Answered: Gao Yang on 5 Feb 2020
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)

Answers (2)

Albin
Albin on 15 Jun 2015
Edited: Albin on 15 Jun 2015
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 on 5 Feb 2020
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.

Community Treasure Hunt

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

Start Hunting!