Unable to use 'Extract bits' block in Simulink for data type int32

7 次查看(过去 30 天)
Hello,
I want to send a data type int32 as an SBUS signal (8 bits) at a time. I am using the 'Extract bits' block to divide the int32 into 4 chunks of 8 bits, something like this [0 7], [8 15], [16 23], [24 31].
I get an error saying;
Data type mismatch. Outport port 1 of '/Modify Scaling Only' is a signal of data type 'uint8'. However it is driving a signal of data type 'int8'.
Data type mismatch. Inport port 3 of '/DTProp2' expects a signal of data type 'int8'. However it is driving a signal of data type 'uint8'.
This block only seems to work when the input data type is an unsigned integer 'uintx' in Matlab 2020b and Matlab 2023b.
It works fine for 'int32' in Matlab 2022b on a different machine.
Any ideas on what could be causing this? Thank you for your help.

回答(1 个)

Namnendra
Namnendra 2024-6-28,10:37
Hi Vishal,
The issue you're encountering with the 'Extract Bits' block in Simulink seems to be related to how different MATLAB versions handle data type conversions and compatibility.
Here are some steps you can take to resolve this issue:
1. Explicitly Convert Data Types
Ensure that the data type of the signal feeding into the 'Extract Bits' block is explicitly converted to `uint32` before extraction. You can use the 'Data Type Conversion' block to achieve this.
2. Use 'Data Type Conversion' Block
Place a 'Data Type Conversion' block before the 'Extract Bits' block to convert the `int32` signal to `uint32`. This should resolve the data type mismatch errors.
3. Set Output Data Type of 'Extract Bits' Block
Explicitly set the output data type of the 'Extract Bits' block to `uint8` to avoid any implicit data type conversions that might cause issues.
Example Implementation
Here is an example of how you can modify your Simulink model to handle the data type conversion:
1. Add a 'Data Type Conversion' Block:
Convert the `int32` signal to `uint32` before feeding it into the 'Extract Bits' block.
2. Configure the 'Extract Bits' Block:
Ensure the 'Extract Bits' block is set to output `uint8` data type.
Simulink Model Configuration
1. Add Blocks:
- Add a 'Data Type Conversion' block.
- Add an 'Extract Bits' block.
- Add a 'Demux' block to split the 32-bit signal into four 8-bit signals.
2. Configure Blocks:
- Set the 'Data Type Conversion' block to convert `int32` to `uint32`.
- Configure the 'Extract Bits' block to extract the desired bits and output `uint8`.
Example Steps:
1. Add and Configure Blocks:
% Create a new Simulink model
model = 'bit_extraction_model';
open_system(new_system(model));
% Add blocks to the model
add_block('simulink/Commonly Used Blocks/Constant', [model '/Constant']);
add_block('simulink/Commonly Used Blocks/Data Type Conversion', [model '/Data Type Conversion']);
add_block('simulink/Logic and Bit Operations/Extract Bits', [model '/Extract Bits']);
add_block('simulink/Signal Routing/Demux', [model '/Demux']);
add_block('simulink/Commonly Used Blocks/Scope', [model '/Scope']);
% Connect blocks
add_line(model, 'Constant/1', 'Data Type Conversion/1');
add_line(model, 'Data Type Conversion/1', 'Extract Bits/1');
add_line(model, 'Extract Bits/1', 'Demux/1');
add_line(model, 'Demux/1', 'Scope/1');
add_line(model, 'Demux/2', 'Scope/2');
add_line(model, 'Demux/3', 'Scope/3');
add_line(model, 'Demux/4', 'Scope/4');
% Set parameters
set_param([model '/Constant'], 'Value', 'int32(123456789)');
set_param([model '/Data Type Conversion'], 'OutDataTypeStr', 'uint32');
set_param([model '/Extract Bits'], 'bitIdxRange', '[0 7; 8 15; 16 23; 24 31]');
set_param([model '/Extract Bits'], 'OutputDataTypeStr', 'uint8');
set_param([model '/Demux'], 'Outputs', '4');
% Open the model
open_system(model);
2. Configure the 'Extract Bits' Block:
- Set the `bitIdxRange` parameter to `[0 7; 8 15; 16 23; 24 31]`.
- Set the `OutputDataTypeStr` parameter to `uint8`.
3. Run the Model:
- Run the model to verify that the `int32` signal is correctly split into four `uint8` signals.
By explicitly converting the data type and ensuring that the 'Extract Bits' block outputs `uint8`, you should be able to resolve the data type mismatch errors across different MATLAB versions.
I hope the above information helps.
Thank you.

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by