I am attempting to write a code which recognises which of my two serial devices are connected to which COM ports. This code will eventually be implemented in to a GUI on MATLAB's App Designer software. I have found a successful method of connecting to one of my serial devices at a time; this involves me listing all the previously taken COM ports and then asking my software to run through all the remaining COM ports until it finds one connected. I would then repeat this process for my second seral device however, as you can imagine this process involves the user staggering the USB connections made and in general it is more user input than I want.
Ideally, I would like the user to plug in both serial devices select one button on the GUI triggering a call-back which connects both devices. The code I have written below doesn't produce any errors and I have stepped through it line by line in MATLAB and it performs as expected, where I can write data to the serial devices. However, when I transfer it to the App Designer after the connection has been established I can't write/read any date to my device.
Below is the new code I developed. I have initially included the functions used and then titled the different call-backs from pushing buttons on the GUI:
function Startup(app):
%% Establishing which ports are already taken
% Produces a list of the COM port numbers that are unavailbale so that the
% app doesn't look for that one to connect to
taken_ports = serialportlist;
u = length(taken_ports);
for l = 1:u
taken_ports_2 = regexp(taken_ports(1,l),'\M','split');
taken_ports_3(l) = str2num(taken_ports_2(1,2));
end
function SerialPort(app)
% Now plug in the USB and it will recognise the correct serial port
for a=1:100
if a == app.takenports3 %if port is already taken it won't try to connect to it
app.LDS1000 = [];
elseif a~= app.takenports3
try
app.port_L=sprintf('COM%d',a);
app.LDS1000 = serialport(app.port_L,9600,'Parity','none','DataBits',8,'StopBits',1,'Timeout',5);
break
catch
end
end
end
function ND_SN(app)
% Detrmining the connection to the ND280/LDS by asking the
% machine what the SN Number is
write(app.ND280,app.ESC,"char");
write(app.ND280,app.A0400,"char");
write(app.ND280,app.CR,"char");
end
function Two_Connect(app)
if app.ND280.NumBytesAvailable ~= 0 %This means that the serial number has been read and Port N is now connected to the ND280
ND_port = regexp(app.port_N,'\M','split');
ND_Port_N = ND_port{1,2};
ND_Port_N2 = str2num(ND_Port_N);
app.takenports3(1,(app.u+1)) = ND_Port_N2; %Adding Port N to the taken_ports_list
% Configuring the terminator and the setting to interface mode
configureTerminator(app.ND280,'CR/LF'); %configure the terminator
read(app.ND280,13,"uint8"); % Need to read it so that this data doesn't get in the way of redaing future data
for ap=1:100
if ap == app.takenports3 %if port is already taken it won't try to connect to it
continue
elseif ap~= app.takenports3
try
app.port_L=sprintf('COM%d',ap);
app.LDS1000 = serialport(app.port_L,9600,'Parity','none','DataBits',8,'StopBits',1,'Timeout',5); %The LDS is now connected to Port L
break
catch
end
end
end
configureTerminator(app.LDS1000,'CR/LF'); %configure the terminator
elseif app.ND280.NumBytesAvailable == 0 %This means that the serial number wasn't read correctly and Port N is instead connected to the LDS.
app.ND280 = [];
app.LDS1000 = serialport(app.port_N,9600,'Parity','none','DataBits',8,'StopBits',1,'Timeout',5); %Connecting Port N to the LDS.
configureTerminator(app.LDS1000,'CR/LF'); %configure the terminator
LD_port = regexp(app.port_N,'\M','split');
LD_Port_N = LD_port{1,2};
LD_Port_N2 = str2num(LD_Port_N);
app.takenports3(1,(app.u+1)) = LD_Port_N2; %Adding Port N to the taken_ports_list
for ad=1:100
if ad == app.takenports3 %if port is already taken it won't try to connect to it
continue
elseif ad~= app.takenports3
try
app.port_L=sprintf('COM%d',ad);
app.ND280 = serialport(app.port_L,9600,'Parity','Even','DataBits',7,'StopBits',2,'Timeout',1); %The ND280 is now connected to Port L
break
catch
end
end
end
configureTerminator(app.ND280,'CR/LF'); %configure the terminator
end
end
Call-back for Connect button pushed:
ND280Conect(app)
ND_SN(app)
Two_Connect(app)