Solved

Too many serial ports, need to remove them

Posted on 2009-04-12
8
1,646 Views
Last Modified: 2013-11-05
I work for a company that does mobile phone (cellphone) unloicking - i.e. unlock to all networks. As part of this, they use cables that connect from the various models of phone to the PC via USB. They register as COM devices in Windows. The problem is that because each phone has a different serial number (probably the IMEI of the phone), every phone appears as a new serial device. This means that his unlock machine has over 150 serial port devices that are just sitting around and will never be used again. The software he uses cannot access port 200+, so every unlock he does he has to remove an old unused COM device, plug in the new one, then switch the COM port number of the new device to whatever the one was that he removed. For example, if he removed COM142, he adds the new device and changes the port number to COM142. It's getting rediculous.

I know that Windows XP stores COM device information in the registry, and would like to know if there's anything I can do to remove them all. I can program in VB.NET and C#, so if it's a case of conditionally removing registry entries I'm happy to do so. I have a virtual machine set up to allow me to revert to a snapshot in case I mess up the registry.

Any ideas how I can remove the ports?
0
Comment
Question by:burningmace
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 92

Expert Comment

by:nobus
ID: 24125299
go to device manager>view tab
click "show hidden devices" and delete them
that should do it
0
 
LVL 5

Author Comment

by:burningmace
ID: 24125397
Yeah, I know you can do that. Problem is there's like 200 of them and I want to remove them programatically.
0
 
LVL 92

Expert Comment

by:nobus
ID: 24126026
then i can't help you
0
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
LVL 70

Expert Comment

by:garycase
ID: 24126406
My code-writing days are long over ... but if I was going to do this, here's the approach I'd take, which should let you do what you want to do:

=>  Install one of the applications that track registry changes, such as the free RegRunner:  http://www.downloadsquad.com/2008/05/05/detect-and-track-registry-changes-with-regrunner/

=>  With RegRunner running, go to Device Manager and delete one of the ports.   You'll now know exactly what keys are modified in the registry ... so it should be fairly simple to write a program to make the modifications you want programmatically.    ... you may need to do a couple of deletions to identify the exact pattern; but I suspect it will be fairly obvious.

=>  To make this question useful for any future EE members with a similar issue, please post your results here :-)
0
 
LVL 15

Expert Comment

by:oobayly
ID: 24129698
You could download devcon, a command line tool for the Device Manager, and do the following:
  1. Create a process the calls devcon with the parameter as "findall =ports"
  2. Setup the process so that we can get the output of it
  3. Loop through each line of the output, and get the port DeviceID & Name
  4. For each port you want to remove, call devcon uninstall "@<Device ID>"
Devcon can be downloaded here:
http://support.microsoft.com/kb/311272

Also, I'll add some code on how to automate this, as it's something I'll find quite useful to have too.
0
 
LVL 15

Accepted Solution

by:
oobayly earned 150 total points
ID: 24129806
Here we go, I copied the devcon executable to c:\windows\system32

Note, as the Device ID may contain &, which is a special character in the shell, the Device ID needs to wrapped in quotes. Also it needs to be prepended by @, I don't know why, but it's done this in the MS example. If it's not prepended with @, the Device ID won't be matched, and nothing will happen.
      string pathToDevCon = @"C:\windows\system32\devcon.exe";
 
      /* Create the Process to retrieve all the ports
       * StartInfo is modified so that we can capture StdOut
       */
      Process ps = new Process();
      ps.StartInfo = new ProcessStartInfo(
        pathToDevCon, "findall =ports");
      ps.StartInfo.RedirectStandardOutput = true;
      ps.StartInfo.UseShellExecute = false;
      ps.Start();
      ps.WaitForExit();
 
      /* Read all the ports into a dictionary object:
       * Output from devcon is in the format <Device ID>        : <Description>
       * Final line lists number of devices returned
       */
      Dictionary<string, string> ports = new Dictionary<string, string>();
      do {
        string line = ps.StandardOutput.ReadLine();
        if (line == null) break;
 
        string[] parts = line.Split(':');
        if (parts.Length == 2) {
          ports.Add(parts[0].Trim(), parts[1].Trim());
        }
      } while (true);
      ps.Dispose();
 
      // Action to take, can be {uninstall | disable | enable}
      string action = "disable";
 
      // Loop through each device, and 
      foreach (string devID in ports.Keys) {
        bool doAction = true; // Use your own logic here to decide whether to remove the device 
        if (!doAction) continue;
 
        // Execute the action on the Device ID, again capturing StdOut
        Process psAction = new Process();
        ps.StartInfo = new ProcessStartInfo(
          pathToDevCon,
          string.Format("{0} \"@{1}\"", action, devID));
        ps.StartInfo.RedirectStandardOutput = true;
        ps.StartInfo.UseShellExecute = false;
        ps.Start();
        ps.WaitForExit();
 
        /* Echo only the 1st line of StdOut from the Process as the 2nd line
         * returns the number of devices affected
         */
        Console.Out.WriteLine(ps.StandardOutput.ReadLine());
 
        ps.Dispose();

Open in new window

0
 
LVL 5

Author Comment

by:burningmace
ID: 24133373
I've had to make a few alterations to your code. You defined psAction then use ps instead, which was probably a typo. I've also modified the code to only process COM ports by checking if the device name begins with the string "Communications Port (COM".

I'm going to the store tomorrow so I'll test it out in situe.
0
 
LVL 5

Author Closing Comment

by:burningmace
ID: 31569308
Excellent. With a few minor modifications this worked perfectly.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question