Warning in reading data from serial port.

I'm trying to read a single character 'A' from serial port, But it is issuing a warning always.
CharVar = fscanf(SerialObj,'%c',1);
Warning: Unsuccessful read: A timeout occurred before the Terminator was reached or SIZE values were available.

Answers (1)

Jan
Jan on 17 Aug 2013
The message seems to tell you, that Matlab did not receive a character and/or the corresponding terminator before the timeout occurred. So it might be useful to show us the command used for opening the port (especially the timeout value) and explain, if you are sure, that the character has been sent in the required period of time.

3 Comments

SerialObj = serial('COM1');
set(SerialObj,'BaudRate',9600)
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';
axes(handles.axes1);
fopen(SerialObj);
try
while(1)
% preview(vid,hImage);
% drawnow;
CharVar = fscanf(SerialObj,'%c',1);
if ~isempty(CharVar)
if strcmp(CharVar(1),'A')
while(1)
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]);
imshow(faceRegion)
% fclose(SerialObj);
facePoints = detectSURFFeatures(faceRegion,'MetricThreshold',10);
[faceFeatures, facePoints] = extractFeatures(faceRegion, facePoints);
load FaceDatabase.mat
for i = 1 : count
boxPairs = matchFeatures(faceFeatures, SurfFeatures(i).features);
dataPoints = facePoints(boxPairs(:, 1), :);
queryPoints = SurfFeatures(i).points(boxPairs(:, 2), :);
PointsArray(i).data = dataPoints;
PointsArray(i).query = queryPoints;
countArray(i) = queryPoints.Count;
end
[maxValue,maxIdx] = max(countArray);
if maxValue > 30
% figure;
% showMatchedFeatures(SurfFeatures(maxIdx).face, faceRegion, PointsArray(maxIdx).data, ...
% PointsArray(maxIdx).query, 'montage');
% title('Putatively Matched Points');
helpdlg(['Authorized, Face ID : ' num2str(maxIdx)],'Welcome');
fwrite(SerialObj,'A');
pause(10);
% [CharVar,CountVar,msg] = fscanf(SerialObj,'%c');
% if strcmp('S',CharVar(1))
% fwrite(SerialObj,'B');
% break;
% end
else
errordlg('Un-Authorized','Sorry');
fwrite(SerialObj,'B');
break;
end
end
continue;
end
else
preview(vid,hImage);
drawnow;
end
end
fclose(SerialObj);
catch
fclose(SerialObj);
end
This is my code. In this part,
CharVar = fscanf(SerialObj,'%c',1);
The warning was issued. But I receive the character in MATLAB. The main Problem is the delay.
What about increasing the value of the TimeOut?

Sign in to comment.

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Tags

Asked:

on 17 Aug 2013

Community Treasure Hunt

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

Start Hunting!