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;

相关阅读 >>

Delphi 7 编写一个ie右键菜单项目:自动保存图片

Delphi 用dll实现插件的简单演示

Delphi中读取指定内存地址的值

Delphi xe listbox 行高根据内容高度进行调速

Delphi 双击listbox1内容,数据插入到memo1/synedit1鼠标指定位置

Delphi 捕捉异常:try..except..end

Delphi ttabcontrol控件使用

Delphi 使用泛型的 tarray 从动态数组中查找指定元素

Delphi twebbrowser流程讲解及如何判断下载网页成功

Delphi ini文件操作 tinifile、tmeminifile

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...