Serial port data collection - help with loop

I'm new to MATLAB and I'm trying to read serial data from COM port 3. The baud rate is 115200. When I run the code below (I found this online), the data is "1x6 char", with one value of my data. How can I make a loop that will capture all of serial data (over a certain time period, and at a certain sampling rate) and create an array of the data? Also, how can I plot that data? Thanks!
s = serial('COM3'); %assigns the object s to serial port
set(s, 'InputBufferSize', 128); %number of bytes in input buffer
set(s, 'FlowControl', 'none');
set(s, 'BaudRate', 115200);
set(s, 'Parity', 'none');
set(s, 'DataBits', 8);
set(s, 'StopBit', 1);
set(s, 'Timeout',100);
%clc;
disp(get(s,'Name'));
prop(1)=(get(s,'BaudRate'));
prop(2)=(get(s,'DataBits'));
prop(3)=(get(s, 'StopBit'));
prop(4)=(get(s, 'InputBufferSize'));
disp([num2str(prop)]);
fopen(s); %opens the serial port
data = fscanf(s);
fclose(s); %close the serial port

답변 (1개)

Rakesh Chavan
Rakesh Chavan 2016년 1월 4일

0 개 추천

Hi,
The number of values to be read can be specified while using the 'fscanf' function. The function signature is:
>> A = fscanf(obj,'format',size)
where size specifies the number of values to be read. The documentation link given below provides more details: http://www.mathworks.com/help/matlab/ref/serial.fscanf.html
Based on the type of data you are reading you can use the plotting functions available in MATLAB. One of the functions is plot. Kindly refer to the documentation link for more information:
-regards

카테고리

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

태그

질문:

2015년 12월 28일

답변:

2016년 1월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by