Serial port data collection - help with loop

3 views (last 30 days)
shambayati
shambayati on 28 Dec 2015
Answered: Rakesh Chavan on 4 Jan 2016
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

Answers (1)

Rakesh Chavan
Rakesh Chavan on 4 Jan 2016
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

Categories

Find more on Entering Commands in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!