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

📄 unit1.pas

📁 STC-Download/STC-ISP下载工具(STC89C51单片机在线下载器) 必须组件: SPComm(必须), VCLSkin(可以去掉) -----------------------
💻 PAS
📖 第 1 页 / 共 2 页
字号:
      end;
      MakePacketList();   //生成数据包列表
    end;
end;

{-------------------------------------------------------------------------------
//定时器定时发送连机探测信号
{-------------------------------------------------------------------------------}
procedure TForm1.SeekTimerTimer(Sender: TObject);
begin

    //if SeekDevice then  //是否发送连机信号
        SendHexPacket(PAK_SEEK);

end;
{-------------------------------------------------------------------------------
  功能: 接收数据包
  参数:
  返回: 从串口接收外设发送的数据包
-------------------------------------------------------------------------------}
procedure TForm1.ComPort1ReceiveData(Sender: TObject; Buffer: Pointer;
  BufferLength: Word);
var
 tBuf : PByte;
 //str,            //格式字符串
 strNum,         //数据包字符串
 strData :String;
 I    : Integer;
 //tHeader :String;   //包头
begin
  //StatusLight(1,false);         //关灯


  I := 0;
  tBuf := PByte(Buffer);
  While I < BufferLength do begin

    strNum := InttoHex(Byte(tBuf^),2);      //字符数据
    strData := strData + strNum;            //组合字符串型数据包
    //Str := Str + strNum + ' ';
    Inc(I);
    Inc(tBuf);

  end;
  {//---------------------------------------------------------------!--}
  //Debug(']Recv: '+StrData);

  if Copy(StrData,length(StrData)-1,2) <>'16' then exit;
  ST_ChipVer := Copy(StrData,length(StrData)-15,2);
  SeekDevice := false;  //发现设备
  BlockSend := false;   //继续发送
{
  if Copy(StrData,1,2) = '68' then begin

    ST_ChipVer := Copy(StrData,length(StrData)-15,2);
    Debug('版本:'+ST_ChipVer);
    SeekDevice := false; //发现设备

  end else
  if Copy(StrData,1,6) = PAK_RCV_OK then begin

    BlockSend := false; //继续发送
  end;
}
end;


{-------------------------------------------------------------------------------}
procedure TForm1.SendBinSize();
begin
end;

{-------------------------------------------------------------------------------}
procedure TForm1.SendBinFile();
begin

end;

{-------------------------------------------------------------------------------}
procedure TForm1.TimeOutRoutine();
begin

end;

procedure TForm1.SpeedButton2Click(Sender: TObject);
var
    sAddress : Integer;
    sPacket : String;
    N,I,sCount : integer;
begin
    ComPort1.StartComm; //打开串口
    ComPort1.BaudRate := 1200;  //初始波特率1200
    Memo2.Clear;
    TimeOut_Count := 0;
    logList.Clear;
    logList.Add('==========================================================');
    logList.Add('STC-Download');
    loglist.Add('作者:吴江涛');
    logList.Add('创建日期:'+DateTimeToStr(now()));
    logList.Add('==========================================================');
    sleep(100);


    {查寻芯片}
    Debug('1.正在连机...');
    Debug('['+TimeToStr(now())+']');
    {~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}
    SeekDevice := True;       //通知定时器发送7F查找设备
    SpeedButton2.Enabled := false;
    SeekTimer.Enabled := True;
    while SeekDevice do begin
        //等待查找成功
        Application.ProcessMessages;
        {****************************************}
        if TimeOutTm.Enabled = false  then TimeOutTm.Enabled := True;
        if TimeOut_Count >= 3 then begin
            TimeOut_Count := 0;
            ProgressBar1.Position := 0;
            SeekDevice := false;
            SpeedButton2.Enabled := True;
            SeekTimer.Enabled := false;
            TimeOutTm.Enabled := false;
            TimeOutCount := 0;
            ComPort1.StopComm;
            Debug('0.连接超时!');
            //showmessage('0.连接超时!');
            exit;
        end;
        {****************************************}

    end;
    SeekTimer.Enabled := false;
    TimeOutTm.Enabled := false;
    TimeOutCount := 0;
    LogList.Add('找到设备! 芯片版本:'+Copy(ST_ChipVer,1,1)+'.'+Copy(ST_ChipVer,2,1));
    Debug('2.连机成功! 固件版本:'+Copy(ST_ChipVer,1,1)+'.'+Copy(ST_ChipVer,2,1));
    Debug('['+TimeToStr(now())+']');


    {调整波特率1}
    Debug('3.正在以更高的波特率连接...');
    Debug('['+TimeToStr(now())+']');
    {~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}
    BlockSend := True;
    {46H,0B9H,6AH,00H,0CH,8FH,0FFH,0F6H,00H,14H,17H,81H,0A6H,16H}
    //SendHexPacket('46B96A000C8FFEE0001417818F16');  //第一次调整波特率1200
    //SendHexPacket('46B96A000C8FFFF700121781A516');  //第一次调整波特率34800
    SendHexPacket('46B96A000C8FFFFA00121781A816');    //第一次调整波特率57600
    SLEEP(100);
    ComPort1.StopComm;
    //ComPort1.BaudRate := 38400;
    ComPort1.BaudRate := 57600;
    ComPort1.StartComm;

    while BlockSend do begin
        //等待设备响应

        Application.ProcessMessages;
        {****************************************}
        if TimeOutTm.Enabled = false  then TimeOutTm.Enabled := True;
        if TimeOut_Count >= 6 then begin
            TimeOut_Count := 0;
            ProgressBar1.Position := 0;
            SeekDevice := false;
            SpeedButton2.Enabled := True;
            TimeOutTm.Enabled := false;
            TimeOutCount := 0;
            ComPort1.StopComm;
            showmessage('写芯片超时!');
            exit;
        end;
        {****************************************}

    end;
    TimeOutTm.Enabled := false;
    TimeOutCount := 0;
    LogList.Add('调整波特率1完成!');

    {调整波特率1}
    {~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}
    BlockSend := True;
                  {46H,0B9H,6AH,00H,0BH,8EH,0FFH,0F6H,00H,14H,17H,23H,16H}
    //SendHexPacket('46B96A000B8EFEE00014170C16');  //第二次调整波特率1200
    SLEEP(100);                         //以38400波特率发送第二次调整命令
    ComPort1.StopComm;
    ComPort1.BaudRate := 1200;
    ComPort1.StartComm;
    SLEEP(100);
    //SendHexPacket('46B96A000B8EFFF70012172216');   //以38400波特率发送第二次调整命令
    SendHexPacket('46B96A000B8EFFFA0012172516');   //以57600波特率发送第二次调整命令

    SLEEP(100);
    ComPort1.StopComm;
    //ComPort1.BaudRate := 38400;        //以调整后的波特率接收
    ComPort1.BaudRate := 57600;
    ComPort1.StartComm;

    while BlockSend do begin
        //等待设备响应

        Application.ProcessMessages;
        {****************************************}
        if TimeOutTm.Enabled = false  then TimeOutTm.Enabled := True;
        if TimeOut_Count >= 6 then begin
            TimeOut_Count := 0;
            ProgressBar1.Position := 0;
            SeekDevice := false;
            SpeedButton2.Enabled := True;
            TimeOutTm.Enabled := false;
            TimeOutCount := 0;
            ComPort1.StopComm;
            showmessage('写芯片超时!');
            exit;
        end;
        {****************************************}

    end;
    TimeOutTm.Enabled := false;
    TimeOutCount := 0;
    LogList.Add('调整波特率2完成!');


    Debug('4.正在发送控制信息...');
    Debug('['+TimeToStr(now())+']');
    {发送帧长度信息,循环发5次}
    {~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}
    for N := 1 to 5 do begin
    BlockSend := True;
                  {46 B9 6A 00 06 80 F0 16}
    SendHexPacket('46B96A000680F016');  //发送帧长度信息

    while BlockSend do begin
        //等待设备响应

        Application.ProcessMessages;
        {****************************************}
        if TimeOutTm.Enabled = false  then TimeOutTm.Enabled := True;
        if TimeOut_Count >= 6 then begin
            TimeOut_Count := 0;
            ProgressBar1.Position := 0;
            SeekDevice := false;
            SpeedButton2.Enabled := True;
            TimeOutTm.Enabled := false;
            TimeOutCount := 0;
            ComPort1.StopComm;
            showmessage('写芯片超时!');
            exit;
        end;
        {****************************************}

    end;
    end;
    TimeOutTm.Enabled := false;
    TimeOutCount := 0;
    LogList.Add('帧长度信息,循环5次完成');


    {擦除MCU程序区}
    Debug('5.正在擦除用户程序区...');
    Debug('['+TimeToStr(now())+']');
   {~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}
    BlockSend := True;
                  {46 B9 6A 00 07 84 21 16 16}
    SendHexPacket('46B96A0007847A6F16'); //擦除61K程序区 512*7A


    while BlockSend do begin
        //等待设备响应

        Application.ProcessMessages;
        {****************************************}
        if TimeOutTm.Enabled = false  then TimeOutTm.Enabled := True;
        if TimeOut_Count >= 10 then begin
            TimeOut_Count := 0;
            ProgressBar1.Position := 0;
            SeekDevice := false;
            SpeedButton2.Enabled := True;
            TimeOutTm.Enabled := false;
            TimeOutCount := 0;
            ComPort1.StopComm;
            showmessage('写芯片超时!');
            exit;
        end;
        {****************************************}

    end;
    TimeOutTm.Enabled := false;
    TimeOutCount := 0;
    LogList.Add('擦除用户程序区完成!');

    {开始写数据}
    {~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}

    //LogList.SaveToFile(ExtractFilePath(Application.ExeName)+'SendLog.txt');

    I := 0;
    sCount := 0;
    sAddress := 0;
    BlockSend := false;
    {发送Bin文件内容}
    Debug('6.正在下载数据!');
    Debug('下载的时间与文件大小相关,请稍侯... ...');
    Debug('['+TimeToStr(now())+']');
    {-------------------------------}
    sCount := PacketList.Count - 1;
    while I <= sCount do begin


        while BlockSend do begin
            if TimeOutTm.Enabled = false  then TimeOutTm.Enabled := True;
            if TimeOut_Count >= 10 then begin
                TimeOut_Count := 0;
                ProgressBar1.Position := 0;
                showmessage('写数据超时!');
                SpeedButton2.Enabled := True;
                TimeOutTm.Enabled := false;
                TimeOutCount := 0;
               exit;
            end;
        Application.ProcessMessages;
        end;
        TimeOutTm.Enabled := false;
        TimeOut_Count := 0;
        
        {46 B9 6A 00 8C 00 00 00}
        sPacket := '46B96A008C000000' + InttoHex(sAddress,4) + '0080' + PacketList.Strings[I];
        sPacket := sPacket + GetByteSum(sPacket) + '16';
        SendHexPacket(sPacket);

        Label6.Caption := '[ ' + InttoHex(I,4) + '/' + InttoHex(sCount,4) + ' ]';
        LogList.Add('----------------' + Label6.Caption + '----------------');

        BlockSend := True;  //阻塞

        inc(I);
        sAddress := sAddress + $80; //地址递增
        ProgressBar1.Position := Round((I / sCount) * 100);


    end;

    {发送完毕数据包}
    {-------------------------------}
    Debug('OK! 文件下载完毕! ');
    Sleep(100);
    Debug('['+TimeToStr(now())+']');
    LogList.Add('----------------' + '数据发送完毕' + '----------------');

    //是否创建日志
    //LogList.SaveToFile(ExtractFilePath(Application.ExeName)+'SendLog.txt');
    {-------------------------------}
    ProgressBar1.Position := 0;
    sndPlaySound(SND_GOODBYE, SND_NODEFAULT Or SND_ASYNC);  //播放提示声音-完成
    //showmessage('恭喜!下载完毕!'+TimeToStr(now()));
    ComPort1.BaudRate := 1200;
    SpeedButton2.Enabled := True;
    ComPort1.StopComm; //关闭串口


end;

procedure TForm1.cboPortChange(Sender: TObject);
begin
  //设置端口名
  //ComPort1.StopComm;
  ComPort1.CommName := cboPort.Text;
  //Caption := '手持维修仪-文件更新程序[Ver:2.2 Build:0603/Raya] [' + ComPort1.CommName+',' +  InttoStr(ComPort1.BaudRate)+']';
  //ComPort1.StartComm;

end;

procedure TForm1.cboBaudRateChange(Sender: TObject);
begin
  //设置波特率
  //ComPort1.StopComm;
  ComPort1.BaudRate := StrtoInt(cboBaudRate.Text);
  //Caption := '手持维修仪-文件更新程序[Ver:2.2 Build:0603/Raya] [' + ComPort1.CommName+',' +  InttoStr(ComPort1.BaudRate)+']';

  //ComPort1.StartComm;

end;


procedure TForm1.TimeOutTMTimer(Sender: TObject);
begin
    Inc(TimeOut_Count,1);   //超时计数器加1
end;

procedure TForm1.FormShow(Sender: TObject);
begin
    sndPlaySound(SND_START, SND_NODEFAULT Or SND_ASYNC);  //播放提示声音-完成

end;

procedure TForm1.Button2Click(Sender: TObject);
begin
{
//载入Hex文件
    OpenDialog1.FilterIndex := 2;
    if OpenDialog1.Execute then
        LoadHexFile(OpenDialog1.FileName);

    MakePacketList();   //生成数据包列表
}
end;

procedure TForm1.SpeedButton3Click(Sender: TObject);
begin
  ReloadTimer.Enabled := not ReloadTimer.Enabled;

  if SpeedButton3.Caption = '&R.重复下载' then
    SpeedButton3.Caption := '&S.停止重复'
  else
    SpeedButton3.Caption := '&R.重复下载';
end;

procedure TForm1.ReloadTimerTimer(Sender: TObject);
begin
  //Memo2.Lines.Add('重复下载..');
  if SpeedButton2.Enabled then SpeedButton2.Click;
end;

end.

⌨️ 快捷键说明

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