Convert 4 bytes to single in Matlab Simulink blocks

39 次查看(过去 30 天)
Hello Friends,
I need some help after watching this video : https://www.youtube.com/watch?v=mYXUWUf7LxU
  • I have a tcpip object (t) that has been created as shown below.
  • The code used to create the object and further process is shown below where in line #2, the port is server is opened.
  • From the controller, I sent a value of 0.8146 to MATLAB.
  • From my observation, the value of 0.8146 that was sent from the controller was actually received as 4 bytes in Matlab. This was also mentioned in the video and the conversion process was done in the command line.
  • I sent several bytes form the controller then I was able to read data using the command in line #4 below, I read the data in cyclic manner using the function myF( ) created and passing object "t" . The function has a while loop as shown.
  • I was able to read each data from Matlab as soon as it is sent from the controller.
Now my problem begins:
  • I want to implement reading a similar approach used in the command line with Simulink to read bytes from the controller but and i get various errors.
  • Shown below is the blocks created in simulink project and details of the function used:
  • The matlab function block, has the following lines of code
  • When i try to run the code, I get the following 6 errors.
  • I try to reduce the error by "commenting out" the while-loop and the error reduced to 4. See the modified function in simulink:
The remaining 4 error i received are as follow:
  • out1 is inferred as a variable-size matrix, but its size is specified as inherited or fixed. Verify 'out1' is defined in terms of non-tunable parameters, or select the 'Variable Size' check box and specify the upper bounds in the Size box.
  • Simulink cannot determine sizes and/or types of the outputs for block 'simulnk_Read_Bytes_from_PLC_controller/MATLAB Function' due to errors in the block body, or limitations of the underlying analysis. The errors might be inaccurate. Fix the indicated errors, or explicitly specify sizes and/or types for all block outputs.
  • Simulink cannot determine sizes and/or types of the outputs for block 'simulnk_Read_Bytes_from_PLC_controller/MATLAB Function' due to errors in the block body, or limitations of the underlying analysis. The errors might be inaccurate. Fix the indicated errors, or explicitly specify sizes and/or types for all block outputs.
  • Error in port widths or dimensions. 'Output Port 1' of 'simulnk_Read_Bytes_from_PLC_controller/MATLAB Function/in1' is a one dimensional vector with 1 elements.
The parameters of the various blocks are as follows:
I need someone to help me in resolving these errors.
Or how else can i implement what i have done in the command line in Matlab simulink? The error has to do with dimension of output port size that was inherited from the blocks.
Thanks
Abiodun
  1 个评论
Walter Roberson
Walter Roberson 2024-1-21
Try initializing
A = zeros(1,4,'uint8');
This will force a size on A, and so (in theory) should eliminate the errors about not being able to deduce a size.

请先登录,再进行评论。

回答(1 个)

Benjamin Thompson
Benjamin Thompson 2024-1-21
If you have the Embedded Coder toolbox there is the Byte Pack, Byte Reversal, and Byte Unpack blocks. If not, you could try implementing that behavior with a C S-Function that takes a single as an input and outputs an array of uint8. Its output function would be:
unsigned char * p_temp_pointer = (unsigned char *)&u0;
y0[0] = p_temp_pointer[0];
y0[1] = p_temp_pointer[1];
y0[2] = p_temp_pointer[2];
y0[3] = p_temp_pointer[3];
Try using S-Function Builder to create this block.
  1 个评论
Abiodun Abiola
Abiodun Abiola 2024-1-21
Thanks for your suggestion.
I was later able to solve the problem with the 4 error when i read this the link below :
I found that by chenging the output data type from -1 to 1 resolved the error.
I will then prodeed to return the while loop and see if the other errors were also resolved.
Thanks
Abiodun

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Large-Scale Modeling 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by