1
votes

I've a constant value in my Simulink model sent using "UDP Send" block. There is a Java Socket program that listens on a particular port for any input values. Now, the value I receive in the Java code isn't the same as the one sent from the Simulink module. For e.g., if I send value 1 from the Simulink model, in the Java code I receive 1072693248.

I use ByteBuffer.wrap(...) to convert byte array into integer. Is there something I got wrong in the Java code?

1

1 Answers

2
votes

The number 1072693248 = 1023 * 2^20 (or 1023 << 20 to use Java notation) is what you get if you take 32 bits of the double representing 1.0 and treat them as a integer. To get this value you need to take the first 32 bits if the numbers are stored in a big-endian way or the last 32 bits if they are stored in a little-endian way. See this question for an example of this - I think this is the little-endian version.

So the problem is caused by Simulink sending a double when your Java code expects an integer. This could easily happen by accident because Simulink, and MATLAB in general, treat numbers as doubles unless you specify otherwise. Assuming you do want to send a 32-bit integer the easiest fix is to update the settings of your Constant block. You can set the output data type explicitly from the Signal Attributes tab of the block parameter dialog, or leave that set to "Inherit from Constant Value" and change the value itself from "1" to e.g. "int32(1)".