zoukankan      html  css  js  c++  java
  • 串口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;
            }
        }

  • 相关阅读:
    MySQL基础语句【学习笔记】
    减一技术应用:生成排列与幂集
    Java实现动态规划法求解0/1背包问题
    LODOP中ADD_PRINT_TABLE、HTM、HTML表格自动分页测试
    LODOP设置判断后执行哪个
    Lodop删除语句Deleted只能内嵌设计维护可用
    Lodop、c-lodop注册与角色简短问答
    LODOP暂存、应用、复原 按钮的区别
    JS判断语句 注意多句时加大括号 回调函数LODOP兼顾写法
    LODOP、C-Lodop简短排查语句
  • 原文地址:https://www.cnblogs.com/yuanchao/p/13384465.html
Copyright © 2011-2022 走看看