Friday 23 January 2009

Plotting Real Time Data

In the earlier post, I've described how to communicate with an external MCU from your PC through MATLAB. Now the following section describes how to get real-time data from it and how to plot it on the screen.

The program captures two multiplexed data sent by MCU. It then demultiplex it and plot the values periodcally into the screen.

The received data is stored in a 100x2 matrix, data. It is then plotted.

%==================Pgm Starts=========================%
try
fclose(instrfind)
end

s1 = serial('COM1','BaudRate',2400,'Parity','none','DataBits',8,'StopBits',1);
set(s1,'InputBufferSize',1024);
fopen(s1)
disp('Serial Communication Set Up Complete');

%async mode
s1.Terminator = 13;
s1.Timeout = 10; %Default
s1.ReadAsyncMode = 'continuous';
%readasync(s1);

k = 1;

data = zeros(100,2);

PLOT = 100;

figure(1);

%=======================Plot Set Up and Grab Handles==================%

subplot(2,1,1);
FirstPlot = plot(data(:,1),'g');
hold on;
axis([1 100 0 5]);
title('First Plot');

subplot(2,1,2);
SecondPlot = plot(data(:,2),'b');
hold on;
axis([1 100 0 5]);
title('SecondPlot');

set(FirstPlot,'Erase','xor');
set(SecondPlot,'Erase','xor');


while(1)

data(k,:) = fread(s1,2,'uint8');

%============Updata pLots==================%

set(FirstPlot,'YData',data(:,1));
set(Secondlot,'YData',data(:,2));
drawnow;
%========================================%

k = k+1;

if(k==PLOT)
k=1;
end

end


querrymail@gmail.com

Saturday 10 January 2009

Serial Communication for Real Time Data Capturing

Suppose that we want to send some data to a Microcontroller Unit and get data from it into our PC. MATLAB have wonderful features for implementing this. Here, I attach a program that will send a data to MCU and gets data from MCU. This is accomplished through serial communication. I used COM1 port of my PC.
The data sent by PC is received USART data register of MCU, and it is verified. This is done only for informing the MCU that we want values. On reception of the value MCU begins to send data which is captured by PC.

%==========================PROGRAM============================%

clear all

try
fclose(instrfind) %Close all bogus connections
end

%==First we need to make a serial communication object. Here it is s1.

s1 = serial('COM1','BaudRate',9600,'Parity','none','DataBits',8,'StopBits',1);
set(s1,'InputBufferSize',1024);
fopen(s1);
disp('Communication Set Up Ok')

%async mode
s1.Terminator = 13;
s1.Timeout = 10; %Default
s1.ReadAsyncMode = 'continuous'; %Set communicatio to Asynchronous Mode. You can also
%set it to 'manual' mode.
readasync(s1);


fwrite(s1,'A'); %Informing MCU


data = fread(s1,10,'uint8'); % reads 10 value from microcontroller

plot(data);



querrymail@gmail.com