怎样判断COM口是否存在?(50分)

  • 主题发起人 主题发起人 luckhacker
  • 开始时间 开始时间
L

luckhacker

Unregistered / Unconfirmed
GUEST, unregistred user!
怎样判断COM口是否存在?
 
不用判断,总存在
 
用COMPORT组件<br><br>&nbsp; ComPort1.Port := 'COM3';<br>&nbsp; ComPort1.BaudRate := br9600;<br>&nbsp; ComPort1.DataBits := dbEight;<br>&nbsp; ComPort1.Parity.Bits := prNone;<br>&nbsp; ComPort1.StopBits := sbOneStopBit;<br>&nbsp; ComPort1.Open;<br>&nbsp; Result := ComPort1.Connected;
 
uses Registry;<br><br>procedure TForm1.Button3Click(Sender: TObject);<br>var <br>&nbsp; &nbsp; reg : TRegistry;<br>&nbsp; &nbsp; ts : TStrings;<br>&nbsp; &nbsp; i : integer;<br>begin<br>&nbsp; &nbsp; reg := TRegistry.Create;<br>&nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; &nbsp; reg.RootKey := HKEY_LOCAL_MACHINE;<br>&nbsp; &nbsp; &nbsp; &nbsp; reg.OpenKey('hardware/devicemap/serialcomm',false);<br>&nbsp; &nbsp; &nbsp; &nbsp; ts := TStringList.Create;<br>&nbsp; &nbsp; &nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; reg.GetValueNames(ts); //获得子键下的所有项<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for i := 0 to ts.Count -1 do<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin //把每一项下的串值显示在Memo1中<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Memo1.Lines.Add(reg.ReadString(ts.Strings));<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; finally<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ts.Free;<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; finally<br>&nbsp; &nbsp; &nbsp; &nbsp; reg.CloseKey;<br>&nbsp; &nbsp; &nbsp; &nbsp; reg.free;<br>&nbsp; &nbsp; end;<br>end;<br><br>以上程序可以判断哪些串口已经被使用.
 
向com口发一个消息,优先级别太高:P<br>在调试环境下,会返回一个messagebox说什么以被占用什么的<br>就这样了。
 
我觉得很简单,先打开该串口,判断是否能打开,能打开则有!
 
<br>&nbsp;procedure TForm1.Button1Click(Sender: TObject); &nbsp; <br>var &nbsp; <br>reg : TRegistry; &nbsp; <br>ts : TStrings; &nbsp; <br>i : integer; &nbsp; <br>begin &nbsp; <br>reg := TRegistry.Create; &nbsp; <br>reg.RootKey := HKEY_LOCAL_MACHINE; &nbsp; <br>reg.OpenKey('hardware/devicemap/serialcomm', &nbsp; <br>false); &nbsp; <br>ts := TStringList.Create; &nbsp; <br>reg.GetValueNames(ts); &nbsp; <br>for i := 0 to ts.Count -1 do begin &nbsp; <br>Memo1.Lines.Add(reg.ReadString(ts.Strings)); &nbsp; <br>end; &nbsp; <br>ts.Free; &nbsp; <br>reg.CloseKey; &nbsp; <br>reg.free; &nbsp; <br>end; &nbsp; <br>&nbsp;<br>
 
多人接受答案了。
 
后退
顶部