Changeset 407 for PinConnection/UCommFrame.pas
- Timestamp:
- Aug 14, 2012, 10:31:30 AM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
PinConnection/UCommFrame.pas
r299 r407 6 6 7 7 uses 8 Classes, UStreamHelper, Dialogs, SysUtils, 8 Classes, UStreamHelper, Dialogs, SysUtils, SpecializedList, UBinarySerializer, 9 9 UCommPin; 10 10 … … 17 17 private 18 18 LastCharIsSpecialChar: Boolean; 19 ReceiveBuffer: T StreamHelper;19 ReceiveBuffer: TBinarySerializer; 20 20 FrameState: TFrameState; 21 21 FFrameErrorCount: Integer; 22 22 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); 24 28 public 25 29 RawDataPin: TCommPin; … … 30 34 ControlCodeFrameEnd: Byte; 31 35 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; 37 37 constructor Create; 38 38 destructor Destroy; override; … … 48 48 constructor TCommFrame.Create; 49 49 begin 50 ReceiveBuffer := TStreamHelper.Create; 50 ReceiveBuffer := TBinarySerializer.Create; 51 ReceiveBuffer.List := TListByte.Create; 52 ReceiveBuffer.OwnsList := True; 51 53 RawDataPin := TCommPin.Create; 52 54 RawDataPin.OnReceive := RawDataReceive; … … 68 70 end; 69 71 70 procedure TCommFrame.FrameDataReceive(Sender: TCommPin; Stream: T Stream);71 var 72 RawData: T StreamHelper;72 procedure TCommFrame.FrameDataReceive(Sender: TCommPin; Stream: TListByte); 73 var 74 RawData: TBinarySerializer; 73 75 I: Integer; 74 76 Character: Byte; … … 76 78 begin 77 79 // Write CRC code to end of frame 78 Stream.Position := 0;79 80 CRC := GetStreamCRC8(Stream); 80 81 81 82 // Byte stuffing 82 Stream.Position := 0;83 83 try 84 RawData := TStreamHelper.Create; 84 RawData := TBinarySerializer.Create; 85 RawData.List := TListByte.Create; 86 RawData.OwnsList := True; 85 87 RawData.WriteByte(SpecialChar); 86 88 RawData.WriteByte(ControlCodeFrameStart); 87 for I := 0 to Stream. Size- 1 do begin88 Character := Stream .ReadByte;89 for I := 0 to Stream.Count - 1 do begin 90 Character := Stream[I]; 89 91 if Character = SpecialChar then begin 90 92 RawData.WriteByte(SpecialChar); … … 102 104 RawData.WriteByte(ControlCodeFrameEnd); 103 105 if Random >= PacketLoss then 104 RawDataPin.Send(RawData );106 RawDataPin.Send(RawData.List); 105 107 106 108 finally … … 114 116 end; 115 117 116 function TCommFrame.ComputeRawSize(DataStream: TStream): Integer; 118 function TCommFrame.ComputeRawSize(DataStream: TListByte): Integer; 119 var 120 I: Integer; 117 121 begin 118 122 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) 122 125 else Inc(Result, 1); 123 126 end; 124 127 125 procedure TCommFrame.RawDataReceive(Sender: TCommPin; Stream: T Stream);128 procedure TCommFrame.RawDataReceive(Sender: TCommPin; Stream: TListByte); 126 129 var 127 130 Character: Byte; … … 130 133 I: Integer; 131 134 begin 132 for I := 0 to Stream. Size- 1 do begin133 Character := Stream .ReadByte;135 for I := 0 to Stream.Count - 1 do begin 136 Character := Stream[I]; 134 137 if LastCharIsSpecialChar then begin 135 138 if Character = ControlCodeSpecialChar then begin … … 139 142 if FrameState = fsInside then 140 143 Inc(FFrameErrorCount); 141 ReceiveBuffer.Size := 0; 144 ReceiveBuffer.List.Count := 0; 145 ReceiveBuffer.Position := 0; 142 146 FrameState := fsInside; 143 147 end else … … 145 149 if FrameState = fsInside then begin 146 150 // Check CRC 147 if ReceiveBuffer. Size> 0 then begin148 ReceiveBuffer.Position := ReceiveBuffer. Size- 1;151 if ReceiveBuffer.List.Count > 0 then begin 152 ReceiveBuffer.Position := ReceiveBuffer.List.Count - 1; 149 153 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); 152 156 153 157 if ExpectedCRC <> CRC then Inc(FCRCErrorCount) 154 158 else begin 155 159 //if Random >= PacketLoss then 156 FrameDataPin.Send(ReceiveBuffer );160 FrameDataPin.Send(ReceiveBuffer.List); 157 161 end; 158 162 end else Inc(FCRCErrorCount); … … 173 177 end; 174 178 175 function TCommFrame.GetStreamCRC8(Stream: T Stream): Byte;179 function TCommFrame.GetStreamCRC8(Stream: TListByte): Byte; 176 180 var 177 181 I: Integer; … … 182 186 begin 183 187 Pom := 0; 184 Stream.Position := 0;185 188 Result := 0; 186 for I := 0 to Stream. Size- 1 do begin187 Stream.Read(Pom, 1);189 for I := 0 to Stream.Count - 1 do begin 190 Pom := Stream[I]; 188 191 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 190 194 else Result := Result shr 1; 191 195 Pom := (Pom shr 1) or ((Pom shl 7) and $80); // Rotace vpravo
Note:
See TracChangeset
for help on using the changeset viewer.