Taking data received from serial port as single element

Hi all,
i am receiving the ASCII values of some data say (*12) through serial port, then after that i am eliminating first character i.e(*)Then i am converting this ASCII values to Decimal values using 'char'. Later i need to process remaining data '12' as single element for giving it as an input to some other function. But problem is my function is taking '1' and '2' as separate values instead of one single value '12'. please give some suggestions in a way such that i should give '12' as input to my function.
Sample Code
ser1 = serial('COM27', 'BaudRate', 9600,'Terminator','*');
fopen(ser1);
while(1)
input3=fread(ser1);
input1=input3(2:end)
input2=char(input1)
input3=input2
output=input3+1;
end
If input is *12 expected output is 13. But i am getting output as 2(1+1),3(2+1)
Thanks in advance

 채택된 답변

Cedric
Cedric 2013년 3월 9일
편집: Cedric 2013년 3월 9일
You don't want to use CHAR, but SSCANF or STR2NUM, to convert your char input1 into a numeric variable.
>> input1 = '12' ;
>> class(input1)
ans =
char
>> str2num(input1)
ans =
12
>> class(ans)
ans =
double
>> sscanf(input1, '%d')
ans =
12
>> class(ans)
ans =
double

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 MATLAB에 대해 자세히 알아보기

태그

질문:

2013년 3월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by