Simulink Question - How to input a decimal value and output hexadecimal string?
조회 수: 11 (최근 30일)
이전 댓글 표시
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
답변 (3개)
Walter Roberson
2019년 8월 16일
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
Walter Roberson
2019년 8월 17일
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);
참고 항목
카테고리
Help Center 및 File Exchange에서 Event Functions에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!