Benjamin Avants asked . 2022-01-12

How can I identify COM port devices on Windows

I am working on several projects that involve using Arduinos and other serial connected devices with a MATLAB GUI. I have a solution for identifying the devices that opens all the COM ports one at a time and queries them. By checking the responses I can identify which devices are connected to which COM ports. This only works if the devices have unique responses to the query sent. I am currently using *IDN? as my query because it seems to be fairly standard. Of course, to interact with the Arduinos, I have to make sure they respond to this query in a predictable way.
 
 
My problem is that this process is fairly slow and a little error prone. In Windows Device Manager, each Port is identified by name and COM number. Is there any way to get these names inside of MATLAB? I want to be able to ID the ports without having to open connections to them all.

serial , arduino , ports , windows , devices

Expert Answer

Prashant Kumar answered . 2024-12-22 11:34:44

It will only work on Windows (7 for sure but maybe not others). I use DOS commands to query the registry in two places to identify which COM ports are connected and then check the USB section of the CurrentContrlSet to match up friendly names.
 
My code my not be completely optimized but it runs in about .1 seconds so it suits my purposes.
 
The result is a cell array with friendly names and COM number pairs for each connected USB-serial device that has a friendly name.
 
Code posted below:
 
Skey = 'HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM';
% Find connected serial devices and clean up the output
[~, list] = dos(['REG QUERY ' Skey]);
list = strread(list,'%s','delimiter',' ');
coms = 0;
for i = 1:numel(list)
  if strcmp(list{i}(1:3),'COM')
      if ~iscell(coms)
          coms = list(i);
      else
          coms{end+1} = list{i};
      end
  end
end
key = 'HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\';
% Find all installed USB devices entries and clean up the output
[~, vals] = dos(['REG QUERY ' key ' /s /f "FriendlyName" /t "REG_SZ"']);
vals = textscan(vals,'%s','delimiter','\t');
vals = cat(1,vals{:});
out = 0;
% Find all friendly name property entries
for i = 1:numel(vals)
  if strcmp(vals{i}(1:min(12,end)),'FriendlyName')
      if ~iscell(out)
          out = vals(i);
      else
          out{end+1} = vals{i};
      end
  end
end
% Compare friendly name entries with connected ports and generate output
for i = 1:numel(coms)
  match = strfind(out,[coms{i},')']);
  ind = 0;
  for j = 1:numel(match)
      if ~isempty(match{j})
          ind = j;
      end
  end
  if ind ~= 0
      com = str2double(coms{i}(4:end));
% Trim the trailing ' (COM##)' from the friendly name - works on ports from 1 to 99
      if com > 9
          length = 8;
      else
          length = 7;
      end
      devs{i,1} = out{ind}(27:end-length);
      devs{i,2} = com;
  end
end

 


Not satisfied with the answer ?? ASK NOW

Frequently Asked Questions

MATLAB offers tools for real-time AI applications, including Simulink for modeling and simulation. It can be used for developing algorithms and control systems for autonomous vehicles, robots, and other real-time AI systems.

MATLAB Online™ provides access to MATLAB® from your web browser. With MATLAB Online, your files are stored on MATLAB Drive™ and are available wherever you go. MATLAB Drive Connector synchronizes your files between your computers and MATLAB Online, providing offline access and eliminating the need to manually upload or download files. You can also run your files from the convenience of your smartphone or tablet by connecting to MathWorks® Cloud through the MATLAB Mobile™ app.

Yes, MATLAB provides tools and frameworks for deep learning, including the Deep Learning Toolbox. You can use MATLAB for tasks like building and training neural networks, image classification, and natural language processing.

MATLAB and Python are both popular choices for AI development. MATLAB is known for its ease of use in mathematical computations and its extensive toolbox for AI and machine learning. Python, on the other hand, has a vast ecosystem of libraries like TensorFlow and PyTorch. The choice depends on your preferences and project requirements.

You can find support, discussion forums, and a community of MATLAB users on the MATLAB website, Matlansolutions forums, and other AI-related online communities. Remember that MATLAB's capabilities in AI and machine learning continue to evolve, so staying updated with the latest features and resources is essential for effective AI development using MATLAB.

Without any hesitation the answer to this question is NO. The service we offer is 100% legal, legitimate and won't make you a cheater. Read and discover exactly what an essay writing service is and how when used correctly, is a valuable teaching aid and no more akin to cheating than a tutor's 'model essay' or the many published essay guides available from your local book shop. You should use the work as a reference and should not hand over the exact copy of it.

Matlabsolutions.com provides guaranteed satisfaction with a commitment to complete the work within time. Combined with our meticulous work ethics and extensive domain experience, We are the ideal partner for all your homework/assignment needs. We pledge to provide 24*7 support to dissolve all your academic doubts. We are composed of 300+ esteemed Matlab and other experts who have been empanelled after extensive research and quality check.

Matlabsolutions.com provides undivided attention to each Matlab assignment order with a methodical approach to solution. Our network span is not restricted to US, UK and Australia rather extends to countries like Singapore, Canada and UAE. Our Matlab assignment help services include Image Processing Assignments, Electrical Engineering Assignments, Matlab homework help, Matlab Research Paper help, Matlab Simulink help. Get your work done at the best price in industry.