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 一个拼图工具的制作思路

Delphi 检查父进程

Delphi 获取网络文件大小

Delphi中窗体的方法

Delphi 调用极光推送服务端http api实例

Delphi 随机字符3

Delphi 限制edit输入 多个例子

Delphi的tclientsocket组件和tserversocket组件(c/s)说明

Delphi webservices传base64字串

Delphi驱动开发研究第一篇--实现原理

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



打赏

取消

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

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

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

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

评论

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

    暂无评论...