Ignore:
Timestamp:
Aug 14, 2012, 10:31:30 AM (12 years ago)
Author:
chronos
Message:
  • Modified: PinConnection package now use for data parameters TListByte instead of TStream class contained in TemplateGenerics package. TStream have Position property which has no meaning for forwarding plain byte memory block. There is TBinarySerializer class used for serializing data instead of TStreamHelper class.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • PinConnection/UCommFrame.pas

    r299 r407  
    66
    77uses
    8   Classes, UStreamHelper, Dialogs, SysUtils,
     8  Classes, UStreamHelper, Dialogs, SysUtils, SpecializedList, UBinarySerializer,
    99  UCommPin;
    1010
     
    1717  private
    1818    LastCharIsSpecialChar: Boolean;
    19     ReceiveBuffer: TStreamHelper;
     19    ReceiveBuffer: TBinarySerializer;
    2020    FrameState: TFrameState;
    2121    FFrameErrorCount: Integer;
    2222    FCRCErrorCount: Integer;
    23     function GetStreamCRC8(Stream: TStream): Byte;
     23    function GetStreamCRC8(Stream: TListByte): Byte;
     24    procedure RawDataReceive(Sender: TCommPin; Stream: TListByte);
     25    procedure RawSetStatus(Sender: TCommPin; Status: Integer);
     26    procedure FrameDataReceive(Sender: TCommPin; Stream: TListByte);
     27    procedure FrameSetStatus(Sender: TCommPin; Status: Integer);
    2428  public
    2529    RawDataPin: TCommPin;
     
    3034    ControlCodeFrameEnd: Byte;
    3135    ControlCodeSpecialChar: Byte;
    32     procedure RawDataReceive(Sender: TCommPin; Stream: TStream);
    33     procedure RawSetStatus(Sender: TCommPin; Status: Integer);
    34     procedure FrameDataReceive(Sender: TCommPin; Stream: TStream);
    35     procedure FrameSetStatus(Sender: TCommPin; Status: Integer);
    36     function ComputeRawSize(DataStream: TStream): Integer;
     36    function ComputeRawSize(DataStream: TListByte): Integer;
    3737    constructor Create;
    3838    destructor Destroy; override;
     
    4848constructor TCommFrame.Create;
    4949begin
    50   ReceiveBuffer := TStreamHelper.Create;
     50  ReceiveBuffer := TBinarySerializer.Create;
     51  ReceiveBuffer.List := TListByte.Create;
     52  ReceiveBuffer.OwnsList := True;
    5153  RawDataPin := TCommPin.Create;
    5254  RawDataPin.OnReceive := RawDataReceive;
     
    6870end;
    6971
    70 procedure TCommFrame.FrameDataReceive(Sender: TCommPin; Stream: TStream);
    71 var
    72   RawData: TStreamHelper;
     72procedure TCommFrame.FrameDataReceive(Sender: TCommPin; Stream: TListByte);
     73var
     74  RawData: TBinarySerializer;
    7375  I: Integer;
    7476  Character: Byte;
     
    7678begin
    7779  // Write CRC code to end of frame
    78   Stream.Position := 0;
    7980  CRC := GetStreamCRC8(Stream);
    8081
    8182  // Byte stuffing
    82   Stream.Position := 0;
    8383  try
    84     RawData := TStreamHelper.Create;
     84    RawData := TBinarySerializer.Create;
     85    RawData.List := TListByte.Create;
     86    RawData.OwnsList := True;
    8587    RawData.WriteByte(SpecialChar);
    8688    RawData.WriteByte(ControlCodeFrameStart);
    87     for I := 0 to Stream.Size - 1 do begin
    88       Character := Stream.ReadByte;
     89    for I := 0 to Stream.Count - 1 do begin
     90      Character := Stream[I];
    8991      if Character = SpecialChar then begin
    9092        RawData.WriteByte(SpecialChar);
     
    102104    RawData.WriteByte(ControlCodeFrameEnd);
    103105    if Random >= PacketLoss then
    104       RawDataPin.Send(RawData);
     106      RawDataPin.Send(RawData.List);
    105107
    106108  finally
     
    114116end;
    115117
    116 function TCommFrame.ComputeRawSize(DataStream: TStream): Integer;
     118function TCommFrame.ComputeRawSize(DataStream: TListByte): Integer;
     119var
     120  I: Integer;
    117121begin
    118122  Result := 5; // FrameStart + CRC + FrameEnd
    119   DataStream.Position := 0;
    120   while DataStream.Position < DataStream.Size do
    121     if DataStream.ReadByte = SpecialChar then Inc(Result, 2)
     123  for I := 0 to DataStream.Count - 1 do
     124    if DataStream[I] = SpecialChar then Inc(Result, 2)
    122125      else Inc(Result, 1);
    123126end;
    124127
    125 procedure TCommFrame.RawDataReceive(Sender: TCommPin; Stream: TStream);
     128procedure TCommFrame.RawDataReceive(Sender: TCommPin; Stream: TListByte);
    126129var
    127130  Character: Byte;
     
    130133  I: Integer;
    131134begin
    132   for I := 0 to Stream.Size - 1 do begin
    133     Character := Stream.ReadByte;
     135  for I := 0 to Stream.Count - 1 do begin
     136    Character := Stream[I];
    134137    if LastCharIsSpecialChar then begin
    135138      if Character = ControlCodeSpecialChar then begin
     
    139142          if FrameState = fsInside then
    140143            Inc(FFrameErrorCount);
    141           ReceiveBuffer.Size := 0;
     144          ReceiveBuffer.List.Count := 0;
     145          ReceiveBuffer.Position := 0;
    142146          FrameState := fsInside;
    143147        end else
     
    145149          if FrameState = fsInside then begin
    146150            // Check CRC
    147             if ReceiveBuffer.Size > 0 then begin
    148               ReceiveBuffer.Position := ReceiveBuffer.Size - 1;
     151            if ReceiveBuffer.List.Count > 0 then begin
     152              ReceiveBuffer.Position := ReceiveBuffer.List.Count - 1;
    149153              CRC := ReceiveBuffer.ReadByte;
    150               ReceiveBuffer.Size := ReceiveBuffer.Size - 1;
    151               ExpectedCRC := GetStreamCRC8(ReceiveBuffer);
     154              ReceiveBuffer.List.Count := ReceiveBuffer.List.Count - 1;
     155              ExpectedCRC := GetStreamCRC8(ReceiveBuffer.List);
    152156
    153157              if ExpectedCRC <> CRC then Inc(FCRCErrorCount)
    154158                else begin
    155159                  //if Random >= PacketLoss then
    156                     FrameDataPin.Send(ReceiveBuffer);
     160                    FrameDataPin.Send(ReceiveBuffer.List);
    157161                end;
    158162            end else Inc(FCRCErrorCount);
     
    173177end;
    174178
    175 function TCommFrame.GetStreamCRC8(Stream: TStream): Byte;
     179function TCommFrame.GetStreamCRC8(Stream: TListByte): Byte;
    176180var
    177181  I: Integer;
     
    182186begin
    183187  Pom := 0;
    184   Stream.Position := 0;
    185188  Result := 0;
    186   for I := 0 to Stream.Size - 1 do begin
    187     Stream.Read(Pom, 1);
     189  for I := 0 to Stream.Count - 1 do begin
     190    Pom := Stream[I];
    188191    for B := 0 to 7 do begin
    189       if ((Result xor Pom) and 1) = 1 then Result := ((Result xor Polynom) shr 1) or $80
     192      if ((Result xor Pom) and 1) = 1 then
     193        Result := ((Result xor Polynom) shr 1) or $80
    190194        else Result := Result shr 1;
    191195      Pom := (Pom shr 1) or ((Pom shl 7) and $80); // Rotace vpravo
Note: See TracChangeset for help on using the changeset viewer.