using the function waitfor() properly

15 views (last 30 days)
I have a code that scans an incoming string from the COM3 serial port, and reads it into a variable. The serial port is constantly sending out this string:
Reader 1:
Reader 2:
Reader 1:
Reader 2:
and so on. there are 2 RFID scanners attached, and when a code is held up to one of them, serial will output soemthing like this:
Reader 1: 0000000f2d%
Reader 2:
Reader 1: 0000000f2d%
Reader 2:
as long as the tag is held by reader 1, this string is exactly 24 characters in length.
here is my code:
addpath('C:\Users\Administrator\Dropbox (********)\******** Team Folder\Matlab\RFID chip reader\RfidChipData');
filename = 'CorrectedRFIDValues.xlsx';
[~, sheets] = xlsfinfo(filename);
% decision = fscanf(tags{portidx});
% [decision, receivedcount] = fscanf(tags{portidx});
delete(instrfind());
deciding_port = serialport('COM3', 9600);
decision = waitfor(readline(deciding_port), strlength, 24));
% if receivedcount < 24
% error('Received less data than expected. Received data was: %s', decision)
% end
%now we know we've got at least 24 characters.
in = decision(11:24);
rows_found = [];
sheets_found = {};
for K = 1 : length(sheets)
this_sheet = sheets{K};
[~, ~, raw] = xlsread(filename, this_sheet);
[rowNum, colNum] = find( strcmp(in, raw));
if ~isempty(rowNum)
rows_found = [rows_found; rowNum];
sheets_found = [sheets_found; repmat({this_sheet}, length(rowNum), 1)];
end
end
I get an error
Error using strlength (line 39)
Not enough input arguments.
Error in Manual_auto_update2>Manual_auto_update2_OutputFcn (line 91)
decision = waitfor(readline(deciding_port), strlength, 24);
Error in gui_mainfcn (line 264)
feval(gui_State.gui_OutputFcn, gui_hFigure, [], gui_Handles);
Error in Manual_auto_update2 (line 42)
gui_mainfcn(gui_State, varargin{:});
if I put that line as
decision = waitfor(readline(deciding_port), strlength(readline(deciding_port)), 24);
I get the error
Error using strlength
Too many input arguments.
Error in Manual_auto_update2>Manual_auto_update2_OutputFcn (line 91)
decision = waitfor(readline(deciding_port), strlength(readline(deciding_port), 24));
Error in gui_mainfcn (line 264)
feval(gui_State.gui_OutputFcn, gui_hFigure, [], gui_Handles);
Error in Manual_auto_update2 (line 42)
gui_mainfcn(gui_State, varargin{:});
I cant define readline() as its own variable, as then it defeats the purpose of the waitfor().
any help is appreciated.
  10 Comments
Geoff Hayes
Geoff Hayes on 4 Feb 2020
wait - when you break out of the loop, which variable are you using? Look closely at your code
while strlength(readline(deciding_port)) <24
deciding_data = readline(deciding_port)
pause(1)
if strlength(readline(deciding_port)) == 24 || strlength(readline(deciding_port)) > 24
break
end
end
You are only setting deciding_data in the body of the loop. The condition just reads the line but doesn't store it anywhere and so outside of the while block you may be using the deciding_data variable which was set previously with a string that is not 24 characters in length. Try simplifying your code to
delete(instrfind());
deciding_port = serialport('COM3', 9600);
deciding_data = readline(deciding_port);
while strlength(deciding_data) < 24
deciding_data = readline(deciding_port);
pause(1);
end
% do something
Note how that everytime we call readline, we assign the result to deciding_data.
avram alter
avram alter on 4 Feb 2020
It doesn't seem to be reading it accurately. I keep getting these invalid unicode characters, which defeats the purpose of the tag. It isn't the reader, as that reads in a different IDE with no problem, and it isn't matlab, as my other code reads it well. is that an issue with how readline works?

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 4 Feb 2020
From the help:
Syntax
Note: no second input argument. What you did was
strlength(readline(deciding_port), 24)
or in other words
strlength(yourString, 24)
Why did you pass 24 into strlength()??? It doesn't want it, just like it said. It takes only one input - the string itself.
  2 Comments
avram alter
avram alter on 4 Feb 2020
Edited: avram alter on 4 Feb 2020
good catch on that. unfortunately, while that was a problem, it still does not solve my overlying issue. I fixed that in the code, but I think I am using waitfor() improperly. Geoff Hayes pointed out that waitfor() expects an object, while I am trying to pass a string retrived from an object. is there a function, or a method that makes matlab continue to scan a serial port, until a single line of a certain length is found?
Walter Roberson
Walter Roberson on 4 Feb 2020
is there a function, or a method that makes matlab continue to scan a serial port, until a single line of a certain length is found?
No. You can have a ByteAvailableFcn callback that does an fgets() of the serial port and checks the length and if the length is proper, stores the data somewhere accessible and calls uiresume() .

Sign in to comment.

More Answers (0)

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!