Модуль "P2KApi" (часть 63) - TP2KAPI.SendFileFromFile3

Рубрика: Исходники
Среда, 21 апреля 2010 г.
Просмотров: 152
последняя версия: 3.0.9 build 961 beta
от 07 декабря 2007 г.
function TP2KAPI.SendFileFromFile3(AFileName: TFileName; Addr: Dword; AfrmProgress: TfrmProgress): boolean;
var Cmd_temp:array[0..2055] of byte;
    Cmd:array[0..3] of char;
    i: word;
    j: dword;
    chk:byte;
    Cmd_Ans:array[0..1023] of char;
    tmpstr:shortstring;
    SendSize, Progress:dword;
    BlockSize:word;
    BlockAddr:dword;
    BufPos:dword;
    DataTmp:Pointer;
    xx: dword;
    //
    fHandle: integer;
    Size: dword;
    Buffer: pbytearray;
    mStream: TStream;
    pFilesData: pbytearray;
    m: integer;
    HexAddr:string[8];
  AnsSize: dword;
  cntBlocks: integer;
  s: string; 

begin
  result:= false;
  if not TelStatus then exit;
  fHandle:= FileOpen(AFileName, fmOpenRead);
  if fHandle = -1 then begin FileClose(fHandle); exit; end;
  Size:= GetFileSize(fHandle, nil);
  FileClose(fHandle);
  mStream:=  TFileStream.Create(AFileName, fmOpenRead);
  //pFilesData:= AllocMem(Size);
  chk:=0;
  SendSize:=Size;
  BlockAddr:=Addr;
  BufPos:=0;
  result:=true;
  AfrmProgress.AdvProgressBar1.Value:= 0;
  AfrmProgress.AdvProgressBar1.MaxValue:= Size;
  result:= true;
  //pFilesData:= AllocMem($800);
  try
    repeat
        // подготовка к залитию данных
        FlashCmd3(#$45);
        HexAddr:= IntToHex(BlockAddr, 8);
        Cmd[0]:= chr( strtoint('$' + HexAddr[1] + HexAddr[2]));
        Cmd[1]:= chr( strtoint('$' + HexAddr[3] + HexAddr[4]));
        Cmd[2]:= chr( strtoint('$' + HexAddr[5] + HexAddr[6]));
        Cmd[3]:= chr( strtoint('$' + HexAddr[7] + HexAddr[8]));
        FlashCmd3(Cmd[0] + Cmd[1] + Cmd[2] + Cmd[3]);
        FillChar(Cmd_ans, 1024, #0); xx:= 1024;
        USB_ReadDataEx(@Cmd_ans, xx,2,xFlashTimeOut);
        CopyMemory(@AnsSize, @Cmd_ans[0], 4);
        AnsSize:= convert_dword(AnsSize);
        if AnsSize = 0 then begin mStream.Free; exit; end;
        // теперь в цикле вливать данные
        j:= 0;
        while (j < AnsSize) and (j < Size) do
          begin
            // первая порция
            if SendSize < $800 then BlockSize:=SendSize else BlockSize:=$800;
            m:= BlockSize mod 8;
            pFilesData:= AllocMem(BlockSize);
            FillMemory(@pFilesData[0], BlockSize, $FF);
            mStream.Read(pFilesData^, BlockSize);
            CopyMemory(@cmd_temp[0], @pFilesData[0], BlockSize);
            FreeMem(pFilesData);     pFilesData:= nil;
            FillChar(Cmd_ans, 1024, #0);
            USB_WriteDataEx(@Cmd_temp, BlockSize, 2, xFlashTimeOut);
            SendSize:=SendSize-BlockSize;
            if m = 0 then begin AfrmProgress.AdvProgressBar1.Value:= AfrmProgress.AdvProgressBar1.Value + BlockSize;
                                Application.ProcessMessages; AfrmProgress.Repaint;
                          end;      
            BlockAddr:=BlockAddr+BlockSize;
            BufPos:=BufPos+BlockSize;
            inc(j, BlockSize);
            // вторая порция
            if SendSize < $800 then BlockSize:=SendSize else BlockSize:=$800;
            if BlockSize > 1 then
            begin
              m:= BlockSize mod 8;
              pFilesData:= AllocMem(BlockSize);
              FillMemory(@pFilesData[0], BlockSize, $FF);
              mStream.Read(pFilesData^, BlockSize);
              CopyMemory(@cmd_temp[0], @pFilesData[0], BlockSize);
              FreeMem(pFilesData);     pFilesData:= nil;
              USB_WriteDataEx(@Cmd_temp, BlockSize, 2, xFlashTimeOut);
              FillChar(Cmd_ans, 1024, #0);  xx:= 1024;
              // прочитать ответ
              USB_ReadDataEx(@Cmd_ans, xx,2,xFlashTimeOut);
              SendSize:=SendSize-BlockSize;
              if m = 0 then begin AfrmProgress.AdvProgressBar1.Value:= AfrmProgress.AdvProgressBar1.Value + BlockSize;
                                  Application.ProcessMessages; AfrmProgress.Repaint;
                            end;
              BlockAddr:=BlockAddr+BlockSize;
              BufPos:=BufPos+BlockSize;
              inc(j, BlockSize);
            end;  
            //////
            //sleep(100);
          end;
        FillChar(Cmd_ans, 1024, #0); xx:= 1024;
        // прочитать завершающий ответ
        USB_ReadDataEx(@Cmd_ans, xx,2,xFlashTimeOut);
      until SendSize = 0;
finally
  mStream.Free;
end;
end;
Rambler's Top100