串口2

BOOL CFontUpdate::OnInitDialog()
{
 CDialog::OnInitDialog();

 // TODO:  在此添加额外的初始化
 FontUpdateInitial();
 return TRUE;  // return TRUE unless you set the focus to a control
 // 异常: OCX 属性页应返回 FALSE
}

int CFontUpdate::GetAckCmdBuf(unsigned char *PAckCmdBuf, int Len)
{
    LONG len, Idx;
 static BYTE RxDataBuf[1024] = {0}; // 设置BYTE数组 An 8-bit integer that is not signed.
 static LONG TotalRcvLen     = 0; // 保存当前缓存的个数
 DWORD AckCmdLen             = 0; // 单片机返回的应答个数

 /* 以下你可以根据自己的通信协议加入处理代码 */
    len = pCtrlComm->Read(RxDataBuf + TotalRcvLen, 1024 - TotalRcvLen);
 if(len > 0)
 {
        TotalRcvLen += len; // 长度累加

 }

 for( Idx = 0; Idx < TotalRcvLen; Idx++ )
    {
        if(   ('O' == RxDataBuf[Idx])
            &&('O' == RxDataBuf[Idx+1])
            &&('#' == RxDataBuf[Idx+2]))
        {
            AckCmdLen = Idx + 3;
            TotalRcvLen -= AckCmdLen;    /* 数据向前移动 */
            BYTE *pTmpBuf = RxDataBuf + AckCmdLen;
            if( Len >= AckCmdLen )
            {
                memcpy(PAckCmdBuf, RxDataBuf, AckCmdLen);
            }
            else
            {
                SetStauesText("超出缓存限制!");
                AckCmdLen = 0;
            }
   
            for(LONG TmpIdx = 0; TmpIdx < TotalRcvLen; TmpIdx++)
            {
                RxDataBuf[TmpIdx] = *(pTmpBuf + TmpIdx);
            }
            break;
        }
    }

    return AckCmdLen;
}

void CFontUpdate::ParaseEraseFlashAck(unsigned char *PAckCmdBuf, int Len)
{
    char TmpStatues[128];
    if(NULL != strstr((char *)PAckCmdBuf, CMD_CLEAR_SPI_FLASH))
    {
        bIsFlashErase = false;
        SetStauesText("擦除Flash成功");

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

void CFontUpdate::ParaseFontUpdateAck(unsigned char *PAckCmdBuf, int Len)
{
    char TmpStatues[128];

    if(NULL != strstr((char *)PAckCmdBuf, CMD_WRITE_SPI_FLASH))
    {
        DWORD AckComWriteSPI_Flash_Addr = 0;
        DWORD AckComWriteSPI_Flash_Len  = 0;

        /* 写入SPI Flash的地址 */
        AckComWriteSPI_Flash_Addr |= (*(PAckCmdBuf + 3) << 24);
        AckComWriteSPI_Flash_Addr |= (*(PAckCmdBuf + 4) << 16);
        AckComWriteSPI_Flash_Addr |= (*(PAckCmdBuf + 5) << 8 );
        AckComWriteSPI_Flash_Addr |= (*(PAckCmdBuf + 6) << 0 );

        AckComWriteSPI_Flash_Len  |= (*(PAckCmdBuf + 7) << 24);
        AckComWriteSPI_Flash_Len  |= (*(PAckCmdBuf + 8) << 16);
        AckComWriteSPI_Flash_Len  |= (*(PAckCmdBuf + 9) << 8 );
        AckComWriteSPI_Flash_Len  |= (*(PAckCmdBuf +10) << 0 );

        if(    (AckComWriteSPI_Flash_Len  == m_LastSndLen)
            && (AckComWriteSPI_Flash_Addr == m_CurSPI_Flash_Addr))
        {
           

 m_CurSndPos         += m_LastSndLen;    /* 已经发送的长度累增 */
            m_CurSPI_Flash_Addr += m_LastSndLen;    /* 当前写文件地址累增 */
            if(m_CurSndPos < m_FontLibFileLen)
            {
                /* 发送剩余的 */
                if( m_FontLibFileLen - m_CurSndPos <= SEND_DATA_LEN_ONE_TIME)
                {
                    m_LastSndLen = m_FontLibFileLen - m_CurSndPos;
                    SendFontDataToCom(m_CurSPI_Flash_Addr, m_pBuffer + m_CurSndPos, m_LastSndLen);
                    bIsFontLibInSending = true;
                }
                else
                {
                    m_LastSndLen = SEND_DATA_LEN_ONE_TIME;
                    SendFontDataToCom(m_CurSPI_Flash_Addr, m_pBuffer + m_CurSndPos, m_LastSndLen);
                    bIsFontLibInSending = true;
                }
                m_TimeOut = 0;

                sprintf(TmpStatues, "写入进度: %d / %d", m_CurSndPos, m_FontLibFileLen);
                SetStauesText(TmpStatues);
            }

 else if(m_CurSndPos == m_FontLibFileLen)    /* 写完成 */
            {
//                KillTimer(RESEND_TIMER);
                bIsFontLibInSending = false;
                m_CurSndPos  = 0;
                m_LastSndLen = 0;
                m_CurSPI_Flash_Addr = 0;
                m_EndUpdFontTime = CTime::GetCurrentTime();

                CTimeSpan timeSpan = m_EndUpdFontTime - m_StartUpdFontTime;     // 两个CTime相减得到CTimeSpan
                LONGLONG nTSeconds = timeSpan.GetTotalSeconds();                // 得到总的秒数
                sprintf(TmpStatues, "字库升级成功,共耗时: %d 秒", nTSeconds);
                SetStauesText(TmpStatues);
                if(NULL != m_pBuffer)
                {
                    delete m_pBuffer;
                    m_pBuffer = NULL;
                }

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

  if(pBtn != NULL)
                {
                    pBtn->EnableWindow(true);
                }
            }

            //CProgressCtrl *pProgressCtrl = (CProgressCtrl *)GetDlgItem(IDC_PROGRESS_UPDATE);
            if(NULL != pProgressCtrl)
            {
                pProgressCtrl->StepIt();
            }
        }

 else    /* 写Flash 出错 !*/
        {
            sprintf(TmpStatues, "MCU写地址出错:AckAddr:%x, AckLen:%d, Addr:%x, Len:%d Pos:%d",\
                            AckComWriteSPI_Flash_Addr, AckComWriteSPI_Flash_Len, \
                            m_CurSPI_Flash_Addr, m_LastSndLen, m_CurSndPos);
            SetStauesText(TmpStatues);

            SendFontDataToCom(m_CurSPI_Flash_Addr, m_pBuffer + m_CurSndPos, m_LastSndLen);
            bIsFontLibInSending = true;
        }
    }

 

你可能感兴趣的:(串口2)