Insert char into array in embedded function

6 vues (au cours des 30 derniers jours)
Tom
Tom le 14 Avr 2018
Commenté : Walter Roberson le 16 Avr 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 commentaires
Walter Roberson
Walter Roberson le 14 Avr 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 le 15 Avr 2018
Modifié(e) : Tom le 15 Avr 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.

Connectez-vous pour commenter.

Réponse acceptée

Walter Roberson
Walter Roberson le 15 Avr 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 commentaires
Tom
Tom le 16 Avr 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?

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Arduino Hardware dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by