source: trunk/Packages/PinConnection/CommPin.pas

Last change on this file was 317, checked in by chronos, 6 months ago
  • Modified: Remove U prefix from unit names.
  • Modified: Use TFormEx for all forms for code simplification.
File size: 4.0 KB
Line 
1unit CommPin;
2
3interface
4
5uses
6 Classes, SpecializedList;
7
8type
9 TCommPin = class;
10 TCommNode = class;
11
12 TDataDiretion = (ddReceive, ddSend);
13 TOnLogDataEvent = procedure (Stream: TListByte; Direction: TDataDiretion) of object;
14 TOnStreamEvent = procedure (Sender: TCommPin; Stream: TListByte) of object;
15 TOnSetStatus = procedure (Sender: TCommPin; Status: Integer) of object;
16
17 { TCommPin }
18
19 TCommPin = class
20 private
21 FOnLogData: TOnLogDataEvent;
22 FOnReceive: TOnStreamEvent;
23 FDataTxCount: Integer;
24 FDataRxCount: Integer;
25 FFrameTxCount: Integer;
26 FFrameRxCount: Integer;
27 FOnSetStatus: TOnSetStatus;
28 FRemotePin: TCommPin;
29 FStatus: Integer;
30 function GetConnected: Boolean;
31 procedure SetRemotePin(AValue: TCommPin);
32 procedure SetStatus(AValue: Integer);
33 protected
34 procedure Receive(Stream: TListByte);
35 procedure ReceiveStatus(AValue: Integer);
36 public
37 Node: TCommNode;
38 constructor Create;
39 destructor Destroy; override;
40 procedure Connect(Pin: TCommPin);
41 procedure Disconnect;
42 procedure Send(Stream: TListByte);
43 procedure ResetCounters;
44 property RemotePin: TCommPin read FRemotePin write SetRemotePin;
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 { TCommNodeSimple }
68
69 TCommNodeSimple = class(TCommNode)
70 public
71 Pin: TCommPin;
72 constructor Create(AOwner: TComponent); override;
73 destructor Destroy; override;
74 end;
75
76
77implementation
78
79{ TCommNodeSimple }
80
81constructor TCommNodeSimple.Create(AOwner: TComponent);
82begin
83 inherited;
84 Pin := TCommPin.Create;
85end;
86
87destructor TCommNodeSimple.Destroy;
88begin
89 Pin.Free;
90 inherited;
91end;
92
93{ TCommNode }
94
95procedure TCommNode.SetActive(const AValue: Boolean);
96begin
97 if FActive = AValue then Exit;
98 FActive := AValue;
99end;
100
101{ TCommPin }
102
103procedure TCommPin.Connect(Pin: TCommPin);
104begin
105 RemotePin := Pin;
106end;
107
108destructor TCommPin.Destroy;
109begin
110 Disconnect;
111 inherited;
112end;
113
114procedure TCommPin.Disconnect;
115begin
116 RemotePin := nil;
117end;
118
119function TCommPin.GetConnected: Boolean;
120begin
121 Result := Assigned(RemotePin);
122end;
123
124procedure TCommPin.SetRemotePin(AValue: TCommPin);
125begin
126 if FRemotePin = AValue then Exit;
127 if Assigned(FRemotePin) then
128 FRemotePin.FRemotePin := nil;
129 FRemotePin := AValue;
130 if Assigned(FRemotePin) then begin
131 FRemotePin.RemotePin := Self;
132 FRemotePin.ReceiveStatus(FStatus);
133 end;
134end;
135
136procedure TCommPin.SetStatus(AValue: Integer);
137begin
138 FStatus := AValue;
139 if Assigned(RemotePin) then RemotePin.ReceiveStatus(AValue);
140end;
141
142constructor TCommPin.Create;
143begin
144 FRemotePin := nil;
145 FStatus := 0;
146 ResetCounters;
147end;
148
149procedure TCommPin.Receive(Stream: TListByte);
150begin
151 Inc(FDataRxCount, Stream.Count);
152 Inc(FFrameRxCount);
153 if Assigned(FOnLogData) then FOnLogData(Stream, ddReceive);
154 if Assigned(FOnReceive) then FOnReceive(Self, Stream);
155end;
156
157procedure TCommPin.ReceiveStatus(AValue: Integer);
158begin
159 if Assigned(FOnSetStatus) then FOnSetStatus(Self, AValue);
160end;
161
162procedure TCommPin.ResetCounters;
163begin
164 FDataTxCount := 0;
165 FDataRxCount := 0;
166 FFrameTxCount := 0;
167 FFrameRxCount := 0;
168end;
169
170procedure TCommPin.Send(Stream: TListByte);
171begin
172 Inc(FDataTxCount, Stream.Count);
173 Inc(FFrameTxCount);
174 if Assigned(FOnLogData) then FOnLogData(Stream, ddSend);
175 if Assigned(RemotePin) then RemotePin.Receive(Stream);
176end;
177
178end.
Note: See TracBrowser for help on using the repository browser.