[英]How can I change a serial COM port number programmatically?
here is my problem: I developed a custom serial driver that relies on Microsoft serenum.sys to get serial ports enumerated. 这是我的问题:我开发了一个依赖于Microsoft serenum.sys的自定义串行驱动程序来枚举串行端口。 Now, I applied this serial driver on top of a multifunction device that splits a single device in six different serial ports - when this happens, the port numbering is completely scrambled, so I need that Serial0 gets COM1, for example, and so on.
现在,我将这个串行驱动程序应用在一个多功能设备之上,该设备将一个设备分成六个不同的串口 - 当发生这种情况时,端口编号完全被扰乱,所以我需要Serial0获取COM1,例如,依此类推。
I searched a bit, and found this: Change COM port via registry, command line or software? 我搜索了一下,发现这个: 通过注册表,命令行或软件更改COM端口?
I tried to use ComDB to get the port names that I need, but while I can make port names available, I don't seem to have an efficient way to associate a SPECIFIC serial port (ie. Serial0) with a SPECIFIC COM port number (ie. COM1). 我尝试使用ComDB来获取我需要的端口名称,但是虽然我可以使端口名称可用,但我似乎没有一种有效的方法将SPECIFIC串行端口(即Serial0)与SPECIFIC COM端口号相关联(即COM1)。
Has anyone already managed to solve this issue? 有没有人设法解决这个问题?
Solved in two parts, with still an open issue. 解决了两个部分,仍然是一个悬而未决的问题。 Starting from the WDK Driver samples on GitHub, https://github.com/Microsoft/Windows-driver-samples
从GitHub上的WDK驱动程序示例开始, https://github.com/Microsoft/Windows-driver-samples
On the SerialReadSymName()
function, I changed it to return the symbolic name I wanted, rather than have it use the index provided from ComDB. 在
SerialReadSymName()
函数中,我将其更改为返回我想要的符号名称,而不是让它使用ComDB提供的索引。 Note that I had to remove serenum from UpperFilters key in the inf file to have it work with this. 请注意,我必须从inf文件中的UpperFilters键中删除serenum才能使用它。
Now I had the working device name (ie. COM1, COM2 etc.) that could be used, but at this stage I got the serial port "Friendly Name" renamed anyway. 现在我有了可以使用的工作设备名称(即COM1,COM2等),但是在这个阶段我仍然重命名了串口“友好名称”。 To solve this contextually, I updated the
SerialCreateOpen()
with the following code: 为了解决这个问题,我使用以下代码更新了
SerialCreateOpen()
:
if (!extension->FriendlyNameSet) //Added this guard to the device extension { DWORD f_set = 1; HANDLE keyHandle; MySerialSetFriendlyName(extension); extension->FriendlyNameSet = TRUE; }
With the function MySerialSetFriendlyName()
defined as follows: 使用
MySerialSetFriendlyName()
函数定义如下:
NTSTATUS MySerialSetFriendlyName(PSERIAL_DEVICE_EXTENSION pDevExt)
{
NTSTATUS status = STATUS_SUCCESS;
WCHAR *FriendlyName, *fnprefix, *fnsuffix, *instanceId;
ULONG FriendlyNameLength, instanceLength, temp, i;
fnprefix = BASE_FRIENDLY_NAME_PREFIX_STR;
fnsuffix = BASE_FRIENDLY_NAME_SUFFIX_STR;
temp = pDevExt->InstanceIdentifier;
instanceLength = 0;
while (temp)
{
instanceLength++;
if (temp < 10)
temp = 0;
else
{
temp /= 10;
if (!temp)
instanceLength++;
}
}
if (instanceLength)
{
instanceId = ExAllocatePool(NonPagedPool, instanceLength);
temp = pDevExt->InstanceIdentifier;
WCHAR digit = L'X';
for (i = 0; i < instanceLength; i++)
{
switch (temp % 10)
{
case 0: digit = L'0'; break;
case 1: digit = L'1'; break;
case 2: digit = L'2'; break;
case 3: digit = L'3'; break;
case 4: digit = L'4'; break;
case 5: digit = L'5'; break;
case 6: digit = L'6'; break;
case 7: digit = L'7'; break;
case 8: digit = L'8'; break;
case 9: digit = L'9'; break;
default: digit = L'X'; break;
}
instanceId[instanceLength - i - 1] = digit;
temp /= 10;
}
FriendlyNameLength = BASE_FRIENDLY_NAME_PREFIX_LEN + instanceLength + BASE_FRIENDLY_NAME_SUFFIX_LEN + 1;
FriendlyName = ExAllocatePool(NonPagedPool, FriendlyNameLength * sizeof(WCHAR));
RtlZeroMemory(FriendlyName, FriendlyNameLength * sizeof(WCHAR));
for (i = 0; i < BASE_FRIENDLY_NAME_PREFIX_LEN; i++)
{
FriendlyName[i] = fnprefix[i];
}
for (i = 0; i < instanceLength; i++)
{
FriendlyName[i + BASE_FRIENDLY_NAME_PREFIX_LEN] = instanceId[i];
}
for (i = 0; i < BASE_FRIENDLY_NAME_SUFFIX_LEN; i++)
{
FriendlyName[i + BASE_FRIENDLY_NAME_PREFIX_LEN + instanceLength] = fnsuffix[i];
}
if (pDevExt->PnpRegistryKey)
status = RtlWriteRegistryValue(RTL_REGISTRY_ABSOLUTE, (PCWSTR)pDevExt->PnpRegistryKey, (PCWSTR)L"FriendlyName", REG_SZ, FriendlyName, FriendlyNameLength * sizeof(WCHAR));
else
status = STATUS_INSUFFICIENT_RESOURCES;
KdPrintEx((DPFLTR_DEFAULT_ID, DPFLTR_ERROR_LEVEL, "%s :: Changed friendly name to %S (NTSTATUS %08X)\n", __FUNCTION__, FriendlyName, status));
if (!NT_SUCCESS(status))
{
status = STATUS_SUCCESS; //it is enough that we came through here
}
}
return status;
}
( BASE_FRIENDLY_NAME_...
are macros that store my chosen friendly name prefix (let's say L"Communications Port (COM"
) and suffix (let's say L")"
. You can make them whatever you need, but you really need to store their length in separate macros, then) (
BASE_FRIENDLY_NAME_...
是存储我选择的友好名称前缀的宏(让我们说L"Communications Port (COM"
)和后缀(让我们说L")"
。你可以BASE_FRIENDLY_NAME_...
制作它们,但你真的需要存储它们在单独的宏中的长度,然后)
That way, friendly name would be set at first device open. 这样,友好名称将在第一个设备打开时设置。 To be sure that this means that device gets the right name right away, I just wrote a Device Coinstaller that on the
DIF_INSTALLDEVICE
command, on post-processing, quickly opened and closed the port. 为了确保这意味着设备立即获得了正确的名称,我刚刚在
DIF_INSTALLDEVICE
命令上编写了一个Device Coinstaller,在后处理时,快速打开并关闭了端口。 This deserves an article in itself and it is simple enough, so I won't publish this now. 这本身就值得一篇文章,这很简单,所以我现在不会发表这篇文章。
The only open issue is this: - This way, ComDB still memorizes the wrong numbering sequence. 唯一开放的问题是: - 这样,ComDB仍然记忆错误的编号序列。 You can force it to behave as you wish by changing the
Com Name Arbiter
bitmasks in the Windows Registry, as specified in the link I provided in the question, but you cannot do that for COM1 and COM2, that are still bound to certain I/O ranges and IRQs. 您可以通过更改Windows注册表中的
Com Name Arbiter
掩码来强制它按照您所希望的方式运行,如我在问题中提供的链接中所指定的那样,但您不能对COM1和COM2执行此操作,但仍然绑定到某些I / O范围和IRQ。
So, it works, provided you will never need to access ComDB in your applications! 因此,只要您不需要在应用程序中访问ComDB,它就可以工作!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.