using the function waitfor() properly
古いコメントを表示
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 日
avram alter
2020 年 2 月 4 日
編集済み: avram alter
2020 年 2 月 4 日
Geoff Hayes
2020 年 2 月 4 日
Could you use a loop to continually read a line from the port until a string of length 24 is observed?
avram alter
2020 年 2 月 4 日
編集済み: avram alter
2020 年 2 月 4 日
Geoff Hayes
2020 年 2 月 4 日
You would need a variable for the output of the readline
data = readline(decision_port);
while strlength(data) < 24
data = readline(decision_port);
% should we add a pause here?
end
avram alter
2020 年 2 月 4 日
編集済み: avram alter
2020 年 2 月 4 日
Geoff Hayes
2020 年 2 月 4 日
maybe there is some whitespace in the string and so data is at least 24 characters. Try using strtrim to remove the whitespace (if that is the problem).
avram alter
2020 年 2 月 4 日
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.
avram alter
2020 年 2 月 4 日
採用された回答
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Graphics Performance についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!