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

📄 unit1.pas

📁 STC-Download/STC-ISP下载工具(STC89C51单片机在线下载器) 必须组件: SPComm(必须), VCLSkin(可以去掉) -----------------------
💻 PAS
📖 第 1 页 / 共 2 页
字号:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Buttons, Grids, ExtCtrls, StdCtrls, ComCtrls, SPComm,mmsystem,
  WinSkinData;

type
  TForm1 = class(TForm)
    OpenDialog1: TOpenDialog;
    Shape1: TShape;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    ComPort1: TComm;
    SeekTimer: TTimer;
    Panel1: TPanel;
    SpeedButton1: TSpeedButton;
    Label4: TLabel;
    Label5: TLabel;
    cboPort: TComboBox;
    cboBaudRate: TComboBox;
    TimeOutTM: TTimer;
    Label6: TLabel;
    Panel2: TPanel;
    DataGrid: TStringGrid;
    ProgressBar1: TProgressBar;
    Memo2: TMemo;
    Splitter1: TSplitter;
    Splitter2: TSplitter;
    SkinData1: TSkinData;
    SpeedButton2: TSpeedButton;
    SpeedButton3: TSpeedButton;
    ReloadTimer: TTimer;
    procedure FormCreate(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
    procedure ComPort1ReceiveData(Sender: TObject; Buffer: Pointer;
      BufferLength: Word);
    procedure SeekTimerTimer(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure cboPortChange(Sender: TObject);
    procedure cboBaudRateChange(Sender: TObject);
    procedure TimeOutTMTimer(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure SpeedButton2Click(Sender: TObject);
    procedure SpeedButton3Click(Sender: TObject);
    procedure ReloadTimerTimer(Sender: TObject);
  private
    { Private declarations }
    procedure TimeOutRoutine();
    procedure LoadBinFile(fileName :String); //装入Bin文件
    procedure LoadHexFile(fileName :String); //装入Hex文件
    procedure ShowCodeMemGrid();  //显示存储区表格
    procedure SetGrid();    //设置表格格式
    procedure SendHexPacket(str: String);   //发送字符串数据包
    procedure SendBinSize();  //发送Bin文件大小
    procedure SendBinFile();  //发送Bin文件内容
    procedure LoadConfig(); //载入配置
    procedure SaveConfig(); //保存配置

  public
    { Public declarations }
  end;

const
    SND_START = 'Start.wav';
    SND_GOODBYE = 'goodbye.wav';
    SND_BEEP = 'beep.wav';

    HELP_DOCUMENT = 'HELP.TXT';

    PAK_SEEK    = '7F'; //连机信号
    PAK_RCV_OK  = '46B968';


var
  Form1: TForm1;
  LogList,
  PacketList :TStringList;


  sBinBuffer  :Pointer; //BIN缓冲区
  CodeSize    :DWORD;

  ST_ChipVer  :String;     //芯片版本号, 目前只处理2.9和3.0版本的

  SeekDevice,              //查找设备
  BlockSend   :Boolean;

  TimeOut_Count :Integer;
  implementation

uses PerformPacket;
//解码HEX文件到PMEM
function DecodeHex(HexFileName:LPCTSTR;PMem:PByte) : integer; stdcall;
external'TaoDecode.dll';  //TaoDecode.dll转换Intel Hex格式到Bin格式

{$R *.dfm}
procedure Debug(str:string);
begin
  form1.Memo2.Lines.Add(str);

end;

function GetByteSum(Str:String):String;
var
  sLen,I :Integer;
  Sum :Byte;
begin
  Str := DeleteSubStr(Str,' ');
  I := 1;
  Sum := 0;
  sLen := Length(Str);
  while I< sLen do begin

    Sum := Sum + HexToInt(copy(Str,I,2));
    I := I + 2;
  end;

  Sum := Sum + 1;

  Result := InttoHex(Sum,2);
end;


procedure TForm1.LoadConfig(); //载入配置
var
    strLst :TStringList;
begin
    strLst := TStringList.Create;
    strLst.LoadFromFile(ExtractFilePath(Application.ExeName)+'config.dat');

    ComPort1.CommName := Trim(strLst.Strings[0]);
    ComPort1.BaudRate := StrToInt(Trim(strLst.Strings[1]));
    cboPort.ItemIndex := StrToInt(strLst.Strings[2]);
    cboBaudRate.ItemIndex := StrToInt(strLst.Strings[3]);
    strLst.Free;


end;

{-----------------------------------------------------------------------------}
procedure TForm1.SaveConfig(); //保存配置
var
    strLst :TStringList;
begin
    strLst := TStringList.Create;
    strLst.Add(Trim(ComPort1.CommName));
    strLst.Add(InttoStr(ComPort1.BaudRate));
    strLst.Add(InttoStr(cboport.ItemIndex));
    strLst.Add(InttoStr(cboBaudRate.ItemIndex));

    strLst.SaveToFile(ExtractFilePath(Application.ExeName)+'config.dat');


    strLst.Free;


end;

{-----------------------------------------------------------------------------}
procedure TForm1.SetGrid();    //设置表格格式
var
    i:Integer;
begin
    for i:= 1 to 16 do
       DataGrid.Cells[i,0] := InttoHex(I-1,2);

    DataGrid.ColWidths[0] := 45;  //列:固定单元格宽度
end;

procedure MakePacketList();
var
    CodeMem :PByte;
    sByte :Byte;
    I,sLen : DWORD;
    str :String;
begin
    GetMem(CodeMem,CodeSize + 10);
    CopyMemory(CodeMem,sBinBuffer,CodeSize);
try
    PacketList.Clear;
    I := 0;
    str := '';
    while (I < CodeSize) do begin
        sByte := CodeMem^;

        if (I mod 128 = 0) then begin
            if Str<>'' then PacketList.Add(Str);    //128个字节为一个包组
            Str := '';

        end;

        Form1.ProgressBar1.Position := Round((I / CodeSize) * 100);

        Str := Str + InttoHex(sByte,2);
        Inc(CodeMem,1);
        Inc(I,1);

    end;
    {-------------------------------------------}
    sLen := $80 - Length(Str) div 2;
    //showmessage(inttostr(sLen));
    for I := 1 to sLen do Str := Str + 'FF';    //补齐到80H个字节

    PacketList.Add(Str);
    
    {-------------------------------------------} //添加冗余数据包
    str := '';
    for I:= 1 to 128 do str := str + 'FF';
    PacketList.Add(Str);
    {-------------------------------------------}
    //是否创建记录
    //packetList.SaveToFile(ExtractFilePath(Application.ExeName)+'Binlog.txt');
    Form1.ProgressBar1.Position := 0;
finally
end;
end;

{-----------------------------------------------------------------------------}
procedure TForm1.ShowCodeMemGrid();  //显示存储区表格
var
    CodeMem    :PByte;  //代码区内容
    sByte      :Byte;
    I,sCol,sRow:DWORD;
begin

    //拷贝内核代码区
    GetMem(CodeMem,CodeSize+1);
    CopyMemory(CodeMem,sBinBuffer,CodeSize);

try
    I:=0; sCol:= 0; sRow:= 0;
    while(I < CodeSize) do begin

        sByte  := CodeMem^;   //从代码存储区取一个字节

        //在IRamGrid显示代码区内存
            if (sCol mod 17 = 0) then begin
                DataGrid.Cells[0,sRow+1] := ' '+inttoHex(sRow*$10,4);
                sCol:= 1;
                sRow := sRow+1;
            end;

            DataGrid.Cells[sCol,sRow] := InttoHex(sByte,2);
            sCol := sCol+1;

        ProgressBar1.Position := Round((I / CodeSize) * 100);
        Inc(CodeMem,1);
        Inc(I,1);
    end;

    DataGrid.RowCount := sRow+1;
    ProgressBar1.Position := 0;
finally
    //FreeMem(CodeMem,CodeSize+1);
end;
end;

//------------------------------------------------------------------------------
//装入BIN文件,内存映像,可直接执行
procedure TForm1.LoadBinFile(fileName :String);
var
    sFileName:PChar;    //BIN文件名
    sBinStream  :TMemoryStream;
begin

    if Trim(FileName) = '' then exit;
    sFileName :=pchar(FileName);

    sBinStream := TMemoryStream.Create;
try

    //从sFileName读Bin文件到sBinBuffer
    sBinStream.LoadFromFile(sFileName);
    CodeSize := sBinStream.Size;    //取文件长度

    Caption := FileName+'  [共'+IntToStr(CodeSize)+'字节]';

    //初始化缓冲区
    GetMem(sBinBuffer,CodeSize+1);
    ZeroMemory(sBinBuffer,CodeSize+1);

    CopyMemory(sBinBuffer,PByte(sBinStream.Memory),CodeSize);

    //在Grid内显示
    ShowCodeMemGrid();

finally
    SBinStream.Free;

end;
end;


//------------------------------------------------------------------------------
//装入Hex文件,先将Hex文件解码成Bin文件(Hex文件必须是Intel格式的)
procedure TForm1.LoadHexFile(fileName :String);
var
    i,N,Count  :Integer;
    sByte  :Byte;
    sFileName:PChar;    //Hex文件名
    sBinTemp :PByte;
begin

    if Trim(FileName) = '' then exit;
    sFileName :=pchar(FileName);

    CodeSize := 65535;  //最大容量64K
    GetMem(sBinTemp,CodeSize+1);
    ZeroMemory(sBinTemp,CodeSize+1);

try

    DecodeHex(sFileName,sBinTemp);  //解码Hex文件到缓冲区

    {取得实际HexToBin后的文件大小}
    {----------------------------}
    inc(sBinTemp,CodeSize);         //反相查找,直到不等于0,退出
    I := 0;
    Count := CodeSize;
    while I < Count do begin
      sByte := sBinTemp^;
      if (sByte <> $00) then begin
        N := Count - I;
        I := Count - 1;
      end;
      Dec(sBinTemp);
      inc(i);
    end;
    Dec(sBinTemp,N-1);
    {----------------------------}
    CodeSize := N+1;
    Caption := FileName+'  [共'+IntToStr(CodeSize)+'字节]';
    
    GetMem(sBinBuffer,CodeSize+1);  //拷贝解码后的Bin文件到缓冲区
    ZeroMemory(sBinBuffer,CodeSize+1);
    CopyMemory(sBinBuffer,sBinTemp,CodeSize+1);

    //在Grid内显示
    ShowCodeMemGrid();


finally
    freemem(sBinTemp,65536);  //最大容量64K
end;

end;

{-------------------------------------------------------------------------------
  功能: 以16进制方式发送字符串
  参数: Str:目标字符串
  返回: 无
  说明: 该方法会调串口发送函数将转换后的16进制数据发送到串口
-------------------------------------------------------------------------------}
procedure TForm1.SendHexPacket(str: String);
var
	I,tlen : Integer;
	buffer : PChar;
begin
    LogList.Add(str);

  //StatusLight(0,True);  //状态灯

  I := 1;
	tLen := Length(str);
	GetMem(buffer,tlen div 2+1);
	while I < tLen do begin

    Buffer^ := Char(HexToInt(copy(str,i,2)));
    inc(Buffer);
    inc(i,2);
	end;
  dec(buffer,tLen div 2);
  {------------------------------------------}

  ComPort1.WriteCommData(buffer,tLen div 2);

  //Debug('>Send: ' + Str);


  freemem(buffer,tlen div 2+1);
end;
//------------------------------------------------------------------------------
procedure TForm1.FormCreate(Sender: TObject);
begin
    SetGrid();
    LoadConfig();


    TimeOut_Count := 0;
    PacketList := TStringList.Create; //创建数据包列表
    LogList := TStringlist.Create;  //创建发送日志

    //Memo,载入帮助文件
    //Memo1.Lines.LoadFromFile(ExtractFilePath(Application.ExeName)+HELP_DOCUMENT);
    sndPlaySound(SND_START, SND_NODEFAULT Or SND_ASYNC);  //播放提示声音-完成

end;

//------------------------------------------------------------------------------
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
    SaveConfig();
    ComPort1.StopComm;
end;
//------------------------------------------------------------------------------

procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
//载入Bin文件
    if OpenDialog1.Execute then begin
      if Trim(OpenDialog1.FileName) = '' then exit;
      if OpenDialog1.FilterIndex = 1 then   //Bin格式文件直接载入
        LoadBinFile(OpenDialog1.FileName)
      else
      if OpenDialog1.FilterIndex = 2 then   //Hex格式文件转换后载入
        LoadHexFile(OpenDialog1.FileName)
      else begin
        ShowMessage('请选择Bin格式或Hex格式文件!');
        exit;

⌨️ 快捷键说明

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