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 日

0 投票

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

1 件のコメント

srikanth
srikanth 2013 年 3 月 11 日
sscanf is working.... thanks for your help

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMATLAB についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by