iterate through multiple serial ports, one after another (not simultaneously)
Show older comments
I have a program that utilizes incoming serial data to do a strcmp with data stored in excel. the string initially comes in looking like this:
Reader(n): 'String'
my code is set up to find the n, and then compare the serial string to the string stored in the Nth column of an excel spreadsheet. A bool value of 1 from this operation will change the Nth panel in a GUI to green, while a bool value of 0 will change the Nth panel to red.
At the moment, my code only iterates from n = 1:2. these are the n values coming in from COM3. I want to use 3 more COM ports, each one carrying n = 3:4, n = 5:6, and n = 7:8. However, I am unsure of how to initialize the ports to be read one after another, no simultaneously. this is my main function:
delete(instrfind('Port', 'COM3')); % removes possibility for 'Port not available' error
tag = serial('COM3'); %initializes the port to be used
fopen(tag); %opens th eport
BOX = char(zeros(2,14)); % matrix to be populated with incoming serial data
TrueValueData = 'C:\RfidChipTrueValues.xlsx';
% location of stored master tags
[~,~,TrueValMat] = xlsread(TrueValueData); % reads our excel file into the proper format
% Creates matrix filled with the correct values
% indexed by box, which is the first row
% all proceeding rows are the master value
for i=1:inf
for n = 1:2
if i>10 % positive reading
readData = fscanf(tag);
if length(readData)>12
BOX(str2double(readData(8)),1:14)= readData(11:24);
if strcmp(TrueValMat{2,n}, BOX(n,:)) %cannot sub-index to CELL types normally, must use this method
set(handles.uipanels(n), 'BackgroundColor', 'g');
else
set(handles.uipanels(n), 'BackgroundColor', 'r');
end
drawnow % allows GUI to update the appearance whenever the loop completes.
if strcmp(TrueValMat{2,1}, BOX(1,:))...
&& strcmp(TrueValMat{2,2}, BOX(2,:)) == 1
set(handles.pushbutton5, 'enable', 'on');
end
end
end
end
end
the GUI at this point works well, with only minor things to be ironed out in the future. the colors change correctly, etc.
This is all for only a single com port. how do I make this work with 3 additional ports?
I know about bytesavailablefcn, but I have no clues how to use it, even after hours spent reading about it, watching videos, etc. obviously I am open to using anything that works, but it will have to really be explained to me.
Thanks so much
Accepted Answer
More Answers (1)
avram alter
on 29 Oct 2019
5 Comments
Walter Roberson
on 29 Oct 2019
In the code presented, we do not see any evidence that you have created as many uipanels listed in handles.uipanels as there are ports in portlist . You appear to have only one entry in handles.uipanels .
You appear to be using GUIDE. If you have multiple objects in your figure that all have the same Tag value 'uipanels' then GUIDE would automatically store all of their handles in handles.uipanels (but in some undetermined order.) If you are creating the uipanels at run-time you need to store their handles in handles.uipanels yourself.
avram alter
on 29 Oct 2019
avram alter
on 29 Oct 2019
Walter Roberson
on 29 Oct 2019
Edited: Walter Roberson
on 29 Oct 2019
Note that you do not need the "== 1" part.
if strcmp(TrueValMat{2,1}, BOX(1,:)) ...
&& strcmp(TrueValMat{2,2}, BOX(2,:)) ...
&& strcmp(TrueValMat{3,1}, BOX(3, :))
set(handles.pushbutton5, 'enable', 'on');
end
avram alter
on 29 Oct 2019
Categories
Find more on Use COM Objects in MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!