-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataComm.pas
1092 lines (976 loc) · 36.1 KB
/
DataComm.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
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Unit DataComm;
{$R+} {range checking ON}
{
Configuration, drivers and information window
for FACE DAQC data communications
v01.01 2002-12-04 Original
v01.02 2002-12-05 Add "By address"
v01.03 2002-12-06 Add "Raw data flow"
v01.04 2002-12-07 alt-M Enter timeout multiplier [1..n] not implemented
v01.05 2002-12-07 Add "Log errors to file"
v01.06 2003-01-26 Replace IFDEF CLX ELSE->IFDEF LINUX ENDIF IFDEF MSWINDOWS
v01.07 2003-01-30 AddrAllocate: (3x) Font.Size 8 --> Font.Height -11
v01.08 2003-03-18 Replace procedure OnDestroyForm by OnCloseForm
v01.09 2003-03-18 Replace btnClose: TButton by btnCancel: TBitBtn
v01.10 2003-05-09 OnClickCheck/cbPause: event handling suppression with tag
v01.11 2003-05-09 OnCreateForm: set cbPause to current Optomux.DebugPause
v01.12 2003-05-25 OnDestroyForm: added back; frmDataComm := NIL
v01.13 2004-12-05 OnCloseForm: add OnClickButton (btnStats);
v02.01 2009-08-16 Add data comm port parametrization and subprograms
v02.02 2009-10-07 PortRecv/SerialRec: replace ReceiveChar by ReceiveString
Range checking ON
v02.03 2009-11-13 Interface Uses: add Winsock
Implementation Uses: add Socket
Replace TCPIP by IP in names
Add needed socket elements to IPType record
Add Socket.pas routine calls to Port* functions
Implement on-the-fly changing of timeout
For sockets, confirm resetting of timeout
Open/TCP: execute Close first (a temporary fix)
v02.04 2009-11-19 Refresh: fix label & edit control visibility bug
v03.01 2011-09-23 IPType: add a objFestoCI element
2011-09-24 OnClickButton/btnRaw: FestoCI raw data flow enable
OnClickButton/btnStats: FestoCI raw data flow disable
OnClickCheck/cbPause: FestoCI raw data pausing
2011-10-17 Refresh: show base and addr in decimal and hex
2011-10-24 PortType: add a objLI8XX element; add Uses LI8XX
2011-10-25 OnClickButton/btnRaw: LI8XX raw data flow enable
OnClickButton/btnStats: LI8XX raw data flow disable
OnClickCheck/cbPause: LI8XX raw data pausing
2011-10-26 Change references of LI8XX to LineIn
2011-10-27 PortTO: changes required if FestoCI is being used
2011-11-01 OnClickButton/btnRaw,/btnStats,OnClickCheck/cbPause: WMT700
.dfm: memoRaw - change MaxLength from 30000 to 0
comboVPI: disable when doing raw data flow
v03.02 2011-12-01 PortRangeMax: change from 32 to 64
Add recognition of protocol CSI1 to debugging code
v03.03 2012-01-17 PortOpen: Hardwire 10 ms as WinSock read timeout
PortRecv: Use new Socket.ReceiveStringTO function
v04.01 2012-01-26 PortGet: New. Move and edit of coms/get_port
test_key, check: Copied from coms
hex2word, str2word, getchunk: Copied from comu
2012-02-01 PortGet: Clean up field format. See new CFG.TXT.
test_key: Existence of a key field now optional.
test_key: A key read of 99 matches any key expected.
v05.01 2012-07-23 Add FileType/FileRec TEXT file, switch = 3 -- extensive
2012-07-24 Add LastErrNo and LastErrMsg to PortType
2012-07-25 Place creation of all objLineIn in INITIALIZATION
v05.02 2012-08-22 .DFM/Virtual port index/ DropDownCount 8 --> 15
v05.03 2012-09-14 OnCreateForm: add protocol to VPI pull down listings
.DFM/Virtual port index/ DropDownCount 15 --> 32
v06.01 2013-12-15 .dfm: memoRaw - change MaxLength back to 30000 from 0
v06.02 2016-07-07 .dfm: memoRaw - change MaxLength back to 0 from 30000
v06.03 2016-07-09 .dfm: memoRaw - change MaxLength to 30000000 (30 MB)
v06.04 2016-07-28 OnChangeText: process if sender=edit3. For changing watchdog "channel".
btnCopy[ToClipboard]: button added
OnClickButton: code added for above (memoRaw only)
v06.05 2016-07-30 btnCopy: put into "raw" group
}
{ The second field (diptoe) of a GetPort configuration line
determines what type of port this is:
-1 Port does not exist in this configuration
0 Bus switch = 1 Diptoe then discarded
1..256 Serial switch = 0 Diptoe then becomes COM number
300 File switch = 3 Diptoe then discarded
301+ IP switch = 2 Diptoe then becomes remote port number
}
INTERFACE
USES
{$IFDEF LINUX}
QButtons, QControls, QForms, QGraphics, QStdCtrls, Types,
{$ENDIF}
{$IFDEF MSWINDOWS}
Buttons, Controls, Forms, Graphics, StdCtrls, Windows, Winsock,
Messages, Classes,
{$ENDIF}
SysUtils,
LblForm, FestoCI, LineIn;
{======== Form related definitions, etc. =====================}
TYPE
TDataComm = class(TForm)
grpConfig: TGroupBox;
comboVPI: TComboBox;
editProtocol: TEdit;
lblProtocol: TLabel;
lbl1: TLabel;
edit1: TEdit;
lbl2: TLabel;
edit2: TEdit;
lbl3: TLabel;
edit3: TEdit;
lbl4: TLabel;
edit4: TEdit;
lbl5: TLabel;
edit5: TEdit;
lbl6: TLabel;
edit6: TEdit;
btnTimeoutApply: TButton;
grpErrstats: TGroupBox;
rbType: TRadioButton;
rbAddr: TRadioButton;
btnRaw: TButton;
memoStats: TMemo;
grpRaw: TGroupBox;
btnCopy: TButton;
cbPause: TCheckBox;
btnClear: TButton;
btnStats: TButton;
memoRaw: TMemo;
cbErrlog: TCheckbox;
btnReset: TButton;
btnUpdate: TButton;
btnCancel: TBitBtn;
PROCEDURE OnCreateForm (Sender: TObject);
PROCEDURE OnCloseForm (Sender: TObject; VAR Action: TCloseAction);
PROCEDURE OnDestroyForm (Sender: TObject);
PROCEDURE OnClickButton (Sender: TObject);
PROCEDURE OnClickCheck (Sender: TObject);
PROCEDURE OnClickRadio (Sender: TObject);
PROCEDURE OnChangeText (Sender: TObject);
PROCEDURE Refresh (port: INTEGER);
private
{ Private declarations }
FmemoRawWarned: BOOLEAN;
public
{ Public declarations }
end;
VAR
frmDataComm: TDataComm;
frmHelp: TLblForm;
VPI_current: INTEGER; {virtual port index currently displayed}
PROCEDURE Select;
PROCEDURE UpdateIt (port: INTEGER);
{======== Port parametrization and procedures =================}
{Port parameters are stored in an array of records.
A record consists of serial, IP, etc. parts.
Which part is active depends on the value of the "switch" element.
}
CONST
PortRangeMin = 1;
PortRangeMax = 64;
TYPE
SerialType = RECORD
handle: THandle;
com: INTEGER; {1 for COM1:, etc.}
speed: DWORD;
databits: BYTE;
parity: STRING;
stopbits: BYTE;
timeout: INTEGER;
END;
BusType = RECORD
handle: THandle;
base,
addr,
chan: WORD;
timeout: INTEGER;
END;
IPType = RECORD
handle: TSocket;
mode, {'TCP' or 'UDP'}
ip_remote: String; {'aaa:bbb:ccc:ddd'}
port_remote,
port_local: INTEGER;
sockaddr_remote: TSockAddr;
objFestoCI: clsFestoCI;
timeout: INTEGER;
END;
FileType = RECORD
handle: TEXT;
filename: String; {incl. net, path, ext}
mode: INTEGER; {0: read, 1: write, 2: append}
param1, {3 integer parameters}
param2, {to be used by protocols}
param3: INTEGER;
timeout: INTEGER; {not used}
END;
PortType = RECORD
exists: BOOLEAN;
switch: INTEGER;
protocol: STRING;
objLineIn: clsLineIn;
SerialRec: SerialType; {switch = 0}
BusRec: BusType; {switch = 1}
IPRec: IPType; {switch = 2}
FileRec: FileType; {switch = 3}
LastErrNo: INTEGER;
LastErrMsg: STRING;
END;
VAR
Ports: ARRAY [PortRangeMin..PortRangeMax] OF PortType;
PROCEDURE PortErrorWindow (port: INTEGER; complainer: String);
PROCEDURE PortGet (VAR f: TEXT; key_expect, port: INTEGER);
FUNCTION PortOpen (port: INTEGER): BOOLEAN;
FUNCTION PortClose (port: INTEGER): BOOLEAN;
FUNCTION PortSend (port: INTEGER; msg: String): BOOLEAN;
FUNCTION PortRecv (port: INTEGER; VAR s: STRING; term: CHAR): BOOLEAN;
FUNCTION PortTO (port, new_timeout: INTEGER): BOOLEAN;
{==============================================================}
Implementation
Uses optomux, Serial, Socket, FatalErr;
CONST errlogname = 'OPTOERR.LOG';
{Error by address axes and matrix}
VAR lblAddrHi: ARRAY [0..$F] OF TLabel;
lblAddrLo: ARRAY [0..$F] OF TLabel;
lblAddrCount: ARRAY [0..$F,0..$F] OF TLabel;
VAR i: INTEGER;
{$R *.dfm}
{-------------------------------------------------------------}
PROCEDURE AddrAllocate (mother: TDataComm);
CONST aLeft = 10;
aTop = 54;
aWidth = 35;
aHeight = 18;
aHGap = 0;
aVGap = 0;
VAR hi, lo: INTEGER;
newLeft, newTop: INTEGER;
BEGIN
newTop := aTop + aHeight + aVGap;
FOR hi := 0 TO $F DO BEGIN
lblAddrHi[hi] := TLabel.Create (frmDataComm);
WITH lblAddrHi[hi] DO BEGIN
Parent := mother.grpErrstats;
Visible := FALSE;
Width := aWidth;
Height := aHeight;
Left := aLeft;
Top := newTop;
newTop := Top + aHeight + aVGap;
IF ((hi MOD 4) = 0) THEN newTop := newTop + aVGap;
Font.Height := -11;
Font.Style := [fsBold];
Caption := IntToHex(hi,1) + '0';
END;
END; {Y-axis}
newLeft := aLeft + aWidth + aHGap;
FOR lo := 0 TO $F DO BEGIN
lblAddrLo[lo] := TLabel.Create (frmDataComm);
WITH lblAddrLo[lo] DO BEGIN
Parent := mother.grpErrstats;
Visible := FALSE;
Width := aWidth;
Height := aHeight;
Top := aTop;
Left := newLeft;
newLeft := Left + aWidth + aHGap;
IF ((lo MOD 4) = 0) THEN newLeft := newLeft + aHGap;
Font.Height := -11;
Font.Style := [fsBold];
Caption := IntToHex(lo,2);
END;
END; {X-axis}
FOR hi := 0 TO $F DO
FOR lo := 0 TO $F DO BEGIN
lblAddrCount[hi,lo] := TLabel.Create (frmDataComm);
WITH lblAddrCount[hi,lo] DO BEGIN
Parent := mother.grpErrstats;
Visible := FALSE;
Width := aWidth;
Height := aHeight;
Top := lblAddrHi[hi].Top;
Left := lblAddrLo[lo].Left;
Font.Height := -11;
Font.Style := [fsBold];
END;
END; {Matrix}
END; {of procedure AddrAllocate}
{-------------------------------------------------------------}
PROCEDURE TDataComm.OnCreateForm (Sender: TObject);
VAR i: INTEGER;
BEGIN
{Fill the virtual port index combo box list}
comboVPI.Clear;
FOR i := PortRangeMin TO PortRangeMax DO
WITH Ports[i] DO
IF exists THEN
comboVPI.AddItem (Format('%2d', [i]) + ' ' + protocol, NIL);
Refresh (VPI_current);
AddrAllocate (Self);
cbErrlog.Caption := '&Log errors to ' + errlogname;
cbErrlog.Tag := 0; {signal event handler not to do anything}
cbErrlog.Checked := Optomux.ErrlogEnableGet;
cbErrlog.Tag := 1;
cbPause.Tag := 0; {signal event handler not to do anything}
cbPause.Checked := Optomux.DebugPauseGet;
cbPause.Tag := 1;
FmemoRawWarned := FALSE;
END; {of procedure OnCreateForm}
{-------------------------------------------------------------}
PROCEDURE TDataComm.OnCloseForm (Sender: TObject; VAR Action: TCloseAction);
{What to do when form closed}
BEGIN
OnClickButton (btnStats);
Action := caFree;
frmDataComm := NIL;
END; {of procedure OnCloseForm}
{-------------------------------------------------------------}
PROCEDURE TDataComm.OnDestroyForm (Sender: TObject);
{What to do when form destroyed -- probably redundant}
BEGIN
frmDataComm := NIL;
END; {of procedure OnDestroyForm}
{-------------------------------------------------------------}
PROCEDURE TDataComm.OnClickButton(Sender: TObject);
VAR pause_save: BOOLEAN;
BEGIN
IF (Sender = btnRaw) THEN BEGIN
comboVPI.Enabled := FALSE;
grpErrstats.Visible := FALSE;
grpRaw.Visible := TRUE;
Optomux.DebugMemoSet (memoRaw);
Optomux.DebugEnableSet (TRUE);
IF (editProtocol.Text = 'FE') THEN
WITH Ports[VPI_current].IPRec DO BEGIN
objFestoCI.DebugMemoSet (memoRaw);
objFestoCI.DebugEnableSet (TRUE);
objFestoCI.DebugPauseSet (cbPause.Checked);
END;
IF (editProtocol.Text = 'LI820') OR
(editProtocol.Text = 'LI840') OR
(editProtocol.Text = 'LI850') OR
(editProtocol.Text = 'WMT700') OR
(editProtocol.Text = 'CSI1') OR
(Ports[VPI_current].switch=3) THEN
WITH Ports[VPI_current] DO BEGIN
objLineIn.DebugMemoSet (memoRaw);
objLineIn.DebugEnableSet (TRUE);
objLineIn.DebugPauseSet (cbPause.Checked);
END;
END;
IF (Sender = btnStats) THEN BEGIN
comboVPI.Enabled := TRUE;
Optomux.DebugEnableSet (FALSE);
Optomux.DebugMemoSet (NIL);
IF (editProtocol.Text = 'FE') THEN
WITH Ports[VPI_current].IPRec DO BEGIN
objFestoCI.DebugEnableSet (FALSE);
objFestoCI.DebugMemoSet (NIL);
END;
IF (editProtocol.Text = 'LI820') OR
(editProtocol.Text = 'LI840') OR
(editProtocol.Text = 'LI850') OR
(editProtocol.Text = 'WMT700') OR
(editProtocol.Text = 'CSI1') OR
(Ports[VPI_current].switch=3) THEN
WITH Ports[VPI_current] DO BEGIN
objLineIn.DebugEnableSet (FALSE);
objLineIn.DebugMemoSet (NIL);
END;
grpRaw.Visible := FALSE;
grpErrstats.Visible := TRUE;
UpdateIt (1 {***TEMPORARY***});
END;
IF (Sender = btnTimeoutApply) THEN BEGIN
TRY
PortTO (VPI_current, StrToInt (edit6.Text));
EXCEPT
edit6.Text := '99';
END;
END;
IF (Sender = btnCopy) THEN BEGIN
pause_save := cbPause.Checked;
cbPause.Tag := 0;
cbPause.Checked := TRUE;
memoRaw.SelectAll;
memoRaw.CopyToClipboard;
memoRaw.SelLength := -1;
cbPause.Checked := pause_save;
cbPause.Tag := 1;
END;
IF (Sender = btnReset) THEN BEGIN
Optomux.ErrstatsClear;
UpdateIt (1 {***TEMPORARY***});
END;
IF (Sender = btnUpdate) THEN BEGIN
UpdateIt (1 {***TEMPORARY***});
END;
IF (Sender = btnCancel) THEN BEGIN
OnClickButton (btnStats);
Self.Release;
frmDataComm := NIL;
END;
IF (Sender = btnClear) THEN BEGIN
memoRaw.Clear;
memoRaw.Lines.Add ('TEXT BUFFER CLEARED');
END;
END; {of procedure OnClickButton}
{---------------------------------------------------------------------}
PROCEDURE TDataComm.OnClickCheck (Sender: TObject);
BEGIN
IF (Sender = cbErrlog) AND (cbErrlog.Tag <> 0) THEN BEGIN
Optomux.ErrlogNameSet (errlogname);
Optomux.ErrlogEnableSet (cbErrlog.Checked);
END;
IF (Sender = cbPause) AND (cbPause.Tag <> 0) THEN BEGIN
Optomux.DebugPauseSet (cbPause.Checked);
IF (editProtocol.Text = 'FE') THEN
WITH Ports[VPI_current].IPRec DO BEGIN
objFestoCI.DebugPauseSet (cbPause.Checked);
END;
IF (editProtocol.Text = 'LI820') OR
(editProtocol.Text = 'LI840') OR
(editProtocol.Text = 'LI850') OR
(editProtocol.Text = 'WMT700') OR
(editProtocol.Text = 'CSI1') OR
(Ports[VPI_current].switch=3) THEN
WITH Ports[VPI_current] DO BEGIN
objLineIn.DebugPauseSet (cbPause.Checked);
END;
END;
END; {of procedure OnClickCheck}
{---------------------------------------------------------------------}
PROCEDURE TDataComm.OnClickRadio(Sender: TObject);
VAR type_sent, addr_sent:BOOLEAN;
hi, lo: INTEGER;
BEGIN
type_sent := rbType.Checked;
addr_sent := rbAddr.Checked;
memoStats.Visible := type_sent;
FOR hi := 0 TO $F DO lblAddrHi[hi].Visible := addr_sent;
FOR lo := 0 TO $F DO lblAddrLo[lo].Visible := addr_sent;
FOR hi := 0 TO $F DO
FOR lo := 0 TO $F DO
lblAddrCount[hi,lo].Visible := addr_sent;
UpdateIt (1 {***TEMPORARY***});
END; {of procedure OnClickRadio}
{---------------------------------------------------------------------}
PROCEDURE TDataComm.OnChangeText(Sender: TObject);
VAR new_value: INTEGER;
BEGIN
IF (Sender = comboVPI) THEN BEGIN
TRY
VPI_current := StrToInt (Copy(comboVPI.Text,1,2));
IF (VPI_current < PortRangeMin) THEN VPI_current := PortRangeMin;
IF (VPI_current > PortRangeMax) THEN VPI_current := PortRangeMax;
Refresh (VPI_current);
UpdateIt (1 {***TEMPORARY***});
EXCEPT
comboVPI.Text := 'invalid';
END;
END;
TRY
IF (Sender = frmDataComm.edit3) AND (frmDataComm.edit3.Text <> '') THEN BEGIN
new_value := StrToInt (frmDataComm.edit3.Text);
Ports[VPI_current].BusRec.chan := new_value;
END;
EXCEPT
END;
END; {of procedure OnChangeText}
{---------------------------------------------------------------------}
PROCEDURE TDataComm.Refresh (port: INTEGER);
{Fill in the form from working variables}
BEGIN
WITH Ports[port] DO BEGIN
comboVPI.Text := IntToStr(port);
editProtocol.Text := protocol;
CASE switch OF
0: WITH SerialRec DO BEGIN
lbl1.Caption := 'COM port'; edit1.Text := IntToStr (com);
lbl2.Caption := 'Speed'; edit2.Text := IntToStr (speed);
lbl3.Caption := 'Data bits'; edit3.Text := IntToStr (databits);
lbl4.Caption := 'Stop bits'; edit4.Text := IntToStr (stopbits);
lbl5.Caption := 'Parity'; edit5.Text := parity;
lbl6.Caption := 'Timeout [ms]'; edit6.Text := IntToStr (timeout);
lbl4.Visible := TRUE; edit4.Visible := TRUE;
lbl5.Visible := TRUE; edit5.Visible := TRUE;
lbl6.Visible := TRUE; edit6.Visible := TRUE;
btnTimeoutApply.Visible := TRUE;
END;
1: WITH BusRec DO BEGIN
lbl1.Caption := 'I/O address'; edit1.Text := IntToStr (base) +
' = 0x' + IntToHex (base, 2);
lbl2.Caption := 'Field address'; edit2.Text := IntToStr (addr) +
' = 0x' + IntToHex (addr, 2);
lbl3.Caption := 'Channel'; edit3.Text := IntToStr (chan);
lbl4.Visible := FALSE; edit4.Visible := FALSE;
lbl5.Visible := FALSE; edit5.Visible := FALSE;
lbl6.Visible := FALSE; edit6.Visible := FALSE;
btnTimeoutApply.Visible := FALSE;
END;
2: WITH IPRec DO BEGIN
lbl1.Caption := 'Mode'; edit1.Text := mode;
lbl2.Caption := 'IP address'; edit2.Text := ip_remote;
lbl3.Caption := 'Remote port'; edit3.Text := IntToStr (port_remote);
lbl4.Caption := 'Local port'; edit4.Text := IntToStr (port_local);
lbl6.Caption := 'Timeout [ms]'; edit6.Text := IntToStr (timeout);
lbl4.Visible := TRUE; edit4.Visible := TRUE;
lbl5.Visible := FALSE; edit5.Visible := FALSE;
lbl6.Visible := TRUE; edit6.Visible := TRUE;
btnTimeoutApply.Visible := TRUE;
END;
3: WITH FileRec DO BEGIN
lbl1.Caption := 'File'; edit1.Text := filename;
lbl2.Caption := 'Mode';
IF (mode = 0) THEN edit2.Text := IntToStr (mode) + ' READ';
IF (mode = 1) THEN edit2.Text := IntToStr (mode) + ' WRITE';
IF (mode = 2) THEN edit2.Text := IntToStr (mode) + ' APPEND';
lbl3.Caption := 'Parameter 1'; edit3.Text := IntToStr (param1) +
' = 0x' + IntToHex (param1, 4);
lbl4.Caption := 'Parameter 2'; edit4.Text := IntToStr (param2) +
' = 0x' + IntToHex (param2, 4);
lbl5.Caption := 'Parameter 3'; edit5.Text := IntToStr (param3) +
' = 0x' + IntToHex (param3, 4);
lbl4.Visible := TRUE; edit4.Visible := TRUE;
lbl5.Visible := TRUE; edit5.Visible := TRUE;
lbl6.Visible := FALSE; edit6.Visible := FALSE;
btnTimeoutApply.Visible := FALSE;
END;
END; {of case}
END; {of with ports[]}
END; {of procedure Refresh}
{---------------------------------------------------------------------}
PROCEDURE Select;
{Come here when this menu item selected on main form}
BEGIN
IF NOT Assigned (frmDataComm)
THEN frmDataComm := TDataComm.Create (Application);
WITH frmDataComm DO BEGIN
Show;
SetFocus;
WindowState := wsNormal;
UpdateIt (1 {***TEMPORARY***});
END;
END; {of procedure Select}
{-------------------------------------------------------------}
PROCEDURE UpdateIt (port: INTEGER);
{This UpdateIt is NOT called at the end of every sample/control period.
Manual intervention required.}
VAR i: INTEGER;
hi,
lo: INTEGER;
protocol: STRING;
BEGIN
IF Assigned (frmDataComm) THEN
IF (frmDataComm.WindowState <> wsMinimized) THEN BEGIN
protocol := DataComm.Ports[port].protocol;
IF frmDataComm.rbType.Checked THEN BEGIN
frmDataComm.memoStats.Clear;
frmDataComm.memoStats.Lines.Add ('This screen does not update automatically');
frmDataComm.memoStats.Lines.Add ('');
frmDataComm.memoStats.Lines.Add
('Since ' + Optomux.ErrstatsClearWhenGet);
frmDataComm.memoStats.Lines.Add ('');
frmDataComm.memoStats.Lines.Add
('COUNT RETRIES ERROR_TYPE LAST_DT >LAST_COMMAND \LAST_RESPONSE\');
frmDataComm.memoStats.Lines.Add
('----- ------- ---------- ------- ------------- ---------------');
IF (protocol = 'OS') OR (protocol = 'DS') THEN
FOR i := 0 TO Optomux.optomux_errno_max DO
WITH Optomux.optomux_errstats^[i] DO
IF (n <> 0) THEN frmDataComm.memoStats.Lines.Add (
IntToStr (n) + ' ' +
IntToStr (retries) + ' ' +
Optomux.optomux_error_list[i] + ' ' +
lasterr_dt + ' ' +
lasterr_cmd + ' ' +
lasterr_res);
frmDataComm.memoStats.Lines.Add ('<END-OF-LIST>');
END; {by type}
IF frmDataComm.rbAddr.Checked THEN BEGIN
FOR hi := 0 TO $F DO
FOR lo := 0 TO $F DO
WITH lblAddrCount[hi,lo] DO
IF (optomux_err_byaddr^[16*hi+lo] > 0)
THEN BEGIN
Font.Color := clRed;
Caption := IntToStr (optomux_err_byaddr^[16*hi+lo]);
Hint := Caption;
ShowHint := TRUE;
END
ELSE BEGIN
Font.Color := clLtGray;
Caption := '-';
ShowHint := FALSE;
END;
END; {by address}
END;
END; {of procedure UpdateIt}
{======== Actual calls to data communications system ==========}
FUNCTION Check_If_Already_Open (port: INTEGER): THandle;
{This function needed if different devices (different logical
ports) are using the same COM interface. If a requested port is a
COM (i.e. serial) interface, this function looks for another logical
port using the same COM interface AND has a valid handle. If so, that
handle is used (the Open function is of course a little different).
If not, then that logical ports handle value is used. The first logical
port opened gets the precedence and its parameters may conflict with
other devices trying to use the same interface. Must be consistent.}
VAR i: INTEGER;
handle_use: THandle;
BEGIN
handle_use := Ports[port].SerialRec.handle;
IF (handle_use = INVALID_HANDLE_VALUE) THEN
FOR i := PortRangeMin TO PortRangeMax DO
IF (Ports[i].SerialRec.com = Ports[port].SerialRec.com) AND
(Ports[i].SerialRec.handle <> INVALID_HANDLE_VALUE)
THEN handle_use := Ports[i].SerialRec.handle;
Check_If_Already_Open := handle_use;
END; {function Check_If_Already_Open}
{-------------------------------------------------------------}
PROCEDURE PortErrorWindow (port: INTEGER; complainer: String);
BEGIN
WITH Ports[port] DO BEGIN
CASE switch OF
0: Serial.LastErrorWindow (complainer);
1: ;
2: Socket.LastErrorWindow (complainer);
3: ;
END; {case switch}
END; {with ports}
END; {procedure PortErrorWindow}
{-------------------------------------------------------------}
PROCEDURE test_key (key_read, key_expect: INTEGER);
CONST nl2 = CHR(13) + CHR(10) + CHR(10);
BEGIN
IF (key_read <> key_expect) AND (key_read <> 99) THEN BEGIN
SetLastError ($20005001);
FatalErr.Msg ('DataComm/test_key',
'Error encountered in a configuration file' + nl2 +
'Line number key field (1st) read was ' + IntToStr(key_read) + nl2 +
'Key expected was ' + IntToStr(key_expect));
END;
{For development, a key read of 99 matches any expected}
END; {of procedure 'test_key'}
{-----------------------------------------------------------}
PROCEDURE check (s: String; code: INTEGER);
CONST nl2 = CHR(13) + CHR(10) + CHR(10);
BEGIN
IF (code <> 0) THEN BEGIN
SetLastError ($20005002);
FatalErr.Msg ('DataComm/check',
'Error encountered in a configuration file' + nl2 +
'Cannot decode >>>' + s + '<<< at position ' + IntToStr(code));
END;
END; {of procedure 'check'}
{------------------------------------------------------------}
FUNCTION hex2word (s: String; VAR code: INTEGER): Word;
{Hexadecimal string to word conversion function.
Stolen, with modifications, from optomux.pas 04/09/93.
s string (input)
code return code (output)
= 0 conversion ok
= 2 string greater than 4 characters
= 3 invalid character in string
hex2word returned function value
}
CONST ofs0 = ORD('0');
ofsucA = ORD('A')-10;
ofslcA = ORD('a')-10;
VAR value: Word;
i: INTEGER;
BEGIN
value := 0;
code := 0;
IF Length(s) > 4
THEN code := 2
ELSE BEGIN
i := 1;
WHILE i <= Length(s) DO BEGIN
CASE s[i] OF
'0'..'9': value := (value Shl 4) Or (ORD(s[i]) - ofs0 );
'A'..'F': value := (value Shl 4) Or (ORD(s[i]) - ofsucA);
'a'..'f': value := (value Shl 4) Or (ORD(s[i]) - ofslcA);
Else code := 3;
END; {of case}
INC(i);
END;
END;
hex2word := value;
END; {of function 'hex2word'}
{-------------------------------------------------------------------}
FUNCTION str2word (s: String; VAR code: INTEGER): Word;
{Convert ascii representation of hexidecimal or decimal to word.
Hexadecimal indicated by leading x, X, 0x, 0X, or trailing h, H.
}
VAR hex: BOOLEAN;
temp: Word;
BEGIN
hex := FALSE;
code := 0;
WHILE (Length(s) > 0) AND (s[1] IN [' ','0','x','X']) DO BEGIN
IF s[1] IN ['x','X'] THEN hex := TRUE;
Delete (s,1,1);
END;
WHILE (Length(s) > 0) AND (s[Length(s)] IN [' ',CHR(13),CHR(10),'h','H'])
DO BEGIN
IF s[Length(s)] IN ['h','H'] THEN hex := TRUE;
Delete (s,Length(s),1);
END;
IF Length(s) > 0
THEN IF hex THEN temp := hex2word (s, code)
ELSE Val (s, temp, code)
ELSE temp := 0;
str2word := temp;
END; {of function 'str2word'}
{------------------------------------------------------------}
FUNCTION getchunk (VAR ifile: TEXT; separator: CHAR): String;
{Get a string token from text file input.
Will not read past end-of-line or end-of-file.
Null-string returned in this case.
}
VAR buffer: String;
ch: CHAR;
FUNCTION more: BOOLEAN;
BEGIN
more := NOT (EOF(ifile) OR EOLN(ifile));
END; {of local function 'more'}
BEGIN
buffer := '';
ch := CHR(0); {suppress Delphi compiler "hint"}
IF more THEN REPEAT
READ (ifile, ch)
UNTIL (ch<>separator) OR NOT more;
IF more THEN REPEAT
IF ch<>separator THEN buffer := buffer+ch;
READ (ifile, ch);
UNTIL (ch=separator) OR NOT more;
getchunk := buffer;
END; {of function 'getchunk'}
{------------------------------------------------------------}
PROCEDURE PortGet (VAR f: TEXT; key_expect, port: INTEGER);
{Read in parameters for a port from config file}
VAR s: String;
diptoe, key_found, code: INTEGER;
BEGIN
WITH DataComm.Ports[port] DO BEGIN
{Use of a key in the file is optional.}
IF (key_expect >= 0) THEN BEGIN {-1 indicates key not present}
READ (f, key_found);
test_key (key_found, key_expect);
END;
READ (f, diptoe); {must be decimal, not hexadecimal}
exists := (diptoe >= 0);
IF exists THEN BEGIN
IF (diptoe-1 IN [0..255]) THEN WITH SerialRec DO BEGIN {this is serial}
switch := 0;
com := diptoe;
READ (f, speed);
READ (f, databits);
parity := getchunk (f,' ');
READ (f, stopbits);
READ (f, timeout);
protocol := getchunk (f,' ');
END;
IF (diptoe = 0) THEN WITH BusRec DO BEGIN {this is a bus device}
switch := 1;
s := getchunk (f,' '); base := str2word (s, code); check (s, code);
s := getchunk (f,' '); addr := str2word (s, code); check (s, code);
s := getchunk (f,' '); chan := str2word (s, code); check (s, code);
protocol := getchunk (f,' ');
END;
IF (diptoe > 300) THEN WITH IPRec DO BEGIN {this is an IP device}
switch := 2;
port_remote := diptoe;
s := getchunk (f,' '); ip_remote := s;
s := getchunk (f,' '); mode := UpperCase(s);
s := getchunk (f,' '); port_local := str2word (s, code); check (s, code);
s := getchunk (f,' '); timeout := str2word (s, code); check (s, code);
protocol := getchunk (f,' ');
IF (protocol = 'FE') THEN BEGIN
objFestoCI := clsFestoCI.Create;
objFestoCI.Setup (
'GET_PORT', mode, ip_remote, port_remote, port_local, timeout);
END;
END;
IF (diptoe = 300) THEN WITH FileRec DO BEGIN {this is a network text file}
switch := 3;
filename := getchunk (f,' ');
s := getchunk (f,' '); mode := str2word (s, code); check (s, code);
s := getchunk (f,' '); param1 := str2word (s, code); check (s, code);
s := getchunk (f,' '); param2 := str2word (s, code); check (s, code);
s := getchunk (f,' '); param3 := str2word (s, code); check (s, code);
protocol := getchunk (f,' ');
END;
END; {if exists}
READLN (f);
END; {with}
END; {of procedure 'PortGet'}
{---------------------------------------------------------------------}
FUNCTION PortOpen (port: INTEGER): BOOLEAN;
VAR fSuccess: BOOLEAN;
BEGIN
fSuccess := TRUE;
WITH Ports[port] DO BEGIN
CASE switch OF
0: WITH SerialRec DO BEGIN
fSuccess := TRUE;
handle := Check_If_Already_Open (port);
IF (handle = INVALID_HANDLE_VALUE) THEN BEGIN
fSuccess := Serial.Open
(handle, com, speed, databits, parity, stopbits, timeout);
IF fSuccess THEN fSuccess := Serial.DTRSet (handle, TRUE);
IF fSuccess THEN fSuccess := Serial.RTSSet (handle, TRUE);
IF fSuccess THEN fSuccess := Serial.EmptyBufferTx (handle);
IF fSuccess THEN fSuccess := Serial.EmptyBufferRx (handle);
END;
END;
1: WITH BusRec DO BEGIN
END;
2: WITH IPRec DO BEGIN
IF (UpperCase(mode) = 'TCP') THEN PortClose (port); {***TEMPORARY***}
fSuccess := Socket.Open
(handle, mode, ip_remote, port_remote, port_local,
sockaddr_remote, 10); {but see use of ReceiveStringTO in PortRecv}
END;
3: WITH FileRec DO BEGIN
{$I-}
AssignFile (handle, filename);
{$I+}
LastErrNo := IOResult;
fSuccess := (LastErrNo = 0);
IF fSuccess THEN BEGIN
{$I-}
CASE mode OF
0: RESET (handle);
1: REWRITE (handle);
2: APPEND (handle);
END; {case mode}
{$I+}
LastErrNo := IOResult;
fSuccess := (LastErrNo = 0);
END;
LastErrMsg := SysUtils.SysErrorMessage(LastErrNo);
END;
END; {case switch}
END; {with ports}
PortOpen := fSuccess;
END; {function PortOpen}
{-------------------------------------------------------------}
FUNCTION PortClose (port: INTEGER): BOOLEAN;
VAR fSuccess: BOOLEAN;
i: INTEGER;
BEGIN
fSuccess := TRUE;
WITH Ports[port] DO BEGIN
CASE switch OF
0: WITH SerialRec DO BEGIN
fSuccess := Serial.DTRSet (handle, FALSE);
IF fSuccess THEN fSuccess := Serial.RTSSet (handle, FALSE);
IF fSuccess THEN fSuccess := Serial.Close (handle);
FOR i := PortRangeMin TO PortRangeMax DO BEGIN
IF (Ports[i].SerialRec.com = com) THEN
Ports[i].SerialRec.handle := INVALID_HANDLE_VALUE;
END;
END;
1: WITH BusRec DO BEGIN
END;
2: WITH IPRec DO BEGIN
fSuccess := Socket.Close (handle);
handle := INVALID_SOCKET;
END;
3: WITH FileRec DO BEGIN
{$I-}
CloseFile (handle);
{$I+}
LastErrNo := IOResult;
fSuccess := (LastErrNo = 0);
LastErrMsg := SysUtils.SysErrorMessage(LastErrNo);
END;
END; {case switch}
END; {with ports}
PortClose := fSuccess;
END; {function PortClose}
{-------------------------------------------------------------}
FUNCTION PortSend (port: INTEGER; msg: String): BOOLEAN;
VAR fSuccess: BOOLEAN;
BEGIN
fSuccess := TRUE;
WITH Ports[port] DO BEGIN
CASE switch OF
0: WITH SerialRec DO BEGIN
fSuccess := Serial.EmptyBufferTx (handle);
IF fSuccess THEN fSuccess := Serial.EmptyBufferRx (handle);
IF fSuccess THEN fSuccess := Serial.SendString (handle, msg);
END;
1: WITH BusRec DO BEGIN
END;
2: WITH IPRec DO BEGIN
fSuccess := Socket.SendString (handle, sockaddr_remote, msg);
END;
3: WITH FileRec DO BEGIN {mode is not checked!}
{$I-}
WRITE (handle, msg); {Note: no explicit EOL termination}
{$I+}
LastErrNo := IOResult;
fSuccess := (LastErrNo = 0);