CPortLib控件如何检查到添加或者减少?
本帖最后由 sohappyoh 于 2015-6-10 23:06 编辑CPortLib控件如何检查到添加或者减少?
解决了:EnumComPorts(ComboBoxPORT.Items) 下面是具体代码:实现自动更新端口列表,并且保留原来的Index
procedure TForm1.EnumComPortLoopTimer(Sender: TObject);
var
ComNameBKP:String;
i, j: integer;
COMList: TStrings;
equ: Bool;
begin
if COMPort.Connected then
exit;
j:=0;
ComNameBKP:=ComboBoxPORT.Items;
equ := True;
COMList := TStringList.Create;
EnumComPorts(COMList);
if COMList.Count = ComboBoxPORT.Items.Count then
begin
for i := 0 to COMList.Count - 1 do
begin
if COMList <> ComboBoxPORT.Items then
equ := False;
if COMList = ComNameBKP then
j:=i;
end;
if (equ = False) then
EnumComPorts(ComboBoxPORT.Items);
end
else
begin
EnumComPorts(ComboBoxPORT.Items);
for i := 0 to COMList.Count - 1 do
begin
if COMList = ComNameBKP then
j:=i;
end;
end;
ComboBoxPORT.ItemIndex := j;
end;
原来的问题:
我的程序启动后,串口拔掉,但是ComComboBox1还是显示,如何实时监测减少或者增加?
我现在实现的是定时器隔一段时间,轮询打开和关闭,查询异常来做的,感觉不太正规!
页:
[1]