串口1

   int nPortNumber;
        int nBuardRate = 115200;
        Parity eParity = NoParity;
        unsigned char DataBits = 8;
        StopBits eStopBits = OneStopBit;//0

        //参数设定部分
     //设定端口
     nPortNumber = m_Port.GetCurSel() + 1;

        //设定波特率
     if(m_Bote.GetCurSel()==0)
     nBuardRate = 300;
     else if(m_Bote.GetCurSel()==1)
     nBuardRate = 600;
        else if(m_Bote.GetCurSel()==2)
     nBuardRate = 1200;
     else if(m_Bote.GetCurSel()==3)
     nBuardRate = 2400;
        else if(m_Bote.GetCurSel()==4)
         nBuardRate = 4800;
        else if(m_Bote.GetCurSel()==5)
     nBuardRate = 9600;
     else if(m_Bote.GetCurSel()==6)
     nBuardRate = 19200;
        else if(m_Bote.GetCurSel()==7)
     nBuardRate = 38400;
        else if(m_Bote.GetCurSel()==8)
     nBuardRate = 43000;
     else if(m_Bote.GetCurSel()==9)
         nBuardRate = 56000;
        else if(m_Bote.GetCurSel()==10)
     nBuardRate = 57600;
    else nBuardRate = 115200;

        //设定奇偶校验
     if(m_Adjust.GetCurSel()==0)
     eParity = NoParity;//2
     else if(m_Adjust.GetCurSel()==1)
            eParity = OddParity;//3
        else 
            eParity = EvenParity;//0

        //设定数据位
     if(m_Data.GetCurSel()==0)
     DataBits = 4;
     else if(m_Data.GetCurSel()==1)
     DataBits = 5;
        else if(m_Data.GetCurSel()==2)
     DataBits = 6;
     else if(m_Data.GetCurSel()==3)
     DataBits = 7;
        else if(m_Data.GetCurSel()==4)
         DataBits = 8;

     //设定停止位
     if(m_Stop.GetCurSel()==0)
     eStopBits = OneStopBit;//0
     else if(m_Stop.GetCurSel()==1)
     eStopBits = TwoStopBits;//1

void CFontUpdate::OnBnClickedButtonUpdateFontLib()
{
 // TODO: 在此添加控件通知处理程序代码

 if(( bIsComPortOpen == false))
  {
  SetStauesText("串口尚未打开!");
  return;
 }

 if(true == bIsFlashErase)
 {
  SetStauesText("还在擦除Flash...");
  return;
 }

 if(true == bIsFontLibInSending)
 {
  SetStauesText("已经在字库升级中...");
  return;
 }

 if(0 != CheckFilePath())
 {
  SetStauesText("字库文件路径无效!");
  return;
 }

 CFile  FontLibFile;
 if ( !FontLibFile.Open( m_strInputFilePathName, CFile::modeRead) )
 {
  SetStauesText("打开字库文件失败!");
  return;
 }

 m_FontLibFileLen = (unsigned long)(FontLibFile.GetLength());
 if(m_FontLibFileLen == 0)
 {
  SetStauesText("字库文件大小为:0!");
  return;
 }

 /* 假如上次未释放该指针  */
    if(NULL != m_pBuffer)
    {
        delete m_pBuffer;
        m_pBuffer = NULL;
    }
 m_pBuffer = new unsigned char[m_FontLibFileLen];
 if(m_pBuffer == NULL)
 {
  SetStauesText("分配内存失败!");
  return; 
 }

/* 读取 Bin File */
 FontLibFile.Read(m_pBuffer, m_FontLibFileLen); //
 FontLibFile.Close();

    /* 关闭按钮无效 */
    CButton *pBtn = (CButton *)GetDlgItem(IDC_BUTTON_OPEN_COM_PORT);
    if(pBtn != NULL)
    {
        pBtn->EnableWindow(false);
    }

    m_StartUpdFontTime = CTime::GetCurrentTime();

 /* 首次发送 128个, 等待应答之后再发送剩余的 */
 if( m_FontLibFileLen < SEND_DATA_LEN_ONE_TIME)
 {
  m_CurSndPos  = 0; // 发送的位置在m_pBuffer中的偏移
  m_LastSndLen = m_FontLibFileLen;
  SendFontDataToCom(m_CurSPI_Flash_Addr, m_pBuffer + m_CurSndPos, m_LastSndLen);
  bIsFontLibInSending = true;
 }
 else
 {
  m_CurSndPos = 0;
  m_LastSndLen = SEND_DATA_LEN_ONE_TIME;
  SendFontDataToCom(m_CurSPI_Flash_Addr, m_pBuffer + m_CurSndPos, m_LastSndLen);
  bIsFontLibInSending = true;
 }

#if 1
 /******************* progress ****************************** */
    pProgressCtrl = (CProgressCtrl *)GetDlgItem(IDC_PROGRESS_UPDATE);
 if(NULL != pProgressCtrl)
 {
  pProgressCtrl->SetPos(0);                           /* 设置位置 */
  pProgressCtrl->SetStep(1);
 }

    unsigned long int Range = m_FontLibFileLen / (SEND_DATA_LEN_ONE_TIME); /* 文件长度 */
    if (Range == 0)    // 说明只需一步即可达到100%
    {
  Range = 1;
    }
    else if (Range > 0 && (m_FontLibFileLen % (SEND_DATA_LEN_ONE_TIME)) != 0) // 说明不是整除, 需要加上 1 (这是为什么呢? 因为如果一个数字是9.5 , 而每当3时进度条进1, 那么需要4次能把进度条推到100%, 而9.5/3 = 3, 所以给它加1变为4, 然后设置进度条范围为0到4)
    {
  Range += 1;
    }
    else // 刚好整除, 则步数刚好为整除结果
    {
    }
 pProgressCtrl->SetRange(0, Range);     /* 设置范围 */
 pProgressCtrl->ShowWindow(SW_SHOW);
 /******************* progress ****************************** */
#endif

// SetTimer(RESEND_TIMER, 100, NULL); /* 设置定时器 超时重发 10 * 100ms */


    CTime StartTime = CTime::GetCurrentTime();
    CTime EndTime   = CTime::GetCurrentTime();
    int AckCmdLen = 1;

    unsigned char AckCmdBuf[256] = {0};
    while( bIsFontLibInSending )   /* 字库升级中 */
    {

#if 1
  if (!DoEvents())
  {
   break;
        }
  Sleep(1);
#endif

 AckCmdLen = GetAckCmdBuf(AckCmdBuf, 256);

        /* 一直接收不到单片机回复,超时10秒退出 */
        if(AckCmdLen != 0)
        {
            ParaseFontUpdateAck(AckCmdBuf, AckCmdLen);
            StartTime = CTime::GetCurrentTime();
        }
        else
        {
            EndTime   = CTime::GetCurrentTime();
            CTimeSpan timeSpan = EndTime - StartTime;     // 两个CTime相减得到CTimeSpan
            LONGLONG nTSeconds = timeSpan.GetTotalSeconds();                // 得到总的秒数
            if( nTSeconds > ACK_TIME_OUT )
            {
//                KillTimer(RESEND_TIMER);
                bIsFontLibInSending = false;
                m_CurSndPos     = 0;
                m_LastSndLen    = 0;
                m_CurSPI_Flash_Addr = 0;
                SetStauesText("字库升级超时失败!");
            }
        }
    }

    // CDialog::OnOK();
}

void CFontUpdate::OnTimer(UINT nIDEvent)
{
 // TODO: Add your message handler code here and/or call default
 char Tmp123[128];
 static int ReSendTime = 0;

    switch(nIDEvent)
    {
        case RESEND_TIMER:
        {
            if(bIsFontLibInSending == false || ReSendTime >= 10)
            {
                bIsFontLibInSending = false;
                m_CurSndPos = 0;
                m_LastSndLen = 0;
                m_CurSPI_Flash_Addr = 0;
                m_LastSndLen = 0;
                sprintf(Tmp123, "重发超过:%d Addr:%x, Len:%d 退出!",\
                                ReSendTime, m_CurSPI_Flash_Addr, m_LastSndLen);
                SetStauesText(Tmp123);

                if(NULL != m_pBuffer)
                {
                    delete m_pBuffer;
                    m_pBuffer = NULL;
                }

                /* 关闭按钮无效 */
                CButton *pBtn = (CButton *)GetDlgItem(IDC_BUTTON_OPEN_COM_PORT);
                if(pBtn != NULL)
    

 {
                    pBtn->EnableWindow(true);
                }

//                KillTimer(RESEND_TIMER);
                return;
            }

            if((m_TimeOut++) >= 25)
            {
                sprintf(Tmp123, "超时重发:%d Addr:%x, Len:%d", ReSendTime++, m_CurSPI_Flash_Addr, m_LastSndLen);
                SetStauesText(Tmp123);
                SendFontDataToCom(m_CurSPI_Flash_Addr, m_pBuffer + m_CurSndPos, m_LastSndLen);
                m_TimeOut = 0;
            }
        }
  break;
    }

 CDialog::OnTimer(nIDEvent);
}

void CFontUpdate::OnBnClickedButtonEraseFlash()
{
 // TODO: 在此添加控件通知处理程序代码

    if(( bIsComPortOpen == false))
  {
  SetStauesText("串口尚未打开!");
  return;
 }

    /* 关闭按钮无效 */
    CButton *pBtn = (CButton *)GetDlgItem(IDC_BUTTON_OPEN_COM_PORT);
    if(pBtn != NULL)
    {
        pBtn->EnableWindow(false);
    }

    SendEraseFlashToCom(FONT_BASE_ADDR_IN_SPI_FLASH, 0);

    CTime StartTime = CTime::GetCurrentTime();
    CTime EndTime   = CTime::GetCurrentTime();
    int AckCmdLen = 1;

    unsigned char AckCmdBuf[256] = {0};
    while(bIsFlashErase)   /* 擦除Flash */
    {
#if 1
        if (!DoEvents())
        {
            break;
        }
        Sleep(1);
#endif
       
        AckCmdLen = GetAckCmdBuf(AckCmdBuf, 256);

        /* 一直接收不到单片机回复,超时10秒退出 */
        if(AckCmdLen != 0)
        {
            ParaseEraseFlashAck(AckCmdBuf, AckCmdLen);
            StartTime = CTime::GetCurrentTime();
        }
        else
        {
             EndTime   = CTime::GetCurrentTime();
            CTimeSpan timeSpan = EndTime - StartTime;     // 两个CTime相减得到CTimeSpan
            LONGLONG nTSeconds = timeSpan.GetTotalSeconds();                // 得到总的秒数
            if( nTSeconds > ERASE_FLASH_ACK_TIME_OUT)
            {
                bIsFlashErase = false;
                SetStauesText("擦除Flash超时失败!");
            }
        }
    }
}

原文地址:https://www.cnblogs.com/yuanchao/p/13384430.html