How to select available COM-port from a table and input this in a serialport() function using MatLab App Designer?

Hello, i have designed a app in MatLab App Designer and need to be able to select a COM port.
The data is recieved from the serialportlist() function as shown below:
function RefreshButtonPushed(app, event)
freeport = serialportlist("available");
freeport = freeport';
app.UITable.Data = freeport;
end
The user can click the different rows to select the desired COM-port and the selection is saved in port.
function UITableCellSelection(app, event)
global port
port = event.Indices;
end
The selected port is then supposed to be put into the function serialport("COM#",115200). My problem is that i cannot figure out how to make the selected port appear in the "COM#". The selected port only outputs:
Error using serialport (line 116)
Unable to connect to the serialport device at port '2 1'. Verify that a device is connected to the port, the port is not in use, and all serialport input arguments and parameter values are supported by the device.
function Mode2ButtonPushed(app, event)
global s2;
global mode2;
global port;
formatSpec = "%s";
A1 = num2str(port);
str = sprintf(formatSpec,A1);
mode2 = 2;
s2 = serialport(str,115200);
write(s2,mode2,"uint8");
delete(s2);
clear s2;
end

1 commentaire

I am only able to extract the row and column of the selected window inside the table. How can i actually extract "COM3" and "COM5"?

Connectez-vous pour commenter.

Réponses (1)

I managed to solve the issue myself. First i made the global port point to the selected row and column as shown below:
function UITableCellSelection(app, event)
global port
indices = event.Indices;
data = app.UITable.Data;
row = indices(:,1);
port = app.UITable.Data(row,:);
end
In the mode button i made put the global port inside the serialport() function.
function Mode1ButtonPushed(app, event)
global s1;
global mode1;
global port;
mode1 = 1;
s1 = serialport(port,115200);
write(s1,mode1,"uint8");
delete(s1);
clear s1;
end

Community Treasure Hunt

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

Start Hunting!

Translated by