Ignore:
Timestamp:
Aug 14, 2012, 10:28:11 AM (12 years ago)
Author:
chronos
Message:
  • Modified: TDallasProgrammer in ISPProgrammer package will use existed external serial port instead creating own.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • ISPProgrammer/Dallas/UDallasProgrammer.pas

    r370 r406  
    77uses
    88  Classes, SysUtils, USerialPort, UCommSerialPort, UCommPin,
    9   UJobProgressView, UStreamHelper, SyncObjs, DateUtils, Dialogs, URegistry,
    10   Forms, UISPProgrammer, Registry;
     9  UJobProgressView, SyncObjs, DateUtils, Dialogs, URegistry,
     10  Forms, UISPProgrammer, Registry, UBinarySerializer, SpecializedList;
    1111
    1212const
     
    2222    FOnLogData: TOnLogDataEvent;
    2323    Pin: TCommPin;
    24     Response: TStreamHelper; // should be thread safe
     24    Response: TBinarySerializer; // should be thread safe
    2525    ResponseLock: TCriticalSection;
    26     ResponseTemp: TStreamHelper;
     26    ResponseTemp: TBinarySerializer;
    2727    ReceiveEvent: TEvent;
    28     FwSerialPort: TCommSerialPort;
     28    SerialPortBackup: TCommSerialPort;
     29    SerialPortBackupPin: TCommPin;
    2930    HexData: TStringList;
    30     Request: TStreamHelper;
     31    Request: TBinarySerializer;
    3132    StartTime: TDateTime;
    3233    WaitResult: TWaitResult;
    33     procedure ReceiveData(Sender: TCommPin; Stream: TStream);
     34    procedure ReceiveData(Sender: TCommPin; Stream: TListByte);
    3435    function WaitForString(EndString: string; Timeout: TDateTime): TWaitResult;
    3536    procedure ResponseClear;
    3637    procedure CheckErrorCode(Value: string);
    37     procedure TruncateBeginOfStream(Stream: TStream);
    3838  protected
    3939    procedure SetActive(AValue: Boolean); override;
     
    7474{ TDallasProgrammer }
    7575
    76 procedure TDallasProgrammer.ReceiveData(Sender: TCommPin; Stream:TStream);
     76procedure TDallasProgrammer.ReceiveData(Sender: TCommPin; Stream: TListByte);
    7777var
    7878  OldPosition: Integer;
     
    8181    ResponseLock.Acquire;
    8282    OldPosition := Response.Position;
    83     Response.Position := Response.Size;
    84     Response.WriteStream(Stream, Stream.Size);
     83    Response.Position := Response.List.Count;
     84    Response.WriteList(Stream, 0, Stream.Count);
    8585    Response.Position := OldPosition;
    8686  finally
     
    134134  inherited;
    135135  if AValue then begin
    136     Request := TStreamHelper.Create;
     136    Request := TBinarySerializer.Create;
     137    Request.List := TListByte.Create;
     138    Request.OwnsList := True;
    137139    HexData := TStringList.Create;
    138140
    139141    SerialPort.Active := False;
    140     FwSerialPort := TCommSerialPort.Create;
    141     FwSerialPort.Name := SerialPort.Name;
    142     FwSerialPort.FlowControl := fcNone;
    143     FwSerialPort.BaudRate := BaudRate;
    144     FwSerialPort.DTR := True;
    145     FwSerialPort.Flush;
    146     FwSerialPort.Purge;
     142    SerialPortBackup.Assign(SerialPort);
     143    SerialPortBackupPin := SerialPort.Pin.RemotePin;
     144    SerialPort.Pin.Disconnect;
     145
     146    //SerialPort.Name := SerialPort.Name;
     147    SerialPort.FlowControl := fcNone;
     148    SerialPort.BaudRate := BaudRate;
     149    SerialPort.DTR := True;
     150    SerialPort.Pin.Connect(Pin);
     151    SerialPort.Flush;
     152    SerialPort.Purge;
    147153    ResponseClear;
    148     FwSerialPort.Pin.Connect(Pin);
    149     FwSerialPort.Active := True;
     154    SerialPort.Active := True;
    150155    if Assigned(FOnLogData) then
    151156      Pin.OnLogData := FOnLogData;
     
    153158    ReadIdentification;
    154159  end else begin
    155     FwSerialPort.Free;
     160    SerialPort.Active := False;
     161    SerialPort.Assign(SerialPortBackup);
     162    SerialPort.Pin.Connect(SerialPortBackupPin);
    156163    SerialPort.Active := True;
    157164    HexData.Free;
     
    174181end;
    175182
    176 procedure TDallasProgrammer.TruncateBeginOfStream(Stream: TStream);
    177 begin
    178   // Delete start of Response stream
    179   //Stream.Position := Stream.Position - 1;
    180   if (Stream.Size - Stream.Position) > 0 then begin
    181     ResponseTemp.Clear;
    182     ResponseTemp.CopyFrom(Stream, Stream.Size - Stream.Position);
    183     Stream.Size := 0;
    184     ResponseTemp.Position := 0;
    185     Stream.CopyFrom(ResponseTemp, ResponseTemp.Size);
    186     Stream.Position := 0;
    187   end else Stream.Size := 0;
    188 end;
    189 
    190183procedure TDallasProgrammer.LoadFromRegistry(Root: HKEY; Key: string);
    191184begin
     
    219212  Active := True;
    220213
    221   Request.Size := 0;
     214  Request.List.Count := 0;
    222215  ResponseClear;
    223216  Request.WriteByte(Ord('D'));
    224217  Request.WriteByte($0D);
    225   Pin.Send(Request);
     218  Pin.Send(Request.List);
    226219  if WaitForString(NewLine, Timeout) <> wrSignaled then begin
    227220    raise Exception.Create(STimeout);
     
    230223  try
    231224    ResponseLock.Acquire;
    232     TruncateBeginOfStream(Response);
     225    Response.List.DeleteItems(0, Response.Position);
     226    Response.Position := 0;
    233227  finally
    234228    ResponseLock.Release;
     
    256250      Value := Response.ReadStringTerminated(NewLine);
    257251      if Value <> '' then begin
    258         TruncateBeginOfStream(Response);
     252        Response.List.DeleteItems(0, Response.Position);
     253        Response.Position := 0;
    259254        //Log(Value);
    260255        HexData.Add(Value);
     
    269264    if Job.Terminate then Break;
    270265  until False;
    271   //for I := 0 to HexData.Count - 1 do
     266  //for I := 0 to 10 do //HexData.Count - 1 do
    272267  //  Log(HexData[I]);
    273268  if not Job.Terminate then
     
    282277  Active := True;
    283278
    284   Request.Size := 0;
     279  Request.List.Count := 0;
    285280  ResponseClear;
    286281  Request.WriteByte(Ord('V'));
    287282  Request.WriteByte($0D);
    288   Pin.Send(Request);
     283  Pin.Send(Request.List);
    289284  if WaitForString(NewLine, Timeout) <> wrSignaled then begin
    290     Pin.Send(Request);
     285    Pin.Send(Request.List);
    291286    raise Exception.Create(STimeout);
    292287  end;
     
    294289  Job.Progress.Max := HexData.Count;
    295290  for I := 0 to HexData.Count - 1 do begin
    296     Request.Size := 0;
     291    Request.List.Count := 0;
    297292    ResponseClear;
    298293    Request.WriteString(HexData[I]);
    299294    Request.WriteByte($0D);
    300     Pin.Send(Request);
     295    Pin.Send(Request.List);
    301296    if ReceiveEvent.WaitFor(Round(Timeout / OneMillisecond)) <> wrSignaled then
    302297      raise Exception.Create(STimeout);
     
    304299      ResponseLock.Acquire;
    305300      Response.Position := 0;
    306       if Response.Size = 0 then
     301      if Response.List.Count = 0 then
    307302        raise Exception.Create(SEmptyBuffer);
    308303      Value := Chr(Response.ReadByte);
     
    322317begin
    323318  Active := True;
    324 
    325   Request.Size := 0;
     319  Request.Clear;
    326320  ResponseClear;
    327321  Request.WriteByte(Ord('L'));
    328322  Request.WriteByte($0D);
    329   Pin.Send(Request);
     323  Pin.Send(Request.List);
    330324  if WaitForString(NewLine, Timeout) <> wrSignaled then begin
    331     Pin.Send(Request);
     325    Pin.Send(Request.List);
    332326    raise Exception.Create(STimeout);
    333327  end;
     
    335329  Job.Progress.Max := HexData.Count;
    336330  for I := 0 to HexData.Count - 1 do begin
    337     Request.Size := 0;
     331    Request.List.Count := 0;
    338332    ResponseClear;
    339333    Request.WriteString(HexData[I]);
    340334    Request.WriteByte($0D);
    341     Pin.Send(Request);
     335    Pin.Send(Request.List);
    342336    if ReceiveEvent.WaitFor(Round(Timeout / OneMillisecond)) <> wrSignaled then
    343337      raise Exception.Create(STimeout);
     
    345339      ResponseLock.Acquire;
    346340      Response.Position := 0;
    347       if Response.Size = 0 then
     341      if Response.List.Count = 0 then
    348342        raise Exception.Create(SEmptyBuffer);
    349343      Value := Chr(Response.ReadByte);
     
    360354begin
    361355  Active := True;
    362 
    363   Request.Size := 0;
     356  Request.Clear;
    364357  ResponseClear;
    365358  Request.WriteByte(Ord('K'));
    366359  Request.WriteByte($0D);
    367   Pin.Send(Request);
     360  Pin.Send(Request.List);
    368361  if WaitForString('>', Timeout) <> wrSignaled then
    369362    raise Exception.Create(STimeout);
     
    387380  repeat
    388381    ResponseClear;
    389     Request.Size := 0;
     382    Request.List.Count := 0;
    390383    Request.WriteByte($0D);
    391     Pin.Send(Request);
     384    Pin.Send(Request.List);
    392385    WaitResult := WaitForString('>', Timeout);
    393386  until (WaitResult = wrSignaled) or ((Now - StartTime) > InitTimeout);
     
    412405  Timeout := 3000 * OneMillisecond;
    413406  ReceiveEvent := TSimpleEvent.Create;
    414   Response := TStreamHelper.Create;
     407  Response := TBinarySerializer.Create;
     408  Response.List := TListByte.Create;
     409  Response.OwnsList := True;
    415410  ResponseLock := TCriticalSection.Create;
    416   ResponseTemp := TStreamHelper.Create;
     411  ResponseTemp := TBinarySerializer.Create;
     412  ResponseTemp.List := TListByte.Create;
     413  ResponseTemp.OwnsList := True;
    417414  Pin := TCommPin.Create;
    418415  Pin.OnReceive := ReceiveData;
    419416  BaudRate := br9600;
     417  SerialPortBackup := TCommSerialPort.Create;
    420418end;
    421419
    422420destructor TDallasProgrammer.Destroy;
    423421begin
     422  Active := False;
     423  SerialPortBackup.Free;
    424424  Pin.Free;
    425425  Response.Free;
Note: See TracChangeset for help on using the changeset viewer.