ASCII String with SCI Transmit

14 次查看(过去 30 天)
Anyone here know how to do a number to string conversion, matlab has a num2str() which would be perfect but it is not compatible with embedded matlab fcn.
My target is a TI chip (F28335). My goal is to send measurement data and see it in hyperterminal but i need to convert the numbers to ASCII before i transfer it out or i will get garbage, which i have been getting.
  2 个评论
Walter Roberson
Walter Roberson 2011-2-22
None of fprintf(), sprintf(), num2str() are in the Embedded Matlab subset,
http://www.mathworks.com/help/toolbox/eml/ug/bq1h2z7-9.html
Kamal Joshi
Kamal Joshi 2015-11-26
Were you able to figure out how to do this within matlab ?

请先登录,再进行评论。

采纳的回答

Daniel
Daniel 2011-2-24
I c, so the answer is that it can not be done unless i use s-function to run other code that has that capability? do i have this correct?
  1 个评论
Walter Roberson
Walter Roberson 2011-2-24
Sorry, I can't answer that with any authority: I'm going by what I've been able to _find_ in the documentation, but I don't have the appropriate toolkits to experiment with.

请先登录,再进行评论。

更多回答(5 个)

Doug Eastman
Doug Eastman 2011-2-22
If you have xPC Target you can use the ASCII Encode/Decode Block.

Walter Roberson
Walter Roberson 2011-2-22
Daniel, it would help to know if there are constraints on the class or range of numbers to be converted. For example is this unsigned integers or is it full floating point in exponential notation (and if so how many digits are you looking for) ?
  1 个评论
Walter Roberson
Walter Roberson 2011-2-22
For simple positive integers:
n = 3141;
if n == 0
s = uint8(48)
else
s = zeros(1,ceil(log10(n+1)),'uint8');
for t = size(s,2):-1:1
s(t) = mod(n,10) + 48;
n = floor(n./10);
end
end
%s is now the vector of uint8

请先登录,再进行评论。


Daniel
Daniel 2011-2-22
it can be double, uint8, etc.
It would be awesome if i can do something like this in an embedded matlab
_int sum;
sum = atoi( string );
printf("Sum = %d\n", sum );_
Embedded matlab has a sprintf function that works like this but it cannot be used in embedded matlab.
The ASCII Encode function for xPC target is perfect but it can not be used for my Ti Target.
  3 个评论
Walter Roberson
Walter Roberson 2011-2-22
To confirm: you have a measurement that might be represented by any of the arithmetic types, and you would like to convert it to an ASCII representation using an arbitrary formatting including possibilities such as '%+09f.2' ?
Or are you saying that you only need to represent integral values and the data type could be whatever is most convenient for programming purposes ?
Doug Eastman
Doug Eastman 2011-2-23
Have you tried the xPC Target block? As far as I know it is not specific to the xPC kernel and could work on the TI processor.

请先登录,再进行评论。


Daniel
Daniel 2011-2-22
I need to keep this generic because i plan to use it has a debug probe, so i can spit out messages out a serial port. I don't want to create different functions for different data types. I have done this a lot in C using itoa(),atoi(), and printf() essentially being able to spit out anything.
Can simulink embedded package do a simple number to string conversion without s-function?
  1 个评论
Walter Roberson
Walter Roberson 2011-2-22
What you are asking for is not simple. glibc went through a lot of trouble to get the conversions right with proper rounding (consider, for example, denormalized numbers.)
I suggest that you consider using the vprintf() source from glibc, provided that the license terms are compatible with your purposes.
Can simulink embedded package do a simple number to string conversion without s-function? I don't know, but the embedded function list I referenced above is suggestive that it does not, as otherwise int2str() would probably be supported at a minimum.
vprintf() is a relatively large routine that would often take up too much memory to be worth-while.
It is not uncommon for programmers to find that they can live with (say) fixed point, two decimal places -- something easily done by round(x*100), convert to string, insert the '.' character at the proper location.

请先登录,再进行评论。


Daniel
Daniel 2011-2-23
I consider simple because i've programmed 8 bit microcontrollers with a 500 dollar compiler that had a simple to use function to convert a number to string and easy to use printf functions for rs232.
Are you suggesting to use vprintf() through s-function?
  2 个评论
Walter Roberson
Walter Roberson 2011-2-23
I've used such things too, but they didn't have floating point conversions in the ones I had.
Walter Roberson
Walter Roberson 2011-2-23
One of the vfprintf source files is at
http://www.opensource.apple.com/source/Libc/Libc-166/stdio.subproj/vfprintf.c
You would not be able to use this directly, as it needs stdarg for the arguments; you would need a layer that converted Matlab variable argument lists to stdarg . I do not know, however, whether embedded Matlab supports variable arguments.

请先登录,再进行评论。

类别

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

产品

Community Treasure Hunt

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

Start Hunting!

Translated by