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 comentarios

Geoff Hayes
Geoff Hayes el 4 de Feb. de 2020
avram - the error message is indicating a problem with strlength which is a built-in MATLAB function and is not a property of the object that you are "waiting" on (which I think is just a character array - can you confirm this?) so I'm not sure if use of waitfor is appropriate in this case.
avram alter
avram alter el 4 de Feb. de 2020
Editada: avram alter el 4 de Feb. de 2020
the error is indeed with strlength()
according to the documentation, waitfor looks for an object, property of the object, and then the value of the property. I guess I cant use waitfor when dealing with a string from an object? my readline(serialport) is a stand-in for an object.
I want matlab to wait until the length of the firstline of incoming serial infomration is 24 characters in length. until that point, MATLAB should continue scanning that serial port, until the length is 24. do you know how that could be done?
Geoff Hayes
Geoff Hayes el 4 de Feb. de 2020
Could you use a loop to continually read a line from the port until a string of length 24 is observed?
avram alter
avram alter el 4 de Feb. de 2020
Editada: avram alter el 4 de Feb. de 2020
I am unsure as to how to do that.
my attempt was this
while strlength(readline(decision_port)) <24
readline(decision_port)
if strlength(readline(deciding_port)) == 24 || strlength(readline(deciding_port)) > 24
break
end
end
the problem is that skips the first line, and moves onto the second.
decision seems to be evaluating Reader 2: (which is the second line), and keeps skipping the first line.
since I didn't supress the output, I get
ans =
"Reader 2: "
I want it to evaluate reader 1, so I am looking for
ans =
"Reader 1: "
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
avram alter el 4 de Feb. de 2020
Editada: avram alter el 4 de Feb. de 2020
that works to put it onto reader one. the problem now is that I am getting inconsistencies. sometimes, it will scan fine, but other times, it will scan, and the loop will break, but the tag will not be registered, as the variable deciding_data will be equal to only the Reader 1: string, and ignore the rest. but the loop will break, as if the length is sufficient.
delete(instrfind());
deciding_port = serialport('COM3', 9600);
deciding_data = readline(deciding_port);
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
Geoff Hayes
Geoff Hayes el 4 de Feb. de 2020
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
avram alter el 4 de Feb. de 2020
still getting the same issue, that it will stop the loop, and the variable will not be recorded
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 el 4 de Feb. de 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?

Iniciar sesión para comentar.

 Respuesta aceptada

Image Analyst
Image Analyst el 4 de Feb. de 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 comentarios

avram alter
avram alter el 4 de Feb. de 2020
Editada: avram alter el 4 de Feb. de 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 el 4 de Feb. de 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() .

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Graphics Performance en Centro de ayuda y File Exchange.

Productos

Versión

R2019b

Etiquetas

Preguntada:

el 4 de Feb. de 2020

Comentada:

el 4 de Feb. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by