public void StopTask() { if (mPcuTask.isTaskBusy() || mCcuTask.isTaskBusy()) { mPcuTask.StopTask(); mCcuTask.StopTask(); mBroadCastTask.StopTask(); } else { bTaskRunning = false; mBroadCastTask.StopTask(); mResetEcuParam.cmdCode = 0x02; //升级失败或者其他,恢复同步指令 mResetEcuTask.Excute(); Thread.Sleep(10); mResetEcuTask.Excute(); Thread.Sleep(10); mResetEcuTask.Excute(); Thread.Sleep(10); mBroadCastTask.ClosePort(); } // }
public void ExcuteTask() { bTaskRunning = true; TaskBuilder(); mResetEcuParam.cmdCode = 0x02; mResetEcuTask.Excute(); if (mParam.bPCU) { mBroadCastTask.RetryMaxCnts = 0; mBroadCastTask.Timerout = 500; // mPcuTask.ExcuteTask(); } else if (mParam.bCCU) { mBroadCastTask.RetryMaxCnts = 0; mBroadCastTask.Timerout = 30 * 1000; } mBroadCastTask.Excute(); }
private void bk_doCcuWork(object sender, DoWorkEventArgs e) { BackgroundWorker bw = sender as BackgroundWorker; mUpgradeRequestTask.Excute(); if (!mCcuEvent.WaitOne()) { return; } mCcuEvent.Reset(); if (this.CcuWorkTask.CancellationPending) { return; } //开始显示进度条 UpdateLviStatus(sender, "", Task_Level.TASK_INIT); BinaryReader mBinStream = new BinaryReader(mCcuStream); int uploadedFileLength = 0; int packageCount = (int)((CCU_FILE_LENGTH / CCU_PACKAGE_SIZE) + (CCU_FILE_LENGTH % CCU_PACKAGE_SIZE != 0 ? 1 : 0)); //当前包号 for (int curPackageNo = 0; curPackageNo < packageCount; curPackageNo++) { string str = String.Format("数据包({0}/{1})", curPackageNo + 1, packageCount); UpdateLviStatus(sender, str, Task_Level.TASK_PROCESS); if (!this.CcuWorkTask.CancellationPending)//取消线程 { int curPackageSize = 0; if ((curPackageNo == packageCount - 1) && (CCU_FILE_LENGTH % CCU_PACKAGE_SIZE != 0)) { curPackageSize = (int)(CCU_FILE_LENGTH % CCU_PACKAGE_SIZE); } else { curPackageSize = CCU_PACKAGE_SIZE; } mUpgradeStartReqParam.PackageID = curPackageNo; mUpgradeStartReqParam.PackageLen = curPackageSize; mUpgradeStartReqParam.AlreadyDownLoadLen = uploadedFileLength; /****************4.发送包开始请求********************/ mUpgradeStartReqTask.Excute(); if (!mCcuEvent.WaitOne()) { mBinStream.Close(); mBinStream.Dispose(); mBinStream = null; return; } mCcuEvent.Reset(); //等待 Thread.Sleep(100); byte[] packageContent = new byte[curPackageSize]; byte[] arrBytes = new byte[CCU_FRAME_SIZE]; int readByteLen = 0; int offset = 0; while (offset < curPackageSize) { if (!this.CcuWorkTask.CancellationPending) { Array.Clear(arrBytes, 0, arrBytes.Length); if (curPackageSize - offset < CCU_FRAME_SIZE) { mBinStream.Read(arrBytes, 0, curPackageSize - offset); readByteLen = curPackageSize - offset; mUpgradeProcessReqParam.FrameData = arrBytes; mUpgradeProcessReqParam.length = curPackageSize - offset; } else { mBinStream.Read(arrBytes, 0, CCU_FRAME_SIZE); readByteLen = CCU_FRAME_SIZE; mUpgradeProcessReqParam.FrameData = arrBytes; mUpgradeProcessReqParam.length = CCU_FRAME_SIZE; } /************************5.发送帧数据************************/ mUpgradeProcessReqTask.Excute(); if (!mCcuEvent.WaitOne()) { mBinStream.Close(); mBinStream.Dispose(); mBinStream = null; return; } mCcuEvent.Reset(); for (int j = 0; j < readByteLen; j++) { packageContent[offset + j] = arrBytes[j]; } offset += readByteLen; uploadedFileLength += readByteLen; Thread.Sleep(3); } else { mBinStream.Close(); mBinStream.Dispose(); mBinStream = null; return; } } /************************5.发送包结束************************/ Thread.Sleep(10); mUpgradeEndReqParam.PackageID = curPackageNo; mUpgradeEndReqParam.CRC = CRC16.GetCrc16(packageContent); mUpgradeEndReqTask.Excute(); if (!mCcuEvent.WaitOne()) { mBinStream.Dispose(); mBinStream.Close(); mBinStream = null; return; } mCcuEvent.Reset(); //通知进度条 bw.ReportProgress(uploadedFileLength * 100 / CCU_FILE_LENGTH); } else { mBinStream.Close(); mBinStream.Dispose(); mBinStream = null; return; } } Thread.Sleep(20); UpdateLviStatus(sender, "MD5验证", Task_Level.TASK_PROCESS); mUpgradeMD5ReqTask.Excute(); if (!mCcuEvent.WaitOne()) { mBinStream.Dispose(); mBinStream.Close(); mBinStream = null; return; } mCcuEvent.Reset(); mBinStream.Close(); mBinStream.Dispose(); mBinStream = null; mCcuStream.Close(); mCcuStream.Dispose(); mCcuStream = null; UpdateLviStatus(sender, "数据传输完成", Task_Level.TASK_PASS); SetProgressValue(sender, 100); return; }
private void TaskBuilder() { //停止同步报文 mStopSyncTask = new SimpleSerialPortTask <pcTakeOverRsp, NullEntity>(); mStopSyncParam = mStopSyncTask.GetRequestEntity(); mStopSyncTask.RetryMaxCnts = 0; mStopSyncTask.Timerout = 1000; mStopSyncParam.DeviceType = 0xF1; mStopSyncParam.HardWareID = 0xF1; // mStopSyncParam.FirmID = 17; //mStopSyncParam.FirmID1 = 35; //mStopSyncParam.FirmID2 = 4; //重启指令 mResetEcuTask = new SimpleSerialPortTask <ResetEcuReq, NullEntity>(); mResetEcuParam = mResetEcuTask.GetRequestEntity(); mResetEcuTask.RetryMaxCnts = 0; mResetEcuTask.Timerout = 1000; mResetEcuParam.deviceType = 0XF1; mResetEcuParam.cmdCode = 0x02; mResetEcuParam.param1 = 0x11223344; //等待同步报文 mBroadCastTask = new SimpleSerialPortTask <NullEntity, pcTakeoverReq>(); mBroadCastTask.RetryMaxCnts = 0; mBroadCastTask.Timerout = 500; mBroadCastTask.SimpleSerialPortTaskOnPostExecute += (object sender, EventArgs e) => { SerialPortEventArgs <pcTakeoverReq> mEventArgs = e as SerialPortEventArgs <pcTakeoverReq>; if (mEventArgs.Data != null) { byte hw1 = (byte)(mEventArgs.Data.hardwareID >> 24); byte hw2 = (byte)(mEventArgs.Data.hardwareID >> 16); byte hw3 = (byte)(mEventArgs.Data.hardwareID >> 8); string softVersion = String.Format("W{0}{1:D2}.{2:D2}", (byte)(mEventArgs.Data.hardwareID >> 24), (byte)(mEventArgs.Data.hardwareID >> 16), (byte)(mEventArgs.Data.hardwareID >> 8)); mStopSyncParam.FirmID = hw1; mStopSyncParam.FirmID1 = hw2; mStopSyncParam.FirmID2 = hw3; //PC接管报文的响应 mStopSyncTask.Excute(); Thread.Sleep(5); mStopSyncTask.Excute(); Thread.Sleep(5); mStopSyncTask.Excute(); Thread.Sleep(5); if (mParam.bPCU) { mPcuTask.ExcuteTask();//开始升级PCU } else if (mParam.bCCU) { mCcuTask.ExcuteTask();//开始升级CCU } } else { if (mParam.bPCU) { mPcuTask.ExcuteTask();//开始升级PCU } else if (mParam.bCCU) { //设备未上电 TaskArgs mArgs = new TaskArgs(); mArgs.msg = "中控未上电"; mArgs.level = Task_Level.TASK_FAIL; ccuListViewItemHandler(sender, mArgs); bTaskRunning = false; mResetEcuParam.cmdCode = 0x02; //升级失败或者其他,恢复同步指令 mResetEcuTask.Excute(); Thread.Sleep(10); mResetEcuTask.Excute(); Thread.Sleep(10); mResetEcuTask.Excute(); Thread.Sleep(10); mBroadCastTask.ClosePort(); } } }; mPcuTask = new PcuTask(this.mParam.mPcuFilePath); //执行结束回调 mPcuTask.TaskDoneHandler += (object sender, EventArgs e) => { TaskArgs mArgs = e as TaskArgs; if (mParam.bCCU && mArgs.level == Task_Level.TASK_PASS) { if (mCcuTask != null) { mCcuTask.ExcuteTask(); } } else { bTaskRunning = false; mResetEcuParam.cmdCode = 0x02; //升级失败或者其他,恢复同步指令 mResetEcuTask.Excute(); Thread.Sleep(10); mResetEcuTask.Excute(); Thread.Sleep(10); mResetEcuTask.Excute(); Thread.Sleep(10); mBroadCastTask.ClosePort(); } }; //PCU Item mPcuTask.PcuLviHandler += (object sender, EventArgs e) => { pcuListViewItemHandler(sender, e); }; //PCU 进度回调 mPcuTask.pcuProgressBarHandler += (object sender, EventArgs e) => { pcuProgressBarHandler(sender, e); }; mCcuTask = new CcuTask(this.mParam.mCcuFilePath); //执行结束后回调 mCcuTask.TaskDoneHandler += (object sender, EventArgs e) => { bTaskRunning = false; TaskArgs mArgs = e as TaskArgs; if (mArgs.level == Task_Level.TASK_PASS) { mResetEcuParam.cmdCode = 0x01; //升级成功后,进行重启 } else { mResetEcuParam.cmdCode = 0x02; //升级失败或者其他,恢复同步指令 } mResetEcuTask.Excute(); Thread.Sleep(10); mResetEcuTask.Excute(); Thread.Sleep(10); mResetEcuTask.Excute(); Thread.Sleep(10); mBroadCastTask.ClosePort(); }; //CCU Item mCcuTask.CcuLviHandler += (object sender, EventArgs e) => { ccuListViewItemHandler(sender, e); }; //CCU 进度回调 mCcuTask.CcuProgressBarHandler += (object sender, EventArgs e) => { ccuProgressBarHandler(sender, e); }; }
private void bk_doPcuWork(object sender, DoWorkEventArgs e) { BackgroundWorker bw = sender as BackgroundWorker; mPcuUpgradeStartTask.Excute(); if (!mPcuEvent.WaitOne()) { return; } if (this.pcuWorkTask.CancellationPending) { return; } //开始显示进度条 UpdateLviStatus(sender, "", Task_Level.TASK_INIT); BinaryReader mBinStream = new BinaryReader(mPcuStream); //多少个包 int uploadedFileLength = 0; int packageCnts = (int)((PCU_FILE_LENGTH / PCU_PACKAGE_SIZE) + (PCU_FILE_LENGTH % PCU_PACKAGE_SIZE != 0 ? 1 : 0)); for (int curPackageNo = 0; curPackageNo < packageCnts; curPackageNo++) { if (!this.pcuWorkTask.CancellationPending) { string str = String.Format("数据包传输中({0}/{1})", curPackageNo + 1, packageCnts); UpdateLviStatus(sender, str, Task_Level.TASK_PROCESS); int curPackageSize = 0; if ((curPackageNo == packageCnts - 1) && (PCU_FILE_LENGTH % PCU_PACKAGE_SIZE != 0)) { curPackageSize = (int)(PCU_FILE_LENGTH % PCU_PACKAGE_SIZE); } else { curPackageSize = PCU_PACKAGE_SIZE; } //Step1: 发送升级请求 mPcuRequestParam.currentPage = curPackageNo; //第几K ID mPcuRequestParam.year = 0x0F; //年 mPcuRequestParam.month = 0x29; //周数 mPcuRequestParam.version = 0x01; //版本好 mPcuRequestParam.fileLength = PCU_FILE_LENGTH; //总包长度 mPcuRequestParam.singlepagesize = curPackageSize; //单个包的长度 mPcuUpgradeRequestTask.Excute(); if (!mPcuEvent.WaitOne()) { mBinStream.Dispose(); mBinStream.Close(); mBinStream = null; return; } mPcuEvent.Reset(); //Step2: 发送帧数据 Thread.Sleep(5); int frameCnts = (int)((curPackageSize / PCU_FRAME_SIZE) + (curPackageSize % PCU_FRAME_SIZE != 0 ? 1 : 0)); byte[] packageContent = new byte[curPackageSize]; byte[] arrBytes = new byte[PCU_FRAME_SIZE]; int readByteLen = 0; int offset = 0; for (int frameNo = 0; frameNo < frameCnts; frameNo++) { if (!this.pcuWorkTask.CancellationPending) { Array.Clear(arrBytes, 0, arrBytes.Length); if (curPackageSize - offset < PCU_FRAME_SIZE) { mBinStream.Read(arrBytes, 0, curPackageSize - offset); readByteLen = curPackageSize - offset; mPcuProcessParam.buff = arrBytes; mPcuProcessParam.length = readByteLen; mPcuProcessParam.currentPage = curPackageNo; mPcuProcessParam.currentSeg = frameNo; } else { mBinStream.Read(arrBytes, 0, PCU_FRAME_SIZE); readByteLen = PCU_FRAME_SIZE; mPcuProcessParam.buff = arrBytes; mPcuProcessParam.length = readByteLen; mPcuProcessParam.currentPage = curPackageNo; mPcuProcessParam.currentSeg = frameNo; } //发送数据 mPcuUpgradeProcessTask.Excute(); if (!mPcuEvent.WaitOne()) { mBinStream.Dispose(); mBinStream.Close(); mBinStream = null; return; } mPcuEvent.Reset(); //把当前一包拷贝到数组里 for (int j = 0; j < readByteLen; j++) { packageContent[offset + j] = arrBytes[j]; } offset += readByteLen; uploadedFileLength += readByteLen; Thread.Sleep(5); } else { mBinStream.Dispose(); mBinStream.Close(); mBinStream = null; return; } } //Step3: 发送升级结束 mPcuDoneParam.currentPage = curPackageNo; mPcuDoneParam.pageChecksum = Util.getChkSum(packageContent); mPcuUpgradeDoneTask.Excute(); if (!mPcuEvent.WaitOne()) { mBinStream.Dispose(); mBinStream.Close(); mBinStream = null; return; } mPcuEvent.Reset(); bw.ReportProgress(uploadedFileLength * 100 / PCU_FILE_LENGTH); Thread.Sleep(10); } else { mBinStream.Dispose(); mBinStream.Close(); mBinStream = null; return; } } //执行PCU跳转 UpdateLviStatus(sender, "跳转中", Task_Level.TASK_PROCESS); mPcuUpgradeRunningTask.Excute(); if (!mPcuEvent.WaitOne()) { mBinStream.Dispose(); mBinStream.Close(); mBinStream = null; return; } mPcuEvent.Reset(); mBinStream.Dispose(); mBinStream.Close(); mBinStream = null; mPcuStream.Dispose(); mPcuStream.Close(); mPcuStream = null; UpdateLviStatus(sender, "数据传输成功", Task_Level.TASK_PASS); }