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

13 visualizaciones (últimos 30 días)
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!

Respuestas (3)

Walter Roberson
Walter Roberson el 16 de Ag. de 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 comentario
Walter Roberson
Walter Roberson el 17 de Ag. de 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);

Iniciar sesión para comentar.


JD Harwell
JD Harwell el 16 de Ag. de 2019
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 comentarios
JD Harwell
JD Harwell el 16 de Ag. de 2019
I just clicked on the link and it pulled the image up as viewable.
I will try again here in multiple ways:

Iniciar sesión para comentar.


JD Harwell
JD Harwell el 16 de Ag. de 2019
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.

Categorías

Más información sobre Simulink Environment Customization en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by