gpt4 book ai didi

delphi - 串行 (COM) 端口名称或标识

转载 作者:行者123 更新时间:2023-12-03 15:07:40 27 4
gpt4 key购买 nike

我有一个使用 cport 访问多个串行端口的程序。

为了进行配置,到目前为止,我只是在组合框中列出了所有可用的端口以进行选择,但是具有(虚拟)串行接口(interface)的驱动程序数量的增加使得最终用户的配置变得很麻烦。

当前的检测适用于 createfile(),但存在的问题是您只能获取存在/不存在,并且可能“忙”作为信息。

但是为了改进,我需要每个 COM 端口一个标识字符串,就像它所连接的硬件设备/驱动程序(设备管理器)一样。这将使用户更容易缩小兼容范围(因为我们提供的串行卡数量有限)

可能可以从 WMI 获得,但那是一个丛林,某人是否有更具体的信息或更好的代码?

(Delphi XE3、Win7+,请勿提供需要额外安装或部署的解决方案)

最佳答案

如果您想枚举 COM 端口并获得一个友好的名称,您可以使用 SetupAPIGUID_DEVINTERFACE_COMPORT设备接口(interface)类。

尝试这个示例

{$APPTYPE CONSOLE}

{$R *.res}

uses
Windows,
SysUtils,
JvSetupApi;

const
GUID_DEVINTERFACE_COMPORT:TGUID='{86E0D1E0-8089-11D0-9CE4-08003E301F73}';

procedure EnumerateCOMPorts;
var
cbRequired : DWORD;
hdev : HDEVINFO;
idev : Integer;
did : TSPDeviceInterfaceData;
pdidd : PSPDeviceInterfaceDetailData;
PropertyBuffer : array[0..255] of Char;
DeviceInfoData: TSPDevInfoData;
PropertyRegDataType: DWORD;
RequiredSize: DWORD;
begin
// enumerate the com ports
hdev := SetupDiGetClassDevs(@GUID_DEVINTERFACE_COMPORT, nil, 0, DIGCF_PRESENT OR DIGCF_DEVICEINTERFACE);
if ( INVALID_HANDLE_VALUE <> THandle(hdev) ) then
begin
try
idev:=0;
ZeroMemory(@did, SizeOf(did));
did.cbSize := SizeOf(did);
repeat
if (SetupDiEnumDeviceInterfaces(hdev, nil, GUID_DEVINTERFACE_COMPORT, idev, did)) then
begin
cbRequired := 0;
SetupDiGetDeviceInterfaceDetail(hdev, @did, nil, 0, cbRequired, nil);
if (ERROR_INSUFFICIENT_BUFFER= GetLastError()) then
begin
pdidd:=AllocMem(cbRequired);
try
pdidd.cbSize := SizeOf(TSPDeviceInterfaceDetailData);
DeviceInfoData.cbSize:= SizeOf(DeviceInfoData);
RequiredSize:=0;
if (SetupDiGetDeviceInterfaceDetail(hdev, @did, pdidd, cbRequired, RequiredSize, @DeviceInfoData)) then
begin

PropertyRegDataType:=0;
RequiredSize:=0;
if SetupDiGetDeviceRegistryProperty(hdev, DeviceInfoData, SPDRP_FRIENDLYNAME, PropertyRegDataType, PBYTE(@PropertyBuffer[0]), SizeOf(PropertyBuffer), RequiredSize) then
Writeln(Format('Friendly Name - %s',[PropertyBuffer]));

if SetupDiGetDeviceRegistryProperty(hdev, DeviceInfoData, SPDRP_DEVICEDESC, PropertyRegDataType, PBYTE(@PropertyBuffer[0]), SizeOf(PropertyBuffer), RequiredSize) then
Writeln(Format('Description - %s',[PropertyBuffer]));

if SetupDiGetDeviceRegistryProperty(hdev, DeviceInfoData, SPDRP_LOCATION_INFORMATION, PropertyRegDataType, PBYTE(@PropertyBuffer[0]), SizeOf(PropertyBuffer), RequiredSize) then
Writeln(Format('Location - %s',[PropertyBuffer]));
end
else
RaiseLastOSError;
finally
FreeMem(pdidd);
end;
end;
end
else
Break;
inc(idev);
until false;
finally
SetupDiDestroyDeviceInfoList(hdev);
end;
end;
end;

begin
try
if not LoadsetupAPI then exit;
EnumerateCOMPorts;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
readln;
end.

这将返回类似这样的内容

enter image description here

注意:JvSetupApi 单元是 JVCL 库的一部分。

关于delphi - 串行 (COM) 端口名称或标识,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16100798/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com