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

2 comments:

Anonymous said...

I like the post you put about the serial communication. The fruit of your hard work is always very sweet for you than for the others. Don't forget to visit me at http://creatingcomputervirus.blogspot.com/

Sukunath B A said...

Thank you friend