-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathMain.pas
More file actions
621 lines (472 loc) · 14.7 KB
/
Main.pas
File metadata and controls
621 lines (472 loc) · 14.7 KB
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
//------------------------------------------------------------------------------
//This Source Code Form is subject to the terms of the Mozilla Public
//License, v. 2.0. If a copy of the MPL was not distributed with this
//file, You can obtain one at http://mozilla.org/MPL/2.0/.
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// Omni-Rig
//
// Copyright (c) 2003 Alex Shovkoplyas, VE3NEA
//
// ve3nea@dxatlas.com
//------------------------------------------------------------------------------
unit Main;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ComCtrls, ExtCtrls, StdCtrls, CustRig, RigObj, RigSett, IniFiles, RigCmds,
AppEvnts, ComServ, AutoApp, AlStrLst, Spin, ShellApi, WinSpool,
ShlObj;
type
TMainForm = class(TForm)
Panel1: TPanel;
OkBtn: TButton;
CancelBtn: TButton;
TabControl1: TTabControl;
ApplicationEvents1: TApplicationEvents;
Timer1: TTimer;
Panel2: TPanel;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Label6: TLabel;
Label10: TLabel;
PortComboBox: TComboBox;
BaudRateComboBox: TComboBox;
DataBitsComboBox: TComboBox;
ParityComboBox: TComboBox;
StopBitsComboBox: TComboBox;
RtsComboBox: TComboBox;
RigComboBox: TComboBox;
Panel3: TPanel;
Image1: TImage;
Label7: TLabel;
Label8: TLabel;
Label9: TLabel;
Label12: TLabel;
Label14: TLabel;
PollSpinEdit: TSpinEdit;
TimeoutSpinEdit: TSpinEdit;
Label13: TLabel;
Label15: TLabel;
Label16: TLabel;
Label17: TLabel;
Label18: TLabel;
Label11: TLabel;
DtrComboBox: TComboBox;
procedure OkBtnClick(Sender: TObject);
procedure CancelBtnClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure TabControl1Changing(Sender: TObject;
var AllowChange: Boolean);
procedure TabControl1Change(Sender: TObject);
procedure ApplicationEvents1Message(var Msg: tagMSG; var Handled: Boolean);
procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
procedure FormHide(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Label15Click(Sender: TObject);
procedure Label17Click(Sender: TObject);
private
FLog: TFileStream;
FLogMode: integer;
procedure LoadRigCommands;
procedure LoadSettings;
procedure CleanRigTypes;
procedure SaveSettings;
procedure ListComPorts;
procedure ListBaudRates;
procedure WmTxQueue(var Msg: TMessage); message WM_TXQUEUE;
procedure WmComStatus(var Msg: TMessage); message WM_COMSTATUS;
procedure WmComParams(var Msg: TMessage); message WM_COMPARAMS;
procedure WmComCustom(var Msg: TMessage); message WM_COMCUSTOM;
procedure OpenLog;
procedure CloseLog;
procedure WMQueryEndSession(var Msg: TMessage); message WM_QUERYENDSESSION;
public
RigTypes: TAlStringList;
Rig1: TRig;
Rig2: TRig;
Sett1: TRigSettings;
Sett2: TRigSettings;
SetBothModes: boolean;
procedure ForceForeground;
function GetVersion: integer;
procedure Log(Msg: AnsiString); overload;
procedure Log(Msg: AnsiString; const Args: array of const); overload;
end;
var
MainForm: TMainForm;
implementation
{$R *.DFM}
//------------------------------------------------------------------------------
// sys
//------------------------------------------------------------------------------
procedure TMainForm.FormCreate(Sender: TObject);
begin
if ComServer.StartMode = smAutomation then Application.ShowMainForm := false;
OpenLog;
RigTypes := TAlStringList.Create;
Rig1 := TRig.Create;
Rig2 := TRig.Create;
Sett1 := TRigSettings.Create;
Sett2 := TRigSettings.Create;
Sett1.Port := 0;
Sett2.Port := 1;
Rig1.RigNumber := 1;
Rig2.RigNumber := 2;
ListComPorts;
ListBaudRates;
LoadRigCommands;
LoadSettings;
Sett1.ToRig(Rig1);
Sett2.ToRig(Rig2);
Rig1.Enabled := true;
Rig2.Enabled := true;
Panel2.Align := alClient;
Width := 214;
Label9.Caption := Format('Version %d.%d', [HiWord(GetVersion), LoWord(GetVersion)]);
{!}if ComServer <> nil then ComServer.UIInteractive := false;
end;
procedure TMainForm.FormDestroy(Sender: TObject);
var
i: integer;
begin
CloseLog;
Rig1.Free;
Rig2.Free;
Sett1.Free;
Sett2.Free;
for i:=0 to RigTypes.Count-1 do RigTypes.Objects[i].Free;
RigTypes.Free;
end;
procedure TMainForm.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
CanClose := (ComServer = nil) or (ComServer.ObjectCount = 0);
if not CanClose then Hide;
end;
procedure TMainForm.ListComPorts;
var
i: integer;
PortName: string;
Siz, Cnt: Cardinal;
Ports: array[0..255] of TPortInfo1;
begin
{!} Exit;
PortComboBox.Items.Clear;
if not EnumPorts(nil, 1, @Ports, SizeOf(Ports), Siz, Cnt) then Exit;
for i:=0 to Cnt-1 do
begin
PortName := Ports[i].pName;
SetLength(PortName, Length(PortName)-1); //delete ":" at the end
if Copy(PortName, 1, 3) = 'COM' then
PortComboBox.Items.Add(PortName);
end;
Log('COM ports found: ' + PortComboBox.Items.CommaText);
{
when this opens COM2, my rig switches to TX mode
PortComboBox.Items.Clear;
for i:=1 to 16 do
begin
PortName := '\\.\COM' + IntToStr(i);
Port := CreateFile(PChar(PortName), GENERIC_READ or GENERIC_WRITE, 0, nil, OPEN_EXISTING, 0, 0);
if (Port <> INVALID_HANDLE_VALUE) or (GetLastError = ERROR_ACCESS_DENIED)
then PortComboBox.Items.Add('COM' + IntToStr(i));
CloseHandle(Port);
end;
Log('COM ports found: ' + PortComboBox.Items.CommaText);
}
end;
procedure TMainForm.ListBaudRates;
const
//standard baud rates defined in Windows.pas as constants
BaudRates: array[0..14] of integer = (CBR_110, CBR_300, CBR_600, CBR_1200,
CBR_2400, CBR_4800, CBR_9600, CBR_14400, CBR_19200, CBR_38400, CBR_56000,
CBR_57600, CBR_115200, CBR_128000, CBR_256000);
var
i: integer;
begin
BaudRateComboBox.Items.Clear;
for i:=0 to High(BaudRates) do
BaudRateComboBox.Items.Add(IntToStr(BaudRates[i]));
end;
procedure TMainForm.Timer1Timer(Sender: TObject);
begin
Rig1.TimerTick;
Rig2.TimerTick;
end;
//------------------------------------------------------------------------------
// load/save INI
//------------------------------------------------------------------------------
function GetAfreetDataFolder: TFileName;
begin
SetLength(Result, MAX_PATH);
SHGetSpecialFolderPath(Application.Handle, @Result[1], CSIDL_APPDATA, true);
Result := PChar(Result) + '\Afreet\';
end;
function GetIniName: TFileName;
var
AppName: TFileName;
begin
if (GetVersion and $FF) < 6 //6.0 = Vista
then
Result := ChangeFileExt(ParamStr(0), '.ini')
else
begin
Result := GetAfreetDataFolder + 'Products\';
AppName := ChangeFileExt(ExtractFileName(ParamStr(0)), '');
Result := Result + AppName + '\';
try ForceDirectories(Result); except end;
Result := Result + AppName + '.ini';
end;
end;
procedure TMainForm.CleanRigTypes;
var
i: integer;
begin
for i:=0 to RigTypes.Count-1 do
RigTypes[i] := ChangeFileExt(RigTypes[i], '');
RigTypes.Sort;
RigTypes.Insert(0, 'NONE');
end;
procedure TMainForm.LoadRigCommands;
var
Cmds: TRigCommands;
i: integer;
Dir: TFileName;
begin
//list supported rig types
Dir := ExtractFilePath(ParamStr(0)) + 'Rigs\';
RigTypes.LoadFileList(Dir + '*.ini');
//load commands for each type
for i:=RigTypes.Count-1 downto 0 do
try
Log('Loading commands from "%s"', [RigTypes[i]]);
Cmds := TRigCommands.Create;
Cmds.FromIni(Dir + RigTypes[i]);
if Cmds.FLog.Count > 0 then Log('Errors:'#13#10 + Cmds.FLog.Text);
if Cmds.FLog.Count = 0
then RigTypes.Objects[i] := Cmds
else begin RigTypes.Delete(i); Cmds.Free; end;
except on E: Exception do
begin
Log(E.Message);
end;
end;
CleanRigTypes;
RigComboBox.Items := RigTypes;
end;
procedure TMainForm.LoadSettings;
var
Ini: TIniFile;
begin
Ini := TIniFile.Create(GetIniName);
try
Sett1.FromIni(Ini, 'RIG1');
Log('RIG 1 settings: ' + Sett1.Text);
Sett2.FromIni(Ini, 'RIG2');
Log('RIG 2 settings: ' + Sett2.Text);
SetBothModes := Ini.ReadBool('General', 'SetBothModes', false);
finally
Ini.Free;
end;
end;
procedure TMainForm.SaveSettings;
var
Ini: TIniFile;
begin
Ini := TIniFile.Create(GetIniName);
try
Sett1.ToIni(Ini, 'RIG1');
Sett2.ToIni(Ini, 'RIG2');
finally
Ini.Free;
end;
end;
//------------------------------------------------------------------------------
// user interface
//------------------------------------------------------------------------------
procedure TMainForm.FormShow(Sender: TObject);
begin
TabControl1.TabIndex := 0;
Panel2.Visible := true;
Panel3.Visible := false;
Sett1.FromRig(Rig1);
Sett2.FromRig(Rig2);
Sett1.ToControls;
ComNotifyVisible;
end;
procedure TMainForm.FormHide(Sender: TObject);
begin
ComNotifyVisible;
end;
procedure TMainForm.TabControl1Changing(Sender: TObject;
var AllowChange: Boolean);
begin
case TabControl1.TabIndex of
0: Sett1.FromControls;
1: Sett2.FromControls;
end;
end;
procedure TMainForm.TabControl1Change(Sender: TObject);
begin
case TabControl1.TabIndex of
0: Sett1.ToControls;
1: Sett2.ToControls;
end;
Panel2.Visible := TabControl1.TabIndex in [0,1];
Panel3.Visible := TabControl1.TabIndex = 2;
end;
procedure TMainForm.OkBtnClick(Sender: TObject);
begin
case TabControl1.TabIndex of
0: Sett1.FromControls;
1: Sett2.FromControls;
end;
Log('RIG 1 settings: ' + Sett1.Text);
Log('RIG 2 settings: ' + Sett2.Text);
Sett1.ToRig(Rig1);
Sett2.ToRig(Rig2);
SaveSettings;
Close;
end;
procedure TMainForm.CancelBtnClick(Sender: TObject);
begin
Close;
end;
//------------------------------------------------------------------------------
// single instance
//------------------------------------------------------------------------------
procedure TMainForm.ForceForeground;
var
CurrThreadID, ActiveThreadID: THandle;
begin
Show;
//to bring our own window to foreground,
//we attach temporarily to the active thread
CurrThreadID := GetCurrentThreadId;
ActiveThreadID := GetWindowThreadProcessId(GetForegroundWindow, nil);
AttachThreadInput(CurrThreadID, ActiveThreadID, true);
try SetForegroundWindow(Application.Handle);
finally AttachThreadInput(CurrThreadID, ActiveThreadID, false); end;
end;
procedure TMainForm.ApplicationEvents1Message(var Msg: tagMSG;
var Handled: Boolean);
begin
//another instance is asking us to show window
with Msg do
Handled := (Message = WM_USER) and (WParam = 73) and (LParam = 88);
if Handled then ForceForeground;
end;
function TMainForm.GetVersion: integer;
var
Dummy: DWord;
Buf: array of Byte;
Info: PVSFixedFileInfo;
Len: UINT;
begin
Result := 0;
SetLength(Buf, GetFileVersionInfoSize(PChar(ParamStr(0)), Dummy));
if Length(Buf) = 0 then Exit;
if not GetFileVersionInfo(PChar(ParamStr(0)), 0, Length(Buf), @Buf[0]) then Exit;
if not VerQueryValue(@Buf[0], '\', Pointer(Info), Len) then Exit;
if Len < SizeOf(TVSFixedFileInfo) then Exit;
Result := Info.dwFileVersionMS;
end;
procedure TMainForm.WmTxQueue(var Msg: TMessage);
begin
case Msg.WParam of
1: Rig1.CheckQueue;
2: Rig2.CheckQueue;
end;
end;
var
H: THandle;
procedure TMainForm.Button1Click(Sender: TObject);
var
Cfg: tCOMMCONFIG;
begin
Cfg.dwSize := sizeof(cfg);
CommConfigDialog('COM1', handle, Cfg);
end;
//------------------------------------------------------------------------------
// debugging log
//------------------------------------------------------------------------------
procedure TMainForm.OpenLog;
begin
//is logging enabled?
with TIniFile.Create(GetIniName)do
try FLogMode := ReadInteger('Debug', 'Log', 0); finally Free; end;
if FLogMode = 0 then Exit;
//create log file
try
FLog := TFileStream.Create(ChangeFileExt(GetIniName, '.log'),
fmCreate or fmShareDenyWrite);
except end;
Log('Omni-Rig started: Version %d.%d', [HiWord(GetVersion), LoWord(GetVersion)]);
end;
procedure TMainForm.CloseLog;
begin
Log('Omni-Rig stopped');
FreeAndNil(FLog);
end;
procedure TMainForm.Log(Msg: AnsiString; const Args: array of const);
begin
Log(Format(Msg, Args));
end;
procedure TMainForm.Log(Msg: AnsiString);
var
S: AnsiString;
begin
//logging disabled
if (FLog = nil) or (FLogMode = 0) then Exit;
//close/reopen log
if FLogMode = 2 then
begin
FreeAndNil(FLog);
try
FLog := TFileStream.Create(ChangeFileExt(GetIniName, '.log'),
fmOpenReadWrite or fmShareDenyWrite);
FLog.Seek(0, soFromEnd);
except end;
end;
S := FormatDateTime('hh:nn:ss.zzz ', Now) + Msg;
if Copy(S, Length(S)-1, 2) <> #13#10 then S := S + #13#10;
FLog.WriteBuffer(PAnsiChar(S)^, Length(S));
end;
procedure TMainForm.WmComStatus(var Msg: TMessage);
begin
DoComNotifyStatus(Msg.WParam);
end;
procedure TMainForm.WmComParams(var Msg: TMessage);
begin
DoComNotifyParams(Msg.WParam, Msg.LParam);
end;
procedure TMainForm.WmComCustom(var Msg: TMessage);
begin
DoComNotifyCustom(Msg.WParam, Pointer(Msg.LParam));
end;
procedure TMainForm.Label15Click(Sender: TObject);
begin
ShellExecute(GetDesktopWindow, 'open',
'http://www.dxatlas.com/OmniRig', '', '', SW_SHOWNORMAL);
end;
procedure TMainForm.Label17Click(Sender: TObject);
begin
ShellExecute(Application.Handle, nil,
'mailto:ve3nea@dxatlas.com?subject=OmniRig', '', '', SW_SHOWNORMAL);
end;
procedure TMainForm.WMQueryEndSession(var Msg: TMessage);
begin
if ComServer <> nil then ComServer.UIInteractive := false;
inherited;
end;
initialization
H := FindWindow('TApplication', 'Omni-Rig');
if H <> 0 then begin PostMessage(H, WM_USER, 73, 88); Halt; end;
CreateMutex(nil, False, 'OMNIRIG');
end.