source: PinConnection/UCommPin.pas

Last change on this file was 414, checked in by chronos, 12 years ago
  • Fixed: Use parent TCommNode Active property instead custom in PinConnection package.
File size: 3.7 KB
Line 
1unit UCommPin;
2
3{$mode Delphi}{$H+}{$M+}
4
5interface
6
7uses
8 Classes, SpecializedList;
9
10type
11 TCommPin = class;
12 TCommNode = class;
13
14 TDataDiretion = (ddReceive, ddSend);
15 TOnLogDataEvent = procedure (Stream: TListByte; Direction: TDataDiretion) of object;
16 TOnStreamEvent = procedure (Sender: TCommPin; Stream: TListByte) of object;
17 TOnSetStatus = procedure (Sender: TCommPin; Status: Integer) of object;
18
19 { TCommPin }
20
21 TCommPin = class
22 private
23 FOnLogData: TOnLogDataEvent;
24 FOnReceive: TOnStreamEvent;
25 FDataTxCount: Integer;
26 FDataRxCount: Integer;
27 FFrameTxCount: Integer;
28 FFrameRxCount: Integer;
29 FOnSetStatus: TOnSetStatus;
30 FStatus: Integer;
31 function GetConnected: Boolean;
32 procedure SetStatus(AValue: Integer);
33 protected
34 procedure Receive(Stream: TListByte);
35 procedure ReceiveStatus(AValue: Integer);
36 public
37 RemotePin: TCommPin;
38 Node: TCommNode;
39 constructor Create;
40 destructor Destroy; override;
41 procedure Connect(Pin: TCommPin);
42 procedure Disconnect;
43 procedure Send(Stream: TListByte);
44 procedure ResetCounters;
45 property Connected: Boolean read GetConnected;
46 property OnLogData: TOnLogDataEvent read FOnLogData write FOnLogData;
47 property DataTxCount: Integer read FDataTxCount;
48 property DataRxCount: Integer read FDataRxCount;
49 property FrameTxCount: Integer read FFrameTxCount;
50 property FrameRxCount: Integer read FFrameRxCount;
51 property Status: Integer read FStatus write SetStatus; // Used for general status bits such as parity bit
52 property OnReceive: TOnStreamEvent read FOnReceive write FOnReceive;
53 property OnSetSatus: TOnSetStatus read FOnSetStatus write FOnSetStatus;
54 end;
55
56 { TCommNode }
57
58 TCommNode = class(TComponent)
59 private
60 protected
61 FActive: Boolean;
62 procedure SetActive(const AValue: Boolean); virtual;
63 public
64 property Active: Boolean read FActive write SetActive;
65 end;
66
67
68implementation
69
70{ TCommNode }
71
72procedure TCommNode.SetActive(const AValue: Boolean);
73begin
74 if FActive = AValue then Exit;
75 FActive := AValue;
76end;
77
78{ TCommPin }
79
80procedure TCommPin.Connect(Pin: TCommPin);
81begin
82 if Pin <> RemotePin then begin
83 Pin.Disconnect;
84 Disconnect;
85 Self.RemotePin := Pin;
86 if Assigned(Pin) then begin
87 Pin.RemotePin := Self;
88 RemotePin.ReceiveStatus(FStatus);
89 end;
90 end;
91end;
92
93destructor TCommPin.Destroy;
94begin
95 Disconnect;
96 inherited;
97end;
98
99procedure TCommPin.Disconnect;
100begin
101 if Assigned(RemotePin) then begin
102 RemotePin.RemotePin := nil;
103 RemotePin := nil;
104 end;
105end;
106
107function TCommPin.GetConnected: Boolean;
108begin
109 Result := Assigned(RemotePin);
110end;
111
112procedure TCommPin.SetStatus(AValue: Integer);
113begin
114 FStatus := AValue;
115 if Assigned(RemotePin) then RemotePin.ReceiveStatus(AValue);
116end;
117
118constructor TCommPin.Create;
119begin
120 RemotePin := nil;
121end;
122
123procedure TCommPin.Receive(Stream: TListByte);
124begin
125 Inc(FDataRxCount, Stream.Count);
126 Inc(FFrameRxCount);
127 if Assigned(FOnLogData) then FOnLogData(Stream, ddReceive);
128 if Assigned(FOnReceive) then FOnReceive(Self, Stream);
129end;
130
131procedure TCommPin.ReceiveStatus(AValue: Integer);
132begin
133 if Assigned(FOnSetStatus) then FOnSetStatus(Self, AValue);
134end;
135
136procedure TCommPin.ResetCounters;
137begin
138 FDataTxCount := 0;
139 FDataRxCount := 0;
140 FFrameTxCount := 0;
141 FFrameRxCount := 0;
142end;
143
144procedure TCommPin.Send(Stream: TListByte);
145begin
146 Inc(FDataTxCount, Stream.Count);
147 Inc(FFrameTxCount);
148 if Assigned(FOnLogData) then FOnLogData(Stream, ddSend);
149 if Assigned(RemotePin) then RemotePin.Receive(Stream);
150end;
151
152
153end.
Note: See TracBrowser for help on using the repository browser.