Insert char into array in embedded function

6 Ansichten (letzte 30 Tage)
Tom
Tom am 14 Apr. 2018
Kommentiert: Walter Roberson am 16 Apr. 2018
Hello, I am trying to send array of data to arduino through Serial Send, however due to not knowing how large records will be ( each can range from 80 to 1000) I want to separate each data with coma in order to read them in arduino ( so I can go byte by byte and whenever there is a coma I will know that that record ends) . I tried inserting them in embedded function and casting result into uint8 but I am getting error about unknown output sizes. Is there any way to do it (or maybe better way to read array od data in arduino)?
function y = fcn(a,b,c,d,e)
q=[a,',',b,',',c,',',d,',',e,'|'];
y = typecast(q,uint8);
Thanks, Tom
  2 Kommentare
Walter Roberson
Walter Roberson am 14 Apr. 2018
As this is an embedded function, then your a b c d e must all be numeric. Are you sure you want to do the equivalent of char() of each of them as you build q? Or do you want to format them?
If I recall correctly, support for sprintf() was not added to Coder until R2018a; as far as I could tell in previous versions the work-around was to use coder.ceval() to invoke sprintf()
Tom
Tom am 15 Apr. 2018
Bearbeitet: Tom am 15 Apr. 2018
a,b,c,d,e all will be numeric values, all I just want to do is separate them by commas in data stream. I attached screen how I would like it to be. Honestly it doesn't need to be commas, any character other than digit that is readable by arduino will do.
EDIT: Now that I think about it, is it possible to run simulation, export data to workspace and run script with them in the same time? At the end I will have the simulation of some circuit in simulink and need to somehow send measurements from it to arduino.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 15 Apr. 2018
You would need to use coder.varsize() to declare the output string probably, and you would sprintf into it. If you are using before r2018a you might need to coder.ceval() to use sprintf.
You cannot just use char() on your values because you need to emit the printable equivalent of your values like '400' not char(400)
  5 Kommentare
Tom
Tom am 16 Apr. 2018
Well, here I am again, when trying to use coder.ceval code generator is returning error:
Cannot assign 'o' from 'coder.ceval' because the type and size of 'o' are not known. To fix, preinitialize 'o'.
My code:
function o = tostring(a,b,c,d,e) %#codegen
coder.varsize('o');
o = coder.ceval('sprintf','%.2f,%.2f,%.2f,%.2f,%.2f.',a,b,c,d,e);
I don't really understand, isn't the point of varsize is to declare that size varies?

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Arduino Hardware finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by