Simulink Question - How to input a decimal value and output hexadecimal string?
12 views (last 30 days)
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!
0 Comments
Answers (3)
Walter Roberson
on 16 Aug 2019
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);
See Also
Categories
Find more on Event Functions 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!