Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
instrhwinfo shows nonexistent ports

Subject: instrhwinfo shows nonexistent ports

From: Erik Cheever

Date: 1 Aug, 2011 19:55:27

Message: 1 of 3

I have two usb-serial adapters, one shows up on COM12 and one on COM13.

If I have the COM12 adapter connected when I start Matlab, and then remove it and connect the COM13 adapter, "instrhwinfo" still shows that COM12 is connected, and it doesn't see COM13.

Whichever adapter is present when MatLab is started shows up in the daqhwinfo list, and adapters added after MatLab is started do not show up.

Is there a way to refresh the list so that currently connected ports are returned by daqhwinfo.

If it matters, COM12 is an arduino UNO, and COM13 is an arduino MEGA.

Subject: instrhwinfo shows nonexistent ports

From: Ankit Desai

Date: 2 Aug, 2011 01:44:11

Message: 2 of 3

"Erik Cheever" <echeeve1@swarthmore.edu> wrote in message <j170bf$o5i$1@newscl01ah.mathworks.com>...
> I have two usb-serial adapters, one shows up on COM12 and one on COM13.
>
> If I have the COM12 adapter connected when I start Matlab, and then remove it and connect the COM13 adapter, "instrhwinfo" still shows that COM12 is connected, and it doesn't see COM13.
>
> Whichever adapter is present when MatLab is started shows up in the daqhwinfo list, and adapters added after MatLab is started do not show up.
>
> Is there a way to refresh the list so that currently connected ports are returned by daqhwinfo.
>
> If it matters, COM12 is an arduino UNO, and COM13 is an arduino MEGA.

Hi Erik,

Currently it is not possible to have the instrhwinfo output refresh for serial ports without restarting MATLAB. The list is created upon MATLAB's startup.

You can contact the technical support and put your vote for this feature. That helps the development team when they look at customer requests for features to work on in the future releases.

-Ankit

Subject: instrhwinfo shows nonexistent ports

From: Lawrence

Date: 1 Aug, 2013 02:39:07

Message: 3 of 3

"Erik Cheever" <echeeve1@swarthmore.edu> wrote in message <j170bf$o5i$1@newscl01ah.mathworks.com>...
> I have two usb-serial adapters, one shows up on COM12 and one on COM13.
>
> If I have the COM12 adapter connected when I start Matlab, and then remove it and connect the COM13 adapter, "instrhwinfo" still shows that COM12 is connected, and it doesn't see COM13.
>
> Whichever adapter is present when MatLab is started shows up in the daqhwinfo list, and adapters added after MatLab is started do not show up.
>
> Is there a way to refresh the list so that currently connected ports are returned by daqhwinfo.
>
> If it matters, COM12 is an arduino UNO, and COM13 is an arduino MEGA.

I know this is a pretty old thread but I did solve this issue. When I say solve, its more of a work around than a refresh of the 'instrhwinfo' return data. It is a follows:
(sorry for plain text, wont let me type it any other way)

% Check if the computer running the program is a PC or UNIX based system
if isunix % isunix will return 1 if a UNIX system is running matlab
    portBuffer = ls('/dev/tty.*'); % list all directories similar containing /dev/tty. in them
                                                 % and store it in the port buffer
else % if isunix = 0, then this is a PC
    portBuffer = ls('COM*'); % list all directories similar containing COM in them and
                                                 % store it in the port buffer
end

% Iterate over all characters in the port buffer to determine the size of
% the current port matrix
for i = 1:numel(portBuffer)
    
    curCharCodeBuffer = uint8(portBuffer(i)); % convert the character into an ascii
                                                                    %number
    if (curCharCodeBuffer == 9) % the delimiter between the ports is a tab, in
                                                               % ascii a tab is 9
        numPorts = numPorts + 1; % increment the number of ports if a tab is
                                                                  % found
        if length > maxLength % compare current port name length with
                                                               % max name length
            maxLength = length; % assign new max length
            length = 0; % reset length counter;
        end
    else % Increment the length is the char is not a tab
        length = length + 1;
    end
end

% Build the curPorts matrix with values calculated above (numPorts & maxLength)
curPorts = char(zeros(numPorts,maxLength));

% Iterate again over portBuffer's length to now determine the different
% port addresses
for i = 1:numel(portBuffer)
    
    curCharCodeBuffer = uint8(portBuffer(i)); % convert char into ascii number
    if ~(curCharCodeBuffer == 9) % compare ascii number to ascii tab
                                                                 % number, 9
        charBuffer = portBuffer(1,i);
        curPorts(row,column) = charBuffer; % cat the current character onto current port name row
        column = column + 1; % increment column counter for next
                                                                % character
    else
        row = row + 1; % If the current char is a tab, then move to
                                                           % next row and restart column counter
        column = 1;
    end
end

From here you have a nice matrix with each row being a char array of the current comPorts open and usable by matlab. From here I have a command that displays the names in a GUI drop down menu and lets the user select their current port. After that you must grab the port name from the 'curPorts' vector and cut off any remaining spaces that might appear at the end of the char array. These spaces might show up because of the presence of another currently usable port that's name is longer than the one you are looking for.

So,

for i = 1:numel(curPorts(myPort,:) % myPort is index val selected in the GUI
    if ~(strcmp(curPorts(myPort,i),' ')
        portNameLen = portNameLen + 1;
    end
end

Now when you can save the port name into some variable with the command:

someVar = curPorts(myPort,portNameLen);

And then you can open a serial com with:

s = serial(someVar);


Hope this helps.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us