Changeset 26
- Timestamp:
- May 25, 2010, 1:51:46 PM (15 years ago)
- Files:
-
- 8 added
- 3 deleted
- 11 edited
Legend:
- Unmodified
- Added
- Removed
-
ApplicationInfo/UApplicationInfo.pas
r25 r26 13 13 MajorVersion: Byte; 14 14 MinorVersion: Byte; 15 BugFixVersion: Byte; 15 16 CompanyName: string; 16 17 CompanyHomePage: string; … … 33 34 MajorVersion := 1; 34 35 MinorVersion := 0; 36 BugFixVersion := 0; 35 37 CompanyName := 'Company'; 36 38 CompanyHomepage := 'http://www.company.cz/'; -
Comm/UCommFrame.pas
r23 r26 7 7 uses 8 8 Classes, UMemoryStreamEx, Dialogs, SysUtils, 9 Forms, U Pin;9 Forms, UCommPin; 10 10 11 11 const … … 28 28 function GetStreamCRC8(Stream: TStream): Byte; 29 29 public 30 RawDataPin: T Pin;31 FrameDataPin: T Pin;30 RawDataPin: TCommPin; 31 FrameDataPin: TCommPin; 32 32 PacketLoss: Real; 33 33 procedure RawDataReceive(Stream: TStream); … … 47 47 begin 48 48 ReceiveBuffer := TMemoryStreamEx.Create; 49 RawDataPin := T Pin.Create;49 RawDataPin := TCommPin.Create; 50 50 RawDataPin.OnReceive := RawDataReceive; 51 FrameDataPin := T Pin.Create;51 FrameDataPin := TCommPin.Create; 52 52 FrameDataPin.OnReceive := FrameDataReceive; 53 53 PacketLoss := 0.005; -
Comm/UCommSerialPort.pas
r23 r26 6 6 7 7 uses 8 Classes, USerialPort, U Pin, SysUtils;8 Classes, USerialPort, UCommPin, SysUtils; 9 9 10 10 type … … 16 16 procedure ReceiveData(Stream: TMemoryStream); 17 17 public 18 DataPin: T Pin;18 DataPin: TCommPin; 19 19 destructor Destroy; override; 20 20 constructor Create; … … 38 38 begin 39 39 inherited; 40 DataPin := T Pin.Create;40 DataPin := TCommPin.Create; 41 41 DataPin.OnReceive := Receive; 42 42 OnReceiveData := ReceiveData; -
Comm/USerialPort.pas
r23 r26 48 48 function GetBaudRateNumeric: Integer; 49 49 function GetName: string; 50 function GetReceiveBuffer:TMemoryStream; 50 51 procedure SetBaudRate(const AValue: TBaudRate); 51 52 procedure SetBaudRateNumeric(const AValue: Integer); … … 70 71 property RTS: Boolean read FRTS write SetRTS; 71 72 property DTR: Boolean read FDTR write SetDTR; 73 property ReceiveBuffer: TMemoryStream read GetReceiveBuffer; 72 74 73 75 property BaudRateNumeric: Integer read GetBaudRateNumeric write SetBaudRateNumeric; … … 132 134 133 135 procedure TSerialPort.Open; 134 var135 DefaultCommConfig: COMMCONFIG;136 Size: LongWord;137 Port: PChar;138 136 begin 139 137 Connect(FName); … … 197 195 begin 198 196 Result := FName; 197 end; 198 199 function TSerialPort.GetReceiveBuffer:TMemoryStream; 200 begin 201 Result := FReceiveThread.Stream; 199 202 end; 200 203 -
Common/UCommon.pas
r20 r26 17 17 function CompareByteArray(Data1, Data2: TArrayOfByte): Boolean; 18 18 function GetUserName: string; 19 function SplitString(var Text: string; Count: Word): string; 19 20 20 21 implementation … … 126 127 end; 127 128 129 function SplitString(var Text: string; Count: Word): string; 130 begin 131 Result := Copy(Text, 1, Count); 132 Delete(Text, 1, Count); 133 end; 134 128 135 end. -
IntelHexFile/UIntelHexFile.pas
r15 r26 4 4 5 5 uses 6 UTextFileStream, SysUtils; 6 Classes, UTextFileStream, SysUtils, Contnrs, UCommon, UMemoryStreamEx, 7 DateUtils; 7 8 8 9 type 9 TArrayofByte = array of Byte; 10 11 TIntelHexFile = class(TTextFileStream) 10 EFileCorrupted = class(Exception); 11 EChecksumError = class(Exception); 12 13 TIntelHexRecordType = (rtData, rtEndOfFile, rtSegmentAddress, rtExtendedAddress); 14 15 TMappedMemory = class(TMemoryStreamEx) 16 BaseAddress: Integer; 17 end; 18 19 { TIntelHexFile } 20 21 TIntelHexFile = class 22 private 23 function GetSize:Integer; 24 procedure WriteBlock(Block: TMappedMemory; StringList:TStringList); 25 function TwoComplement(Value: Byte): Byte; 12 26 public 13 function ReadContent: TArrayOfByte; 27 Blocks: TObjectList; // of TMappedMemory 28 BytePerLine: Byte; 29 procedure LoadFromFile(FileName: string); 30 procedure SaveToFile(FileName: string); 31 procedure LoadFromStringList(StringList:TStringList); 32 procedure SaveToStringList(StringList:TStringList); 33 procedure LoadFromBinFile(FileName: string); 34 procedure SaveToBinFile(FileName: string); 35 constructor Create; 36 destructor Destroy; override; 37 property Size: Integer read GetSize; 14 38 end; 15 39 … … 19 43 { TIntelHexFile } 20 44 21 function TIntelHexFile.ReadContent: TArrayOfByte; 45 function TIntelHexFile.GetSize:Integer; 46 var 47 I: Integer; 48 begin 49 Result := 0; 50 for I := 0 to Blocks.Count - 1 do 51 Result := Result + TMappedMemory(Blocks[I]).Size; 52 end; 53 54 procedure TIntelHexFile.WriteBlock(Block:TMappedMemory; StringList:TStringList) 55 ; 56 var 57 I: Integer; 58 L: Integer; 59 CheckSum: Byte; 60 OutputLine: string; 61 Count: Integer; 62 Line: TMemoryStreamEx; 63 begin 64 Line := TMemoryStreamEx.Create; 65 Block.Position := 0; 66 for L := 0 to (Block.Size div BytePerLine) do begin 67 Count := BytePerLine; 68 if Count > (Block.Size - Block.Position) then 69 Count := Block.Size - Block.Position; 70 if Count > 0 then begin 71 Line.Size := 4 + Count; 72 Line.Position := 0; 73 Line.WriteByte(Count); 74 Line.WriteByte(((Block.Position + Block.BaseAddress) shr 8) and $ff); 75 Line.WriteByte((Block.Position + Block.BaseAddress) and $ff); 76 Line.WriteByte(Integer(rtData)); 77 Line.WriteStreamPart(TStream(Block), Count); 78 79 CheckSum := TwoComplement(Line.Sum); 80 Line.WriteByte(CheckSum); 81 82 OutputLine := ':'; 83 Line.Position := 0; 84 for I := 0 to Line.Size - 1 do 85 OutputLine := OutputLine + IntToHex(Line.ReadByte, 2); 86 StringList.Add(OutputLine); 87 end; 88 end; 89 Line.Destroy; 90 end; 91 92 function TIntelHexFile.TwoComplement(Value:Byte):Byte; 93 begin 94 Result := (not Value + 1) and $ff; 95 end; 96 97 procedure TIntelHexFile.LoadFromFile(FileName: string); 98 var 99 StringList: TStringList; 100 begin 101 StringList := TStringList.Create; 102 StringList.LoadFromFile(UTF8Decode(FileName)); 103 LoadFromStringList(StringList); 104 StringList.Destroy; 105 end; 106 107 procedure TIntelHexFile.LoadFromStringList(StringList: TStringList); 22 108 var 23 109 Row: string; 24 110 DataCount: Byte; 25 RecordType: Byte;26 I: Integer; 27 C RC: Byte;28 ComputedCRC: Byte;29 Address: Word;111 RecordType: TIntelHexRecordType; 112 I: Integer; 113 CheckSum: Byte; 114 LastAddress: Integer; 115 Address: Integer; 30 116 SegmentAddress: Word; 31 117 ExtendedAddress: Word; 32 EndOfFile: Boolean; 33 34 function SplitString(var Text: string; Count: Word): string; 35 begin 36 Result := Copy(Text, 1, Count); 37 Delete(Text, 1, Count); 38 end; 39 40 begin 118 Block: TMappedMemory; 119 Line: TMemoryStreamEx; 120 begin 121 Line := TMemoryStreamEx.Create; 122 Blocks.Clear; 123 Block := nil; 124 LastAddress := -1; 41 125 ExtendedAddress := 0; 42 126 SegmentAddress := 0; 43 EndOfFile := False; 44 repeat 45 ComputedCRC := 0; 46 Row := ReadLn; 47 if SplitString(Row, 1) <> ':' then raise Exception.Create('File corrupted'); 48 for I := 0 to (Length(Row) div 2) - 2 do 49 ComputedCRC := ComputedCRC + StrToInt('$' + Row[I * 2 + 1] + Row[I * 2 + 2]); 50 ComputedCRC := not (ComputedCRC ) + 1; 51 52 DataCount := StrToInt('$' + SplitString(Row, 2)); 53 Address := StrToInt('$' + SplitString(Row, 4)) + (SegmentAddress shl 4) + 54 (ExtendedAddress shl 16); 55 RecordType := StrToInt('$' + SplitString(Row, 2)); 127 for I := 0 to StringList.Count - 1 do begin 128 Row := Trim(StringList[I]); 129 if SplitString(Row, 1) <> ':' then 130 raise EFileCorrupted.Create('File corrupted'); 131 132 Line.Position := 0; 133 Line.Size := Length(Row) div 2; 134 while Length(Row) > 0 do 135 Line.WriteByte(StrToInt('$' + SplitString(Row, 2))); 136 137 // Read CheckSum 138 Line.Position := Line.Size - 1; 139 CheckSum := Line.ReadByte; 140 Line.Size := Line.Size - 1; 141 if CheckSum <> TwoComplement(Line.Sum) then 142 raise EChecksumError.Create('Checksum error ' + Row); 143 144 Line.Position := 0; 145 DataCount := Line.ReadByte; 146 Address := ((Line.ReadByte shl 8) or Line.ReadByte) + 147 + (SegmentAddress shl 4) + (ExtendedAddress shl 16); 148 149 if LastAddress <> Address then begin 150 Block := TMappedMemory.Create; 151 Block.BaseAddress := Address; 152 Blocks.Add(Block); 153 end; 154 RecordType := TIntelHexRecordType(Line.ReadByte); 56 155 case RecordType of 57 0: begin // Data 58 if Length(Result) < (Address + DataCount) then 59 SetLength(Result, Address + DataCount); 60 for I := 0 to DataCount - 1 do begin 61 Result[Address + I] := StrToInt('$' + SplitString(Row, 2)); 62 end; 63 end; 64 1: begin // End-of-file 65 EndOfFile := True; 66 end; 67 2: begin // Segment address 68 SegmentAddress := StrToInt('$' + SplitString(Row, 4)); 156 rtData: begin 157 Block.WriteStreamPart(Line, Line.Size - Line.Position); 158 end; 159 rtEndOfFile: begin 160 Break; 161 end; 162 rtSegmentAddress: begin 163 SegmentAddress := (Line.ReadByte shl 8) or Line.ReadByte; 69 164 ExtendedAddress := 0; 70 165 end; 71 4: begin // Linear extended address72 ExtendedAddress := StrToInt('$' + SplitString(Row, 4));166 rtExtendedAddress: begin 167 ExtendedAddress := (Line.ReadByte shl 8) or Line.ReadByte; 73 168 SegmentAddress := 0; 74 169 end; 75 170 end; 76 CRC := StrToInt('$' + SplitString(Row, 2)); 77 if CRC <> ComputedCRC then raise Exception.Create('Checksum error'); 78 until Eof or EndOfFile; 171 LastAddress := Address + DataCount; 172 end; 173 Line.Destroy; 174 end; 175 176 procedure TIntelHexFile.SaveToFile(FileName: string); 177 var 178 StringList: TStringList; 179 begin 180 StringList := TStringList.Create; 181 SaveToStringList(StringList); 182 StringList.SaveToFile(UTF8Decode(FileName)); 183 StringList.Destroy; 184 end; 185 186 procedure TIntelHexFile.SaveToStringList(StringList: TStringList); 187 var 188 I: Integer; 189 Line: TMemoryStreamEx; 190 OutputLine: string; 191 CheckSum: Byte; 192 begin 193 StringList.Clear; 194 195 for I := 0 to Blocks.Count - 1 do 196 WriteBlock(TMappedMemory(Blocks[I]), StringList); 197 198 Line := TMemoryStreamEx.Create; 199 200 // Write EndOfFile 201 Line.Size := 4; 202 Line.WriteByte(0); 203 Line.WriteByte(0); 204 Line.WriteByte(0); 205 Line.WriteByte(Integer(rtEndOfFile)); 206 207 CheckSum := TwoComplement(Line.Sum); 208 209 Line.Position := 0; 210 OutputLine := ':'; 211 for I := 0 to Line.Size - 1 do 212 OutputLine := OutputLine + IntToHex(Line.ReadByte, 2); 213 OutputLine := OutputLine + IntToHex(CheckSum, 2); 214 StringList.Add(OutputLine); 215 216 Line.Destroy; 217 end; 218 219 procedure TIntelHexFile.LoadFromBinFile(FileName:string); 220 var 221 NewBlock: TMappedMemory; 222 begin 223 // TODO: analyze empty areas with FF bytes and split them to blocks 224 Blocks.Clear; 225 NewBlock := TMappedMemory.Create; 226 NewBlock.LoadFromFile(UTF8Decode(FileName)); 227 end; 228 229 procedure TIntelHexFile.SaveToBinFile(FileName:string); 230 var 231 MergedMemory: TMemoryStreamEx; 232 I: Integer; 233 begin 234 MergedMemory := TMemoryStreamEx.Create; 235 236 // Fill unused space by unused data (FF) 237 for I := 0 to Blocks.Count - 1 do 238 if MergedMemory.Size < (TMappedMemory(Blocks[I]).BaseAddress + TMappedMemory(Blocks[I]).Size) then 239 MergedMemory.Size := (TMappedMemory(Blocks[I]).BaseAddress + TMappedMemory(Blocks[I]).Size); 240 MergedMemory.FillByte($ff, MergedMemory.Size); 241 242 // Write all memory blocks 243 for I := 0 to Blocks.Count - 1 do begin 244 MergedMemory.Position := TMappedMemory(Blocks[I]).BaseAddress; 245 MergedMemory.WriteStream(TMappedMemory(Blocks[I]), TMappedMemory(Blocks[I]).Size); 246 end; 247 MergedMemory.SaveToFile(UTF8Decode(FileName)); 248 MergedMemory.Destroy; 249 end; 250 251 constructor TIntelHexFile.Create; 252 begin 253 Blocks := TObjectList.Create; 254 BytePerLine := 16; 255 end; 256 257 destructor TIntelHexFile.Destroy; 258 begin 259 Blocks.Destroy; 260 inherited Destroy; 79 261 end; 80 262 -
MemoryStreamEx/UMemoryStreamEx.pas
r25 r26 6 6 7 7 uses 8 Classes, DateUtils ;8 Classes, DateUtils, syncobjs; 9 9 10 10 type … … 23 23 procedure WriteCardinal(Data: Cardinal); 24 24 procedure WriteInt64(Data: Int64); 25 procedure WriteString(Data: string); 25 26 procedure WriteShortString(Data: ShortString); 26 27 procedure WriteAnsiString(Data: string); 27 28 procedure WriteUnixTime(Data: TDateTime); 28 procedure WriteStream(Stream: TStream; Count: Integer);29 29 procedure WriteDouble(Value: Double); 30 30 procedure WriteSingle(Value: Single); 31 procedure WriteStream(Stream: TStream; Count: Integer); 32 procedure WriteStreamPart(Stream: TStream; Count: Integer); 31 33 function ReadByte: Byte; 32 34 function ReadWord: Word; 33 35 function ReadCardinal: Cardinal; 34 36 function ReadInt64: Int64; 37 function ReadString: string; 35 38 function ReadShortString: string; 36 39 function ReadAnsiString: string; 40 function ReadStringTerminated(Terminator: string = #0): string; 37 41 function ReadUnixTime: TDateTime; 38 42 function ReadDouble: Double; 39 43 function ReadSingle: Single; 40 44 procedure ReadStream(var Stream: TStream; Count: Integer); 45 procedure ReadStreamPart(var Stream: TStream; Count: Integer); 46 function Sum: Byte; 47 procedure FillByte(Data: Byte; Count: Integer); 41 48 constructor Create; 42 49 property Endianness: TEndianness read FEndianness write SetEndianness; 43 50 end; 44 51 52 { TThreadMemoryStreamEx } 53 54 TThreadMemoryStreamEx = class(TMemoryStreamEx) 55 Lock: TCriticalSection; 56 constructor Create; 57 destructor Destroy; override; 58 end; 59 45 60 implementation 46 61 47 62 { TMemoryStreamEx } 48 49 procedure TMemoryStreamEx.WriteAnsiString(Data: string);50 var51 StringLength: Longint;52 begin53 StringLength := Length(Data);54 Write(StringLength, SizeOf(StringLength));55 Write(Data[1], StringLength);56 end;57 63 58 64 function TMemoryStreamEx.ReadAnsiString: string; … … 67 73 end; 68 74 75 function TMemoryStreamEx.ReadStringTerminated(Terminator: string = #0): string; 76 var 77 Data: Char; 78 I: Integer; 79 OldPosition: Integer; 80 begin 81 OldPosition := Position; 82 Result := ''; 83 I := 1; 84 repeat 85 if Position >= Size then Break; 86 Data := Chr(ReadByte); 87 if Data <> Terminator[I] then Result := Result + Data 88 else Inc(I); 89 until I > Length(Terminator); 90 if not (I > Length(Terminator)) then begin 91 Result := ''; 92 Position := OldPosition; 93 end; 94 end; 95 69 96 function TMemoryStreamEx.ReadByte: Byte; 70 97 begin … … 82 109 ReadBuffer(Result, SizeOf(Int64)); 83 110 if SwapData then Result := Swap(Result); 111 end; 112 113 function TMemoryStreamEx.ReadString:string; 114 begin 115 SetLength(Result, Size - Position); 116 if (Size - Position) > 0 then 117 Read(Result[1], Size - Position) 118 else Result := ''; 84 119 end; 85 120 … … 106 141 end; 107 142 143 procedure TMemoryStreamEx.ReadStreamPart(var Stream:TStream;Count:Integer); 144 var 145 Buffer: array of Byte; 146 begin 147 if Count > 0 then begin 148 SetLength(Buffer, Count); 149 ReadBuffer(Buffer[0], Count); 150 if Stream.Size < (Stream.Position + Count) then 151 Stream.Size := Stream.Position + Count; 152 Stream.Write(Buffer[0], Count); 153 end; 154 end; 155 156 procedure TMemoryStreamEx.WriteStreamPart(Stream:TStream;Count:Integer); 157 var 158 Buffer: array of Byte; 159 begin 160 if Count > Stream.Size then Count := Stream.Size; // Limit max. stream size 161 if Count > 0 then begin 162 SetLength(Buffer, Count); 163 Stream.Read(Buffer[0], Count); 164 Write(Buffer[0], Count); 165 end; 166 end; 167 108 168 constructor TMemoryStreamEx.Create; 109 169 begin … … 124 184 begin 125 185 ReadBuffer(Result, SizeOf(Single)); 186 end; 187 188 function TMemoryStreamEx.Sum: Byte; 189 begin 190 Result := 0; 191 Position := 0; 192 while Position < Size do 193 Result := (Result + ReadByte) and $ff; 194 end; 195 196 procedure TMemoryStreamEx.FillByte(Data:Byte;Count:Integer); 197 var 198 I: Integer; 199 begin 200 for I := 0 to Count - 1 do 201 WriteByte(Data); 126 202 end; 127 203 … … 135 211 begin 136 212 FEndianness := AValue; 137 {$if def FPC_LITTLE_ENDIAN}213 {$if defined(FPC_LITTLE_ENDIAN)} 138 214 SwapData := FEndianness = enBig; 139 {$elseif def FPC_BIG_ENDIAN}215 {$elseif defined(FPC_BIG_ENDIAN)} 140 216 SwapData := FEndianness = enLittle; 141 217 {$endif} 142 218 end; 143 219 220 procedure TMemoryStreamEx.WriteAnsiString(Data: string); 221 var 222 StringLength: Longint; 223 begin 224 StringLength := Length(Data); 225 Write(StringLength, SizeOf(StringLength)); 226 Write(Data[1], StringLength); 227 end; 228 144 229 procedure TMemoryStreamEx.WriteByte(Data: Byte); 145 230 begin … … 157 242 if SwapData then Data := Swap(Data); 158 243 Write(Data, SizeOf(Int64)); 244 end; 245 246 procedure TMemoryStreamEx.WriteString(Data:string); 247 begin 248 if Length(Data) > 0 then 249 Write(Data[1], Length(Data)); 159 250 end; 160 251 … … 202 293 end; 203 294 295 { TThreadMemoryStreamEx } 296 297 constructor TThreadMemoryStreamEx.Create; 298 begin 299 inherited Create; 300 Lock := TCriticalSection.Create; 301 end; 302 303 destructor TThreadMemoryStreamEx.Destroy; 304 begin 305 Lock.Destroy; 306 inherited Destroy; 307 end; 308 204 309 end. -
Registry/URegistry.pas
r2 r26 1 1 unit URegistry; 2 3 {$MODE Delphi} 2 4 3 5 interface -
StringListEx/UStringListEx.pas
r25 r26 1 1 unit UStringListEx; 2 3 2 // Date: 2010-04-14 4 3 4 {$mode Delphi}{$H+} 5 5 interface 6 6 7 uses Classes; 8 7 9 type 8 10 -
TextFileStream/UTextFileStream.pas
r2 r26 4 4 unit UTextFileStream; 5 5 6 {$mode Delphi}{$H+} 7 6 8 interface 7 9 8 uses Classes, Dialogs,SysUtils;10 uses Classes, SysUtils; 9 11 10 12 type -
VarIntSerializer/UVarIntSerializer.pas
r18 r26 285 285 var 286 286 Data: Byte; 287 I: Cardinal;288 287 StoredPosition: Integer; 289 288 begin
Note:
See TracChangeset
for help on using the changeset viewer.