| 1 | unit CommPin; | 
|---|
| 2 |  | 
|---|
| 3 | interface | 
|---|
| 4 |  | 
|---|
| 5 | uses | 
|---|
| 6 | Classes, SpecializedList; | 
|---|
| 7 |  | 
|---|
| 8 | type | 
|---|
| 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 |  | 
|---|
| 77 | implementation | 
|---|
| 78 |  | 
|---|
| 79 | { TCommNodeSimple } | 
|---|
| 80 |  | 
|---|
| 81 | constructor TCommNodeSimple.Create(AOwner: TComponent); | 
|---|
| 82 | begin | 
|---|
| 83 | inherited; | 
|---|
| 84 | Pin := TCommPin.Create; | 
|---|
| 85 | end; | 
|---|
| 86 |  | 
|---|
| 87 | destructor TCommNodeSimple.Destroy; | 
|---|
| 88 | begin | 
|---|
| 89 | Pin.Free; | 
|---|
| 90 | inherited; | 
|---|
| 91 | end; | 
|---|
| 92 |  | 
|---|
| 93 | { TCommNode } | 
|---|
| 94 |  | 
|---|
| 95 | procedure TCommNode.SetActive(const AValue: Boolean); | 
|---|
| 96 | begin | 
|---|
| 97 | if FActive = AValue then Exit; | 
|---|
| 98 | FActive := AValue; | 
|---|
| 99 | end; | 
|---|
| 100 |  | 
|---|
| 101 | { TCommPin } | 
|---|
| 102 |  | 
|---|
| 103 | procedure TCommPin.Connect(Pin: TCommPin); | 
|---|
| 104 | begin | 
|---|
| 105 | RemotePin := Pin; | 
|---|
| 106 | end; | 
|---|
| 107 |  | 
|---|
| 108 | destructor TCommPin.Destroy; | 
|---|
| 109 | begin | 
|---|
| 110 | Disconnect; | 
|---|
| 111 | inherited; | 
|---|
| 112 | end; | 
|---|
| 113 |  | 
|---|
| 114 | procedure TCommPin.Disconnect; | 
|---|
| 115 | begin | 
|---|
| 116 | RemotePin := nil; | 
|---|
| 117 | end; | 
|---|
| 118 |  | 
|---|
| 119 | function TCommPin.GetConnected: Boolean; | 
|---|
| 120 | begin | 
|---|
| 121 | Result := Assigned(RemotePin); | 
|---|
| 122 | end; | 
|---|
| 123 |  | 
|---|
| 124 | procedure TCommPin.SetRemotePin(AValue: TCommPin); | 
|---|
| 125 | begin | 
|---|
| 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; | 
|---|
| 134 | end; | 
|---|
| 135 |  | 
|---|
| 136 | procedure TCommPin.SetStatus(AValue: Integer); | 
|---|
| 137 | begin | 
|---|
| 138 | FStatus := AValue; | 
|---|
| 139 | if Assigned(RemotePin) then RemotePin.ReceiveStatus(AValue); | 
|---|
| 140 | end; | 
|---|
| 141 |  | 
|---|
| 142 | constructor TCommPin.Create; | 
|---|
| 143 | begin | 
|---|
| 144 | FRemotePin := nil; | 
|---|
| 145 | FStatus := 0; | 
|---|
| 146 | ResetCounters; | 
|---|
| 147 | end; | 
|---|
| 148 |  | 
|---|
| 149 | procedure TCommPin.Receive(Stream: TListByte); | 
|---|
| 150 | begin | 
|---|
| 151 | Inc(FDataRxCount, Stream.Count); | 
|---|
| 152 | Inc(FFrameRxCount); | 
|---|
| 153 | if Assigned(FOnLogData) then FOnLogData(Stream, ddReceive); | 
|---|
| 154 | if Assigned(FOnReceive) then FOnReceive(Self, Stream); | 
|---|
| 155 | end; | 
|---|
| 156 |  | 
|---|
| 157 | procedure TCommPin.ReceiveStatus(AValue: Integer); | 
|---|
| 158 | begin | 
|---|
| 159 | if Assigned(FOnSetStatus) then FOnSetStatus(Self, AValue); | 
|---|
| 160 | end; | 
|---|
| 161 |  | 
|---|
| 162 | procedure TCommPin.ResetCounters; | 
|---|
| 163 | begin | 
|---|
| 164 | FDataTxCount := 0; | 
|---|
| 165 | FDataRxCount := 0; | 
|---|
| 166 | FFrameTxCount := 0; | 
|---|
| 167 | FFrameRxCount := 0; | 
|---|
| 168 | end; | 
|---|
| 169 |  | 
|---|
| 170 | procedure TCommPin.Send(Stream: TListByte); | 
|---|
| 171 | begin | 
|---|
| 172 | Inc(FDataTxCount, Stream.Count); | 
|---|
| 173 | Inc(FFrameTxCount); | 
|---|
| 174 | if Assigned(FOnLogData) then FOnLogData(Stream, ddSend); | 
|---|
| 175 | if Assigned(RemotePin) then RemotePin.Receive(Stream); | 
|---|
| 176 | end; | 
|---|
| 177 |  | 
|---|
| 178 | end. | 
|---|