How can you separate incoming serial data?

조회 수: 22 (최근 30일)
alex korzaniewski
alex korzaniewski 2013년 4월 20일
I have incoming serial data from an Arduino board that update continuously. The data is separated by questions marks (?). How would I go about reading the data in MATLAB and separating it into a (12x1) column vector so I can call an index of that vector? I have tried fscanf() but have been unable to get the results I want.
From Arduino:
152?-9?215?-669?2541?1273?-141?27?-452?20.30?97242.00?345.62? ect
delete(instrfind)
s=serial('COM4','BaudRate',9600); %start serial port to arduino
s.ReadAsyncMode = 'continuous';
fopen(s); %open port
while (s.Status == 'open')
C=fscanf(s, '?');
C
if(s.Status ~= 'open')
break;
end
end

채택된 답변

Matthias
Matthias 2013년 4월 20일
I don't have an Arduino, so I couldn't test it. But how about that:
c_str=regexp(C,'?','split');
  댓글 수: 1
alex korzaniewski
alex korzaniewski 2013년 4월 20일
편집: alex korzaniewski 2013년 4월 20일
Thank you so much for the quick reply. The code worked perfectly and did exactly what I wanted. I ended up with:
delete(instrfind)
s=serial('COM4','BaudRate',9600); %start serial port to arduino
s.ReadAsyncMode = 'continuous';
fopen(s); %open port
while (s.Status == 'open')
C=fscanf(s);
c_str = regexp(C, '?', 'split')
if(s.Status ~= 'open')
break;
end
end
With this, I was able to get a matrix where I could pull out certain indexes. Ex:
c_str =
Columns 1 through 6
'143' '-10' '216' '-59' '-11' '-206'
Columns 7 through 11
'-133' '-115' '-483' '21.70' '97483.00'
Columns 12 through 13
'324.90' [1x2 char]
c_str(10) =
'21.70'
This is real time data collected from a 10 DOF IMU.
Thanks again for the help!!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB Support Package for Arduino Hardware에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by