-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommon.pas
executable file
·279 lines (248 loc) · 7.35 KB
/
common.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
unit Common;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, StrUtils, UnitDatabase, Registry;
type
TScannerMemoryRow = class
Slot: Integer;
Bank: Integer;
BankName: String;
Frq: Real;
Modul: String;
Dly: Integer;
Lo: Integer;
FrqDescription: String;
SeenCount: Integer;
SeenDate: TDateTime;
Valid: boolean;
Model: String;
LookupForFrqDescriptions: Boolean;
function isFree: Boolean;
function Contains(text: String): Boolean;
procedure Clear;
procedure SetData(a_bank, a_slot, a_frq, a_modul, a_dly, a_lo: String);
procedure SetData(a_bank, a_slot:Integer; a_frq: Real; a_modul: String; a_dly, a_lo: Integer);
procedure UpdateData(a_frq, a_modul, a_dly, a_lo: String);
procedure UpdateData(memory_row: TScannerMemoryRow);
procedure SetExtraData;
end;
TScannerMemory = array of TScannerMemoryRow;
IScanner = interface
function UpdateSlot(memory_row: TScannerMemoryRow): Boolean;
function GetMemorySlots: TScannerMemory;
function NumberOfBanks: Integer;
function NumberOfSlots: Integer;
function Alive: SmallInt;
function ModelAliased: String;
function Model: String;
function PortId: String;
function ReadOnly: Boolean; // ostrożnie, to mówi czy calosciowy mechanizm jest readonly, nie zas czy w danym momencue jest readonly
function Loaded: Boolean;
function SupportsDescription:Boolean; // to mowi, czy skaner wspiera przechowywanie opisow
procedure Unload;
end;
function FindPosOfSlotInScannerMemory(scanner_memory: TScannerMemory; slot: Integer):Integer;
function FloatToFrq(frq: Real; sepatator: String = '.'; pad_left: Boolean = false):String;
function FrqToFloat(frq: String): Real;
function GetSerialPortFriendlyName(port: string): string;
function GetSerialPortNames: TStringList;
function GetSerialPortsInfo:String;
implementation
function TScannerMemoryRow.isFree: Boolean;
begin
Result:= (Frq <= 0);
end;
function TScannerMemoryRow.Contains(text: String): Boolean;
var
all_strings: String;
begin
Result:=false;
all_strings:=Lowercase(BankName + FloatToFrq(Frq) + FrqDescription);
text:=Lowercase(AnsiReplaceText(Trim(text),',','.'));
if text = IntToStr(Slot) then begin
Result:=true;
end;
if NPos(text, all_strings, 1) > 0 then begin
Result:=True;
end;
end;
procedure TScannerMemoryRow.Clear;
begin
Frq:=0;
Modul:='FM';
Dly:=0;
Lo:=0;
FrqDescription:='';
SetExtraData;
end;
procedure TScannerMemoryRow.SetData(a_bank, a_slot, a_frq, a_modul, a_dly, a_lo: String);
begin
Bank:=StrToInt(IfThen(Trim(a_bank) <> '', a_bank, '-1'));
Slot:=StrToInt(IfThen(Trim(a_slot) <> '', a_slot, '-1'));
Frq:=StrToFloat(AnsiReplaceText(Trim(a_frq),'.',','));
Modul:=Trim(a_modul);
Dly:=StrToInt(IfThen(Trim(a_dly) <> '', a_dly, '-1'));
Lo:=StrToInt(IfThen(Trim(a_lo) <> '', a_lo, '-1'));
SetExtraData;
end;
procedure TScannerMemoryRow.SetData(a_bank, a_slot:Integer; a_frq: Real; a_modul: String; a_dly, a_lo: Integer);
begin
Bank:=a_bank;
Slot:=a_slot;
Frq:=a_frq;
Modul:=Trim(a_modul);
Dly:=a_dly;
Lo:=a_lo;
SetExtraData;
end;
procedure TScannerMemoryRow.UpdateData(a_frq, a_modul, a_dly, a_lo: String);
begin
Frq:=FrqToFloat(a_frq);
Modul:=Trim(a_modul);
Dly:=StrToInt(IfThen(Trim(a_dly) <> '', a_dly, '-1'));
Lo:=StrToInt(IfThen(Trim(a_lo) <> '', a_lo, '-1'));
SetExtraData;
end;
procedure TScannerMemoryRow.UpdateData(memory_row: TScannerMemoryRow);
begin
Frq:=memory_row.Frq;
Modul:=memory_row.Modul;
Dly:=memory_row.Dly;
Lo:=memory_row.Lo;
SetExtraData;
end;
procedure TScannerMemoryRow.SetExtraData;
var
seen_data: TStringList;
begin
BankName:=IfThen(Bank = -1, IntToStr(Bank), FormDatabase.BankNameLookup(Model, Bank));
if LookupForFrqDescriptions then begin
FrqDescription:=IfThen(Frq > 0, FormDatabase.FrqDescriptionLookup(Frq), '');
end;
SeenCount:=0;
seen_data:=FormDatabase.FreqHeardLookup(Frq);
if seen_data.Count = 2 then begin
SeenCount:=StrToInt(seen_data[0]);
if (SeenCount > 0) and (seen_data[1] <> '') then begin
SeenDate:=StrToDateTime(seen_data[1]);
end;
end;
end;
function FindPosOfSlotInScannerMemory(scanner_memory: TScannerMemory; slot: Integer):Integer;
var
i: Integer;
begin
for i:=1 to Length(scanner_memory) do begin
if scanner_memory[i].Slot = slot then begin
Result:=i;
Exit;
end;
end;
Result:=-1;
end;
function GetSerialPortFriendlyName(port: string): string;
function FriendlyName(key: string; port: string): string;
var
r : TRegistry;
k : TStringList;
i : Integer;
ck: string;
rs: string;
begin
r := TRegistry.Create;
k := TStringList.Create;
r.RootKey := HKEY_LOCAL_MACHINE;
r.OpenKeyReadOnly(key);
r.GetKeyNames(k);
r.CloseKey;
try
for i := 0 to k.Count - 1 do
begin
ck := key + k[i] + '\'; // current key
// looking for "PortName" stringvalue in "Device Parameters" subkey
if r.OpenKeyReadOnly(ck + 'Device Parameters') then
begin
if r.ReadString('PortName') = port then
begin
//Memo1.Lines.Add('--> ' + ck);
r.CloseKey;
r.OpenKeyReadOnly(ck);
rs := r.ReadString('FriendlyName');
Break;
end // if r.ReadString('PortName') = port ...
end // if r.OpenKeyReadOnly(ck + 'Device Parameters') ...
// keep looking on subkeys for "PortName"
else // if not r.OpenKeyReadOnly(ck + 'Device Parameters') ...
begin
if r.OpenKeyReadOnly(ck) and r.HasSubKeys then
begin
rs := FriendlyName(ck, port);
if rs <> '' then Break;
end; // if not (r.OpenKeyReadOnly(ck) and r.HasSubKeys) ...
end; // if not r.OpenKeyReadOnly(ck + 'Device Parameters') ...
end; // for i := 0 to k.Count - 1 ...
result := rs;
finally
r.Free;
k.Free;
end; // try ...
end; // function findFriendlyName ...
begin
Result:=FriendlyName('\System\CurrentControlSet\Enum\', port);
end;
function GetSerialPortNames: TStringList;
var
reg : TRegistry;
l,v : TStringList;
n : integer;
begin
v := TStringList.Create;
l := TStringList.Create;
reg := TRegistry.Create;
try
reg.RootKey := HKEY_LOCAL_MACHINE;
if reg.OpenKeyReadOnly('HARDWARE\DEVICEMAP\SERIALCOMM') then begin
reg.GetValueNames(l);
for n := 0 to l.Count - 1 do begin
v.Add(reg.ReadString(l[n]));
end;
end;
finally
reg.Free;
end;
Result := v;
end;
function GetSerialPortsInfo:String;
var
ports, ports_ex: TStringList;
n: Integer;
begin
ports_ex:=TStringList.Create;
ports:=GetSerialPortNames;
for n := 0 to ports.Count - 1 do begin
ports_ex.Add(GetSerialPortFriendlyName(ports[n]));
end;
Result:=ports_ex.CommaText;
end;
function FrqToFloat(frq: String): Real;
begin
frq:= IfThen(Trim(frq) = '', '0', Trim(frq));
frq:= AnsiReplaceText(frq,'.',',');
Result :=StrToFloat(frq);
end;
function FloatToFrq(frq: Real; sepatator: String = '.'; pad_left: Boolean = false):String;
var
l_frq: TStringList;
u_frq: String;
begin
l_frq:=TStringlist.Create;
l_frq.Delimiter:=',';
l_frq.DelimitedText:=FloatToStr(frq);
if l_frq.Count = 1 then begin
l_frq.Add('0');
end;
u_frq:=IfThen(pad_left, AddChar('0',l_frq[0],4), l_frq[0]);
Result:= u_frq + sepatator + AddCharR('0',l_frq[1],4);
end;
end.