Insert char into array in embedded function

6 次查看(过去 30 天)
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 个评论
Walter Roberson
Walter Roberson 2018-4-14
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 2018-4-15
编辑:Tom 2018-4-15
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.

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2018-4-15
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 个评论
Tom
Tom 2018-4-16
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?

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Arduino Hardware 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by