⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unit1.~pas

📁 Philips ARM lpc2000家族ISP下载源码集成了串口调试助手源码是个非常优秀的软件
💻 ~PAS
📖 第 1 页 / 共 4 页
字号:
    CloseFile(HexFile);
  end;
  Result := FileLen;
end;

procedure TForm1.LoadDosMessage(buff: string);
var
  i, len, state, Sector, Address: Integer;
  str: string;
  ch: Char;
begin
  len := Length(buff);
  str := '';
  state := 0;
  for i := 1 to len do
  begin
    ch := buff[i];
    if ch >= ' ' then
    begin
      str := str + ch;
    end
    else if ch = #13 then
    begin//行分析
      if (state = 0) and (str = 'Connected') then//链接成功
      begin
        state := 1;
      end
      else if (state = 1) and (str = 'Device selected') then
      begin
        state := 2;
      end
      else if state = 2 then
      begin
        len := Length(str);
        if Copy(str, 1, 6) = 'Memory' then
        begin
          Address := StrToInt('$' + Copy(str, len - 8, 8));
          Sector := GetSectorNumber(Address);
          if Copy(str, 8, 5) = 'blank' then
            StringGrid2.Cells[4, Sector + 1] := 'Y'
          else
            StringGrid2.Cells[4, Sector + 1] := '';
        end;
      end;
      str := '';//行分析结束
    end;
  end;
end;

function TForm1.WinExecAndWait32(FileName: string; Visibility: Integer; var ShowString: string): Cardinal;
var
  sa: TSecurityAttributes;
  hReadPipe, hWritePipe: THandle;
  ret: BOOL;
//  strBuff: array[0..255] of Char;
  strBuff: array[0..4095] of Char;
  lngBytesread: DWORD;
  WorkDir: String;
  StartupInfo: TStartupInfo;
  ProcessInfo: TProcessInformation;
begin
  FillChar(sa, Sizeof(sa), #0);
  sa.nLength := Sizeof(sa);
  sa.bInheritHandle := True;
  sa.lpSecurityDescriptor := nil;
  CreatePipe(hReadPipe, hWritePipe, @sa, 0);

  WorkDir := ExtractFileDir(Application.ExeName);
  FillChar(StartupInfo, Sizeof(StartupInfo), #0);
  StartupInfo.cb := Sizeof(StartupInfo);
  StartupInfo.dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
  StartupInfo.wShowWindow := Visibility;

  StartupInfo.hStdOutput := hWritePipe;
  StartupInfo.hStdError := hWritePipe;

  if not CreateProcess(
    nil,
    PChar(FileName),               { pointer to command line string }
    @sa,                           { pointer to process security attributes }
    @sa,                           { pointer to thread security attributes }
    True,                          { handle inheritance flag }
//    CREATE_NEW_CONSOLE or          { creation flags }
    NORMAL_PRIORITY_CLASS,
    nil,                           { pointer to new environment block }
    PChar(WorkDir),                { pointer to current directory name, PChar}
    StartupInfo,                   { pointer to STARTUPINFO }
    ProcessInfo)                   { pointer to PROCESS_INF }
    then
  begin
    Result := INFINITE {-1};
  end
  else
  begin
    ret := CloseHandle(hWritePipe);
    ShowString := '';
    while ret do
    begin
      FillChar(strBuff, Sizeof(strBuff), #0);
      ret := ReadFile(hReadPipe, strBuff, 256, lngBytesread, nil);
      ShowString := ShowString + strBuff;
    end;

    Application.ProcessMessages;
    WaitforSingleObject(ProcessInfo.hProcess, INFINITE);//
    GetExitCodeProcess(ProcessInfo.hProcess, Result);
    CloseHandle(ProcessInfo.hProcess);  { to prevent memory leaks }
    CloseHandle(ProcessInfo.hThread);
    CloseHandle(hReadPipe);
  end;
end;

function TForm1.GetIspStartStr: string;
var
  str: string;
begin
  str := ' COM(' + Copy(ComboBox6.Text, 4, 1) + ',';//COM1~Com9
  str := str + ComboBox7.Text + ')';//
  str := str + ' ' + 'DEVICE(' + ComboBox5.Text + ',';//器件名称
  str := str + ComboBox8.Text + ')';//波特率
  Result := str;
end;

function TForm1.GetIspReadSecurityStr: string;
var
  str: string;
begin
  str := str + ' READSECURITY';
  Result := str;
end;


function TForm1.GetIspCtrlStr: string;
var
  str: string;
begin
  str := ' ';
  if CheckBox3.Checked then //允许DTR/RTS
  begin
    str := str + 'HARDWARE(BOOTEXEC,';
    str := str + SpinEdit1.Text + ',';//T1
    str := str + SpinEdit2.Text + ')';//T2
  end;
  Result := str;
end;

function TForm1.GetIspReadRAMStr: string;
var
  str: string;
begin
//  str := ' ';
//  str := ' READ(0x40000000, 0x400000ff, c:\test.hex)';
//  str := ' READ(0x40000000, 0x100, c:\test.hex)';
  str := ' READ(0x100, 0x100, c:\test.hex)';
  Result := str;
end;

function TForm1.GetIspEraseStr: string;
var
  str: string;
  i, m, n: integer;
begin
  str := ' ';
  if CheckBox4.Checked then//允许删除
  begin
    if ComboBox12.Text = '全部扇区' then
      str := str + ' ERASE(DEVICE, NOPROTECTISP)'//删除全部扇区
    else if ComboBox12.Text = '文件扇区' then
    begin
      if StrToInt(ComboBox9.Text) > 16 then
        str := str + ' ERASE(DEVICE, NOPROTECTISP)'//删除全部扇区
      else
        for i := 0 to StrToInt(ComboBox9.Text) do
        begin
          str := str + ' ERASE(' + IntToStr(i + 1) + ', NOPROTECTISP)'//删除全部扇区
        end;
    end
    else if ComboBox12.Text = '指定扇区' then
    begin
      m := StrToInt(ComboBox10.Text);
      n := StrToInt(ComboBox11.Text);
      if n > 16 then n := 16;
      if m < n then m := 0;
      for i := m to n do
      begin
        str := str + ' ERASE(' + IntToStr(i + 1) + ', NOPROTECTISP)'//删除全部扇区
      end;
    end
  end;
  Result := str;
end;

function TForm1.GetIspBlankCheckStr(Sector: Integer): string;
var
  str: string;
begin
  str := str + ' BLANKCHECK(0x';
  str := str + IntToHex(GetSectorTop(Sector), 8) + ', 0x';//起始地址
  str := str + IntToHex(GetSectorBottom(Sector), 8) + ')';//终止地址
  Result := str;
end;

function TForm1.GetIspHexFileNameStr(FileName: string): string;
var
  str: string;
begin
  if CheckBox8.Checked then//加密
  begin
    if not CheckBox6.Checked then //填充0
      str := ' HEXFILE(' + FileName + ', NOCHECKSUMS, NOFILL, NOPROTECTISP, CODEREADPROTECTION)'
    else
      str := ' HEXFILE(' + FileName + ', NOCHECKSUMS, FILL, NOPROTECTISP, CODEREADPROTECTION)';
  end
  else
  begin//不加密
    if not CheckBox6.Checked then //填充0
      str := ' HEXFILE(' + FileName + ', NOCHECKSUMS, NOFILL, NOPROTECTISP)'
    else
      str := ' HEXFILE(' + FileName + ', NOCHECKSUMS, FILL, NOPROTECTISP)';
  end;
  Result := str;
end;


function TForm1.GetIspVerifyStr(FileName: string): string;
var
  str: string;
begin
  str := '';
  if CheckBox7.Checked then //校验
    str := ' VERIFY(' + FileName + ', NOCHECKSUMS)';
  Result := str;
end;

function TForm1.GetIspTimeoutsStr: string;
var
  str: string;
begin
  str := ' TIMEOUTS(' + SpinEdit3.Text + ',' + SpinEdit4.Text + ')';
  Result := str;
end;

function TForm1.GetSectorNumber(Address: Integer): Integer;
var
  Sector: integer;
begin
  Sector := Address shr 12;//4KB
  if Sector >= $78 then//0x16~0x1A
    Sector := Sector - $62//4KB
  else if Sector > 8 then//0x08~0x15
  begin
    Sector := Sector shr 3;//1/8
	  Sector := Sector + 7;//32KB
  end;
//  else //0x00~0x07
//  	Sector = Sector;//4KB
  Result := Sector;
end;

function TForm1.GetDeviceFlashSize(Device: string): Integer;
var
  Sector: integer;
begin
  if Device = 'LPC2138' then Sector := 26
  else if Device = 'LPC2136' then Sector := 14
  else if Device = 'LPC2134' then Sector := 10
  else if Device = 'LPC2132' then Sector := 8
  else if Device = 'LPC2131' then Sector := 7
  else Sector := 0;
  Result := Sector;
end;

function TForm1.GetDeviceRAMSize(Device: string): Integer;
var
  RAMSize: integer;
begin
  if Device = 'LPC2138' then RAMSize := 32 * 1024
  else if Device = 'LPC2136' then RAMSize := 32 * 1024
  else if Device = 'LPC2134' then RAMSize := 16 * 1024
  else if Device = 'LPC2132' then RAMSize := 16 * 1024
  else if Device = 'LPC2131' then RAMSize := 8 * 1024
  else RAMSize := 0;
  Result := RAMSize;
end;

function TForm1.GetSectorSize(Sector: Integer): Integer;
var
  Size: integer;
begin
  if (Sector >= $08) and (Sector <= $15) then
    Size := 32 * 1024//32K
  else
    Size := 4 * 1024;//4K
  Result := Size;
end;

function TForm1.GetSectorTop(Sector: Integer): Integer;
var
  SectorTop: integer;
begin
  if Sector < $08 then
    SectorTop := Sector * $1000
  else if Sector <= $15 then
    SectorTop := (Sector - 7) * $8000
  else
    SectorTop := (Sector - $16) * $1000 + $78000;
  Result := SectorTop;
end;

function TForm1.GetSectorBottom(Sector: Integer): Integer;
var
  SectorBottom: integer;
begin
  if Sector < $08 then
    SectorBottom := Sector * $1000 + $fff
  else if Sector <= $15 then
    SectorBottom := (Sector - 7) * $8000 + $7fff
  else
    SectorBottom := (Sector - $16) * $1000 + $78fff;
  Result := SectorBottom;
end;

procedure TForm1.SetUserIDStr;
var
  j, len, address: integer;
  str: string;
  Bval, Bvar: Byte;
  Wval, Wvar: Word;
  Dval, Dvar: Dword;
begin
//  for i := 1 to StringGrid3.RowCount - 1 do
//  begin
    if (Edit5.Text <> '') and (Edit6.Text <> '') then
    begin
      str := Edit5.Text;//地址
      if str[1] <> '0' then
        address := StrToInt(str)
      else
      begin
        Delete(str, 1, 2);
        address := StrToInt('$' + str);
      end;
      str := ComboBox13.Text;//类型
      if str = '短整型' then
      begin
        str := Edit6.Text;//序列码
        if str[1] <> '0' then
          Wvar := StrToInt(str)
        else
        begin
          Delete(str, 1, 2);
          Wvar := StrToInt('$' + str);
        end;
        Wval := GetStringGridWord(address);
        if Wval <> $ffff then
        begin
          if Wval = Wvar then
            CheckBox12.Checked := True
          else
            CheckBox12.Checked := False;
        end
        else
        begin
          SetStringGridWord(address, Wvar);
        end;
      end
      else if str = '整型' then
      begin
        str := Edit6.Text;//序列码
        if str[1] <> '0' then
          Dvar := StrToInt(str)
        else
        begin
          Delete(str, 1, 2);
          Dvar := StrToInt('$' + str);
        end;
        Dval := GetStringGridDWord(address);
        if Dval <> $ffffffff then
        begin
          if Dval = Dvar then
            CheckBox12.Checked := True
          else
            CheckBox12.Checked := False;
        end
        else
        begin
          SetStringGridDWord(address, Dvar);
        end;
      end
      else if str = '字符型' then
      begin
        len := Length(Edit6.Text);//序列码
        str := Edit6.Text;//序列码
        for j := 1 to len do
        begin
          Bvar := Byte(str[j]);
          Bval := GetStringGridByte(address);
          if Bval <> $ff then
          begin
            if Bval = Bvar then
              CheckBox12.Checked := True
            else
              CheckBox12.Checked := False;
          end
          else
          begin
            SetStringGridByte(address, Bvar);
            CheckBox12.Checked := True;
          end;
          address := address + 1;
        end;
      end;
    end;
//  end;
end;

procedure TForm1.SetCodeStr;
var
  i, Sector, CodeLen: Integer;
begin
  if Edit4.Text = '' then Exit;
  Sector := GetDeviceFlashSize(ComboBox5.Text);
  if Sector > 0 then
  begin
    CodeLen := GetSectorNumber(StrToInt(Edit4.Text) - 1);
    for i := 0 to Sector do
    begin
      if i <= CodeLen then
        StringGrid2.Cells[3, i + 1] := 'Y'
      else
        StringGrid2.Cells[3, i + 1] := '';
    end;
  end;
end;

procedure TForm1.SetBlankCheckStr;
var
  i, Sector, CodeLen: Integer;
begin
  if Edit4.Text = '' then Exit;
  Sector := GetDeviceFlashSize(ComboBox5.Text);
  if Sector > 0 then
  begin
    CodeLen := GetSectorNumber(StrToInt(Edit4.Text) - 1);
    for i := 0 to Sector do
    begin
      if i <= CodeLen then
        StringGrid2.Cells[3, i + 1] := 'Y'
      else
        StringGrid2.Cells[3, i + 1] := '';
    end;
  end;
end;


procedure TForm1.Button6Click(Sender: TObject);
var
  str, buff: string;
  i: integer;
begin
  if not FileExists(Edit3.Text) then
  begin
    StatusBar1.Panels[1].Text := '请选择正确的Hex文件!!!';
    Exit;
  end;
  Memo3.Lines.Clear;
  PageControl2.ActivePage := TabSheet4;
  str := Edit2.Text + GetIspStartStr() + GetIspCtrlStr();
  if CheckBox4.Checked then str := str + GetIspEraseStr();
  if CheckBox5.Checked then
  begin
    for i := 0 to StrToInt(ComboBox9.Text) do
    begin
      str := str + GetIspBlankCheckStr(i);
    end;
  end;
  str := str + GetIspHexFileNameStr(Edit3.Text);
  str := str + GetIspVerifyStr(Edit3.Text);
  str := str + GetIspTimeoutsStr();
  if WinExecAndWait32(str, ShowModeSele, buff) <> 0 then
  begin
    StatusBar1.Panels[1].Text := '编程操作失败!!!';
  end
  else

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -