-
Notifications
You must be signed in to change notification settings - Fork 3
/
Unit1.pas
693 lines (608 loc) · 20.9 KB
/
Unit1.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
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
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ComCtrls, ExtCtrls;
type
TForm1 = class(TForm)
GroupBox1: TGroupBox;
UpDown1: TUpDown; // channel up/down
Label1: TLabel; // channel display
Label2: TLabel; // frequency display
CheckBox1: TCheckBox; // LPD433 check box
GroupBox2: TGroupBox;
RadioButton1: TRadioButton; // odd parity
RadioButton2: TRadioButton; // even parity
RadioButton3: TRadioButton; // no parity
GroupBox3: TGroupBox;
RadioButton4: TRadioButton; // 1 stop bit
RadioButton5: TRadioButton; // 1.5 stop bits
RadioButton6: TRadioButton; // 2 stop bits
GroupBox4: TGroupBox;
RadioButton7: TRadioButton; // 7 data bits
RadioButton8: TRadioButton; // 8 data bits
ComboBox1: TComboBox; // baud rate selector
ComboBox2: TComboBox; // operating mode selector
ComboBox3: TComboBox; // Tx power selector
Button1: TButton; // perform factory reset
Button2: TButton; // read configuration button
Button3: TButton; // send configuration button
Label3: TLabel;
Panel1: TPanel;
Panel2: TPanel;
Panel3: TPanel;
Panel4: TPanel;
procedure FormCreate(Sender: TObject);
procedure FormActivate(Sender: TObject);
procedure UpDown1Click(Sender: TObject; Button: TUDBtnType);
procedure CheckBox1Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure ComboBox1or2Change(Sender: TObject);
procedure FormDblClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
uses shared;
var CommFile: THandle;
RxBuffer:string;
const Running:boolean=false; // respond to buttons only if true
const EOL=''; // use #13+#10 for CR-LF terminated
DLY=0; // delay between sending command strings
function strip(s:string):string; // strip trailing control characters
begin
while (length(s)<>0) and (s[length(s)]<#32) do delete(s,length(s),1);
strip:=s
end;
function at2ok(s:string):string; // convert AT command into OK response
begin
if (length(s)>=1) and (s[1]='A') then s[1]:='O';
if (length(s)>=2) and (s[2]='T') then s[2]:='K';
at2ok:=s
end;
procedure ChannelUpdate;
var s:string;
begin
with Form1 do
begin
s:=inttostr(UpDown1.Position);
while length(s)<3 do s:='0'+s;
Label1.Caption:=s;
s:=floattostrF(433.0+UpDown1.Position*0.4, ffFixed,4,1)+' MHz';
Label2.Caption:=s
end
end;
procedure InitControls;
begin
with Form1 do
begin
UpDown1.Position:=1; // channel 1
ChannelUpdate;
ComboBox1.ItemIndex:=3; // 9600 baud
ComboBox2.ItemIndex:=2; // FU3
ComboBox3.ItemIndex:=7; // 100mw/20dBm
RadioButton3.Checked:=true; // no parity
RadioButton4.Checked:=true; // 1 stop bit
RadioButton8.Checked:=true // 8 data bits
end
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Application.Title:=Form1.Caption;
Form1.DoubleBuffered:=true;
// most configuration must be carried out in the first call to
// FormActivate as the comm port hasn't been selected yet.
CheckBox1.Checked:=true; // LPD433 locked
InitControls
end;
procedure TForm1.FormActivate(Sender: TObject);
var DeviceName:array [0..80] of char;
DCB:TDCB;
Config:string;
CommTOs:TCommTimeouts;
proc:string;
begin
if InitFlag then
begin
InitFlag:=false;
RxBuffer:='';
try
StrPCopy(DeviceName, '\\.\'+CommPortName);
proc:='CreateFile';
CommFile:=CreateFile(DeviceName,
GENERIC_READ or GENERIC_WRITE,
0, Nil,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, 0);
if (CommFile=INVALID_HANDLE_VALUE) then
begin
showmessage('Serial I/O error: '+proc+' failed'+#13+#13+
'device = '+Devicename+#13+
'handle = 0x'+IntToHex(CommFile,8));
halt
end;
proc:='SetupComm';
if not SetupComm(CommFile, 1024, 1024) then
begin
showmessage('Serial I/O error: '+proc+' failed'+#13+#13+
'device = '+Devicename+#13+
'handle = 0x'+IntToHex(CommFile,8));
halt
end;
proc:='GetCommState';
if not GetCommState(CommFile, DCB) then
begin
showmessage('Serial I/O error: '+proc+' failed'+#13+#13+
'device = '+Devicename+#13+
'handle = 0x'+IntToHex(CommFile,8));
halt
end;
Config:='baud=9600 parity=n data=8 stop=1'#0;
proc:='BuildCommDCB';
if not BuildCommDCB(@Config[1], DCB) then
begin
showmessage('Serial I/O error: '+proc+' failed'+#13+#13+
'device = '+Devicename+#13+
'handle = 0x'+IntToHex(CommFile,8));
halt
end;
proc:='SetCommState';
if not SetCommState(CommFile, DCB) then
begin
showmessage('Serial I/O error: '+proc+' failed'+#13+#13+
'device = '+Devicename+#13+
'handle = 0x'+IntToHex(CommFile,8));
halt
end;
with CommTOs do
begin
ReadIntervalTimeout := 0; // 0
ReadTotalTimeoutMultiplier := 0; // 0
ReadTotalTimeoutConstant := 300; // 300
WriteTotalTimeoutMultiplier := 0; // 0
WriteTotalTimeoutConstant := 300 // 300
end;
proc:='SetCommTimeouts';
if not SetCommTimeouts(CommFile, CommTOs) then
begin
showmessage('Serial I/O error: '+proc+' failed'+#13+#13+
'device = '+Devicename+#13+
'handle = 0x'+IntToHex(CommFile,8));
halt
end
except
try CloseHandle(CommFile); except end;
showmessage('Serial I/O error: '+proc+' exception'+#13+#13+
'device = '+Devicename+#13+
'handle = 0x'+IntToHex(CommFile,8));
halt
end;
Running:=true
end
end;
procedure TForm1.UpDown1Click(Sender: TObject; Button: TUDBtnType);
begin
ChannelUpdate
end;
procedure TForm1.CheckBox1Click(Sender: TObject);
begin
if CheckBox1.Checked then begin
UpDown1.Max:=4;
CheckBox1.Font.Color:=clBlack;
CheckBox1.Caption:='LPD433 lock'
end
else begin
UpDown1.Max:=254; // max 127 in datasheet
CheckBox1.Font.Color:=clRed;
CheckBox1.Caption:='UNLOCKED'
end;
ChannelUpdate
end;
procedure TForm1.ComboBox1or2Change(Sender: TObject);
begin
case ComboBox2.ItemIndex of 1:if ComboBox1.ItemIndex>2 then
begin
ComboBox1.ItemIndex:=2;
if Sender=ComboBox1 then
ShowMessage('4800 bps maximum in FU2 mode')
else
ShowMessage('Baud rate reduced to 4800 bps')
end;
3:if ComboBox1.ItemIndex>0 then
begin
ComboBox1.ItemIndex:=0;
if Sender=ComboBox1 then
ShowMessage('1200 bps maximum in FU4 mode')
else
ShowMessage('Baud rate reduced to 1200 bps')
end
end { of case }
end;
(*
1.1 clear Rx buffer
1.2 write "AT+DEFAULT"
2.3 read "OK+DEFAULT"
2.4 response bad
*)
procedure TForm1.Button1Click(Sender: TObject);
var s,e,r:string; // AT command, expected response, actual response
put,got:DWORD;
b:array [1..80] of char;
begin
if not Running then exit;
Running:=false;
Button1.Enabled:=false;
// clear Rx buffer
try
if not PurgeComm(CommFile, PURGE_RXCLEAR) then // clear buffer
begin
showmessage('Serial I/O error: PurgeComm failed (1.1)');
halt
end
except
showmessage('Serial I/O error: PurgeComm exception (1.1)');
halt
end;
// send factory reset command "AT+DEFAULT"
s:='AT+DEFAULT'+EOL;
e:=at2ok(s);
try
if not WriteFile(CommFile, s[1], length(s), put, nil) then // write command
begin
showmessage('Serial I/O error: WriteFile failed (1.2)');
halt
end
except
showmessage('Serial I/O error: WriteFile exception (1.2)');
halt
end;
// get factory reset response
try
if not ReadFile(CommFile, b[1], sizeof(b), got, nil) then // read response
begin
showmessage('Serial I/O error: ReadFile failed (1.3)');
halt
end
except
showmessage('Serial I/O error: ReadFile exception (1.3)');
halt
end;
r:=copy(b,1,got);
if strip(r)<>strip(e) then
showmessage('invalid response (1.4)'+#13+
'command <'+strip(s)+'>'+#13+
'response <'+strip(r)+'>')
else
InitControls; // synchronize controls to factory reset state
Button1.Enabled:=true;
Running:=true
end;
(*
2.1 clear Rx buffer
2.2 write "AT+RX"
2.3 read "OK+B9600
OK+RC001
OK+RP:+20dBm
OK+FU3"
2.4 write "AT+V"
2.5 read "HC-12_V2.3"
(#data bits, parity, #stop bits are NOT returned)
*)
procedure TForm1.Button2Click(Sender: TObject);
var s,cfg,ver:string;
put,got:DWORD;
b:array [1..80] of char;
begin
if not Running then exit;
Running:=false;
Button2.Enabled:=false;
// clear Rx buffer
try
if not PurgeComm(CommFile, PURGE_RXCLEAR) then // clear buffer
begin
showmessage('Serial I/O error: PurgeComm failed (2.1)');
halt
end
except
showmessage('Serial I/O error: PurgeComm exception (2.1)');
halt
end;
// send read all configurations command "AT+RX"
s:='AT+RX'+EOL;
try
if not WriteFile(CommFile, s[1], length(s), put, nil) then // write command
begin
showmessage('Serial I/O error: WriteFile failed (2.2)');
halt
end
except
showmessage('Serial I/O error: WriteFile exception (2.2)');
halt
end;
// get read all configurations response
try
if not ReadFile(CommFile, b[1], sizeof(b), got, nil) then // read response
begin
showmessage('Serial I/O error: ReadFile failed (2.3)');
halt
end
except
showmessage('Serial I/O error: ReadFile exception (2.3)');
halt
end;
cfg:=strip(copy(b,1,got));
// *****************************************
if DLY<>0 then sleep(DLY); // ******* delay between AT commands *******
// *****************************************
// send get version information command "AT+V"
s:='AT+V'+EOL;
try
if not WriteFile(CommFile, s[1], length(s), put, nil) then // write command
begin
showmessage('Serial I/O error: WriteFile failed (2.4)');
halt
end
except
showmessage('Serial I/O error: WriteFile exception (2.4)');
halt
end;
// get version information response
try
if not ReadFile(CommFile, b[1], sizeof(b), got, nil) then // read response
begin
showmessage('Serial I/O error: ReadFile failed (2.5)');
halt
end
except
showmessage('Serial I/O error: ReadFile exception (2.5)');
halt
end;
ver:=strip(copy(b,1,got));
showmessage('configuration response:'+#13+cfg+#13+#13+
'version response:' +#13+ver);
Button2.Enabled:=true;
Running:=true
end;
(*
3.1 clear Rx buffer
3.2 write "AT+Bxxxx" // set baud rate (1200 .. 115200)
3.3 read "OK+Bxxxx
3.4 response bad
3.5 write "AT+Cxxx" // set channel number (001 .. 127)
3.6 read "OK+Cxxx"
3.7 response bad
3.11 write "AT+FUx" // set operating mode (FU1 .. FU4)
3.12 read "OK+FUx"
3.13 response bad
3.14 write "AT+Px" // set Tx power level (1 .. 8)
3.15 read "OK+Px"
3.16 response bad
3.17 write "AT+Unps" // set serial format (#data bits, parity, #stop bits)
3.18 read "AT+Unps"
3.19 response bad
*)
procedure TForm1.Button3Click(Sender: TObject);
var s,e,r:string; // AT command, expected response, actual response
put,got:DWORD;
b:array [1..80] of char;
begin
if not Running then exit;
Running:=false;
Button3.Enabled:=false;
// clear Rx buffer
try
if not PurgeComm(CommFile, PURGE_RXCLEAR) then // clear buffer
begin
showmessage('Serial I/O error: PurgeComm failed (3.1)');
halt
end
except
showmessage('Serial I/O error: PurgeComm exception (3.1)');
halt
end;
// send set baud rate command "AT+Bxxxx"
case ComboBox1.ItemIndex of 0:s:='AT+B1200'+EOL;
1:s:='AT+B2400'+EOL;
2:s:='AT+B4800'+EOL;
3:s:='AT+B9600'+EOL;
4:s:='AT+B19200'+EOL;
5:s:='AT+B38400'+EOL;
6:s:='AT+B57600'+EOL;
7:s:='AT+B115200'+EOL
else s:='AT'+EOL // default to no action
end; { of case }
e:=at2ok(s);
try
if not WriteFile(CommFile, s[1], length(s), put, nil) then // write command
begin
showmessage('Serial I/O error: WriteFile failed (3.2)');
halt
end
except
showmessage('Serial I/O error: WriteFile exception (3.2)');
halt
end;
// get set baud rate response
try
if not ReadFile(CommFile, b[1], sizeof(b), got, nil) then // read response
begin
showmessage('Serial I/O error: ReadFile failed (3.3)');
halt
end
except
showmessage('Serial I/O error: ReadFile exception (3.3)');
halt
end;
r:=copy(b,1,got);
if strip(r)<>strip(e) then
showmessage('invalid response (3.4)'+#13+
'command <'+strip(s)+'>'+#13+
'response <'+strip(r)+'>');
// *****************************************
if DLY<>0 then sleep(DLY); // ******* delay between AT commands *******
// *****************************************
// send set channel/frequency command "AT+Cxxx"
s:='AT+C'+Label1.Caption+EOL;
e:=at2ok(s);
try
if not WriteFile(CommFile, s[1], length(s), put, nil) then // write command
begin
showmessage('Serial I/O error: WriteFile failed (3.5)');
halt
end
except
showmessage('Serial I/O error: WriteFile exception (3.5)');
halt
end;
// get set channel/frequency response
try
if not ReadFile(CommFile, b[1], sizeof(b), got, nil) then // read response
begin
showmessage('Serial I/O error: ReadFile failed (3.6)');
halt
end
except
showmessage('Serial I/O error: ReadFile exception (3.6)');
halt
end;
r:=copy(b,1,got);
if strip(r)<>strip(e) then
showmessage('invalid response (3.7)'+#13+
'command <'+strip(s)+'>'+#13+
'response <'+strip(r)+'>');
// *****************************************
if DLY<>0 then sleep(DLY); // ******* delay between AT commands *******
// *****************************************
// send set operating mode command "AT+FUx"
case ComboBox2.ItemIndex of 0:s:='AT+FU1'+EOL;
1:s:='AT+FU2'+EOL;
2:s:='AT+FU3'+EOL;
3:s:='AT+FU4'+EOL
else s:='AT'+EOL // default to no action
end; { of case }
e:=at2ok(s);
try
if not WriteFile(CommFile, s[1], length(s), put, nil) then // write command
begin
showmessage('Serial I/O error: WriteFile failed (3.11)');
halt
end
except
showmessage('Serial I/O error: WriteFile exception (3.11)');
halt
end;
// get set operating mode response
try
if not ReadFile(CommFile, b[1], sizeof(b), got, nil) then // read response
begin
showmessage('Serial I/O error: ReadFile failed (3.12)');
halt
end
except
showmessage('Serial I/O error: ReadFile exception (3.12)');
halt
end;
r:=copy(b,1,got);
if strip(r)<>strip(e) then
showmessage('invalid response (3.13)'+#13+
'command <'+strip(s)+'>'+#13+
'response <'+strip(r)+'>');
// *****************************************
if DLY<>0 then sleep(DLY); // ******* delay between AT commands *******
// *****************************************
// send set Tx power level command "AT+Px"
case ComboBox3.ItemIndex of 0:s:='AT+P1'+EOL; // minimum power
1:s:='AT+P2'+EOL;
2:s:='AT+P3'+EOL;
3:s:='AT+P4'+EOL;
4:s:='AT+P5'+EOL;
5:s:='AT+P6'+EOL;
6:s:='AT+P7'+EOL;
7:s:='AT+P8'+EOL // maximum power
else s:='AT'+EOL // default to no action
end; { of case }
e:=at2ok(s);
try
if not WriteFile(CommFile, s[1], length(s), put, nil) then // write command
begin
showmessage('Serial I/O error: WriteFile failed (3.14)');
halt
end
except
showmessage('Serial I/O error: WriteFile exception (3.14)');
halt
end;
// get set Tx power level response
try
if not ReadFile(CommFile, b[1], sizeof(b), got, nil) then // read response
begin
showmessage('Serial I/O error: ReadFile failed (3.15)');
halt
end
except
showmessage('Serial I/O error: ReadFile exception (3.15)');
halt
end;
r:=copy(b,1,got);
if strip(r)<>strip(e) then
showmessage('invalid response (3.16)'+#13+
'command <'+strip(s)+'>'+#13+
'response <'+strip(r)+'>');
// *****************************************
if DLY<>0 then sleep(DLY); // ******* delay between AT commands *******
// *****************************************
// send set serial format command "AT+Unps"
s:='AT+U';
if RadioButton7.Checked then s:=s+'7' else // #data bits (n)
if RadioButton8.Checked then s:=s+'8';
if RadioButton1.Checked then s:=s+'O' else // parity bit (p)
if RadioButton2.Checked then s:=s+'E' else
if RadioButton3.Checked then s:=s+'N';
if RadioButton4.Checked then s:=s+'1' else // #stop bits (s)
if RadioButton5.Checked then s:=s+'3' else
if RadioButton6.Checked then s:=s+'2';
if length(s)<>7 then s:='AT'; // no action on error
s:=s+EOL;
e:=at2ok(s);
try
if not WriteFile(CommFile, s[1], length(s), put, nil) then // write command
begin
showmessage('Serial I/O error: WriteFile failed (3.17)');
halt
end
except
showmessage('Serial I/O error: WriteFile exception (3.17)');
halt
end;
// get set serial format response
try
if not ReadFile(CommFile, b[1], sizeof(b), got, nil) then // read response
begin
showmessage('Serial I/O error: ReadFile failed (3.18)');
halt
end
except
showmessage('Serial I/O error: ReadFile exception (3.18)');
halt
end;
r:=copy(b,1,got);
if strip(r)<>strip(e) then
showmessage('invalid response (3.19)'+#13+
'command <'+strip(s)+'>'+#13+
'response <'+strip(r)+'>');
// finished HC-12 configuration
Button3.Enabled:=true;
Running:=true
end;
procedure TForm1.FormDblClick(Sender: TObject);
begin
showmessage('(c) Robert Rozee 2016'#13#13'Release 6 (29-may-2016)')
end;
end.