using the function waitfor() properly
10 次查看(过去 30 天)
显示 更早的评论
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 个评论
Geoff Hayes
2020-2-4
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.
采纳的回答
Image Analyst
2020-2-4
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 个评论
Walter Roberson
2020-2-4
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() .
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Whos 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!