delphi 获取系统串口列表  
官方Delphi 学习QQ群: 682628230(三千人)\n
频道

delphi 获取系统串口列表


var
  TotalComNum : Integer = 0;
  HaveOpenCom : string = '0';


USES Registry;


procedure GetComListFromReg();
var
  reg:TRegistry; // 注: 要引用Registry单元
  Namelst:TStrings;
  i,j:integer;
  str:string;
  b:Boolean;
begin
  Namelst:=TStringList.Create;
  reg:=TRegistry.Create;
  reg.RootKey :=HKEY_LOCAL_MACHINE;
  b := reg.OpenKey('HARDWARE\DEVICEMAP\SERIALCOMM',true);
  //ShowMessage('open ='+ BoolToStr(b));
  reg.GetValueNames(Namelst);
  //Form1.memo1.Clear;
  //for i := 0  to Namelst.Count-1 do
  // Form1.memo1.Lines.Add(reg.ReadString(Namelst[i]));
  //ShowMessage('namelst = ' + IntToStr(Namelst.Count) + 'totalcomnum = ' + IntToStr(TotalComNum));
  if Namelst.Count <> TotalComNum then
  begin
      TotalComNum  := Namelst.Count;
      if HaveOpenCom = '0' then
      begin
          //ShowMessage('123');
          Form8.ComboBox1.Items.Clear;
          for i := 0  to Namelst.Count-1 do
          begin
            //ShowMessage(reg.ReadString(Namelst[i]));
            Form8.ComboBox1.Items.Add(reg.ReadString(Namelst[i]));
          end;
        // 排序
          for i := 0  to Namelst.Count-2 do
            for j := i+1  to Namelst.Count-1 do
                if Form8.ComboBox1.Items[i] > Form8.ComboBox1.Items[j] then
                begin
                    str := Form8.ComboBox1.Items[i];
                    Form8.ComboBox1.Items[i] := Form8.ComboBox1.Items[j];
                    Form8.ComboBox1.Items[j] := str;
                end;
          Form8.ComboBox1.itemindex := 0;
      end
      else
      begin
          //ShowMessage('456');
          //ShowMessage('*******'+ HaveOpenCom);
          for i := 0  to Namelst.Count-1 do
          begin
              //ShowMessage('-------' + reg.ReadString(Namelst[i]));
              if HaveOpenCom = reg.ReadString(Namelst[i]) then
              begin
                  reg.CloseKey;
                  reg.Free;
                  Namelst.Free;
                  Exit;
                  //break;
              end;
          end;
          //ShowMessage('i=' + IntToStr(i) + 'count=' + IntToStr(Namelst.Count));
          if i = Namelst.Count then
          begin
              //ShowMessage('close');
             // Form8.Button1.Click;
          end;
          //ShowMessage('***i=' + IntToStr(i) + 'count=' + IntToStr(Namelst.Count));
      end;
  end;
  reg.CloseKey;
  reg.Free;
  Namelst.Free;
end;


procedure TForm8.FormCreate(Sender: TObject);
begin
GetComListFromReg();
end;


推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号

执行时间: 3.1831500530243 seconds