Convert 4 bytes to single in Matlab Simulink blocks
조회 수: 14 (최근 30일)
이전 댓글 표시
Hello Friends,
- 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
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
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Computer Vision with Simulink에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!