Changeset 26


Ignore:
Timestamp:
May 25, 2010, 1:51:46 PM (14 years ago)
Author:
george
Message:
  • Přidáno: Knihovna VectorObject pro vykreslování vektorových objektů.
  • Přidáno: Knihovna PersistentForm pro uchování stavu oken v registrech.
  • Přidáno: LastOpenedList pro uchování naposledy otevřených souborů.
  • Upraveno: Aktualizace různých knihoven.
Files:
8 added
3 deleted
11 edited

Legend:

Unmodified
Added
Removed
  • ApplicationInfo/UApplicationInfo.pas

    r25 r26  
    1313    MajorVersion: Byte;
    1414    MinorVersion: Byte;
     15    BugFixVersion: Byte;
    1516    CompanyName: string;
    1617    CompanyHomePage: string;
     
    3334  MajorVersion := 1;
    3435  MinorVersion := 0;
     36  BugFixVersion := 0;
    3537  CompanyName := 'Company';
    3638  CompanyHomepage := 'http://www.company.cz/';
  • Comm/UCommFrame.pas

    r23 r26  
    77uses
    88  Classes, UMemoryStreamEx, Dialogs, SysUtils,
    9   Forms, UPin;
     9  Forms, UCommPin;
    1010
    1111const
     
    2828    function GetStreamCRC8(Stream: TStream): Byte;
    2929  public
    30     RawDataPin: TPin;
    31     FrameDataPin: TPin;
     30    RawDataPin: TCommPin;
     31    FrameDataPin: TCommPin;
    3232    PacketLoss: Real;
    3333    procedure RawDataReceive(Stream: TStream);
     
    4747begin
    4848  ReceiveBuffer := TMemoryStreamEx.Create;
    49   RawDataPin := TPin.Create;
     49  RawDataPin := TCommPin.Create;
    5050  RawDataPin.OnReceive := RawDataReceive;
    51   FrameDataPin := TPin.Create;
     51  FrameDataPin := TCommPin.Create;
    5252  FrameDataPin.OnReceive := FrameDataReceive;
    5353  PacketLoss := 0.005;
  • Comm/UCommSerialPort.pas

    r23 r26  
    66
    77uses
    8   Classes, USerialPort, UPin, SysUtils;
     8  Classes, USerialPort, UCommPin, SysUtils;
    99
    1010type
     
    1616    procedure ReceiveData(Stream: TMemoryStream);
    1717  public
    18     DataPin: TPin;
     18    DataPin: TCommPin;
    1919    destructor Destroy; override;
    2020    constructor Create;
     
    3838begin
    3939  inherited;
    40   DataPin := TPin.Create;
     40  DataPin := TCommPin.Create;
    4141  DataPin.OnReceive := Receive;
    4242  OnReceiveData := ReceiveData;
  • Comm/USerialPort.pas

    r23 r26  
    4848    function GetBaudRateNumeric: Integer;
    4949    function GetName: string;
     50    function GetReceiveBuffer:TMemoryStream;
    5051    procedure SetBaudRate(const AValue: TBaudRate);
    5152    procedure SetBaudRateNumeric(const AValue: Integer);
     
    7071    property RTS: Boolean read FRTS write SetRTS;
    7172    property DTR: Boolean read FDTR write SetDTR;
     73    property ReceiveBuffer: TMemoryStream read GetReceiveBuffer;
    7274
    7375    property BaudRateNumeric: Integer read GetBaudRateNumeric write SetBaudRateNumeric;
     
    132134
    133135procedure TSerialPort.Open;
    134 var
    135   DefaultCommConfig: COMMCONFIG;
    136   Size: LongWord;
    137   Port: PChar;
    138136begin
    139137  Connect(FName);
     
    197195begin
    198196  Result := FName;
     197end;
     198
     199function TSerialPort.GetReceiveBuffer:TMemoryStream;
     200begin
     201  Result := FReceiveThread.Stream;
    199202end;
    200203
  • Common/UCommon.pas

    r20 r26  
    1717function CompareByteArray(Data1, Data2: TArrayOfByte): Boolean;
    1818function GetUserName: string;
     19function SplitString(var Text: string; Count: Word): string;
    1920
    2021implementation
     
    126127end;
    127128
     129function SplitString(var Text: string; Count: Word): string;
     130begin
     131  Result := Copy(Text, 1, Count);
     132  Delete(Text, 1, Count);
     133end;
     134
    128135end.
  • IntelHexFile/UIntelHexFile.pas

    r15 r26  
    44
    55uses
    6   UTextFileStream, SysUtils;
     6  Classes, UTextFileStream, SysUtils, Contnrs, UCommon, UMemoryStreamEx,
     7  DateUtils;
    78
    89type
    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;
    1226  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;
    1438  end;
    1539
     
    1943{ TIntelHexFile }
    2044
    21 function TIntelHexFile.ReadContent: TArrayOfByte;
     45function TIntelHexFile.GetSize:Integer;
     46var
     47  I: Integer;
     48begin
     49  Result := 0;
     50  for I := 0 to Blocks.Count - 1 do
     51    Result := Result + TMappedMemory(Blocks[I]).Size;
     52end;
     53
     54procedure TIntelHexFile.WriteBlock(Block:TMappedMemory; StringList:TStringList)
     55  ;
     56var
     57  I: Integer;
     58  L: Integer;
     59  CheckSum: Byte;
     60  OutputLine: string;
     61  Count: Integer;
     62  Line: TMemoryStreamEx;
     63begin
     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;
     90end;
     91
     92function TIntelHexFile.TwoComplement(Value:Byte):Byte;
     93begin
     94  Result := (not Value + 1) and $ff;
     95end;
     96
     97procedure TIntelHexFile.LoadFromFile(FileName: string);
     98var
     99  StringList: TStringList;
     100begin
     101     StringList := TStringList.Create;
     102  StringList.LoadFromFile(UTF8Decode(FileName));
     103  LoadFromStringList(StringList);
     104  StringList.Destroy;
     105end;
     106
     107procedure TIntelHexFile.LoadFromStringList(StringList: TStringList);
    22108var
    23109  Row: string;
    24110  DataCount: Byte;
    25   RecordType: Byte;
    26   I: Integer;
    27   CRC: Byte;
    28   ComputedCRC: Byte;
    29   Address: Word;
     111  RecordType: TIntelHexRecordType;
     112  I: Integer;
     113  CheckSum: Byte;
     114  LastAddress: Integer;
     115  Address: Integer;
    30116  SegmentAddress: Word;
    31117  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;
     120begin
     121  Line := TMemoryStreamEx.Create;
     122  Blocks.Clear;
     123  Block := nil;
     124  LastAddress := -1;
    41125  ExtendedAddress := 0;
    42126  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);
    56155    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;
    69164        ExtendedAddress := 0;
    70165      end;
    71       4: begin // Linear extended address
    72         ExtendedAddress := StrToInt('$' + SplitString(Row, 4));
     166      rtExtendedAddress: begin
     167        ExtendedAddress := (Line.ReadByte shl 8) or Line.ReadByte;
    73168        SegmentAddress := 0;
    74169      end;
    75170    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;
     174end;
     175
     176procedure TIntelHexFile.SaveToFile(FileName: string);
     177var
     178  StringList: TStringList;
     179begin
     180  StringList := TStringList.Create;
     181  SaveToStringList(StringList);
     182  StringList.SaveToFile(UTF8Decode(FileName));
     183  StringList.Destroy;
     184end;
     185
     186procedure TIntelHexFile.SaveToStringList(StringList: TStringList);
     187var
     188  I: Integer;
     189  Line: TMemoryStreamEx;
     190  OutputLine: string;
     191  CheckSum: Byte;
     192begin
     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;
     217end;
     218
     219procedure TIntelHexFile.LoadFromBinFile(FileName:string);
     220var
     221  NewBlock: TMappedMemory;
     222begin
     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));
     227end;
     228
     229procedure TIntelHexFile.SaveToBinFile(FileName:string);
     230var
     231  MergedMemory: TMemoryStreamEx;
     232  I: Integer;
     233begin
     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;
     249end;
     250
     251constructor TIntelHexFile.Create;
     252begin
     253  Blocks := TObjectList.Create;
     254  BytePerLine := 16;
     255end;
     256
     257destructor TIntelHexFile.Destroy;
     258begin
     259  Blocks.Destroy;
     260  inherited Destroy;
    79261end;
    80262
  • MemoryStreamEx/UMemoryStreamEx.pas

    r25 r26  
    66
    77uses
    8   Classes, DateUtils;
     8  Classes, DateUtils, syncobjs;
    99
    1010type
     
    2323    procedure WriteCardinal(Data: Cardinal);
    2424    procedure WriteInt64(Data: Int64);
     25    procedure WriteString(Data: string);
    2526    procedure WriteShortString(Data: ShortString);
    2627    procedure WriteAnsiString(Data: string);
    2728    procedure WriteUnixTime(Data: TDateTime);
    28     procedure WriteStream(Stream: TStream; Count: Integer);
    2929    procedure WriteDouble(Value: Double);
    3030    procedure WriteSingle(Value: Single);
     31    procedure WriteStream(Stream: TStream; Count: Integer);
     32    procedure WriteStreamPart(Stream: TStream; Count: Integer);
    3133    function ReadByte: Byte;
    3234    function ReadWord: Word;
    3335    function ReadCardinal: Cardinal;
    3436    function ReadInt64: Int64;
     37    function ReadString: string;
    3538    function ReadShortString: string;
    3639    function ReadAnsiString: string;
     40    function ReadStringTerminated(Terminator: string = #0): string;
    3741    function ReadUnixTime: TDateTime;
    3842    function ReadDouble: Double;
    3943    function ReadSingle: Single;
    4044    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);
    4148    constructor Create;
    4249    property Endianness: TEndianness read FEndianness write SetEndianness;
    4350  end;
    4451
     52  { TThreadMemoryStreamEx }
     53
     54  TThreadMemoryStreamEx = class(TMemoryStreamEx)
     55    Lock: TCriticalSection;
     56    constructor Create;
     57    destructor Destroy; override;
     58  end;
     59
    4560implementation
    4661
    4762{ TMemoryStreamEx }
    48 
    49 procedure TMemoryStreamEx.WriteAnsiString(Data: string);
    50 var
    51   StringLength: Longint;
    52 begin
    53   StringLength := Length(Data);
    54   Write(StringLength, SizeOf(StringLength));
    55   Write(Data[1], StringLength);
    56 end;
    5763
    5864function TMemoryStreamEx.ReadAnsiString: string;
     
    6773end;
    6874
     75function TMemoryStreamEx.ReadStringTerminated(Terminator: string = #0): string;
     76var
     77  Data: Char;
     78  I: Integer;
     79  OldPosition: Integer;
     80begin
     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;
     94end;
     95
    6996function TMemoryStreamEx.ReadByte: Byte;
    7097begin
     
    82109  ReadBuffer(Result, SizeOf(Int64));
    83110  if SwapData then Result := Swap(Result);
     111end;
     112
     113function TMemoryStreamEx.ReadString:string;
     114begin
     115  SetLength(Result, Size - Position);
     116  if (Size - Position) > 0 then
     117    Read(Result[1], Size - Position)
     118    else Result := '';
    84119end;
    85120
     
    106141end;
    107142
     143procedure TMemoryStreamEx.ReadStreamPart(var Stream:TStream;Count:Integer);
     144var
     145  Buffer: array of Byte;
     146begin
     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;
     154end;
     155
     156procedure TMemoryStreamEx.WriteStreamPart(Stream:TStream;Count:Integer);
     157var
     158  Buffer: array of Byte;
     159begin
     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;
     166end;
     167
    108168constructor TMemoryStreamEx.Create;
    109169begin
     
    124184begin
    125185  ReadBuffer(Result, SizeOf(Single));
     186end;
     187
     188function TMemoryStreamEx.Sum: Byte;
     189begin
     190  Result := 0;
     191  Position := 0;
     192  while Position < Size do
     193    Result := (Result + ReadByte) and $ff;
     194end;
     195
     196procedure TMemoryStreamEx.FillByte(Data:Byte;Count:Integer);
     197var
     198  I: Integer;
     199begin
     200  for I := 0 to Count - 1 do
     201    WriteByte(Data);
    126202end;
    127203
     
    135211begin
    136212  FEndianness := AValue;
    137   {$ifdef FPC_LITTLE_ENDIAN}
     213  {$if defined(FPC_LITTLE_ENDIAN)}
    138214  SwapData := FEndianness = enBig;
    139   {$elseifdef FPC_BIG_ENDIAN}
     215  {$elseif defined(FPC_BIG_ENDIAN)}
    140216  SwapData := FEndianness = enLittle;
    141217  {$endif}
    142218end;
    143219
     220procedure TMemoryStreamEx.WriteAnsiString(Data: string);
     221var
     222  StringLength: Longint;
     223begin
     224  StringLength := Length(Data);
     225  Write(StringLength, SizeOf(StringLength));
     226  Write(Data[1], StringLength);
     227end;
     228
    144229procedure TMemoryStreamEx.WriteByte(Data: Byte);
    145230begin
     
    157242  if SwapData then Data := Swap(Data);
    158243  Write(Data, SizeOf(Int64));
     244end;
     245
     246procedure TMemoryStreamEx.WriteString(Data:string);
     247begin
     248  if Length(Data) > 0 then
     249    Write(Data[1], Length(Data));
    159250end;
    160251
     
    202293end;
    203294
     295{ TThreadMemoryStreamEx }
     296
     297constructor TThreadMemoryStreamEx.Create;
     298begin
     299  inherited Create;
     300  Lock := TCriticalSection.Create;
     301end;
     302
     303destructor TThreadMemoryStreamEx.Destroy;
     304begin
     305  Lock.Destroy;
     306  inherited Destroy;
     307end;
     308
    204309end.
  • Registry/URegistry.pas

    r2 r26  
    11unit URegistry;
     2
     3{$MODE Delphi}
    24
    35interface
  • StringListEx/UStringListEx.pas

    r25 r26  
    11unit UStringListEx;
    2  
    32// Date: 2010-04-14
    43
     4{$mode Delphi}{$H+}
    55interface
    6  
     6
     7uses Classes;
     8
    79type
    810 
  • TextFileStream/UTextFileStream.pas

    r2 r26  
    44unit UTextFileStream;
    55
     6{$mode Delphi}{$H+}
     7
    68interface
    79
    8 uses Classes, Dialogs, SysUtils;
     10uses Classes, SysUtils;
    911
    1012type
  • VarIntSerializer/UVarIntSerializer.pas

    r18 r26  
    285285var
    286286  Data: Byte;
    287   I: Cardinal;
    288287  StoredPosition: Integer;
    289288begin
Note: See TracChangeset for help on using the changeset viewer.