Изменить номер COM-порта программно

3693
MatthewKing

В диспетчере устройств я могу щелкнуть правой кнопкой мыши последовательное устройство, перейти на вкладку «Параметры порта», нажать «Дополнительно» и изменить номер COM-порта в раскрывающемся меню.

Мне нужно автоматизировать этот шаг на большом количестве компьютеров. Есть ли способ написать этот процесс или сделать это программно?

4
Это все одна и та же версия Windows? RockPaperLizard 9 лет назад 0
Это не так - это может быть WinXP, Win7 или Win8. Сказав это, я с радостью приму решение, которое работает только на одном (лучше, чем ничего!) MatthewKing 9 лет назад 0
Хммм .... любопытно, какое конкретное применение для этого процесса? mdpc 9 лет назад 0

1 ответ на вопрос

2
Jim Fell

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.