- Published on
How to Remove In-Use COM Ports on Windows
- Authors

- Name
- Daisuke Kobayashi
- https://twitter.com
When working with embedded devices, every newly connected device can get assigned another COM port, and before long the port numbers may climb into the dozens.
This time I was asked at work to build a tool that could remove those accumulated ports, so I investigated several approaches. These are my notes from that work.
Remove them from Device Manager
Run the following commands from the command line. On Windows 7 and similar systems where UAC is enabled, be careful to launch the command prompt with administrator privileges, otherwise the devices will not be shown.
devmgmt.msc
In [View] -> [Show hidden devices], unused ports appear as semi-transparent entries, and you can remove them with the [Delete] key.
Remove them from the registry
COM port usage information is managed in the following registry value.
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\COM Name Arbiter\ComDB
ComDB is stored as a 32-byte binary value, and each port is judged to be in use or free based on whether the corresponding bit flag is set. The assignment seems to be arranged so that larger port numbers correspond to higher-order bits, as follows.
0000 4D 05 00 00 00 00 00 00
Com 8 7 6 5 4 3 2 1 4D -> 0 1 0 0 1 1 0 1
Com 16 15 14 13 12 11 10 9 05 -> 0 0 0 0 0 1 0 1
If you clear this information, new devices will start getting assigned from lower port numbers again. To do that, I wrote the following program.
int BitPosToClearFlag(const int value)
{
switch (value) {
case 1: return 0x01;
case 2: return 0x03;
case 3: return 0x07;
case 4: return 0x0f;
case 5: return 0x1f;
case 6: return 0x3f;
case 7: return 0x7f;
case 8: return 0xff;
default: return 0xff;
}
}
BOOL ClearComPort(const int com_number /* com_number 以降をクリアする */)
{
TCHAR szSubKey[256] = _T("SYSTEM\\CurrentControlSet\\Control\\COM Name Arbiter");
DWORD result, dwType;
HKEY hOpendKey;
BYTE pBuffer[32];
DWORD nMaxLength = sizeof(pBuffer);
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, szSubKey, 0, KEY_READ | KEY_WRITE,
&hOpendKey) == ERROR_SUCCESS) {
result = RegQueryValueEx(hOpendKey, _T("ComDB"), 0,
&dwType, (LPBYTE)pBuffer, &nMaxLength);
if (result != ERROR_SUCCESS)
return;
// バイナリのどの場所からクリアするか
int start_index = com_number / 8;
// 開始位置のビットクリアのためのフラグ
int clear_bit = BitToClearFlag((com_number + 7) % 8);
for (int i = start_index; i < nMaxLength; i++) {
if (i == start_index)
pBuffer[i] &= clear_bit;
else
pBuffer[i] = 0;
}
result = RegSetValueEx(hOpendKey, _T("ComDB"), 0, dwType,
pBuffer, nMaxLength);
RegCloseKey(hOpendKey);
if (result != ERROR_SUCCESS)
return FALSE;
}
return TRUE;
}
However, devices that had already been connected once still kept information in the registry, and they inherited the previous port number when reconnected. For example, in the case of FTDI chips, information remains under the following registry keys.
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\FTDIBUS HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\VID_0403&PID_6001
To clear that information, I tried deleting the registry keys with RegDeleteKey, but the deletion failed, perhaps due to permissions. So this time I used the tool called DevCon, described in the next section, to remove that information.
DevCon
DevCon is a debugging tool included in WinDDK. It is apparently a command-line utility with functionality equivalent to Device Manager. Using it, I was able to remove the remaining information from the registry as well. In my case I only needed to remove FTDI-related information, so I used the ftdiclean batch file below.
ftdiclean: A batch file that clears FTDI device information using DevCon
https://github.com/mss/ftdiclean
DevCon Reference:
http://msdn.microsoft.com/en-us/library/ff544707(v=vs.85).aspx
Japanese explanation:
http://blogs.msdn.com/b/jpwdkblog/archive/2009/06/16/devcon-setupdi-api-devcon.aspx
The following document explains how to stop port numbers from being incremented when a new device is connected.
http://liionbms.com/pdf/DisableFTDI_Enumeration.pdf
The following document describes countermeasures specific to FTDI chips.
http://www.ftdichip.com/Support/Documents/AppNotes/AN_123_How%20COM%20Ports_Are%20Allocated%20on%20Driver_Installation.pdf