Changeset 288


Ignore:
Timestamp:
Oct 24, 2011, 9:01:08 AM (13 years ago)
Author:
george
Message:
  • Added: TCommPin now support state number forwarding. State value is designed mainly for simulating parity bit and possibly other state bits.
Location:
PinConnection
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • PinConnection/UCommFrame.pas

    r119 r288  
    1919  TFrameState = (fsOutside, fsStart, fsInside, fsEnd);
    2020
     21  { TCommFrame }
     22
    2123  TCommFrame = class
    2224  private
     
    3234    PacketLoss: Real;
    3335    procedure RawDataReceive(Sender: TCommPin; Stream: TStream);
     36    procedure RawSetStatus(Sender: TCommPin; Status: Integer);
    3437    procedure FrameDataReceive(Sender: TCommPin; Stream: TStream);
     38    procedure FrameSetStatus(Sender: TCommPin; Status: Integer);
    3539    constructor Create;
    3640    destructor Destroy; override;
     
    103107end;
    104108
     109procedure TCommFrame.FrameSetStatus(Sender: TCommPin; Status: Integer);
     110begin
     111  RawDataPin.Status := Status;
     112end;
     113
    105114procedure TCommFrame.RawDataReceive(Sender: TCommPin; Stream: TStream);
    106115var
     
    148157end;
    149158
     159procedure TCommFrame.RawSetStatus(Sender: TCommPin; Status: Integer);
     160begin
     161  FrameDataPin.Status := Status;
     162end;
     163
    150164function TCommFrame.GetStreamCRC8(Stream: TStream): Byte;
    151165var
  • PinConnection/UCommHub.pas

    r272 r288  
    1818    function GetPin(Index: Integer): TCommPin;
    1919    procedure Receive(Sender: TCommPin; Stream: TStream);
     20    procedure SetStatus(Sender: TCommPin; Status: Integer);
    2021  public
    2122    constructor Create;
     
    5455end;
    5556
     57procedure TCommHub.SetStatus(Sender: TCommPin; Status: Integer);
     58var
     59  I: Integer;
     60begin
     61  if FActive then begin
     62    // Broadcast received packet to all other pins
     63    for I := 0 to FPins.Count - 1 do
     64      if Sender <> FPins[I] then
     65        TCommPin(FPins[I]).Status := Status;
     66  end;
     67end;
     68
    5669constructor TCommHub.Create;
    5770begin
     
    6881begin
    6982  Pin.OnReceive := Receive;
     83  Pin.OnSetSatus := SetStatus;
    7084  Result := FPins.Add(Pin);
    7185end;
     
    7488begin
    7589  Pin.OnReceive := nil;
     90  Pin.OnSetSatus := nil;
    7691  Result := TCommPin(FPins.Extract(Pin));
    7792end;
     
    90105begin
    91106  Pin.OnReceive := Receive;
     107  Pin.OnSetSatus := SetStatus;
    92108  FPins.Insert(Index, Pin);
    93109end;
     
    107123  Result := TCommPin.Create;
    108124  Result.OnReceive := Receive;
     125  Result.OnSetSatus := SetStatus;
    109126  FPins.Add(Result);
    110127end;
  • PinConnection/UCommPin.pas

    r247 r288  
    1414  TOnLogDataEvent = procedure (Stream: TStream; Direction: TDataDiretion) of object;
    1515  TOnStreamEvent = procedure (Sender: TCommPin; Stream: TStream) of object;
     16  TOnSetStatus = procedure (Sender: TCommPin; Status: Integer) of object;
    1617
    1718  { TCommPin }
     
    2526    FFrameTxCount: Integer;
    2627    FFrameRxCount: Integer;
     28    FOnSetStatus: TOnSetStatus;
     29    FStatus: Integer;
    2730    function GetConnected: Boolean;
     31    procedure SetStatus(AValue: Integer);
     32  protected
     33    procedure Receive(Stream: TStream);
     34    procedure ReceiveStatus(AValue: Integer);
    2835  public
    2936    RemotePin: TCommPin;
     
    3340    procedure Disconnect;
    3441    procedure Send(Stream: TStream);
    35     procedure Receive(Stream: TStream);
    3642    procedure ResetCounters;
    37     property OnReceive: TOnStreamEvent read FOnReceive write FOnReceive;
    3843    property Connected: Boolean read GetConnected;
    3944    property OnLogData: TOnLogDataEvent read FOnLogData write FOnLogData;
     
    4247    property FrameTxCount: Integer read FFrameTxCount;
    4348    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;
    4452  end;
    4553
     
    5563    Disconnect;
    5664    Self.RemotePin := Pin;
    57     Pin.RemotePin := Self;
     65    if Assigned(Pin) then begin
     66      Pin.RemotePin := Self;
     67      RemotePin.ReceiveStatus(FStatus);
     68    end;
    5869  end;
    5970end;
     
    7889end;
    7990
     91procedure TCommPin.SetStatus(AValue: Integer);
     92begin
     93  FStatus := AValue;
     94  if Assigned(RemotePin) then RemotePin.ReceiveStatus(AValue);
     95end;
     96
    8097constructor TCommPin.Create;
    8198begin
     
    90107  Stream.Position := 0;
    91108  if Assigned(FOnReceive) then FOnReceive(Self, Stream);
     109end;
     110
     111procedure TCommPin.ReceiveStatus(AValue: Integer);
     112begin
     113  if Assigned(FOnSetStatus) then FOnSetStatus(Self, AValue);
    92114end;
    93115
  • PinConnection/UCommSerialPort.pas

    r272 r288  
    1010
    1111type
     12
     13  { TCommSerialPort }
     14
    1215  TCommSerialPort = class(TSerialPort)
    1316  private
    1417    procedure Receive(Sender: TCommPin; Stream: TStream);
     18    procedure SetStatus(Sender: TCommPin; AValue: Integer);
    1519    procedure ReceiveData(Stream: TMemoryStream);
    1620  public
     
    3236end;
    3337
     38procedure TCommSerialPort.SetStatus(Sender: TCommPin; AValue: Integer);
     39begin
     40  try
     41    Lock.Acquire;
     42    if (AValue and 1) = 1 then Parity := paMark else Parity := paSpace;
     43  finally
     44    Lock.Release;
     45  end;
     46end;
     47
    3448constructor TCommSerialPort.Create;
    3549begin
     
    3852  Pin := TCommPin.Create;
    3953  Pin.OnReceive := Receive;
     54  Pin.OnSetSatus := SetStatus;
    4055  OnReceiveData := ReceiveData;
    4156end;
  • PinConnection/UCommThread.pas

    r272 r288  
    3636    FInputBufferLock: TCriticalSection;
    3737    FDataAvailable: TEvent;
    38     procedure ReceiveData(Sender: TCommPin; Stream: TStream);
     38    FStatusEvent: TEvent;
     39    FStatusValue: Integer;
     40    procedure PinReceiveData(Sender: TCommPin; Stream: TStream);
     41    procedure PinSetStatus(Sender: TCommPin; Status: Integer);
    3942    procedure ExtReceiveData(Sender: TCommPin; Stream: TStream);
     43    procedure ExtSetStatus(Sender: TCommPin; AStatus: Integer);
    4044    procedure SetActive(const AValue: Boolean);
    4145  public
     
    5155{ TCommThread }
    5256
    53 procedure TCommThread.ReceiveData(Sender: TCommPin; Stream:TStream);
     57procedure TCommThread.PinReceiveData(Sender: TCommPin; Stream:TStream);
    5458begin
    5559  if FActive then Ext.Send(Stream);
     60end;
     61
     62procedure TCommThread.PinSetStatus(Sender: TCommPin; Status: Integer);
     63begin
     64  if FActive then Ext.Status := Status;
    5665end;
    5766
     
    6877    FInputBufferLock.Release;
    6978    StreamHelper.Free;
     79  end;
     80end;
     81
     82procedure TCommThread.ExtSetStatus(Sender: TCommPin; AStatus: Integer);
     83begin
     84  try
     85    FInputBufferLock.Acquire;
     86    FStatusValue := AStatus;
     87    FStatusEvent.SetEvent;
     88  finally
     89    FInputBufferLock.Release;
    7090  end;
    7191end;
     
    96116  Ext := TCommPin.Create;
    97117  Ext.OnReceive := ExtReceiveData;
     118  Ext.OnSetSatus := ExtSetStatus;
    98119  Pin := TCommPin.Create;
    99   Pin.OnReceive := ReceiveData;
     120  Pin.OnReceive := PinReceiveData;
     121  Pin.OnSetSatus := PinSetStatus;
    100122  FDataAvailable := TSimpleEvent.Create;
     123  FStatusEvent := TSimpleEvent.Create;
    101124end;
    102125
     
    108131  Ext.Free;
    109132  Pin.Free;
     133  FStatusEvent.Free;
    110134  FDataAvailable.Free;
    111135  inherited Destroy;
     
    119143    with Parent do
    120144    repeat
     145      // Check if new data arrived
    121146      if FDataAvailable.WaitFor(1) = wrSignaled then
    122147      try
     
    130155        FInputBufferLock.Release;
    131156      end; // else Yield;
     157
     158      // Check if state changed
     159      if FStatusEvent.WaitFor(1) = wrSignaled then
     160      try
     161        FInputBufferLock.Acquire;
     162        Pin.Status := FStatusValue;
     163      finally
     164        FStatusEvent.ResetEvent;
     165        FInputBufferLock.Release;
     166      end;
    132167    until Terminated;
    133168  finally
Note: See TracChangeset for help on using the changeset viewer.