-
Notifications
You must be signed in to change notification settings - Fork 3
/
uWEBIO.pas
1661 lines (1462 loc) · 45.2 KB
/
uWEBIO.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 uWEBIO;
{
uWEBIO v0.91 - 25.11.2015
}
//{$DEFINE ThreadTimeing} // Enables Thread-Timeing Calculation
interface
uses
Windows, Classes, Controls, Forms, StdCtrls, Buttons, Spin, SyncObjs, Sockets, ExtCtrls;
const
MAX_MODULES_ = 10;
MAX_ONCHANGE_THREADS_ = 50;
MAX_CHANS_ = 24;
MAX_RECONNECTS_ = 10;
ReplyTimeout_ : DWORD = 10000;
type
tLastCommandItem = record
command : string;
time : Dword;
end;
// LastCommand
tLastCommand = class
private
fID : Word;
Items : Array of tLastCommandItem;
procedure Del( value : Word );
public
constructor Create( ID : Word );
procedure Add( value : string );
procedure Check( value : string );
end;
tWEBIO_CHANS = ( CHAN2, CHAN12, CHAN24 );
tWEBIO_fSettings = record
IP : String;
Port : WORD;
Password : String;
Channels : tWEBIO_CHANS;
ConnectTime : DWord;
Reconnects : Byte;
end;
// OnChange
tOnChange = procedure (Sender : TObject; ModuleID, Chan : Byte; Value : boolean);
tOnChangeThread = class(TThread)
private
{ Private-Deklarationen }
fSender : TObject;
fSynchronized : Boolean;
fModuleID, fChan : Byte;
fRecDigital : tOnChange;
fbValue : Boolean;
procedure Execution;
protected
procedure Execute; override;
public
procedure SetInfos( Rec : tOnChange; Sender : TObject; ModuleID, Chan : integer; Value : boolean; Synchronized : Boolean = False ); overload;
end;
tOnChangeEvent = class
private
Threads : Array of tOnChangeThread;
CritSection : TCriticalSection;
fLastEventTime : Array of DWord;
fLastEventTimeSynchronized : Array of DWord;
fInterval : Word;
function NilAndReturnIndex: integer;
// procedure NilUnusedThreads;
public
constructor Create;
destructor Destroy; override;
property Interval : Word read fInterval write fInterval default 0;
function Add( Rec : tOnChange; Sender : TObject; ModuleID, Chan, ChanOffset : integer; Value : boolean; Synchronized : Boolean = False ): integer; overload;
end;
// Base
tBaseCard = class
private
fModuleNum : Byte;
fActiveModule : Byte;
fSimulation : array [0..MAX_MODULES_-1] of boolean;
fError : Array[0..MAX_MODULES_-1] of integer;
fOnChangeHandler : tOnChangeEvent;
procedure SetActiveCard( val : byte );
function GetSimulation : Boolean;
procedure SetSimulation( val : boolean );
function GetError: integer;
function GetOnChangeInterval: Word;
procedure SetOnChangeInterval( val : Word );
public
constructor Create;
destructor Destroy; override;
function IsError( ModuleID : SmallInt = -1 ) : integer;
property Simulation : Boolean read GetSimulation write SetSimulation default false;
property Modules : Byte read fModuleNum default 0;
property ActiveCard : Byte read fActiveModule write SetActiveCard default 0;
property ErrorCode : integer read GetError;
property OnChangeInterval : Word read GetOnChangeInterval write SetOnChangeInterval;
end;
tWEBIOThread = class(TThread)
private
{ Private-Deklarationen }
{$IFDEF ThreadTimeing}
fQueryPerformanceFrequency : Int64;
fRuntime : Extended;
{$ENDIF}
fFrequency : SmallInt;
procedure SetFrequency( val : SmallInt );
protected
property Frequency : SmallInt read fFrequency write SetFrequency default 10;
public
constructor Create( CreateSuspended : boolean );
procedure Execute; override;
{$IFDEF ThreadTimeing}
property Runtime : Extended read fRuntime;
{$ENDIF}
end;
tWEBIO = class(tBaseCard)
private
TCPClient : array [0..MAX_MODULES_-1] of TTCPClient;
fSettings : array [0..MAX_MODULES_-1] of tWEBIO_fSettings;
fDigitalInVal,
fDigitalOutVal,
fDigitalInValSim : array [0..MAX_MODULES_-1] of DWord;
fDigitalIn,
fDigitalInSim,
fDigitalOut : Array [0..MAX_MODULES_-1, 0..MAX_CHANS_-1] of boolean;
myThread : tWEBIOThread;
fOnChange, fOnChangeSynchronized,
fOnOutChange, fOnOutChangeSynchronized : tOnChange;
lastCommands : array [0..MAX_MODULES_-1] of tLastCommand;
fDeInited : boolean;
procedure pSetSimulation( val : boolean );
function pGetSimulation: boolean;
procedure Reset( ModuleID : Integer = -1 );
procedure TcpClientConnect(Sender: TObject);
procedure TcpClientDisconnect(Sender: TObject);
procedure TcpClientError(Sender: TObject; SocketError: Integer);
procedure TcpClientReceive(Sender: TObject; Buf: PAnsiChar; var DataLen: Integer);
procedure TcpClientSend(Sender: TObject; Buf: PAnsiChar; var DataLen: Integer);
function SendBuffer( str: String; ModuleID : Integer = -1 ):Integer;
public
InStrings : Array [0..MAX_MODULES_-1, 0..MAX_CHANS_-1] of String;
OutStrings : Array [0..MAX_MODULES_-1, 0..MAX_CHANS_-1] of String;
constructor Create;
destructor Destroy; override;
procedure DeInit;
function Connect( IP : string; Port : Word; Password : string; Channels : tWEBIO_CHANS; ModuleID : Integer = -1 ) : integer;
function Disconnect( ModuleID : Integer = -1 ) : integer;
procedure SetSimulation( val : boolean; ModuleID : SmallInt = -1 );
property Simulation : boolean read pGetSimulation write pSetSimulation;
function IsConnected( ModuleID : Integer = -1 ): boolean;
function Read1DigitalInput( chan : byte; ModuleID : SmallInt = -1 ): byte;
function Read1DigitalOutput( chan : byte; ModuleID : SmallInt = -1 ): byte;
function ReadAllDigitalInput( var value : Byte; ModuleID : SmallInt = -1 ) : boolean;
function Write1DigitalOutput( chan, value : byte; ModuleID : SmallInt = -1 ) : integer;
function WriteAllDigitalOutput( value : boolean; ModuleID : SmallInt = -1 ): integer;
function Write1DigitalInputSim( chan, value : byte; ModuleID : SmallInt = -1 ) : integer;
property OnChange : tOnChange read fOnChange write fOnChange;
property OnChangeSynchronized : tOnChange read fOnChangeSynchronized write fOnChangeSynchronized;
property OnOutChange : tOnChange read fOnOutChange write fOnOutChange;
property OnOutChangeSynchronized : tOnChange read fOnOutChangeSynchronized write fOnOutChangeSynchronized;
end;
TFrmWEBIO = class(TForm)
mmoError: TMemo;
pnlChans: TPanel;
lblOutCaption: TLabel;
ShapeIn6: TShape;
ShapeIn8: TShape;
ShapeIn1: TShape;
ShapeIn2: TShape;
ShapeIn3: TShape;
ShapeIn4: TShape;
ShapeIn5: TShape;
ShapeIn7: TShape;
lblInCaption: TLabel;
SBtnOut01: TSpeedButton;
SBtnOut02: TSpeedButton;
SBtnOut03: TSpeedButton;
SBtnOut04: TSpeedButton;
SBtnOut05: TSpeedButton;
SBtnOut06: TSpeedButton;
SBtnOut07: TSpeedButton;
SBtnOut08: TSpeedButton;
lblOut1: TLabel;
lblOut3: TLabel;
lblOut2: TLabel;
lblOut5: TLabel;
lblOut4: TLabel;
lblOut6: TLabel;
lblOut7: TLabel;
lblOut8: TLabel;
SBtnOut09: TSpeedButton;
SBtnOut10: TSpeedButton;
SBtnOut11: TSpeedButton;
SBtnOut12: TSpeedButton;
lblOut9: TLabel;
lblOut10: TLabel;
lblOut11: TLabel;
lblOut12: TLabel;
ShapeIn10: TShape;
ShapeIn12: TShape;
ShapeIn9: TShape;
ShapeIn11: TShape;
grpIn: TGroupBox;
lblIn1: TLabel;
lblIn6: TLabel;
lblIn2: TLabel;
lblIn8: TLabel;
lblIn3: TLabel;
lblIn5: TLabel;
lblIn7: TLabel;
lblIn4: TLabel;
lblIn10: TLabel;
lblIn12: TLabel;
lblIn9: TLabel;
lblIn11: TLabel;
chkIn1: TCheckBox;
chkIn2: TCheckBox;
chkIn3: TCheckBox;
chkIn4: TCheckBox;
chkIn5: TCheckBox;
chkIn6: TCheckBox;
chkIN7: TCheckBox;
chkIN8: TCheckBox;
chkIN9: TCheckBox;
chkIN10: TCheckBox;
chkIN11: TCheckBox;
chkIN12: TCheckBox;
chkSimulation: TCheckBox;
pnlTop: TPanel;
lblFrequency: TLabel;
lblCard: TLabel;
pnlInit: TPanel;
seFrequenz: TSpinEdit;
selCard: TSpinEdit;
grpConnection: TGroupBox;
lbledtIP: TLabeledEdit;
lbledtPort: TLabeledEdit;
lbledtPassword: TLabeledEdit;
btnConnect: TButton;
grpData: TGroupBox;
lbledtSend: TLabeledEdit;
lbledtReceive: TLabeledEdit;
btnTime: TButton;
btnGetCounter: TButton;
btnDiagnostics: TButton;
ShapeIn18: TShape;
ShapeIn20: TShape;
ShapeIn13: TShape;
ShapeIn14: TShape;
ShapeIn15: TShape;
ShapeIn16: TShape;
ShapeIn17: TShape;
ShapeIn19: TShape;
SBtnOut13: TSpeedButton;
SBtnOut14: TSpeedButton;
SBtnOut15: TSpeedButton;
SBtnOut16: TSpeedButton;
SBtnOut17: TSpeedButton;
SBtnOut18: TSpeedButton;
SBtnOut19: TSpeedButton;
SBtnOut20: TSpeedButton;
SBtnOut21: TSpeedButton;
SBtnOut22: TSpeedButton;
SBtnOut23: TSpeedButton;
SBtnOut24: TSpeedButton;
ShapeIn22: TShape;
ShapeIn24: TShape;
ShapeIn21: TShape;
ShapeIn23: TShape;
lblOut13: TLabel;
lblOut14: TLabel;
lblOut15: TLabel;
lblOut16: TLabel;
lblOut17: TLabel;
lblOut18: TLabel;
lblOut19: TLabel;
lblOut20: TLabel;
lblOut21: TLabel;
lblOut22: TLabel;
lblOut23: TLabel;
lblOut24: TLabel;
chkIn13: TCheckBox;
chkIn14: TCheckBox;
chkIn15: TCheckBox;
chkIn16: TCheckBox;
chkIn17: TCheckBox;
chkIn18: TCheckBox;
chkIN19: TCheckBox;
chkIN20: TCheckBox;
chkIN21: TCheckBox;
chkIN22: TCheckBox;
chkIN23: TCheckBox;
chkIN24: TCheckBox;
lblIn13: TLabel;
lblIn18: TLabel;
lblIn14: TLabel;
lblIn20: TLabel;
lblIn15: TLabel;
lblIn17: TLabel;
lblIn19: TLabel;
lblIn16: TLabel;
lblIn22: TLabel;
lblIn24: TLabel;
lblIn21: TLabel;
lblIn23: TLabel;
rgChans: TRadioGroup; procedure FormShow(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
procedure tmrRefreshTimer(Sender: TObject);
procedure SButtonClick(Sender: TObject);
procedure seFrequenzChange(Sender: TObject);
procedure CheckBoxINClick(Sender: TObject);
procedure CheckBoxSimulationClick(Sender: TObject);
procedure selCardChange(Sender: TObject);
procedure selCardKeyPress(Sender: TObject; var Key: Char);
procedure SetShape( ID : byte; state : boolean );
procedure seFrequenzKeyPress(Sender: TObject; var Key: Char);
procedure btnConnectClick(Sender: TObject);
procedure btnGetCounterClick(Sender: TObject);
procedure btnTimeClick(Sender: TObject);
procedure btnDiagnosisClick(Sender: TObject);
private
{ Private-Deklarationen}
tmrRefresh: TTimer;
fRefresh_fSettings : boolean;
public
{ Public-Deklarationen}
end;
var
FrmWEBIO: TFrmWEBIO;
WEBIO : tWEBIO;
function Matches(const S,Wildcard: String): Boolean;
implementation
uses
MMSystem, Math, StrUtils, Masks, SysUtils, Graphics,
PingSend,
uTLH.Numeral, uTLH.Strings, uTLH.SysUtils;
{$R *.DFM}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
constructor tLastCommand.Create( ID : Word );
begin
inherited Create;
SetLength( Items, 0 );
fID := ID;
end;
procedure tLastCommand.Add( value : string );
begin
if NOT Assigned(self) then
Exit;
if ( Matches( value,'*input*' ) ) then
value := 'input'
else if (Matches( value,'*output*' ) ) then
value := 'output'
else
begin
value := StringReplace( value,'GET /','',[rfReplaceAll] );
value := StringReplace( value,'?PW=','',[rfReplaceAll] );
value := StringReplace( value,'&','',[rfReplaceAll] );
end;
SetLength( Items, Length( Items )+1 );
Items[ Length( Items )-1 ].Command := value;
Items[ Length( Items )-1 ].Time := GetTickCount;
end;
procedure tLastCommand.Del( value : Word );
var
i : integer;
begin
if NOT Assigned(self) then
Exit;
if ( value > Length(Items)-1 ) then
Exit;
for i := value to Length( Items )-2 do
Items[i] := Items[i+1];
SetLength( Items, Length( Items )-1 );
end;
procedure tLastCommand.Check( value : string );
var
Time : DWord;
i : integer;
begin
if NOT Assigned(self) then
Exit;
Time := GetTickCount;
for i := Length( Items )-1 downTo 0 do
begin
if ( Matches( value, '*' + Items[i].Command + '*' ) ) OR (( Items[i].Command = 'time' ) AND ( Matches( value, '??.??.????*??:??:??' ) ) ) then
begin
// if Assigned( FrmWebIO ) then
// if FrmWebIO.Visible then
// FrmWebIO.mmoError.Lines.Add( Format( 'ID: %d - RECEIVED response for cmd: "%s".', [ fID, Items[i].Command ] ) );
Del( i );
end
else if ( Time-Items[i].time >= ReplyTimeout_ ) then
begin
if Assigned( FrmWebIO ) then
if FrmWebIO.Visible then
FrmWebIO.mmoError.Lines.Add( Format( 'ID: %d - Never received response for cmd: "%s".', [ fID, Items[i].Command ] ) );
WEBIO.fError[fID] := -100;
Del( i );
end;
end;
end;
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
procedure TFrmWEBIO.FormCreate(Sender: TObject);
begin
tmrRefresh := TTimer.Create(self);
tmrRefresh.Interval := 10;
tmrRefresh.OnTimer := tmrRefreshTimer;
tmrRefresh.Enabled := false;
selCard.MaxValue := WEBIO.Modules;
if WEBIO.Modules > 0 then
begin
PnlInit.Font.Color := clGreen;
end;
end;
procedure TFrmWEBIO.FormShow(Sender: TObject);
begin
tmrRefresh.Enabled := true;
fRefresh_fSettings := true;
end;
procedure TFrmWEBIO.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
tmrRefresh.Enabled := false;
end;
procedure TFrmWEBIO.tmrRefreshTimer(Sender: TObject);
var
i : Integer;
comp : TComponent;
begin
chkSimulation.Checked := WEBIO.fSimulation[ WEBIO.fActiveModule ];
if WEBIO.fSimulation[ WEBIO.fActiveModule ] then
grpIn.Font.Color := clBlack
else
grpIn.Font.Color := clGray;
grpIn.Enabled := WEBIO.fSimulation[ WEBIO.fActiveModule ];
if WEBIO.TCPClient[ WEBIO.fActiveModule ].Active then
begin
pnlInit.Font.Color := clLime;
pnlInit.Caption := 'Init OK'
end
else
begin
pnlInit.Font.Color := clRed;
pnlInit.Caption := 'Init fehler'
end;
if WEBIO.TCPClient[ WEBIO.fActiveModule ].Active then
FrmWEBIO.btnConnect.Caption := 'Disconnect'
else
FrmWEBIO.btnConnect.Caption := 'Connect';
for i := 1 to MAX_CHANS_ do
begin
comp := self.FindComponent( Format( 'lblIn%d', [ i ]));
if comp <> NIL then
(comp as TLabel).Caption := WEBIO.InStrings[ WEBIO.fActiveModule, i-1 ];
comp := self.FindComponent( Format( 'lblOut%d', [ i ]));
if comp <> NIL then
(comp as TLabel).Caption := WEBIO.OutStrings[ WEBIO.fActiveModule, i-1 ];
if WEBIO.Simulation then
SetShape( i, WEBIO.fDigitalInSim[ WEBIO.fActiveModule, i-1] )
else
SetShape( i, WEBIO.fDigitalIn[ WEBIO.fActiveModule, i-1] );
comp := self.FindComponent( Format( 'SBtnOut%.02d', [ i ]));
if comp <> NIL then
(comp as TSpeedButton).Down := WEBIO.fDigitalOut[ WEBIO.fActiveModule, i-1 ];
end;
if fRefresh_fSettings then
begin
lbledtIP.Text := WEBIO.fSettings[ WEBIO.fActiveModule ].IP;
lbledtPort.Text := IntToStr( WEBIO.fSettings[ WEBIO.fActiveModule ].Port );
lbledtPassword.Text := WEBIO.fSettings[ WEBIO.fActiveModule ].Password;
if Assigned( WEBIO.myThread ) then
seFrequenz.Value := WEBIO.myThread.Frequency;
fRefresh_fSettings := false;
if ( WEBIO.fSettings[ WEBIO.fActiveModule ].Channels = CHAN24 ) then
begin
pnlChans.Height := 502;
self.Height := 745;
rgChans.ItemIndex := 2;
end
else if ( WEBIO.fSettings[ WEBIO.fActiveModule ].Channels = CHAN12 ) then
begin
pnlChans.Height := 261;
self.Height := 745-ABS(261-502);
rgChans.ItemIndex := 1;
end
else
begin
pnlChans.Height := 60;
self.Height := 745-ABS(60-502);
rgChans.ItemIndex := 0;
end;
end;
end;
//---------------------------------------------------------------------------
// Form
//---------------------------------------------------------------------------
procedure TFrmWEBIO.SButtonClick(Sender: TObject);
var
s : string;
nr : integer;
begin
s := TSpeedButton(Sender).Name;
nr := StrToIntDef(Copy(s, Length(s) - 1, 2), 1);
WEBIO.Write1DigitalOutput(nr, Integer(TSpeedButton(Sender).Down) );
end;
procedure TFrmWEBIO.CheckBoxINClick(Sender: TObject);
var
s : string;
c, i : integer;
val : Word;
begin
if NOT (sender is TCheckBox) then
Exit;
s := (Sender as TCheckBox).Name;
c := StrToIntDef( RightStr( s, Length(s)-5 ), -1);
WEBIO.fDigitalInSim[ WEBIO.fActiveModule, c-1] := (Sender as TCheckBox).checked;
val := 0;
for i := 0 to MAX_CHANS_-1 do
val := WriteBitInDWord(val, Byte(WEBIO.fDigitalInSim[ WEBIO.fActiveModule, i]), i);
WEBIO.fDigitalInValSim[ WEBIO.fActiveModule ] := val;
if WEBIO.Simulation then
begin
if Assigned(WEBIO.OnChange) then
WEBIO.fOnChangeHandler.Add( WEBIO.OnChange, self, WEBIO.fActiveModule, c, 0, (sender as TCheckBox).checked, false );
if Assigned(WEBIO.OnChangeSynchronized) then
WEBIO.fOnChangeHandler.Add( WEBIO.OnChangeSynchronized, self, WEBIO.fActiveModule, c, 0, (sender as TCheckBox).checked, true );
end;
end;
procedure TFrmWEBIO.seFrequenzChange(Sender: TObject);
begin
if NOT (Sender is TSpinEdit) then
Exit;
WEBIO.myThread.Frequency := (Sender as TSpinEdit).Value;
fRefresh_fSettings := true;
end;
procedure TFrmWEBIO.seFrequenzKeyPress(Sender: TObject; var Key: Char);
begin
Key := #0;
end;
procedure TFrmWEBIO.selCardChange(Sender: TObject);
var
val : integer;
begin
if NOT (Sender is TSpinEdit) then
Exit;
val := (Sender as TSpinEdit).Value-1;
if NOT (val <= 0) or (val > WEBIO.fModuleNum) then
WEBIO.fActiveModule := val;
(Sender as TSpinEdit).Value := WEBIO.fActiveModule;
end;
procedure TFrmWEBIO.selCardKeyPress(Sender: TObject; var Key: Char);
begin
Key := #0;
end;
procedure TFrmWEBIO.CheckBoxSimulationClick(Sender: TObject);
begin
WEBIO.Simulation := chkSimulation.Checked;
end;
procedure TFrmWEBIO.btnConnectClick(Sender: TObject);
var
chans : tWEBIO_CHANS;
begin
case rgChans.ItemIndex of
0 : chans := CHAN2;
1 : chans := CHAN12;
2 : chans := CHAN24;
else
Exit;
end;
if WEBIO.TCPClient[ WEBIO.fActiveModule ].Active then
WEBIO.Disconnect
else
if WEBIO.Connect( lbledtIP.Text, StrToIntDef( lbledtPort.Text, 0 ), lbledtPassword.Text, chans, WEBIO.fActiveModule ) = 0 then
fRefresh_fSettings := true;
end;
procedure TFrmWEBIO.btnGetCounterClick(Sender: TObject);
begin
lbledtSend.Text := 'GET /counter?PW=%s&';
WebIO.SendBuffer( Format( 'GET /counter?PW=%s&', [ WEBIO.fSettings[ WEBIO.fActiveModule ].Password ] ), WEBIO.fActiveModule );
end;
procedure TFrmWEBIO.btnTimeClick(Sender: TObject);
begin
lbledtSend.Text := 'GET /time?PW=%s&';
WebIO.SendBuffer( Format( 'GET /time?PW=%s&', [ WEBIO.fSettings[ WEBIO.fActiveModule ].Password ] ), WEBIO.fActiveModule );
end;
procedure TFrmWEBIO.btnDiagnosisClick(Sender: TObject);
begin
lbledtSend.Text := 'GET /diagnosis?PW=%s&';
WebIO.SendBuffer( Format( 'GET /diagnosis?PW=%s&', [ WEBIO.fSettings[ WEBIO.fActiveModule ].Password ] ), WEBIO.fActiveModule );
end;
procedure TFrmWEBIO.SetShape( ID : byte; state : boolean );
var
comp : TComponent;
begin
comp := self.FindComponent( Format('ShapeIn%d',[ID]) );
if comp <> NIL then
begin
if state then
(comp as TShape).Brush.Color := clLime
else
(comp as TShape).Brush.Color := clGreen;
end;
comp := self.FindComponent( Format('chkIn%d',[ID]) );
if comp <> NIL then
(comp as TCheckBox).checked := state
end;
//---------------------------------------------------------------------------
// OnChange
//---------------------------------------------------------------------------
constructor tOnChangeEvent.Create;
begin
inherited;
fInterval := 0;
SetLength( fLastEventTime, MAX_CHANS_ );
SetLength( fLastEventTimeSynchronized, MAX_CHANS_ );
SetLength( Threads, MAX_ONCHANGE_THREADS_ );
CritSection := TCriticalSection.Create;
end;
destructor tOnChangeEvent.Destroy;
begin
fInterval := 0;
CritSection.free;
inherited;
end;
//procedure tOnChangeEvent.NilUnusedThreads;
//var
// i : Integer;
//begin
// CritSection.Enter;
// for i := 0 to Length(Threads)-1 do
// begin
// if Assigned(Threads[i])
// if (Threads[i].Terminated) then
// begin
// Threads[i].free;
// Threads[i] := nil;
// end;
// end;
// CritSection.Leave;
//end;
function tOnChangeEvent.NilAndReturnIndex: integer;
var
i : Integer;
begin
result := -1;
CritSection.Enter;
for i := 0 to Length(Threads)-1 do
begin
if Assigned(Threads[i]) then
if (Threads[i].Terminated) then
begin
Threads[i].free;
Threads[i] := nil;
end;
if (Threads[i] = nil) then
begin
result := i;
Break;
end;
end;
CritSection.Leave;
end;
function tOnChangeEvent.Add( Rec : tOnChange; Sender : TObject; ModuleID, Chan, ChanOffset : integer; Value : boolean; Synchronized : Boolean = False ): integer;
var
i : integer;
begin
result := -2;
if Synchronized then
begin
if NOT (timeGetTime > fLastEventTime[ chan-1+ChanOffset ]+fInterval) then
Exit;
fLastEventTime[ chan-1+ChanOffset ] := timeGetTime;
end
else
begin
if NOT (timeGetTime > fLastEventTimeSynchronized[ chan-1+ChanOffset ]+fInterval) then
Exit;
fLastEventTimeSynchronized[ chan-1+ChanOffset ] := timeGetTime;
end;
result := -1;
i := NilAndReturnIndex;
if (i < 0) then
Exit;
Threads[i] := tOnChangeThread.Create( true );
Threads[i].SetInfos( Rec, Sender, ModuleID, Chan, Value, Synchronized );
Threads[i].Resume;
result := 0;
end;
procedure tOnChangeThread.SetInfos( Rec : tOnChange; Sender : TObject; ModuleID, Chan : integer; Value : boolean; Synchronized : Boolean = false );
begin
fRecDigital := Rec;
fSender := Sender;
fModuleID := ModuleID;
fChan := Chan;
fbValue := value;
fSynchronized := synchronized;
end;
Procedure tOnChangeThread.Execution;
begin
if Assigned(fRecDigital) then
fRecDigital( fSender, fModuleID, fChan, fbValue );
end;
Procedure tOnChangeThread.Execute;
begin
if fSynchronized then
Synchronize( Execution )
else
Execution;
Terminate;
end;
//---------------------------------------------------------------------------
// Base
//---------------------------------------------------------------------------
constructor tBaseCard.Create;
begin
fOnChangeHandler := tOnChangeEvent.Create;
end;
destructor tBaseCard.Destroy;
begin
fOnChangeHandler.Free;
end;
function tBaseCard.GetSimulation : Boolean;
begin
result := fSimulation[ fActiveModule ];
end;
procedure tBaseCard.SetSimulation( val : boolean );
begin
if (fSimulation[ fActiveModule ] = val) then
Exit;
fSimulation[ fActiveModule ] := val;
end;
procedure tBaseCard.SetActiveCard( val : byte );
begin
if (fActiveModule = val ) OR ( val > Modules-1 ) then
Exit;
fActiveModule := val;
end;
function tBaseCard.IsError( ModuleID : SmallInt = -1 ): integer;
begin
if (ModuleID = -1) then
ModuleID := fActiveModule
else
ModuleID := Min( MAX_MODULES_, Max( 0, ModuleID ) );
result := fError[ ModuleID ];
end;
function tBaseCard.GetError: integer;
begin
result := fError[ fActiveModule ];
end;
function tBaseCard.GetOnChangeInterval: Word;
begin
result := fOnChangeHandler.Interval;
end;
procedure tBaseCard.SetOnChangeInterval( val : Word );
begin
fOnChangeHandler.Interval := val;
end;
procedure tWEBIOThread.SetFrequency( val : SmallInt );
begin
if NOT Assigned(self) then
Exit;
if (fFrequency = val) then
Exit;
if (val < 1) or (val > 1000) then
Exit;
fFrequency := val;
end;
constructor tWEBIOThread.Create( CreateSuspended : boolean );
begin
inherited;
{$IFDEF ThreadTimeing}
QueryPerformanceFrequency( fQueryPerformanceFrequency );
{$ENDIF}
fFrequency := 10;
end;
Procedure tWEBIOThread.Execute;
const
TIMEOUTTIME = 1000;
BUFFERSIZE = 100;
var
{$IFDEF ThreadTimeing}
fStartTime, fEndTime : Int64;
{$ENDIF}
i, c : integer;
start : dword;
buffer : array [ 0..BUFFERSIZE-1 ] of AnsiChar;
TimeOut : Boolean;
val : DWord;
bVal : Boolean;
begin
while (not Terminated) AND (not suspended) do
begin
if (NOT Assigned( WEBIO )) OR (NOT ( WEBIO.Modules > 0 )) then
begin
Sleep(Frequency);
continue;
end;
{$IFDEF ThreadTimeing}
QueryPerformanceCounter( fStartTime );
{$ENDIF}
for c := 0 to WEBIO.Modules-1 do
begin
if ( Suspended or Terminated ) then
break;
if ( NOT WebIO.TCPClient[c].Active ) OR ( GetTickCount < WEBIO.fSettings[c].ConnectTime+500 ) then
Continue;
Fillchar( buffer, SizeOf( buffer), 0);
WebIO.SendBuffer( Format( 'GET /input?PW=%s&', [ WebIO.fSettings[c].Password ] ), c );
start := GetTickCount;
TimeOut := false;
while (NOT TimeOut) AND ( WebIO.TCPClient[c].Active ) AND ( ( NOT Suspended ) AND ( NOT Terminated ) ) do
begin
TimeOut := (GetTickCount - start > TIMEOUTTIME);
if WebIO.TCPClient[c].WaitForData( 10 ) then
begin
i := WebIO.TCPClient[c].PeekBuf( buffer, Length( Buffer ) );
WebIO.TCPClient[c].ReceiveBuf( buffer, i );
if ( Matches( buffer,'input;*')) then
begin
val := HexToInt( GetTok( buffer, 2, ';' ) );
if ( WEBIO.fDigitalInVal[ c ] <> val ) then
begin
WEBIO.fDigitalInVal[ c ] := val;
for i := 0 to MAX_CHANS_-1 do
begin
bVal := Boolean( ReadBitFromDWord( val, i ) );
if ( WEBIO.fDigitalIn[ c, i ] <> bVal) then
begin
if Assigned(WEBIO.OnChange) then
WEBIO.fOnChangeHandler.Add( WEBIO.OnChange, self, c, i+1, 0, bVal, false );
if Assigned(WEBIO.OnChangeSynchronized) then
WEBIO.fOnChangeHandler.Add( WEBIO.OnChangeSynchronized, self, c, i+1, 0, bVal, true );
WEBIO.fDigitalIn[ c, i ] := bVal;
end;
end;
end;
end;
WEBIO.lastCommands[c].Check( buffer );
break;
end;
end;
if TimeOut then
WEBIO.fError[c] := -2000
else
WEBIO.fError[c] := 0;
end;
{$IFDEF ThreadTimeing}
QueryPerformanceCounter( fEndTime );
fRuntime := ( fEndTime - fStartTime ) * 1000 / fQueryPerformanceFrequency;
if ( fRuntime >= Frequency ) then
begin
fRuntime := fRuntime;
end
else if ( fRuntime <= Frequency ) then
Sleep( Frequency-Trunc(fRuntime) )
else
{$ENDIF}
Sleep(Frequency);
end;
end;
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
constructor tWEBIO.Create;
var
i : integer;
begin
inherited;
fDeInited := false;
for i := 0 to MAX_MODULES_-1 do
begin
TCPClient[i] := TTCPClient.Create( nil );
TCPClient[i].OnConnect := TcpClientConnect;
TCPClient[i].OnDisconnect := TcpClientDisconnect;
TCPClient[i].OnReceive := TcpClientReceive;
TCPClient[i].OnError := TCPClientError;
TCPClient[i].OnSend := TCPClientSend;
fSettings[i].IP := '192.168.2.1';
fSettings[i].Port := 80;
fSettings[i].Password := '';
fSettings[i].Channels := CHAN2;
fSettings[i].ConnectTime := 0;
fSettings[i].Reconnects := 0;
lastCommands[i] := tLastCommand.Create(i);
end;
myThread := tWEBIOThread.Create( false );
myThread.FreeOnTerminate := false;
end;
destructor tWEBIO.Destroy;
begin
DeInit;