Serial port data collection - help with loop

4 ビュー (過去 30 日間)
shambayati
shambayati 2015 年 12 月 28 日
回答済み: Rakesh Chavan 2016 年 1 月 4 日
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 日
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

カテゴリ

Help Center および File ExchangeEntering Commands についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by