USB Interfacing and Real Time Data Plotting With MATLAB
USB Interfacing and Real Time Data Plotting With MATLAB
USB Interfacing and Real Time Data Plotting With MATLAB
Table of Contents
Introduction................................................................................................................................................3
Intro.......................................................................................................................................................3
Required Materials................................................................................................................................3
MATLAB Programming............................................................................................................................3
Install Required Functions.....................................................................................................................3
Open COM Port.....................................................................................................................................4
Setup the Stripchart...............................................................................................................................5
Timer Function......................................................................................................................................5
Callback Function..................................................................................................................................6
Appendix....................................................................................................................................................6
MATLAB Realtime Plot Code..............................................................................................................6
MATLAB getData function...................................................................................................................7
References..................................................................................................................................................9
Introduction
Intro
This tutorial will explain the process of creating a real time plot using MATLAB. Data from a
USB port will be read and plotted on a virtual oscilloscope. We will use a device that will
write data to a USB port. Then in MATLAB we will write a program to continuously read and
display the value from the port.
Required Materials
The following materials are required to follow the tutorial:
1. A device that is continuously writing to the USB port, this can be a microcontroller or a USB
based sensor (Note: you need to know what the data stream looks like)
2. MATLAB programming knowledge
MATLAB Programming
Install Required Functions
To easily implement real-time data plotting we will use a pre-made function available in
MATLAB File Exchange: http://www.mathworks.com/matlabcentral/fileexchange/
Go to the following website and download the 'Test and Measurement Widgets' function by
Scott Hirsch: http://www.mathworks.com/matlabcentral/fileexchange/4722-test-andmeasurement-widgets/content/demo_tmwidgets.m
Then set the MATLAB path to point to the files we just downloaded. To do this, in MATLAB
click File Set Path. A new window will come up as shown in Figure 1. Click Add Folder...
and browse to the location of the Strip Chart folder. Click Save and close the window.
The complete MATLAB code is located in the Appendix titled 'MATLAB Realtime Plot Code'
and 'MATLAB getData function'. What follows are some explanations and code segments that
we have used in this project. (These code segments are only for teaching purposes, refer to the
appendix for the specific values we used to interface with our USB device).
For our system a device is connected to the USB port that continuously reads ADC data and
sends it over to the USB. Our device also needs to be sent a start command to begin
transmission. The data stream has this format (your device will vary):
-- Payload
-uint8 token
3
-uint8 pktcnt
-uint8 msgSize
-uint8 deviceID
-uint16 pktctr
-uint8 adc_chan[ADC_SAMPLES][CHAN_PER_DEV]
11 samples 4 channels
rows
columns
-uint8 accel[3]
We need to write MATLAB code that will parse this data stream. But before that is done, we
need a way for MATLAB to communicate with our device. The following section will
describe how this is accomplished.
This code will open and connect to COM3 with a speed of 256000 baud and 8 bits of data
(make sure you know which COM port your USB device is connected to, in my case the
device is connected to USB COM port 3 with a baud rate of 256000)
figure(1); stripchart(Fs,AxesWidth);
For more info on the stripchart function, type 'help stripchart' in the MATLAB console. A
picture of the stripchart figure is shown in Figure 2.
Timer Function
Next we need a way to continuously read the COM port at fixed time intervals. For this case
we will use a timer function to accomplish this task. The following code will create a timer
object.
%% Setup a timer
% The timer has a callback that reads the serial port and updates the
% stripchart
t = timer('TimerFcn', @(x,y)getData(board), 'Period', 0.1);
set(t,'ExecutionMode','fixedRate');
start(t);
We created and started a timer called t that will execute every 0.1sec. When the timer is called
it will also run the callback function called 'getData' which will be explained in the next
section.
Callback Function
Every time the timer function finishes its countdown we will tell it to read the data from the
COM port and update the strip chart with that data. To do this we will create a new function
called 'getData'. In MATLAB click File New Script and copy the following code:
function getData(board)
data = fread(board,10);
% read 10 bytes
fread(board, board.BytesAvailable);
The function 'getData' will read 10 bytes from the COM port, clear the COM port buffer and
update the stripchart. Note: (normally it is not a good idea to clear the buffer because we will
lose some data. The UBS device we are using has a control byte called TOKEN that we will
use to synchronize with it.)
This completes the MATLAB code. After clicking the play button you should know be able to
read and plot data from a device connected to your USB port.
Appendix
MATLAB Realtime Plot Code
close all;
%
clear all;
%
clc;
%
fclose('all');
%
delete(instrfindall);
delete(timerfindall);
% *************************************************
% Constants
% *************************************************
TOKEN
= 238;
BAUDERATE
= 256000;
INPUTBUFFER = 512;
START
= [255 7 3]; % 0XFF, 0X07, 0X03
STOP
= [255 9 3]; % 0XFF, 0X09, 0X03
TMR_PERIOD = 0.003;
payload = zeros(100,1);
%% Parse the Data
start = 1;
for j = 1:1:PACKETS
% Wait for token to arrive for start of payload
while (fread(board,1) ~= TOKEN)
end
payload(1:100,1) = fread(board, 100);
%fread(board, board.BytesAvailable);
disp('Reading data');
m = 6;
for i=start:1:start+10
data1(i) = payload(m);
data2(i) = payload(m+1);
data3(i) = payload(m+2);
data4(i) = payload(m+3);
end
%
%
%
%
Channel
Channel
Channel
Channel
1
2
3
4
sample
sample
sample
sample
i
i
i
i
m=m+4;
end
start = start + 11;
References
"File Exchange Pick of the Week." Advanced MATLAB: Timer Objects. Web. 30 Apr. 2012.
<http://blogs.mathworks.com/pick/2008/05/05/advanced-matlab-timer-objects/>.
"Thread Subject: Serial Port at 115200 Baud Rate for Real Time Plot." Serial Port at
115200 Baud Rate for Real Time Plot. Web. 30 Apr. 2012.
<http://www.mathworks.com/matlabcentral/newsreader/view_thread/240440>.