1 | unit UCommPin;
|
---|
2 |
|
---|
3 | {$mode Delphi}{$H+}{$M+}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, SpecializedList;
|
---|
9 |
|
---|
10 | type
|
---|
11 | TCommPin = class;
|
---|
12 |
|
---|
13 | TDataDiretion = (ddReceive, ddSend);
|
---|
14 | TOnLogDataEvent = procedure (Stream: TListByte; Direction: TDataDiretion) of object;
|
---|
15 | TOnStreamEvent = procedure (Sender: TCommPin; Stream: TListByte) of object;
|
---|
16 | TOnSetStatus = procedure (Sender: TCommPin; Status: Integer) of object;
|
---|
17 |
|
---|
18 | { TCommPin }
|
---|
19 |
|
---|
20 | TCommPin = class
|
---|
21 | private
|
---|
22 | FOnLogData: TOnLogDataEvent;
|
---|
23 | FOnReceive: TOnStreamEvent;
|
---|
24 | FDataTxCount: Integer;
|
---|
25 | FDataRxCount: Integer;
|
---|
26 | FFrameTxCount: Integer;
|
---|
27 | FFrameRxCount: Integer;
|
---|
28 | FOnSetStatus: TOnSetStatus;
|
---|
29 | FStatus: Integer;
|
---|
30 | function GetConnected: Boolean;
|
---|
31 | procedure SetStatus(AValue: Integer);
|
---|
32 | protected
|
---|
33 | procedure Receive(Stream: TListByte);
|
---|
34 | procedure ReceiveStatus(AValue: Integer);
|
---|
35 | public
|
---|
36 | RemotePin: TCommPin;
|
---|
37 | constructor Create;
|
---|
38 | destructor Destroy; override;
|
---|
39 | procedure Connect(Pin: TCommPin);
|
---|
40 | procedure Disconnect;
|
---|
41 | procedure Send(Stream: TListByte);
|
---|
42 | procedure ResetCounters;
|
---|
43 | property Connected: Boolean read GetConnected;
|
---|
44 | property OnLogData: TOnLogDataEvent read FOnLogData write FOnLogData;
|
---|
45 | property DataTxCount: Integer read FDataTxCount;
|
---|
46 | property DataRxCount: Integer read FDataRxCount;
|
---|
47 | property FrameTxCount: Integer read FFrameTxCount;
|
---|
48 | property FrameRxCount: Integer read FFrameRxCount;
|
---|
49 | property Status: Integer read FStatus write SetStatus; // Used for general status bits such as parity bit
|
---|
50 | property OnReceive: TOnStreamEvent read FOnReceive write FOnReceive;
|
---|
51 | property OnSetSatus: TOnSetStatus read FOnSetStatus write FOnSetStatus;
|
---|
52 | end;
|
---|
53 |
|
---|
54 |
|
---|
55 | implementation
|
---|
56 |
|
---|
57 | { TCommPin }
|
---|
58 |
|
---|
59 | procedure TCommPin.Connect(Pin: TCommPin);
|
---|
60 | begin
|
---|
61 | if Pin <> RemotePin then begin
|
---|
62 | Pin.Disconnect;
|
---|
63 | Disconnect;
|
---|
64 | Self.RemotePin := Pin;
|
---|
65 | if Assigned(Pin) then begin
|
---|
66 | Pin.RemotePin := Self;
|
---|
67 | RemotePin.ReceiveStatus(FStatus);
|
---|
68 | end;
|
---|
69 | end;
|
---|
70 | end;
|
---|
71 |
|
---|
72 | destructor TCommPin.Destroy;
|
---|
73 | begin
|
---|
74 | Disconnect;
|
---|
75 | inherited;
|
---|
76 | end;
|
---|
77 |
|
---|
78 | procedure TCommPin.Disconnect;
|
---|
79 | begin
|
---|
80 | if Assigned(RemotePin) then begin
|
---|
81 | RemotePin.RemotePin := nil;
|
---|
82 | RemotePin := nil;
|
---|
83 | end;
|
---|
84 | end;
|
---|
85 |
|
---|
86 | function TCommPin.GetConnected: Boolean;
|
---|
87 | begin
|
---|
88 | Result := Assigned(RemotePin);
|
---|
89 | end;
|
---|
90 |
|
---|
91 | procedure TCommPin.SetStatus(AValue: Integer);
|
---|
92 | begin
|
---|
93 | FStatus := AValue;
|
---|
94 | if Assigned(RemotePin) then RemotePin.ReceiveStatus(AValue);
|
---|
95 | end;
|
---|
96 |
|
---|
97 | constructor TCommPin.Create;
|
---|
98 | begin
|
---|
99 | RemotePin := nil;
|
---|
100 | end;
|
---|
101 |
|
---|
102 | procedure TCommPin.Receive(Stream: TListByte);
|
---|
103 | begin
|
---|
104 | Inc(FDataRxCount, Stream.Count);
|
---|
105 | Inc(FFrameRxCount);
|
---|
106 | if Assigned(FOnLogData) then FOnLogData(Stream, ddReceive);
|
---|
107 | if Assigned(FOnReceive) then FOnReceive(Self, Stream);
|
---|
108 | end;
|
---|
109 |
|
---|
110 | procedure TCommPin.ReceiveStatus(AValue: Integer);
|
---|
111 | begin
|
---|
112 | if Assigned(FOnSetStatus) then FOnSetStatus(Self, AValue);
|
---|
113 | end;
|
---|
114 |
|
---|
115 | procedure TCommPin.ResetCounters;
|
---|
116 | begin
|
---|
117 | FDataTxCount := 0;
|
---|
118 | FDataRxCount := 0;
|
---|
119 | FFrameTxCount := 0;
|
---|
120 | FFrameRxCount := 0;
|
---|
121 | end;
|
---|
122 |
|
---|
123 | procedure TCommPin.Send(Stream: TListByte);
|
---|
124 | begin
|
---|
125 | Inc(FDataTxCount, Stream.Count);
|
---|
126 | Inc(FFrameTxCount);
|
---|
127 | if Assigned(FOnLogData) then FOnLogData(Stream, ddSend);
|
---|
128 | if Assigned(RemotePin) then RemotePin.Receive(Stream);
|
---|
129 | end;
|
---|
130 |
|
---|
131 |
|
---|
132 | end.
|
---|