using ICSharpCode.Core; using Mesnac.Action.Base; using Mesnac.Action.ChemicalWeighing.Product.PptPlan.entity; using Mesnac.Action.ChemicalWeighing.Technical.PmtRecipe; using Mesnac.Action.ChemicalWeighing.Technical.PmtRecipe.entity; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace Mesnac.Action.ChemicalWeighing.Product.PptPlan { public class PlanDownloadAction : ChemicalWeighingAction, IAction { private RuntimeParameter _runtime; private DbMCControl _clientGridControl = null; //网格计划控件 private string selectedPlanId = null;//选中计划号 private string selectedPlanState = null; public void Run(RuntimeParameter runtime) { base.RunIni(runtime); //必须要调用的 ICSharpCode.Core.LoggingService.Debug("下传计划信息..."); //获取界面控件 DbMCControl clientGridControl = this.GetDbMCControlByKey(Mesnac.Basic.DataSourceFactory.MCDbType.Local, "Base_PlanInfo").FirstOrDefault(); if (clientGridControl == null) { ICSharpCode.Core.LoggingService.Error("{生产计划-修改计划信息} 缺少计划列表网格控件..."); return; } this._runtime = runtime; this._clientGridControl = clientGridControl; DataGridView clientGridView = this._clientGridControl.BaseControl as DataGridView; if (clientGridView.SelectedRows.Count != 1) { string msg1_1 = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_Product_PptPlan_ModifyPlanNumAction_msg1_1")); //请选择 MessageBox.Show(msg1_1, Mesnac.Basic.LanguageHelper.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information); this._runtime.IsReturn = true; return; } //获取选中的计划号 this.selectedPlanId = clientGridView.SelectedRows[0].Cells["plan_Id"].Value as string; this.selectedPlanState = clientGridView.SelectedRows[0].Cells["plan_state"].Value as string; if(this.selectedPlanState != "待执行") { if (this.selectedPlanState == "已完成") { MessageBox.Show("计划已执行完成请勿重复下传", Mesnac.Basic.LanguageHelper.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information); return; } DialogResult res = MessageBox.Show("计划正在执行中,是否要重传该计划!!!"); if(res != DialogResult.OK) { MessageBox.Show("取消重传!!!", Mesnac.Basic.LanguageHelper.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information); return; } } //判断下位机准备好信号 /*if (!(BasePlcHelper.Instance.ChemicalWeighing_PC_ConfirmRemote.NowValue.ToInt() == 1 && BasePlcHelper.Instance.ChemicalWeighing_PC_ConfirmReady.NowValue.ToInt() == 1)) { MessageBox.Show("下位机未准备好!!!"); return; }*/ //获取配方编号 recipe_id string reciprId = clientGridView.SelectedRows[0].Cells["recipe_id"].Value as string; writeToPlc(reciprId, this.selectedPlanId); //判断下传是否成功 if (reciprId == readRecipeId()) { ICSharpCode.Core.LoggingService.Info("{生产计划-计划下传} 计划下传成功..."); //下传成功后下传RpFinished信号 bool writeRpFinished = BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.ChemicalWeighing_PC_RpFinished, new object[] { true }); } else { MessageBox.Show("计划下传失败!!!"); } //开启线程读取计划执行状态 Thread t = new Thread(readPlanState); t.Start(); } /// /// 读取PLC执行的配方编号 /// /// public string readRecipeId() { bool readResult = BasePlcHelper.Instance.PlcRead(BasePlcHelper.Instance.ChemicalWeighing_PC_RecipeCode, out int[] res); string readRecipeId = intArrayToStr(res); return readRecipeId; } /// /// 读取plc计划执行状态 /// public void readPlanState() { while (true) { var writeResult1 = BasePlcHelper.Instance.PlcRead(BasePlcHelper.Instance.ChemicalWeighing_PC_SetCarNumber, out int[] setAmount); //读取完成数量,测试采用设定数量 //bool finishAmount = BasePlcHelper.Instance.PlcRead(BasePlcHelper.Instance.ChemicalWeighing_PC_RecipeNum, out int[] res); bool finishAmount = BasePlcHelper.Instance.PlcRead(BasePlcHelper.Instance.ChemicalWeighing_PC_SetCarNumber, out int[] res); bool planNoResult = BasePlcHelper.Instance.PlcRead(BasePlcHelper.Instance.ChemicalWeighing_PC_PlanNo, out int[] planNo); bool confirmRunResult = BasePlcHelper.Instance.PlcRead(BasePlcHelper.Instance.ChemicalWeighing_PC_ConfirmRun, out int[] confirmRun); string planNoStr = intArrayToStr(planNo) == "" ? "030aabf32dbe4f4e8f9eb8bb65b59b69" : intArrayToStr(planNo); if (planNoStr != "") { //根据计划编号修改计划状态、完成数量 Base_PlanInfo planInfo = PlanHelper.getPlanInfoByPlanId(planNoStr); planInfo.real_Amount = Convert.ToInt32(intArrayToStr(res)); if (planInfo.plan_Amount == planInfo.real_Amount) { planInfo.plan_State = 2; PlanHelper.UpdateBasePlanInfo(planInfo); //判断完成信号,正式环境需要判断完成状态 /*if (confirmRun[1] == 1) { ICSharpCode.Core.LoggingService.Info("{生产计划-计划下传} 计划" + planNoStr + "执行完成..."); //判断下一计划的运行方式 Base_PlanInfo nextPlanInfo = PlanHelper.getNextPlanInfoByOrder(planInfo.plan_order); if (nextPlanInfo.run_Type == 0) { writeToPlc(nextPlanInfo.recipe_Id, nextPlanInfo.plan_Id); } else { return; } }*/ ICSharpCode.Core.LoggingService.Info("{生产计划-计划下传} 计划" + planNoStr + "执行完成..."); //判断下一计划的运行方式 Base_PlanInfo nextPlanInfo = PlanHelper.getNextPlanInfoByPlanOrder(planInfo.plan_order); if (nextPlanInfo.run_Type == 0) { writeToPlc(nextPlanInfo.recipe_Id, nextPlanInfo.plan_Id); } else { return; } } else { planInfo.plan_State = 1; PlanHelper.UpdateBasePlanInfo(planInfo); } } Thread.Sleep(1000 * 3); } } /// /// 将配方数据写入PLC地址 /// /// /// public void writeToPlc(string recipe,string planId) { //获取配方信息、获取工艺参数信息 Base_RecipeInfo info = RecipeHelper.GetRecipeById(recipe); //通过计划编号获取选中计划 Base_PlanInfo planInfo = PlanHelper.getPlanInfoByPlanId(planId); #region 下传配方参数 //配方编号info.recipeId string recipeId = info.recipeId.PadRight(10, '0'); string recipeName = info.recipeName; object[] recipeidArray = new object[10]; for(int i = 0; i < 10; i++) { recipeidArray[i] = recipeId.Substring(i, 1); } //配方编号 bool resultFromPlc = BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.ChemicalWeighing_PC_RecipeCode, recipeidArray); //配方名称 bool writeResult = BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.ChemicalWeighing_PC_RecipeName, recipeidArray); //设定车数 var writeResult1 = BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.ChemicalWeighing_PC_SetCarNumber, new object[] { planInfo.car_Amount }); //格数 var writeResult2 = BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.ChemicalWeighing_PC_SetGeNumber, new object[] { planInfo.grid_Amount }); //层数 var writeResult3 = BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.ChemicalWeighing_PC_SetLineNumber, new object[] { planInfo.line_Amount }); //通过配方编号获取物料信息、参数信息 List recipeMaterials = RecipeHelper.GetRecipeMaterialInfoByRecipeId(info.recipeId); //混料速度1 var writeResult4 = BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.ChemicalWeighing_PC_Hlsd1, new object[] { 18 }); //混料速度2 var writeResult5 = BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.ChemicalWeighing_PC_Hlsd2, new object[] { 20 }); //混料速度3 var writeResult6 = BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.ChemicalWeighing_PC_Hlsd3, new object[] { 22 }); //进料时间 var writeResult13 = BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.ChemicalWeighing_PC_Jlsj, new object[] { 12 }); //混料时间 var writeResult14 = BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.ChemicalWeighing_PC_Hlsj, new object[] { 17 }); //VCC加料 Base_RecipeMaterial recipeMaterial = recipeMaterials.Where(x => x.materialId.Contains("202205280001")).FirstOrDefault(); decimal vccJl = recipeMaterial.materialWeight; var writeResult7 = BasePlcHelper.Instance.PlcWriteFloatByDataKey(BasePlcHelper.Instance.ChemicalWeighing_PC_VCCJl, (float)vccJl); ////VCC加料误差 List recipeCratParams1 = RecipeHelper.GerCratParamListByRecipeAndMaterial(info.recipeId, "202205280001"); Base_RecipeCratParam recipeCratParam = recipeCratParams1.Where(x => x.materialId.Equals(recipeMaterial.materialId) && x.paramName.Contains("VCC加料误差")).FirstOrDefault(); decimal vccjlwc = Convert.ToDecimal(recipeCratParam.paramValue); var writeResult8 = BasePlcHelper.Instance.PlcWriteFloatByDataKey(BasePlcHelper.Instance.ChemicalWeighing_PC_VCCJlWc, (float)vccjlwc); ////GFA加料 Base_RecipeMaterial recipeMaterial2 = recipeMaterials.Where(x => x.materialId.Contains("202205280002")).FirstOrDefault(); decimal gfajl = recipeMaterial2.materialWeight; var writeResult9 = BasePlcHelper.Instance.PlcWriteFloatByDataKey(BasePlcHelper.Instance.ChemicalWeighing_PC_GFAJl, (float)gfajl); ////GFA加料误差 List recipeCratParams2 = RecipeHelper.GerCratParamListByRecipeAndMaterial(info.recipeId, "202205280002"); Base_RecipeCratParam recipeCratParam2 = recipeCratParams2.Where(x => x.materialId.Equals(recipeMaterial2.materialId) && x.paramName.Contains("GFA加料误差")).FirstOrDefault(); decimal gfajlwc = Convert.ToDecimal(recipeCratParam2.paramValue); var writeResult10 = BasePlcHelper.Instance.PlcWriteFloatByDataKey(BasePlcHelper.Instance.ChemicalWeighing_PC_GFAJlWc, (float)gfajlwc); ////树脂加料 Base_RecipeMaterial recipeMaterial3 = recipeMaterials.Where(x => x.materialId.Contains("202205280003")).FirstOrDefault(); decimal szjl = recipeMaterial3.materialWeight; var writeResult11 = BasePlcHelper.Instance.PlcWriteFloatByDataKey(BasePlcHelper.Instance.ChemicalWeighing_PC_SZJl, (float)szjl); ////树脂加料误差 List recipeCratParams3 = RecipeHelper.GerCratParamListByRecipeAndMaterial(info.recipeId, "202205280003"); Base_RecipeCratParam recipeCratParam3 = recipeCratParams3.Where(x => x.materialId.Equals(recipeMaterial3.materialId) && x.paramName.Contains("树脂加料误差")).FirstOrDefault(); decimal szjlwc = Convert.ToDecimal(recipeCratParam3.paramValue); var writeResult12 = BasePlcHelper.Instance.PlcWriteFloatByDataKey(BasePlcHelper.Instance.ChemicalWeighing_PC_SZJlWc, (float)szjlwc); #endregion } public string intArrayToStr(int[] array) { string str = ""; if (array.Length > 0) { for (int i = 0; i < array.Length; i++) { if (array[i] > 0) { str = str + array[i]; } } } return str; } } }