How to convert numbers into ASCII array in Matlab Function in Simulink?

Hi,
to send Data via serial communication to a certain device I have to transform three numbers into an ASCII array and afterwards into a uint8 array. The numbers usually look like this:
a = 1400; %always between 1000 and 2000
b = 20;
c = -10; %b and c are angles in [°], always between -45° and 45°
The resulting Arrays have to look like this:
string = [1400,20,-10,]
out = [49 52 48 48 44 50 48 44 45 49 48 44]
The ',' are important for the receiving device to understand the signal.
For now in simulation I used following code and it worked fine:
string = [num2str(a),',',num2str(b),',',num2str(c),','];
out = uint8(string);
But for the realtime-use I have to compile the Matlab Function which means I can not use num2str or any other function like sscanf, ssprintf ect...
Do you have any idea how I could obtain the same result without using num2str?
Sincerely,
Thomas
Edit: Found a Solution. See Comment below.

2 Commenti

Well.. i talked to a collegue this morning and we figured something out (that works explicitly for this task). If anyone is interested in it, here's a possible solution:
a = 1400;
b = -10;
%convert a
cnt = 4;
a_arr = zeros(cnt+1,1);
for i=cnt:-1:1
a_arr(i)=mod(a,10);
a=floor(a/10);
end
a_arr(5)=44; %','
%convert b
if b < 0
vz = 45; %'-'
b = -1*b;
else
vz=43; %'+'
end
cnt = 2;
b_arr=zeros(cnt+2,1);
for i=cnt:-1:1
b_arr(i+1)=mod(b,10)
b=floor(b/10)
end
b_arr(1)=vz;
b_arr(cnt+2)=44; %','
%c works like b
%put them together
out = zeros(9,1);
for i=1:1:5;
if u1_arr(i)<10 %numbers, ASCII->uint8->+48 (see ASCII table)
out(i)=a_arr(i)+48;
else %signs '-', '+' or ',' are already in uint8
out(i)=a_arr(i);
end
end
for i = 6:1:9
if b_arr(i-5)<10
out(i)=b_arr(i-5)+48;
else
out(i)=b_arr(i-5);
end
end
which results in
out = [49 52 48 48 44 45 49 48 44]
This way seems a bit clumsy, but it works and can be compiled.
Sincerely,
Thomas
Thanks for posting your self-solved problem. I was looking at doing this myself and was working out a way to do it and generate code.

Accedi per commentare.

Risposte (0)

Categorie

Scopri di più su Simulink in Centro assistenza e File Exchange

Richiesto:

il 24 Gen 2013

Community Treasure Hunt

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

Start Hunting!

Translated by