Simulink Question - How to input a decimal value and output hexadecimal string?
Show older comments
Hello,
For quick background, I am a biochemist and have no programming background at all. This is also my first go at using Matlab/Simulink. I am using Simulink to model my experiment and also using Simulink to interface with a number of probes and controllers.
One controller requires COM input as hexadecimal string. I will be inputing a decimal number into a block and would like to end with a hexadecimal string output that is fed through my COM port to the controller.
After a week of trying, I think my approach should be to create an S-function block that does two things:
1) Take my numerical input and convert it to hexadecimal. I have found this can be accomplished with Matlab’s “dec2hex” function. The problem I run into is that the output of this function is char. Simulink does not seem to be able to handle char signal. So, I would like to:
2) have Matlab continue on and convert this char signal to string. I saw in the help files that there is a function to do this.
My problem is that I don’t fully understand the formatting even when I’m looking at it in a help file.
How would I go about creating a block that can take a number input, send it over to Matlab, and return it as a hexadecimal string that is supported by Simulink (as the controller requires this format for communication).
Thank you!
Answers (3)
Walter Roberson
on 16 Aug 2019
0 votes
Which release are you using? Until R2019a, signals cannot be char. The work-around is to return uint8(dec2hex(value,SIZE)) . When you pass the serial send block uint8() then the binary values will be sent without change, which the other end will "see" as character.
1 Comment
Walter Roberson
on 17 Aug 2019
function y = fcn(u)
y = zeros(1, 4, 'uint8');
U = uint16(u);
u16 = uint16(16);
y(4) = mod(U, u16);
U = (U-uint16(y(4)))/u16;
y(3) = mod(U, u16);
U = (U-uint16(y(3)))/u16;
y(2) = mod(U, u16);
U = (U-uint16(y(2)))/u16;
y(1) = uint8(U);
JD Harwell
on 16 Aug 2019
0 votes
4 Comments
Walter Roberson
on 16 Aug 2019
What is the data type coming off of the constant block? You might need to
y = zeros(1,4,'uint8');
y = uint8(dec2hex(uint16(u),4);
JD Harwell
on 16 Aug 2019
Edited: Walter Roberson
on 16 Aug 2019
Walter Roberson
on 16 Aug 2019
Sorry, that image is not readable.
JD Harwell
on 16 Aug 2019
JD Harwell
on 16 Aug 2019
0 votes
Categories
Find more on String in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
