Communication with PIC Microcontroller

I have designed a face recognition system. If the face is recognized, I'm sending a character 'a' via serial port to microcontroller. If the face is not authorized, I'm sending a character 'b' via serial port to microcontroller.
Also, When I switch on the controller, it sends a character 'c' to MATLAB, at that time only it acquires the face image.
I have designed the face recognition system, Now my problem is how can I read the data from Microcontroller?
The code keeps on running and when MATLAB reads a character from serial port, it acquires the image. How to do it?

 Respuesta aceptada

Walter Roberson
Walter Roberson el 14 de Ag. de 2013

1 voto

serial() to create a port object, fopen() to open it. Configure it with a bytes available count of 1. Configure it to not be in line mode, including configuring the terminator as empty. fread() from the port, requesting 1 character. You can either loop the fread(), "polling" for actions, or you can set the bytes available function to have a call-back when the data is available.

7 comentarios

Sabarinathan Vadivelu
Sabarinathan Vadivelu el 14 de Ag. de 2013
Sir, my problem is, My program keeps on running, It should acquire face only when I receive a character from Microcontroller. How to do it?
Walter Roberson
Walter Roberson el 14 de Ag. de 2013
You will need to show your code.
function varargout = Enroll_GUI(varargin)
% ENROLL_GUI MATLAB code for Enroll_GUI.fig
% ENROLL_GUI, by itself, creates a new ENROLL_GUI or raises the existing
% singleton*.
%
% H = ENROLL_GUI returns the handle to a new ENROLL_GUI or the handle to
% the existing singleton*.
%
% ENROLL_GUI('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in ENROLL_GUI.M with the given input arguments.
%
% ENROLL_GUI('Property','Value',...) creates a new ENROLL_GUI or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before Enroll_GUI_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to Enroll_GUI_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help Enroll_GUI
% Last Modified by GUIDE v2.5 12-Aug-2013 18:03:09
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @Enroll_GUI_OpeningFcn, ...
'gui_OutputFcn', @Enroll_GUI_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before Enroll_GUI is made visible.
function Enroll_GUI_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to Enroll_GUI (see VARARGIN)
% Choose default command line output for Enroll_GUI
handles.output = hObject;
global vid
vid = videoinput('winvideo', 1, 'YUY2_640x480');
src = getselectedsource(vid);
vid.FramesPerTrigger = 1;
vidRes = get(vid, 'VideoResolution');
nBands = get(vid, 'NumberOfBands');
hImage = image( zeros(vidRes(2), vidRes(1), nBands) );
preview(vid, hImage);
vid.ReturnedColorspace = 'rgb';
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes Enroll_GUI wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = Enroll_GUI_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in Capture_PushButton.
function Capture_PushButton_Callback(hObject, eventdata, handles)
% hObject handle to Capture_PushButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global vid capturedImage faceRegion
capturedImage = getsnapshot(vid);
faceDetector = vision.CascadeObjectDetector();
bboxes = step(faceDetector, capturedImage);
faceRegion = imcrop(capturedImage,bboxes);
faceRegion = rgb2gray(faceRegion);
obj = fspecial('unsharp');
faceRegion = imfilter(faceRegion,obj);
faceRegion = imresize(faceRegion,[400 400]);
axes(handles.axes1);
imshow(faceRegion)
% --- Executes on button press in Enroll_PushButton.
function Enroll_PushButton_Callback(hObject, eventdata, handles)
% hObject handle to Enroll_PushButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global faceRegion
facePoints = detectSURFFeatures(faceRegion,'MetricThreshold',10);
[faceFeatures, facePoints] = extractFeatures(faceRegion, facePoints);
if exist('FaceDatabase.mat','file')
load FaceDatabase.mat
count = count + 1;
SurfFeatures(count).features = faceFeatures;
SurfFeatures(count).points = facePoints;
SurfFeatures(count).face = faceRegion;
save FaceDatabase.mat SurfFeatures count
helpdlg(['Face Enrolled. Face ID : ' num2str(count)],'Success')
else
SurfFeatures(1).features = faceFeatures;
SurfFeatures(1).points = facePoints;
SurfFeatures(1).face = faceRegion;
count = 1;
save FaceDatabase.mat SurfFeatures count
helpdlg(['Face Enrolled. Face ID : ' num2str(count)],'Success')
end
% --- Executes on button press in New_PushButton.
function New_PushButton_Callback(hObject, eventdata, handles)
% hObject handle to New_PushButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
arrayfun(@cla,findall(0,'type','axes'))
global vid
vid = videoinput('winvideo', 1, 'YUY2_640x480');
src = getselectedsource(vid);
vid.FramesPerTrigger = 1;
vidRes = get(vid, 'VideoResolution');
nBands = get(vid, 'NumberOfBands');
hImage = image( zeros(vidRes(2), vidRes(1), nBands) );
preview(vid, hImage);
vid.ReturnedColorspace = 'rgb';
% --- Executes on button press in Delete_PushButton.
function Delete_PushButton_Callback(hObject, eventdata, handles)
% hObject handle to Delete_PushButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
errordlg('Database Deleted','Warning');
delete('FaceDatabase.mat');
This is the code I had wrote.
This code Enrolls a face in the database.
Sabarinathan Vadivelu
Sabarinathan Vadivelu el 14 de Ag. de 2013
Now my question is this code runs forever. But when I receive any character from the serial port the image should be acquired. Likewise I need to change it.
Walter Roberson
Walter Roberson el 14 de Ag. de 2013
It appears to me that the code you show would capture one image per time that you push the Capture Pushbutton. Do you see something different?
You have no incorporated any of the serial port code into your routines.
Sabarinathan Vadivelu
Sabarinathan Vadivelu el 14 de Ag. de 2013
Yes sir, Till that only I had done it. Now I need to know how can I do that above mentioned question.
Walter Roberson
Walter Roberson el 14 de Ag. de 2013
serial() to create the port object... and so on, as I wrote above.
If you want code examples, search MATLAB Answers for "serial"

Iniciar sesión para comentar.

Más respuestas (0)

Productos

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by