Change COM Port Number programmatically

5

2

In Device Manager, I can right click on a serial device, navigate to the Port Settings tab, click Advanced, and change the COM Port Number via a drop-down menu.

I need to automate this step across a large number of PCs. Is there a way to script this process, or to do it programmatically?

Matthew King

Posted 2014-12-11T06:14:54.683

Reputation: 153

Are they all the same version of Windows? – RockPaperLizard – 2014-12-11T06:46:50.130

They are not - could be WinXP, Win7, or Win8. Having said that, I'll happily accept a solution that only works on one (better than nothing!) – Matthew King – 2014-12-11T06:51:54.540

Hmmm....curious what is the specific application for this process? – mdpc – 2014-12-11T07:22:04.867

Answers

3

This question is probably best asked on stackoverflow. That said, take a look at ComDBReleasePort and ComDBClaimPort in the COM Port Database. However, changing COM port assignments programmaticly is not exactly considered a best practice. For example, what happens if two more more concurrent applications are fighting to get the same COM assignment?

Windows dynamically assigns COM port numbers for a good reason. Ideally, your application should be written so that it searches for the specific COM port it needs. This can be done via polling or by using the Windows Management Interface (WMI).

If you're referencing some application that someone else wrote, and you're just stuck with someone else's program design (or lack thereof), then you may want to consider adding a batch script to modify the registry key under HKLM\Hardware\DEVICEMAP\SERIALCOMM at startup, since the new value(s) are not likely to hold between re-boots.

I don't know how how well this last option would work, but if you're a serious PowerShell or Python user, you could theoretically use either scripting language to directly access the APIs mentioned at the beginning of my response for a one-time COM port re-assignment.

Jim Fell

Posted 2014-12-11T06:14:54.683

Reputation: 5 069