private void ExplainReadVerPacket(byte[] RecBuf)
        {
            string strVer = "";

            if (UpdateType == enUpdateType.地锁系统)
            {
                Protocol6009Struct rxStruct = Extract6009RxBuffer(RecBuf);
                if (false == rxStruct.isSuccess || rxStruct.CommandIDByte != 0x40)
                {
                    return;
                }
                strVer  = "软件版本" + ": " + rxStruct.DataByte[0].ToString("X2") + "." + rxStruct.DataByte[1].ToString("X2") + "\n";
                strVer += "硬件版本" + ": " + rxStruct.DataByte[2].ToString("X2") + "." + rxStruct.DataByte[3].ToString("X2") + "\n";
                strVer += "协议版本" + ": " + rxStruct.DataByte[4].ToString("X2") + "." + rxStruct.DataByte[5].ToString("X2");
            }
            else if (UpdateType == enUpdateType.无线模块系统)
            {
                Protocol6009Struct rxStruct = Extract6009RxBuffer(RecBuf);
                if (false == rxStruct.isSuccess || rxStruct.CommandIDByte != 0x40)
                {
                    return;
                }
                strVer  = "软件版本" + ": " + rxStruct.DataByte[0].ToString("X2") + "." + rxStruct.DataByte[1].ToString("X2") + "\n";
                strVer += "硬件版本" + ": " + rxStruct.DataByte[2].ToString("X2") + "." + rxStruct.DataByte[3].ToString("X2") + "\n";
                strVer += "协议版本" + ": " + rxStruct.DataByte[4].ToString("X2") + "." + rxStruct.DataByte[5].ToString("X2");
            }
            timerUpdate.Stop();
            UpdateStep = enUpdateProc.Update_End;
            MessageBox.Show(strVer, "程序版本", MessageBoxButtons.OK, MessageBoxIcon.Information);
            btRdVersionInfo.Enabled = true;
            btStartUpdate.Enabled   = true;
            gpSelectType.Enabled    = true;
            return;
        }
 private void btRdVersionInfo_Click(object sender, EventArgs e)
 {
     if (FrmMain.strComPort == "")
     {
         MessageBox.Show("请先打开通讯端口!", "串口错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
         return;
     }
     UpdateStep              = enUpdateProc.Read_Version;
     UpdateRetryTimes        = 3;
     btRdVersionInfo.Enabled = false;
     btStartUpdate.Enabled   = false;
     gpSelectType.Enabled    = false;
     UpdateWaitTime          = 1;
     timerUpdate.Start();
 }
        private void TransmitReadVerPacket()
        {
            int iLoop;

            if (UpdateStep != enUpdateProc.Read_Version)
            {
                return;
            }
            if (UpdateRetryTimes == 0)
            {
                timerUpdate.Stop();
                UpdateStep = enUpdateProc.Update_End;
                MessageBox.Show("读版本错误,主板没有应答!", "读版本错误", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                btRdVersionInfo.Enabled = true;
                btStartUpdate.Enabled   = true;
                gpSelectType.Enabled    = true;
                return;
            }
            if (UpdateType == enUpdateType.地锁系统)
            {
                Protocol6009Struct txStruct = new Protocol6009Struct();
                txStruct.CommandIDByte = 0x40;
                // 发送数据
                dataTransmit(_6009CreatTxBuffer(txStruct));
            }
            else if (UpdateType == enUpdateType.无线模块系统)
            {
                Protocol6009Struct txStruct = new Protocol6009Struct();
                txStruct.CommandIDByte = 0x40;
                // 发送数据
                dataTransmit(_6009CreatTxBuffer(txStruct));
            }
            else
            {
                timerUpdate.Stop();
                UpdateStep = enUpdateProc.Update_End;
                btRdVersionInfo.Enabled = true;
                btStartUpdate.Enabled   = true;
                gpSelectType.Enabled    = true;
                MessageBox.Show("请选择一个集中器类型");
                return;
            }
            UpdateWaitTime = 80;
            UpdateRetryTimes--;
        }
        private void UpdateBootDownCount()
        {
            if (UpdateStep != enUpdateProc.Boot_EnterUpdate)
            {
                return;
            }
            if (BootUpdateLastDownCounter == 0)
            {
                timerUpdate.Stop();
                UpdateStep = enUpdateProc.Update_End;
                MessageBox.Show("没有进入到升级模式,请检查连线是否正确!", "未进入升级模式", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                prgBarUpdate.Visible     = false;
                lbFront.Visible          = false;
                lbMiddle.Visible         = false;
                lbRear.Visible           = false;
                btOpenUpdateFile.Enabled = true;
                btRdVersionInfo.Enabled  = true;
                btStartUpdate.Enabled    = true;
                rbtBootUpdate.Enabled    = true;
                rbtAppUpdate.Enabled     = true;
                gpSelectType.Enabled     = true;
                // 恢复原先的波特率
                setUartBaudrate(iLastPortBaudrate);
                btStartUpdate.Text = "开始升级";
                return;
            }
            if (--BootUpdateLastDownCounter % 100 != 0)
            {
                return;
            }
            lbFront.Text  = "请在";
            lbMiddle.Text = (BootUpdateLastDownCounter / 100).ToString("D");
            lbRear.Text   = "秒钟内按下主板的复位按键或重新上电!";
            int x = 147, y = 300;

            lbFront.Location     = new System.Drawing.Point(x, y);
            lbMiddle.Location    = new System.Drawing.Point(x + lbFront.Size.Width, y - 18);
            lbRear.Location      = new System.Drawing.Point(x + lbFront.Size.Width + lbMiddle.Size.Width, y);
            prgBarUpdate.Visible = false;
            lbFront.Visible      = true;
            lbMiddle.Visible     = true;
            lbRear.Visible       = true;
            return;
        }
        private void ExplainAppPacket(byte[] RecBuf)
        {
            UInt16 u16Crc;

            byte[] dataBuf = new byte[100];

            if (UpdateType == enUpdateType.地锁系统)
            {
                Protocol6009Struct rxStruct = Extract6009RxBuffer(RecBuf);
                if (false == rxStruct.isSuccess || rxStruct.CommandIDByte != 0xF1)
                {
                    return;
                }
                Array.Copy(rxStruct.DataByte, dataBuf, rxStruct.DataByte.Length);
            }
            else if (UpdateType == enUpdateType.无线模块系统)
            {
                Protocol6009Struct rxStruct = Extract6009RxBuffer(RecBuf);
                if (false == rxStruct.isSuccess || rxStruct.CommandIDByte != 0xF4)
                {
                    return;
                }
                Array.Copy(rxStruct.DataByte, dataBuf, rxStruct.DataByte.Length);
            }
            else
            {
                return;
            }

            u16Crc = (UInt16)(dataBuf[0] + dataBuf[1] * 256);
            AppUpdateWriteAddress = dataBuf[2] + (dataBuf[3] << 8) +
                                    (dataBuf[4] << 16) + (dataBuf[5] << 24);
            if (u16Crc != updateCrc)
            {
                UpdateWaitTime        = 3;
                AppUpdateWriteAddress = 0;
                return;
            }
            if (dataBuf[6] == 0xAA)
            {
                AppUpdateWriteAddress += AppUpdateCountOnePacket;
                prgBarUpdate.Maximum   = updateLength;
                prgBarUpdate.Value     = (AppUpdateWriteAddress > updateLength) ? updateLength : AppUpdateWriteAddress;
                prgBarUpdate.Visible   = true;
                if (AppUpdateWriteAddress >= updateLength)
                {
                    timerUpdate.Stop();
                    UpdateStep = enUpdateProc.Update_End;
                    if (UpdateType == enUpdateType.地锁系统)
                    {
                        MessageBox.Show("程序升级成功,主板将在10秒钟内重新启动。", "升级成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else if (UpdateType == enUpdateType.无线模块系统)
                    {
                        MessageBox.Show("无线模块升级程序下发成功。", "升级成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    btOpenUpdateFile.Enabled = true;
                    btRdVersionInfo.Enabled  = true;
                    rbtBootUpdate.Enabled    = true;
                    rbtAppUpdate.Enabled     = true;
                    btStartUpdate.Enabled    = true;
                    gpSelectType.Enabled     = true;
                    btStartUpdate.Text       = "开始升级";
                    return;
                }
                UpdateWaitTime   = 1;
                UpdateRetryTimes = 5;
            }
        }
        private void TransmitAppPacket()
        {
            int iLoop, iPos;

            if (UpdateRetryTimes == 0)
            {
                timerUpdate.Stop();
                UpdateStep = enUpdateProc.Update_End;
                MessageBox.Show("数据传输出错,升级失败!", "升级失败", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                prgBarUpdate.Visible     = false;
                btOpenUpdateFile.Enabled = true;
                btRdVersionInfo.Enabled  = true;
                btStartUpdate.Enabled    = true;
                rbtBootUpdate.Enabled    = true;
                rbtAppUpdate.Enabled     = true;
                gpSelectType.Enabled     = true;
                btStartUpdate.Text       = "开始升级";
                return;
            }
            if (UpdateType == enUpdateType.地锁系统)
            {
                Protocol6009Struct txStruct = new Protocol6009Struct();
                txStruct.CommandIDByte = 0xF1;
                // 数据域
                if (updateLength - AppUpdateWriteAddress > AppUpdateCountOnePacket)
                {
                    AppUpdateCurPkgBytes = AppUpdateCountOnePacket;
                }
                else
                {
                    AppUpdateCurPkgBytes = updateLength - AppUpdateWriteAddress;
                }
                txStruct.DataByte = new byte[AppUpdateCurPkgBytes + 12];
                iLoop             = 0;
                // 增加CRC
                txStruct.DataByte[iLoop++] = (byte)updateCrc;
                txStruct.DataByte[iLoop++] = (byte)(updateCrc >> 8);
                // 增加写入地址
                txStruct.DataByte[iLoop++] = (byte)AppUpdateWriteAddress;
                txStruct.DataByte[iLoop++] = (byte)(AppUpdateWriteAddress >> 8);
                txStruct.DataByte[iLoop++] = (byte)(AppUpdateWriteAddress >> 16);
                txStruct.DataByte[iLoop++] = (byte)(AppUpdateWriteAddress >> 24);
                // 增加写入代码总长度
                txStruct.DataByte[iLoop++] = (byte)updateLength;
                txStruct.DataByte[iLoop++] = (byte)(updateLength >> 8);
                txStruct.DataByte[iLoop++] = (byte)(updateLength >> 16);
                txStruct.DataByte[iLoop++] = (byte)(updateLength >> 24);
                // 本包升级代码的长度
                txStruct.DataByte[iLoop++] = (byte)AppUpdateCurPkgBytes;
                txStruct.DataByte[iLoop++] = (byte)(AppUpdateCurPkgBytes >> 8);
                // 升级数据
                iPos = AppUpdateWriteAddress;
                for (; iLoop < txStruct.DataByte.Length; iLoop++)
                {
                    txStruct.DataByte[iLoop] = updateBuf[iPos++];
                }

                // 发送数据
                dataTransmit(_6009CreatTxBuffer(txStruct));
                UpdateWaitTime = 200;
                UpdateRetryTimes--;
            }
            else if (UpdateType == enUpdateType.无线模块系统)
            {
                Protocol6009Struct txStruct = new Protocol6009Struct();
                txStruct.CommandIDByte = 0xF4;
                // 数据域
                if (updateLength - AppUpdateWriteAddress > AppUpdateCountOnePacket)
                {
                    AppUpdateCurPkgBytes = AppUpdateCountOnePacket;
                }
                else
                {
                    AppUpdateCurPkgBytes = updateLength - AppUpdateWriteAddress;
                }
                txStruct.DataByte = new byte[AppUpdateCurPkgBytes + 12];
                iLoop             = 0;
                // 增加CRC
                txStruct.DataByte[iLoop++] = (byte)updateCrc;
                txStruct.DataByte[iLoop++] = (byte)(updateCrc >> 8);
                // 增加写入地址
                txStruct.DataByte[iLoop++] = (byte)AppUpdateWriteAddress;
                txStruct.DataByte[iLoop++] = (byte)(AppUpdateWriteAddress >> 8);
                txStruct.DataByte[iLoop++] = (byte)(AppUpdateWriteAddress >> 16);
                txStruct.DataByte[iLoop++] = (byte)(AppUpdateWriteAddress >> 24);
                // 增加写入代码总长度
                txStruct.DataByte[iLoop++] = (byte)updateLength;
                txStruct.DataByte[iLoop++] = (byte)(updateLength >> 8);
                txStruct.DataByte[iLoop++] = (byte)(updateLength >> 16);
                txStruct.DataByte[iLoop++] = (byte)(updateLength >> 24);
                // 本包升级代码的长度
                txStruct.DataByte[iLoop++] = (byte)AppUpdateCurPkgBytes;
                txStruct.DataByte[iLoop++] = (byte)(AppUpdateCurPkgBytes >> 8);
                // 升级数据
                iPos = AppUpdateWriteAddress;
                for (; iLoop < txStruct.DataByte.Length; iLoop++)
                {
                    txStruct.DataByte[iLoop] = updateBuf[iPos++];
                }

                // 发送数据
                dataTransmit(_6009CreatTxBuffer(txStruct));
                UpdateWaitTime = 200;
                UpdateRetryTimes--;
            }
            UpdateWaitTime = 80;
            UpdateRetryTimes--;
        }
 private void btStartUpdate_Click(object sender, EventArgs e)
 {
     if (btStartUpdate.Text == "停止升级")
     {
         UpdateStep = enUpdateProc.Update_End;
         timerUpdate.Stop();
         MessageBox.Show("升级进程被终止!", "升级中断", MessageBoxButtons.OK, MessageBoxIcon.Stop);
         btStartUpdate.Text       = "开始升级";
         btOpenUpdateFile.Enabled = true;
         btRdVersionInfo.Enabled  = true;
         rbtBootUpdate.Enabled    = true;
         rbtAppUpdate.Enabled     = true;
         gpSelectType.Enabled     = true;
         return;
     }
     if (FrmMain.strComPort == "")
     {
         MessageBox.Show("请先打开通讯端口!", "打开端口", MessageBoxButtons.OK, MessageBoxIcon.Error);
         return;
     }
     loadUpdateBuf();
     if (updateLength == 0)
     {
         MessageBox.Show("请选择有效的升级文件!", "无效文件", MessageBoxButtons.OK, MessageBoxIcon.Error);
         return;
     }
     if (FrmMain.strComPort == "")
     {
         MessageBox.Show("请先打开通讯端口!", "打开端口", MessageBoxButtons.OK, MessageBoxIcon.Information);
         return;
     }
     if (rbtBootUpdate.Checked == true)
     {
         if (FrmMain.strComPort == "USB")
         {
             MessageBox.Show("Boot下升级只能在串口下进行!", "端口错误", MessageBoxButtons.OK, MessageBoxIcon.Stop);
             return;
         }
         iLastPortBaudrate = setUartBaudrate(115200);
         if (iLastPortBaudrate == 0)
         {
             MessageBox.Show("打开端口出现错误!", "端口错误", MessageBoxButtons.OK, MessageBoxIcon.Information);
             return;
         }
         UpdateStep                = enUpdateProc.Boot_EnterUpdate;
         UpdateRetryTimes          = 1000;
         BootUpdateLastDownCounter = 10 * (1000 / timerUpdate.Interval) + 5;
         btStartUpdate.Enabled     = false;
     }
     else
     {
         UpdateStep            = enUpdateProc.App_UpdateInProcing;
         AppUpdateWriteAddress = 0;
         UpdateRetryTimes      = 5;
     }
     btOpenUpdateFile.Enabled = false;
     btRdVersionInfo.Enabled  = false;
     rbtBootUpdate.Enabled    = false;
     rbtAppUpdate.Enabled     = false;
     gpSelectType.Enabled     = false;
     btStartUpdate.Text       = "停止升级";
     // 关闭监控功能
     UpdateWaitTime = 3;
     timerUpdate.Start();
 }
        private void ExplainBootPacket(byte[] RecBuf)
        {
            UInt16 crc;
            byte   bLen;

            if (RecBuf[0] != SyncWord1 || RecBuf[1] != SyncWord2)
            {
                return;
            }
            bLen = RecBuf[2];
            crc  = calCrc16(RecBuf, 2, (UInt32)(bLen + 1));
            if (crc != (RecBuf[bLen + 4] * 256 + RecBuf[bLen + 3]))
            {
                return;
            }
            if (RecBuf[3] == 0x02)          // 进入升级命令的应答
            {
                if (RecBuf[5] == 0 && RecBuf[6] == 0)
                {
                    UpdateStep           = enUpdateProc.Boot_UpdateInProcing;
                    UpdateWaitTime       = 1;
                    UpdateRetryTimes     = 5;
                    BootUpdateCurrentPkg = 0;
                    BootUpdateTotalPkg   = updateBuf[3] * 256 + updateBuf[2];
                    prgBarUpdate.Maximum = BootUpdateTotalPkg;
                    prgBarUpdate.Value   = BootUpdateCurrentPkg;
                    prgBarUpdate.Visible = true;
                    lbFront.Visible      = false;
                    lbMiddle.Visible     = false;
                    lbRear.Visible       = false;
                    return;
                }
            }
            else if (RecBuf[3] == 0x04)     // 发送升级数据包应答
            {
                if (RecBuf[7] == 0 && RecBuf[8] == 0)
                {
                    UpdateStep           = enUpdateProc.Boot_UpdateInProcing;
                    UpdateWaitTime       = 1;
                    UpdateRetryTimes     = 5;
                    BootUpdateCurrentPkg = RecBuf[5] + RecBuf[6] * 256;
                    prgBarUpdate.Maximum = BootUpdateTotalPkg;
                    prgBarUpdate.Value   = BootUpdateCurrentPkg;
                    prgBarUpdate.Visible = true;
                    lbFront.Visible      = false;
                    lbMiddle.Visible     = false;
                    lbRear.Visible       = false;
                    if (++BootUpdateCurrentPkg >= BootUpdateTotalPkg)
                    {
                        timerUpdate.Stop();
                        UpdateStep = enUpdateProc.Update_End;
                        MessageBox.Show("程序升级成功,主板将在10秒钟内重新启动。", "升级成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        btOpenUpdateFile.Enabled = true;
                        btRdVersionInfo.Enabled  = true;
                        rbtBootUpdate.Enabled    = true;
                        rbtAppUpdate.Enabled     = true;
                        btStartUpdate.Enabled    = true;
                        gpSelectType.Enabled     = true;
                        // 恢复原波特率
                        setUartBaudrate(iLastPortBaudrate);
                        btStartUpdate.Text = "开始升级";
                    }
                }
            }
        }
        private void TransmitBootPacket()
        {
            int    iLoop, iPos;
            UInt16 crc;

            if (UpdateStep == enUpdateProc.Boot_EnterUpdate)
            {
                byte[] txBuf = new byte[19];
                iPos          = 0;
                txBuf[iPos++] = SyncWord1;
                txBuf[iPos++] = SyncWord2;
                txBuf[iPos++] = (byte)(txBuf.Length - 5);           // 长度
                txBuf[iPos++] = 0x01;                               // 命令标识-进入升级指令
                txBuf[iPos++] = 0x04;                               // 命令选项
                for (iLoop = 0; iLoop < 12; iLoop++)
                {
                    txBuf[iPos++] = updateBuf[iLoop];
                }
                crc           = calCrc16(txBuf, 2, (UInt32)(iPos - 2));
                txBuf[iPos++] = (byte)crc;
                txBuf[iPos++] = (byte)(crc >> 8);
                dataTransmit(txBuf);                                // 发送数据
                UpdateWaitTime = 15;
            }
            else if (UpdateStep == enUpdateProc.Boot_UpdateInProcing)
            {
                if (UpdateRetryTimes == 0)
                {
                    timerUpdate.Stop();
                    UpdateStep = enUpdateProc.Update_End;
                    MessageBox.Show("数据传输出错,升级失败!", "升级失败", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    prgBarUpdate.Visible     = false;
                    lbFront.Visible          = false;
                    lbMiddle.Visible         = false;
                    lbRear.Visible           = false;
                    btOpenUpdateFile.Enabled = true;
                    btRdVersionInfo.Enabled  = true;
                    btStartUpdate.Enabled    = true;
                    rbtBootUpdate.Enabled    = true;
                    rbtAppUpdate.Enabled     = true;
                    gpSelectType.Enabled     = true;
                    // 恢复原先的波特率
                    setUartBaudrate(iLastPortBaudrate);
                    btStartUpdate.Text = "开始升级";
                    return;
                }

                byte[] txBuf = new byte[141];
                iPos          = 0;
                txBuf[iPos++] = SyncWord1;
                txBuf[iPos++] = SyncWord2;
                txBuf[iPos++] = (byte)(txBuf.Length - 5);           // 长度
                txBuf[iPos++] = 0x03;                               // 命令标识-发送升级数据包
                txBuf[iPos++] = 0x04;                               // 命令选项
                for (iLoop = 0; iLoop < 4; iLoop++)
                {
                    txBuf[iPos++] = updateBuf[iLoop];
                }
                txBuf[iPos++] = (byte)BootUpdateCurrentPkg;
                txBuf[iPos++] = (byte)(BootUpdateCurrentPkg >> 8);
                for (iLoop = 0; iLoop < 128; iLoop++)
                {
                    txBuf[iPos++] = updateBuf[(BootUpdateCurrentPkg + 1) * BootUpdateCountOnePacket + iLoop];
                }
                crc           = calCrc16(txBuf, 2, (UInt32)(iPos - 2));
                txBuf[iPos++] = (byte)crc;
                txBuf[iPos++] = (byte)(crc >> 8);
                dataTransmit(txBuf);
                UpdateRetryTimes--;
                UpdateWaitTime = 80;
            }
            else if (UpdateStep == enUpdateProc.Update_End)
            {
                timerUpdate.Stop();
            }
            else
            {
                timerUpdate.Stop();
            }
        }