Serial port data collection - help with loop

3 visualizzazioni (ultimi 30 giorni)
shambayati
shambayati il 28 Dic 2015
Risposto: Rakesh Chavan il 4 Gen 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

Risposte (1)

Rakesh Chavan
Rakesh Chavan il 4 Gen 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

Categorie

Scopri di più su Entering Commands in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by