| 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 | 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 |
|
|---|
| 68 | implementation
|
|---|
| 69 |
|
|---|
| 70 | { TCommNode }
|
|---|
| 71 |
|
|---|
| 72 | procedure TCommNode.SetActive(const AValue: Boolean);
|
|---|
| 73 | begin
|
|---|
| 74 | if FActive = AValue then Exit;
|
|---|
| 75 | FActive := AValue;
|
|---|
| 76 | end;
|
|---|
| 77 |
|
|---|
| 78 | { TCommPin }
|
|---|
| 79 |
|
|---|
| 80 | procedure TCommPin.Connect(Pin: TCommPin);
|
|---|
| 81 | begin
|
|---|
| 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;
|
|---|
| 91 | end;
|
|---|
| 92 |
|
|---|
| 93 | destructor TCommPin.Destroy;
|
|---|
| 94 | begin
|
|---|
| 95 | Disconnect;
|
|---|
| 96 | inherited;
|
|---|
| 97 | end;
|
|---|
| 98 |
|
|---|
| 99 | procedure TCommPin.Disconnect;
|
|---|
| 100 | begin
|
|---|
| 101 | if Assigned(RemotePin) then begin
|
|---|
| 102 | RemotePin.RemotePin := nil;
|
|---|
| 103 | RemotePin := nil;
|
|---|
| 104 | end;
|
|---|
| 105 | end;
|
|---|
| 106 |
|
|---|
| 107 | function TCommPin.GetConnected: Boolean;
|
|---|
| 108 | begin
|
|---|
| 109 | Result := Assigned(RemotePin);
|
|---|
| 110 | end;
|
|---|
| 111 |
|
|---|
| 112 | procedure TCommPin.SetStatus(AValue: Integer);
|
|---|
| 113 | begin
|
|---|
| 114 | FStatus := AValue;
|
|---|
| 115 | if Assigned(RemotePin) then RemotePin.ReceiveStatus(AValue);
|
|---|
| 116 | end;
|
|---|
| 117 |
|
|---|
| 118 | constructor TCommPin.Create;
|
|---|
| 119 | begin
|
|---|
| 120 | RemotePin := nil;
|
|---|
| 121 | end;
|
|---|
| 122 |
|
|---|
| 123 | procedure TCommPin.Receive(Stream: TListByte);
|
|---|
| 124 | begin
|
|---|
| 125 | Inc(FDataRxCount, Stream.Count);
|
|---|
| 126 | Inc(FFrameRxCount);
|
|---|
| 127 | if Assigned(FOnLogData) then FOnLogData(Stream, ddReceive);
|
|---|
| 128 | if Assigned(FOnReceive) then FOnReceive(Self, Stream);
|
|---|
| 129 | end;
|
|---|
| 130 |
|
|---|
| 131 | procedure TCommPin.ReceiveStatus(AValue: Integer);
|
|---|
| 132 | begin
|
|---|
| 133 | if Assigned(FOnSetStatus) then FOnSetStatus(Self, AValue);
|
|---|
| 134 | end;
|
|---|
| 135 |
|
|---|
| 136 | procedure TCommPin.ResetCounters;
|
|---|
| 137 | begin
|
|---|
| 138 | FDataTxCount := 0;
|
|---|
| 139 | FDataRxCount := 0;
|
|---|
| 140 | FFrameTxCount := 0;
|
|---|
| 141 | FFrameRxCount := 0;
|
|---|
| 142 | end;
|
|---|
| 143 |
|
|---|
| 144 | procedure TCommPin.Send(Stream: TListByte);
|
|---|
| 145 | begin
|
|---|
| 146 | Inc(FDataTxCount, Stream.Count);
|
|---|
| 147 | Inc(FFrameTxCount);
|
|---|
| 148 | if Assigned(FOnLogData) then FOnLogData(Stream, ddSend);
|
|---|
| 149 | if Assigned(RemotePin) then RemotePin.Receive(Stream);
|
|---|
| 150 | end;
|
|---|
| 151 |
|
|---|
| 152 |
|
|---|
| 153 | end.
|
|---|