DELPHI怎么自动搜索串口啊?

解决方案 »

  1.   


    function GetComPortList: TStrings;
    var
      Reg: TRegistry;
      sts1,sts2: TStrings;
      i: Integer;
      RegPath: string;  //注册表中存放串口路径
    begin
      Result := nil;
      Reg := TRegistry.Create;
      try
        sts1 := TStringList.Create;
        try
          sts2 := TStringList.Create;
          Reg.RootKey := HKEY_LOCAL_MACHINE;
          RegPath := 'hardware\devicemap\SerialComm';
          if Reg.OpenKey(RegPath, False) then
          begin
            Reg.GetValueNames(sts1);
            for i := 0 to sts1.Count-1 do
              sts2.Add(Reg.ReadString(sts1.Strings[i]));
          end;
          Result := sts2;
          sts2 := nil;
        finally
          FreeAndNil(sts1);
        end;
      finally
        Reg.CloseKey;
        FreeAndNil(Reg);
      end;
    end;