Hello community,
just a question: is it easily possible to get console IO functions to work within the MBD toolbox? I want to send formatted strings over UART (LINFlex).
What I tried so far:
function s = printfUnsigned(f, varargin) % f = input string + specifier , varargin = variables
len = 3; % string length hardcoded
coder.cinclude('stdio.h');
s = char(zeros(len,1));
%cs = [s; 0]; % NULL-terminated string for use in C
coder.ceval('sprintf_s', coder.ref(s(1)), uint32(len), coder.rref(f(1)), varargin{:}); % s = cs
end
This uses sprintf_s with a hardcoded string length (so no buffer) and this actually works. Then you can just send the string out over the LinFlex block. The strings look horrible, though, because of the fixed size.
So is there a possibility to use any other standard C IO function or is it just not possible?
Many thanks,
Markus
EDIT: I also tried the built-in MATLAB conversion functions like int2str and similar, of course. These also allocate memory on the fly, so they don't work.
Hi Markus,
Here is a basic example i've used to send some text over the UART. It is now pretty but it may be used to sent char by char of any text.
The problem is with variables since Ecoder does not support code generation for functions like num2str().
The model looks like this:
Using the Byte_Pack/Byte_Unpack you may concatenate various strings. The string to char serialization is done in the state-flow.
Running this on the MPC5744P DevKit that is connected over the microUSB SDA port with produce:
It will be nice if you could find a way to concatenate numbers.
I see that Simulink has some specialized blocks for Encode/Decode that might do what you want:ASCII Encode/Decode - Simulink . Unfortunately, i do not have a license for that toolbox to give it a try.
Does it helps?
Best regards,
Daniel
Thank you for your suggestion. I can't open your Simulink model since I'm one version behind yours (2016b) but I do see what you did there. Your solution combined with mine would probably be the best but yours also has one drawback: the '\0' after each char. So: Would it be possible to get rid of the '\0' chars between the sent characters or is this limited/hard-coded by the LinFlex output block?
My solution right now is to use a hard-coded string length and the sprintf_s function which uses the predefined string as a buffer to format every value to. This has the disadvantage that it will always put out the whole string (not used space is filled with '\0's).
The ASCII encode/decode blocks are - as I already said - not an option because they fail to compile. (Heap related?)
Maybe just writing out raw data and formatting on the receiving end is the way to go. I can accept that if this is not easily possible like setting a compile flag or similar tricks.
Best regards,
Markus
Hi markusransberger,
I think you will find this interesting. With MATLAB/Simulink R2018a there are new block-set for STRING manipulation within SIMULINK models: What’s New in R2018a! » Guy on Simulink
Best regards,
Daniel
Hi Markus,
I have exported to 2015b to be easier for importing in various formats.
Would it be possible to get rid of the '\0' chars between the sent characters or is this limited/hard-coded by the LinFlex output block?
I shall check this tomorrow. Now that you pointed out - i also noticed. I used it on a different hardware S32K and it was compact.
The ASCII encode/decode blocks are - as I already said - not an option because they fail to compile. (Heap related?)
To0 bad i can't test. I'll try to get an evaluation license from MATHWORKS. What's the error? Perhaps we can tweak the linker command file.
Best regards,
Daniel