필터 지우기
필터 지우기

Simulink Question - How to input a decimal value and output hexadecimal string?

조회 수: 7 (최근 30일)
JD Harwell
JD Harwell 2019년 8월 16일
댓글: Walter Roberson 2019년 8월 17일
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!

답변 (3개)

Walter Roberson
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
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);

댓글을 달려면 로그인하십시오.


JD Harwell
JD Harwell 2019년 8월 16일
Hi Walter,
Thank you for the quick response. I use R2019a.
I am not in front of my computer right now, but what I was trying was taking a constant block that was being passed a value from a mask that is my desired set point.
I fed that to a Matlab function block where my function was y = uint8(dec2hex(u,4) but I could never get past errors at that block.
Thank you.
  댓글 수: 4
JD Harwell
JD Harwell 2019년 8월 16일
I just clicked on the link and it pulled the image up as viewable.
I will try again here in multiple ways:

댓글을 달려면 로그인하십시오.


JD Harwell
JD Harwell 2019년 8월 16일
Thank you, Walter. I will give this a try. Are these parameters that I need to enter in the Simulink constant block or are they lines that need to be included in the MatLab function side of things?
Please feel free to correct and educate me if I’m using terms incorrectly as I’m going to attempt to gain some level of proficiency in next few years to save a lot of hassle in my research.

카테고리

Help CenterFile Exchange에서 Simulink Environment Customization에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by