-
Notifications
You must be signed in to change notification settings - Fork 3
/
Unit2.pas
161 lines (129 loc) · 3.31 KB
/
Unit2.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Registry, ExtCtrls;
type
TForm2 = class(TForm)
ListBox1: TListBox;
Label1: TLabel;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Panel1: TPanel;
Panel2: TPanel;
Panel3: TPanel;
Panel4: TPanel;
procedure ListBox1DblClick(Sender: TObject);
procedure FormActivate(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDeactivate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure ListBox1KeyPress(Sender: TObject; var Key: Char);
procedure Button3Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form2: TForm2;
implementation
{$R *.DFM}
uses Shared, Unit1;
var CommPortList:TStringList;
const InitList:boolean=true;
procedure FindPorts;
var reg:TRegistry;
i:integer;
s:string;
begin
reg:=TRegistry.Create;
CommPortList.Clear;
with Form2 do
begin
ListBox1.Clear;
with reg do
try
RootKey:=HKEY_LOCAL_MACHINE;
if OpenKeyReadOnly('hardware\devicemap\serialcomm') then
begin
try
GetValueNames(CommPortList);
for i:=0 to CommPortList.Count-1 do
begin
// showmessage(commportlist.strings[i]); // port description
s:=ReadString(CommPortList.Strings[i]); // port name
// showmessage(s);
ListBox1.Items.Add(s + ' = ' + CommPortList.Strings[i]);
CommPortList.Strings[i]:=s // overwrite description with name
end
except
showmessage('exception accessing registry')
end
end
finally
CloseKey;
reg.Free
end;
if ListBox1.Items.Count<>0 then ListBox1.ItemIndex:=0;
Button3.Enabled:=((ListBox1.ItemIndex<>-1));
ListBox1.SetFocus
end
end;
procedure TForm2.FormCreate(Sender: TObject);
begin
CommPortList:=TStringList.Create
end;
procedure TForm2.FormActivate(Sender: TObject); // also button1 click
begin
if InitList then
begin
InitList:=false;
FindPorts
end
end;
procedure TForm2.ListBox1DblClick(Sender: TObject);
begin
CommPortName:=CommPortList.Strings[ListBox1.ItemIndex];
InitFlag:=true;
Application.ProcessMessages;
Form1.Show;
Form2.Close
end;
procedure TForm2.ListBox1KeyPress(Sender: TObject; var Key: Char);
begin
if (Key=#13) and (ListBox1.ItemIndex<>-1) then
begin
CommPortName:=CommPortList.Strings[ListBox1.ItemIndex];
InitFlag:=true;
Application.ProcessMessages;
Form1.Show;
Form2.Close
end
end;
procedure TForm2.FormDeactivate(Sender: TObject);
begin
if not InitFlag then Form2.SetFocus
end;
procedure TForm2.Button1Click(Sender: TObject);
begin
halt
end;
procedure TForm2.Button2Click(Sender: TObject);
begin
FindPorts
end;
procedure TForm2.Button3Click(Sender: TObject);
begin
if ListBox1.ItemIndex<>-1 then
begin
CommPortName:=CommPortList.Strings[ListBox1.ItemIndex];
InitFlag:=true;
Application.ProcessMessages;
Form1.Show;
Form2.Close
end
end;
end.