You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
lj_plc/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/ChemicalWeighingPlc/PlcPlanHelper.cs

1889 lines
82 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using ICSharpCode.Core;
using System.Data;
using Mesnac.Action.ChemicalWeighing.Entity;
using System.Threading;
using System.Configuration;
using Mesnac.Action.ChemicalWeighing.Technical.PmtRecipe.entity;
namespace Mesnac.Action.ChemicalWeighing.ChemicalWeighingPlc
{
/// <summary>
/// 计划列表下传PLC辅助类
/// </summary>
public class PlcPlanHelper
{
#region 事件定义
public static object sender = null;
public static event EventHandler OnUpdatePlanStateFromPlc;
public static event EventHandler AutoDownloadPlan;
public static event EventHandler OnAutoAddPlan;
public static event EventHandler OnChangePlan;
#endregion
#region 字段定义
private static string originalMixPlcPlanTableName = String.Empty;
private static string originalMillPlcPlanTableName = String.Empty;
#endregion
#region 下传计划列表至PLC
/// <summary>
/// 下传计划列表至PLC
/// </summary>
/// <param name="msgList">输出消息列表</param>
/// <returns>成功返回true失败返回false</returns>
public static bool DownloadPlanData(List<string> msgList)
{
bool result = true;
List<Entity.RT_Plan> list = null;
//if (Global.PublicVar.Instance.ChemicalWeighingPlcOnlineFlag) //|| Global.PublicVar.Instance.IsLocalTest)
if (true)
{
if (result)
{
if (list == null)
{
list = Product.PptPlan.PlanHelper.GetUnExePlanEntityList(1);
}
result = DownloadPlanDataToChe(list, msgList);
}
}
return result;
}
/// <summary>
/// 下传计划列表至PLC(自动)
/// </summary>
/// <param name="msgList">输出消息列表</param>
/// <returns>成功返回true失败返回false</returns>
public static bool AutoDownloadPlanData(List<string> msgList)
{
bool result = true;
List<Entity.RT_Plan> list = null;
#region 读取应答信号
if (!(BasePlcHelper.Instance.ChemicalWeighing_PLC_LoadingStatus.NowValue.ToInt() == 1 && BasePlcHelper.Instance.ChemicalWeighing_PC_LoadingStatus.NowValue.ToInt() == 0))
{
return false;
}
#endregion
if (result)
{
if (list == null)
{
list = Product.PptPlan.PlanHelper.GetUnExePlanEntityList(1);
}
result = DownloadPlanDataToChe(list, msgList);
}
return result;
}
#region 下传计划列表至小料PLC
/// <summary>
/// 下传计划列表至小料PLC
/// </summary>
/// <param name="list">计划列表</param>
/// <param name="msgList">输出的消息列表</param>
/// <returns>成功返回true失败返回flase</returns>
public static bool DownloadPlanDataToChe(List<Entity.RT_Plan> list, List<string> msgList)
{
try
{
#region 定义变量
//int cnt = 0; //循环计数
string msg = String.Empty;
List<Int16> writeDataList = new List<Int16>();
PropertyInfo[] ps = typeof(RT_Plan).GetProperties();
PropertyInfo[] psMaterial = typeof(Pmt_weigh).GetProperties();
List<Pmt_weigh> listMaterial = new List<Pmt_weigh>(); //计划对应的配方对应的物料列表
Pmt_recipe currentPmtRecipe = new Pmt_recipe(); //当前配方
RT_Plan nextRTPlan = null; //下一条计划
Pmt_recipe nextPmtRecipe = null; //当前配方
bool planDownFlag = false;
if (msgList == null)
{
msgList = new List<string>();
}
#endregion
#region 验证处理-配置
if (list == null || list.Count == 0)
{
msg = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_ChemicalWeighingPlc_PlcPlanHelper_Mix_msg40")); //小料-计划信息下传PLC失败无可下传计划!
msgList.Add(msg);
ICSharpCode.Core.LoggingService<PlcPlanHelper>.Error(msg);
return false;
}
List<PlcWriter> writerList = PlcSchemaHelper.Instance.GetPlcWriter("PlanRecipeData"); //配方头数据
if (writerList == null || writerList.Count == 0)
{
msg = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_ChemicalWeighingPlc_PlcPlanHelper_Mix_msg10")); //小料-计划信息下传PLC失败在PlcSchema.xml中没有RT_Plan配置节!
msgList.Add(msg);
ICSharpCode.Core.LoggingService<PlcPlanHelper>.Error(msg);
return false;
}
List<PlcWriter> writerList1 = PlcSchemaHelper.Instance.GetMaterialPlcWriter("PlanMaterialData"); //物料数据
if (writerList == null || writerList.Count == 0)
{
msg = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_ChemicalWeighingPlc_PlcPlanHelper_Mix_msg10")); //小料-计划信息下传PLC失败在PlcSchema.xml中没有RT_Plan配置节!
msgList.Add(msg);
ICSharpCode.Core.LoggingService<PlcPlanHelper>.Error(msg);
return false;
}
writerList = writerList.Concat(writerList1).ToList();
#endregion
#region 验证处理-数据
if (list == null || list.Count == 0)
{
msg = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_ChemicalWeighingPlc_PlcPlanHelper_Mix_msg15")); //小料-计划信息下传PLC失败当班计划列表为空!
msgList.Add(msg);
return false;
}
else
{
#region 根据配方recipeID获取物料信息
string recipeID = list[0].Recipe_ID;
listMaterial = Product.PptPlan.PlanHelper.GetPmt_weighList(recipeID);
if(listMaterial != null)
{
Global.PublicVar.Instance.currentListPmt_weigh = listMaterial;
}
#endregion
#region 根据配方recipeID获取配方信息
currentPmtRecipe = Technical.PmtRecipe.RecipeHelper.GetRecipeEntityByGUID(recipeID);
#endregion
#region 获取下一配方信息
nextRTPlan = Product.PptPlan.PlanHelper.GetNextPlan((int)list[0].Plan_Serial);
if(nextRTPlan != null){
nextPmtRecipe = Technical.PmtRecipe.RecipeHelper.GetRecipeEntityByGUID(nextRTPlan.Recipe_ID);
}
#endregion
}
#endregion
#region 业务处理
foreach (PlcWriter writer in writerList)
{
writeDataList.Clear();
#region 验证处理
if (writer.EquipRunName != "Recipe_Data_Download_BaseInf" && writer.EquipRunName != "Hopper_Recipe_Detail_Inf")
{
continue;
}
#endregion
#region 获取PLC品牌名称处理西门子与其他PLC差异
string equipBrand = Mesnac.Equips.Factory.Instance.GetEquipBrandByRunName(writer.EquipRunName);
writer.EquipBrand = equipBrand;
#endregion
#region 整理数据
if (writer.EquipRunName == "Recipe_Data_Download_BaseInf")
{
Entity.RT_Plan entity = null;
for (int i = 0; i < writer.ThisCount; i++)
{
if (list != null && i < list.Count)
{
entity = list[i];
DateTime timeNow = DateTime.Now;
foreach (PlcWriteItem item in writer.SchemaList)
{
item.ClearData();
item.EquipBrand = equipBrand; //设置PLC设备品牌
foreach (PropertyInfo pi in ps)
{
if (item.DataFieldName == pi.Name)
{
item.SetValue = pi.GetValue(entity, null);
break;
}
else if (item.EquipRunName == "DownLoad_H_Year")
{
item.SetValue = timeNow.Year;
break;
}
else if (item.EquipRunName == "DownLoad_H_Month")
{
item.SetValue = timeNow.Month;
break;
}
else if (item.EquipRunName == "DownLoad_H_Date")
{
item.SetValue = timeNow.Day;
break;
}
else if (item.EquipRunName == "DownLoad_H_Hour")
{
item.SetValue = timeNow.Hour;
break;
}
else if (item.EquipRunName == "DownLoad_H_Minute")
{
item.SetValue = timeNow.Minute;
break;
}
else if (item.EquipRunName == "DownLoad_H_Second")
{
item.SetValue = timeNow.Second;
break;
}
else if (item.EquipRunName == "DownLoad_MachineCode")
{
item.SetValue = "01";
break;
}
else if (item.EquipRunName == "DownLoad_TotalSetWeight")
{
if (currentPmtRecipe != null && currentPmtRecipe.Total_Weight != null)
{
item.SetValue = Convert.ToInt32(currentPmtRecipe.Total_Weight * 1000);
}
break;
}
else if (item.EquipRunName == "DownLoad_TotalErrorWeight")
{
if (currentPmtRecipe != null && currentPmtRecipe.Total_Error != null)
{
item.SetValue = Convert.ToInt32(currentPmtRecipe.Total_Error * 1000);
}
break;
}
else if (item.EquipRunName == "DownLoad_1LOT")
{
if (currentPmtRecipe != null && currentPmtRecipe.GroupBags != null)
{
item.SetValue = (Int32)currentPmtRecipe.GroupBags;
}
break;
}
else if (item.EquipRunName == "DownLoad_NextPlanCode")
{
if (nextPmtRecipe != null && nextPmtRecipe.Recipe_Name != null)
{
item.SetValue = nextPmtRecipe.Recipe_Name;
}
break;
}
else if (item.EquipRunName == "DownLoad_NextTotalSetWeight")
{
if (nextPmtRecipe != null && nextPmtRecipe.Total_Weight != null)
{
item.SetValue = Convert.ToInt32(nextPmtRecipe.Total_Weight * 1000);
}
break;
}
else if (item.EquipRunName == "DownLoad_NextPlanNum")
{
if (nextRTPlan != null && nextRTPlan.Plan_Num != null)
{
item.SetValue = Convert.ToInt32(nextRTPlan.Plan_Num);
}
break;
}
else if (item.EquipRunName == "DownLoad_SeparateNum")
{
if (entity != null && entity.Plan_Num != null)
{
item.SetValue = entity.Plan_Num; //后期会有配置项进行设置
}
break;
}
}
writeDataList.AddRange(item.WriteData());
}
}
else
{
foreach (PlcWriteItem item in writer.SchemaList)
{
item.ClearData();
item.EquipBrand = equipBrand; //设置PLC设备品牌
writeDataList.AddRange(item.WriteData());
}
}
}
}
if (writer.EquipRunName == "Hopper_Recipe_Detail_Inf")
{
Pmt_weigh entity = null;
for (int i = 0; i < writer.ThisCount; i++)
{
if (listMaterial != null && i < listMaterial.Count)
{
entity = listMaterial[i];
DateTime timeNow = DateTime.Now;
foreach (PlcWriteItem item in writer.SchemaList)
{
item.ClearData();
item.EquipBrand = equipBrand; //设置PLC设备品牌
foreach (PropertyInfo pi in psMaterial)
{
if (item.DataFieldName == pi.Name)
{
item.SetValue = pi.GetValue(entity, null);
if (item.DataFieldName == "Set_Weight" || item.DataFieldName == "Set_Error")
{
object temd = item.SetValue;
item.SetValue = Convert.ToDouble(temd) * 1000;
}
else if (item.DataFieldName == "Weight_Id")
{
object temd = item.SetValue;
item.SetValue = Convert.ToInt32(temd) - 1;
}
break;
}
else if (item.EquipRunName == "DownLoad_BinNo")
{
Pmt_Bin pmtBin = Product.PptPlan.PlanHelper.GetPmt_Bin(entity.Material_ID);
if (pmtBin != null && pmtBin.Bin_Serial != null)
{
item.SetValue = pmtBin.Bin_Serial;
}
else
{
item.SetValue = 100; //对应手动秤功能
}
break;
}
}
writeDataList.AddRange(item.WriteData());
}
}
else
{
foreach (PlcWriteItem item in writer.SchemaList)
{
item.ClearData();
item.EquipBrand = equipBrand; //设置PLC设备品牌
writeDataList.AddRange(item.WriteData());
}
}
}
}
#endregion
#region 下传数据
object[] buff = new object[writeDataList.Count];
Array.Copy(writeDataList.ToArray(), buff, buff.Length);
ICSharpCode.Core.LoggingService<PlcPlanHelper>.Debug(String.Format("[{0}]开始下传计划信息...", writer.EquipRunName));
if (!BasePlcHelper.Instance.PlcWriteByRunName(writer.EquipRunName, buff))
{
msg = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_ChemicalWeighingPlc_PlcPlanHelper_Mix_msg50")); //小料-计划信息下传PLC[{0}]失败...
msg = String.Format(msg, writer.EquipRunName);
msgList.Add(msg);
ICSharpCode.Core.LoggingService<PlcPlanHelper>.Error(msg);
return false;
}
else
{
planDownFlag = true;
}
ICSharpCode.Core.LoggingService<PlcPlanHelper>.Debug(String.Format("[{0}]计划信息下传完毕...", writer.EquipRunName));
#endregion
}
#endregion
#region 交互逻辑
if (planDownFlag)
{
#region 下传请求信号
bool result = BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.ChemicalWeighing_PC_LoadingStatus, new object[] { 1 });
if (!result)
{
msg = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_ChemicalWeighingPlc_PlcPlanHelper_Mix_msg20")); //小料-计划信息下传PLC向PLC下传请求信号失败!
msgList.Add(msg);
ICSharpCode.Core.LoggingService<PlcPlanHelper>.Error(msg);
return false;
}
#endregion
#region 数据下传完成后,将该条计划的状态更改为已下传
Product.PptPlan.PlanHelper.UpdatePlanState(list[0].Plan_Id, 4);
planDownFlag = false;
#endregion
if (AutoDownloadPlan != null)
{
AutoDownloadPlan(sender, System.EventArgs.Empty);
}
}
#endregion
return true;
}
catch (Exception ex)
{
msgList.Add(ex.Message);
ICSharpCode.Core.LoggingService<PlcPlanHelper>.Error(ex.Message, ex);
return false;
}
finally
{
#region 交互逻辑
if (Global.PublicVar.Instance.PlcDownloadMode == 1)
{
//#region 清除请求信号
//bool result = BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.Mix_SysAction_ExchPlanInfoRep_Req, new object[] { 0 });
//if (!result)
//{
// BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.Mix_SysAction_ExchPlanInfoRep_Req, new object[] { 0 });
//}
//#endregion
}
#endregion
}
}
#endregion
#endregion
#region 获取PLC计划列表中某条计划中允许修改的最小车数
/// <summary>
/// 获取上辅机PLC计划列表中某条计划中允许修改的最小车数
/// </summary>
/// <param name="planID">要获取的允许最小车数的计划号</param>
/// <returns>返回对应计划号的最小车数</returns>
//public static int GetMixMinPlanNumFromPlc(string planID)
//{
// DataTable dt = BasePlcHelper.Instance.Mix_PlanState_PlcPlanList.LastValue.ToObject() as DataTable;
// if (dt != null && dt.Rows.Count > 0)
// {
// foreach(DataRow dr in dt.Rows)
// {
// if (Mesnac.Basic.DataProcessor.RowValue(dr, "PlanID", String.Empty) == planID)
// {
// return Mesnac.Basic.DataProcessor.RowValue(dr, "NumModifyMin", 0);
// }
// }
// return 0;
// }
// else
// {
// return 0;
// }
//}
#endregion
#region 获取PLC计划列表中某条计划的计划数
/// <summary>
/// 获取PLC计划列表中某条计划的计划数
/// </summary>
/// <param name="planID">计划编号</param>
/// <param name="defaultValue">默认值</param>
/// <returns>返回对应计划号的计划数</returns>
//public static int GetMixPlanNumFromPlc(string planID, int defaultValue)
//{
// DataTable dt = BasePlcHelper.Instance.Mix_PlanState_PlcPlanList.LastValue.ToObject() as DataTable;
// if (dt != null && dt.Rows.Count > 0)
// {
// foreach (DataRow dr in dt.Rows)
// {
// if (Mesnac.Basic.DataProcessor.RowValue(dr, "PlanID", String.Empty) == planID)
// {
// return Mesnac.Basic.DataProcessor.RowValue(dr, "PlanNum", defaultValue);
// }
// }
// return defaultValue;
// }
// else
// {
// return defaultValue;
// }
//}
#endregion
#region 把PLC的计划状态更新至数据库
/// <summary>
/// 把PLC的计划状态更新至数据库
/// </summary>
public static void UpdatePlanStateFromPlc()
{
try
{
bool flag = false;
#region 小料同步计划状态和完成数
if (BasePlcHelper.Instance.ChemicalWeighing_PLC_Plan_Status_ShakeHand.NowValue.ToInt() == 1 && BasePlcHelper.Instance.ChemicalWeighing_PLC_Plan_Status_FeedBack.NowValue.ToInt() == 0)
{
int planSerial = BasePlcHelper.Instance.ChemicalWeighing_Plan_Serial.NowValue.ToInt(); //计划序号
int planState = BasePlcHelper.Instance.ChemicalWeighing_Plan_Status.NowValue.ToInt(); //计划状态
int planNum = BasePlcHelper.Instance.ChemicalWeighing_Plan_Number.NowValue.ToInt(); //计划数量
int planRestNum = BasePlcHelper.Instance.ChemicalWeighing_Remain_Number.NowValue.ToInt(); //剩余数量
int planComNum = planNum - planRestNum; //完成数量
if (planState == (int)Entity.PlanState.Producting)
{
Product.PptPlan.PlanHelper.UpdatePlanInfo(planSerial, planState, planNum, planComNum);
}
else if (planState == (int)Entity.PlanState.Completed)
{
Product.PptPlan.PlanHelper.UpdatePlanInfo(planSerial, planState, planNum, planComNum,DateTime.Now.ToString());
}
else
{
return;
}
flag = true;
//PlanListReDown();20211205三元现场注释掉
#region 此处需要将RT_Plan中计划写入到LR_Plan中同时向LR_Recipe中存储信息
RT_Plan rtPlan = Product.PptPlan.PlanHelper.GetPlanDataEntity(planSerial);
if (rtPlan != null)
{
LR_plan lrPlan = new LR_plan();
Product.PptPlan.PlanHelper.EntityToEntity(rtPlan, lrPlan);
if (Product.PptPlan.PlanHelper.LRPlanExists(lrPlan.Plan_Id))
{
//更新
if (planState == (int)Entity.PlanState.Producting)
{
Product.PptPlan.PlanHelper.LRUpdatePlanInfo(lrPlan.Plan_Id, planState, planNum, planComNum);
}
else if (planState == (int)Entity.PlanState.Completed)
{
Product.PptPlan.PlanHelper.LRUpdatePlanInfo(lrPlan.Plan_Id, planState, planNum, planComNum, DateTime.Now.ToString());
#region 执行删除计划操作
Product.PptPlan.PlanHelper.DeletePlan(rtPlan.Plan_Id);
#endregion
}
}
else
{
//添加(LR_plan)
Product.PptPlan.PlanHelper.LRAddPlan(lrPlan);
//添加(LR_recipe)
LR_recipe lrRecipe = new LR_recipe();
lrRecipe.Plan_id = rtPlan.Plan_Id;
lrRecipe.Equip_Code = rtPlan.Equip_Code;
string recipeID = rtPlan.Recipe_ID;
List<Pmt_weigh> listPmtWeigh = Product.PptPlan.PlanHelper.GetPmt_weighList(recipeID);
foreach (Pmt_weigh tempPW in listPmtWeigh)
{
lrRecipe.Weight_ID = tempPW.Weight_Id - 1;
lrRecipe.Set_Weight = (float)tempPW.Set_Weight;
lrRecipe.Set_Error = (float)tempPW.Set_Error;
lrRecipe.CPK_Error = (decimal)0;
Base_MaterialInfo pmtMaterial = Product.PptPlan.PlanHelper.GetPmt_material(tempPW.Material_ID);
if (pmtMaterial != null)
{
lrRecipe.Material_Code = pmtMaterial.materialId;
lrRecipe.Material_Name = pmtMaterial.materialName;
lrRecipe.Batch_number = pmtMaterial.putTime.ToString();
lrRecipe.remark = null;
Product.PptPlan.PlanHelper.AddLR_recipe(lrRecipe);
}
}
}
}
#endregion
}
#endregion
#region 触发事件
if (flag)
{
#region 下传请求信号
bool result = BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.ChemicalWeighing_PLC_Plan_Status_FeedBack, new object[] { 1 });
if (!result)
{
string msg = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_ChemicalWeighingPlc_PlcPlanHelper_Mix_msg30")); //小料-计划信息下传PLC向PLC下传计划状态请求信号失败!
ICSharpCode.Core.LoggingService<PlcPlanHelper>.Error(msg);
}
#endregion
#region 判断等待计划数和正在运行的计划数。如果两个都是0清空RT_Plan表。
int waitPlanCount = Product.PptPlan.PlanHelper.GetUnExePlanNum();
int runningPlanCount = Product.PptPlan.PlanHelper.GetRunningPlanNum();
int downPlanCount = Product.PptPlan.PlanHelper.GetDownPlanNum();
if (waitPlanCount == 0 && runningPlanCount == 0 && downPlanCount == 0)
{
Product.PptPlan.PlanHelper.ClearRT_Plan();
}
#endregion
if (OnUpdatePlanStateFromPlc != null)
{
OnUpdatePlanStateFromPlc(sender, System.EventArgs.Empty);
}
}
#endregion
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService<PlcPlanHelper>.Error("把PLC的计划状态更新至数据库异常" + ex.Message, ex);
}
finally
{
Global.PublicVar.Instance.LocalPlanIsRefresh = false;
}
}
#endregion
#region 处理HMI的配方添加计划请求
/// <summary>
/// 处理HMI的配方添加计划请求
/// </summary>
public static void AutoAddPlan()
{
try
{
bool flag = false;
#region 读取并处理HMI的配方添加计划请求添加计划
if (BasePlcHelper.Instance.ChemicalWeighing_HMI_Confirm_Recipe_ShakeHand.NowValue.ToInt() == 1 && (BasePlcHelper.Instance.ChemicalWeighing_HMI_Confirm_Recipe_FeedBack.NowValue.ToInt() <= 0 || BasePlcHelper.Instance.ChemicalWeighing_HMI_Confirm_Recipe_FeedBack.NowValue.ToInt() == 65535))
{
#region 从PLC中获取要添加的计划信息
String sysMachine = BasePlcHelper.Instance.ChemicalWeighing_Sys_Machine1.NowValue.ToString(); //机台号
String recipeName = BasePlcHelper.Instance.ChemicalWeighing_Plan_Code1.NowValue.ToString(); //配方名称
int hmiPlanNum = BasePlcHelper.Instance.ChemicalWeighing_HMI_Plan_Num.NowValue.ToInt(); //设定数量
if (String.IsNullOrEmpty(sysMachine))
{
sysMachine = "01";
}
#endregion
#region 补全计划信息
DateTime planDate = DateTime.Now; //当前计划日期
int shiftId = Technical.TechnicalHelper.GetCurrentShiftime(); //当前班次号
Base_RecipeInfo pmtRecipe = Technical.PmtRecipe.RecipeHelper.GetRecipeByName(recipeName);
string recipeId = null;
if (pmtRecipe != null && pmtRecipe.recipeId != null)
{
recipeId = pmtRecipe.recipeId;
}
else
{
string msg = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_Product_PptPlan_AutoAddPlan_logContent_6")); //HMI添加计划请求没有找到相关配方的信息!
ICSharpCode.Core.LoggingService<PlcPlanHelper>.Error(msg);
#region 下传反馈失败的信号
bool result = BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.ChemicalWeighing_HMI_Confirm_Recipe_FeedBack, new object[] { 3 });
if (!result)
{
string msg1 = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_Product_PptPlan_AutoAddPlan_logContent_7")); //HMI添加计划请求没有找到相关配方的信息时写反馈信号失败!
ICSharpCode.Core.LoggingService<PlcPlanHelper>.Error(msg1);
}
#endregion
return;
}
#endregion
#region 执行添加计划操作
string planID = Product.PptPlan.PlanHelper.AddPlan(sysMachine, planDate, shiftId, recipeId, hmiPlanNum);
flag = true;
#region 将等待计划数写入PLC
WaitPlanNumToPLC();
#endregion
#endregion
#region 触发事件
if (OnAutoAddPlan != null)
{
OnAutoAddPlan(sender, System.EventArgs.Empty);
}
#endregion
#region 记录日志
string logContent = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_Product_PptPlan_AutoAddPlan_logContent_1")); //计划编号:{0}HMI请求添加完毕!
logContent = String.Format(logContent, planID);
//base.DBLog(logContent); cfm20210729暂时注释
#endregion
}
#endregion
#region HMI添加计划请求交互并向PLC写入计划列表数据
if (flag)
{
#region 下传请求信号
bool result = BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.ChemicalWeighing_HMI_Confirm_Recipe_FeedBack, new object[] { 1 });
if (!result)
{
string msg = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_Product_PptPlan_AutoAddPlan_logContent_2")); //HMI添加计划请求成功后向PLC下传反馈信号失败!
ICSharpCode.Core.LoggingService<PlcPlanHelper>.Error(msg);
}
#endregion
#region 向PLC写入计划列表数据
#region 定义变量
List<Int16> writeDataList = new List<Int16>();
PropertyInfo[] ps = typeof(RT_Plan).GetProperties();
List<RT_Plan> rtPlanList = Product.PptPlan.PlanHelper.GetPlanEntityListByNum(5);
#endregion
#region 验证处理-配置
if (rtPlanList == null || rtPlanList.Count == 0)
{
string msg = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_Product_PptPlan_AutoAddPlan_logContent_3")); //HMI添加计划请求成功后计划列表获取失败!
ICSharpCode.Core.LoggingService<PlcPlanHelper>.Error(msg);
}
List<PlcWriter> writerList = PlcSchemaHelper.Instance.GetPlcWriter("PlanListData"); //计划列表数据
if (writerList == null || writerList.Count == 0)
{
string msg = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_Product_PptPlan_AutoAddPlan_logContent_4")); //HMI添加计划请求成功后计划列表写入数据结构获取失败!
ICSharpCode.Core.LoggingService<PlcPlanHelper>.Error(msg);
}
#endregion
#region 业务处理
foreach (PlcWriter writer in writerList)
{
writeDataList.Clear();
#region 验证处理
if (writer.EquipRunName != "PlanList_Info")
{
continue;
}
#endregion
#region 获取PLC品牌名称处理西门子与其他PLC差异
string equipBrand = Mesnac.Equips.Factory.Instance.GetEquipBrandByRunName(writer.EquipRunName);
writer.EquipBrand = equipBrand;
#endregion
#region 整理数据
if (writer.EquipRunName == "PlanList_Info")
{
Entity.RT_Plan entity = null;
for (int i = 0; i < writer.ThisCount; i++)
{
if (rtPlanList != null && i < rtPlanList.Count)
{
entity = rtPlanList[i];
DateTime timeNow = DateTime.Now;
foreach (PlcWriteItem item in writer.SchemaList)
{
item.ClearData();
item.EquipBrand = equipBrand; //设置PLC设备品牌
foreach (PropertyInfo pi in ps)
{
if (item.DataFieldName == pi.Name)
{
item.SetValue = pi.GetValue(entity, null);
break;
}
else if (item.EquipRunName == "DownLoad_MachineCode")
{
item.SetValue = "01";
break;
}
}
writeDataList.AddRange(item.WriteData());
}
}
}
}
if (writeDataList.Count < 200)
{
int fillNum = 200 - writeDataList.Count;
for (int i = 0; i < fillNum; i++)
{
writeDataList.AddRange(new short[] { 0 });
}
}
#endregion
#region 下传数据
object[] buff = new object[writeDataList.Count];
Array.Copy(writeDataList.ToArray(), buff, buff.Length);
ICSharpCode.Core.LoggingService<PlcPlanHelper>.Debug(String.Format("[{0}]开始下传计划信息...", writer.EquipRunName));
if (!BasePlcHelper.Instance.PlcWriteByRunName(writer.EquipRunName, buff))
{
string msg = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_Product_PptPlan_AutoAddPlan_logContent_5")); //HMI添加计划请求成功后计划列表写入PLC失败!
ICSharpCode.Core.LoggingService<PlcPlanHelper>.Error(msg);
}
ICSharpCode.Core.LoggingService<PlcPlanHelper>.Debug(String.Format("[{0}]计划列表信息下传完毕...", writer.EquipRunName));
#endregion
}
#endregion
#endregion
}
#endregion
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService<PlcPlanHelper>.Error("把PLC的计划状态更新至数据库异常" + ex.Message, ex);
}
finally
{
Global.PublicVar.Instance.LocalPlanIsRefresh = false;
}
}
#endregion
#region 处理HMI的修改计划请求
/// <summary>
/// 处理HMI的修改计划请求
/// </summary>
public static void ChangePlan()
{
try
{
bool flag = false;
#region 读取并处理HMI的配方添加计划请求添加计划
if (BasePlcHelper.Instance.ChemicalWeighing_HMI_Request_Recipe_Change_ShakeHand.NowValue.ToInt() == 1 && (BasePlcHelper.Instance.ChemicalWeighing_HMI_Request_Recipe_Change_FeedBack.NowValue.ToInt() <= 0 || BasePlcHelper.Instance.ChemicalWeighing_HMI_Request_Recipe_Change_FeedBack.NowValue.ToInt() == 65535))
{
#region 从PLC中获取要添加的计划信息
int planCommand = BasePlcHelper.Instance.ChemicalWeighing_HMI_Request_Recipe_Change_Command.NowValue.ToInt(); //命令
String sysMachine = BasePlcHelper.Instance.ChemicalWeighing_Sys_Machine2.NowValue.ToString(); //机台号
int[] planIdData;
BasePlcHelper.Instance.PlcRead(BasePlcHelper.Instance.ChemicalWeighing_HMIChange_PlanId, out planIdData);
string planId = Mesnac.Basic.DataProcessor.ToString1(planIdData); //计划
int ChangePlanNum = BasePlcHelper.Instance.ChemicalWeighing_HMIChange_PlanNum.NowValue.ToInt(); //设定数量
if (String.IsNullOrEmpty(sysMachine))
{
sysMachine = "01";
}
#endregion
#region 验证(PLC中读取过来的planid对应的数据库中的计划必须是存在的)
RT_Plan currentRT_Plan = Product.PptPlan.PlanHelper.GetPlanDataEntity(planId);
if (currentRT_Plan == null)
{
ChangePlanErrorReturn();
return;
}
#endregion
#region 判断命令0默认 1刷新 2上移 3下移 4修改车数 5删除计划并执行操作
if (planCommand == 1)
{
//刷新事件将最新的计划列表重写入PLC。
}
else if (planCommand == 2)
{
#region 获取交换位置对象并判断是否可以上移(条件:在该条计划前,是否有一条未运行中的计划)并执行上移
int swagPlanSerial = (int)currentRT_Plan.Plan_Serial - 1;
RT_Plan swagRtPLan = Product.PptPlan.PlanHelper.GetPlanBySerialUp(swagPlanSerial);
if (swagRtPLan == null)
{
ChangePlanErrorReturn();
return;
}
else
{
currentRT_Plan.Plan_Serial = currentRT_Plan.Plan_Serial - 1;
swagRtPLan.Plan_Serial = swagRtPLan.Plan_Serial + 1;
Product.PptPlan.PlanHelper.UpdatePlanSerial(currentRT_Plan);
Product.PptPlan.PlanHelper.UpdatePlanSerial(swagRtPLan);
#region 重新下传计划列表
flag = PlanListReDown();
#endregion
}
#endregion
}
else if (planCommand == 3)
{
#region 获取交换位置对象并判断是否可以下移(条件:在该条计划后,是否有一条未运行中的计划)并执行下移
int swagPlanSerial = (int)currentRT_Plan.Plan_Serial + 1;
RT_Plan swagRtPLan = Product.PptPlan.PlanHelper.GetPlanBySerialDown(swagPlanSerial);
if (swagRtPLan == null)
{
ChangePlanErrorReturn();
return;
}
else
{
currentRT_Plan.Plan_Serial = currentRT_Plan.Plan_Serial + 1;
swagRtPLan.Plan_Serial = swagRtPLan.Plan_Serial - 1;
Product.PptPlan.PlanHelper.UpdatePlanSerial(currentRT_Plan);
Product.PptPlan.PlanHelper.UpdatePlanSerial(swagRtPLan);
#region 重新下传计划列表
flag = PlanListReDown();
#endregion
}
#endregion
}
else if (planCommand == 4)
{
#region 执行修改计划操作
Product.PptPlan.PlanHelper.UpdatePlanNum(planId, ChangePlanNum);
#endregion
#region 重新下传计划列表
flag = PlanListReDown();
#endregion
}
else if (planCommand == 5)
{
#region 获取计划号对应的序号
int delSerial = (int)currentRT_Plan.Plan_Serial;
#endregion
#region 执行删除计划操作
Product.PptPlan.PlanHelper.DeletePlan(planId);
#endregion
#region 整理修改后续计划的序号(-1
List<RT_Plan> mRtPlan = Product.PptPlan.PlanHelper.GetPlanListAfterPlanSerial(delSerial);
if (mRtPlan != null && mRtPlan.Count > 0)
{
foreach (RT_Plan tempRT in mRtPlan)
{
tempRT.Plan_Serial = tempRT.Plan_Serial - 1;
Product.PptPlan.PlanHelper.UpdatePlanSerial(tempRT);
}
}
#endregion
#region 将等待计划数写入PLC
WaitPlanNumToPLC();
#endregion
#region 重新下传计划列表
flag = PlanListReDown();
#endregion
}
else
{
//默认
}
#endregion
#region 触发事件
if (OnChangePlan != null)
{
OnChangePlan(sender, System.EventArgs.Empty);
}
#endregion
}
#endregion
#region HMI修改计划请求交互
if (flag)
{
#region 下传请求信号
bool result = BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.ChemicalWeighing_HMI_Request_Recipe_Change_FeedBack, new object[] { 1 });
if (!result)
{
string msg = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_Product_PptPlan_ChangePlan_logContent_1")); //HMI修改计划请求成功后向PLC下传反馈信号失败!
ICSharpCode.Core.LoggingService<PlcPlanHelper>.Error(msg);
}
#endregion
}
#endregion
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService<PlcPlanHelper>.Error("从HMI修改计划异常" + ex.Message, ex);
}
}
#endregion
#region 修改计划出现异常反馈到PLC
public static void ChangePlanErrorReturn()
{
bool result = BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.ChemicalWeighing_HMI_Request_Recipe_Change_FeedBack, new object[] { 3 });
if (!result)
{
string msg = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_Product_PptPlan_ChangePlan_logContent_2")); //HMI修改计划请求成功后向PLC下传异常反馈信号失败!
ICSharpCode.Core.LoggingService<PlcPlanHelper>.Error(msg);
}
}
#endregion
#region 执行完修改计划操作后重新向PLC写入计划列表数据
public static bool PlanListReDown()
{
#region 定义变量
bool reFlag = false;
List<Int16> writeDataList = new List<Int16>();
PropertyInfo[] ps = typeof(RT_Plan).GetProperties();
List<RT_Plan> rtPlanList = Product.PptPlan.PlanHelper.GetPlanEntityListByNum(5);
#endregion
#region 验证处理-配置
if (rtPlanList == null || rtPlanList.Count == 0)
{
string msg = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_Product_PptPlan_AutoAddPlan_logContent_3")); //HMI添加计划请求成功后计划列表获取失败!
ICSharpCode.Core.LoggingService<PlcPlanHelper>.Error(msg);
}
List<PlcWriter> writerList = PlcSchemaHelper.Instance.GetPlcWriter("PlanListData"); //计划列表数据
if (writerList == null || writerList.Count == 0)
{
string msg = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_Product_PptPlan_AutoAddPlan_logContent_4")); //HMI添加计划请求成功后计划列表写入数据结构获取失败!
ICSharpCode.Core.LoggingService<PlcPlanHelper>.Error(msg);
}
#endregion
#region 业务处理
foreach (PlcWriter writer in writerList)
{
writeDataList.Clear();
#region 验证处理
if (writer.EquipRunName != "PlanList_Info")
{
continue;
}
#endregion
#region 获取PLC品牌名称处理西门子与其他PLC差异
string equipBrand = Mesnac.Equips.Factory.Instance.GetEquipBrandByRunName(writer.EquipRunName);
writer.EquipBrand = equipBrand;
#endregion
#region 整理数据
if (writer.EquipRunName == "PlanList_Info")
{
Entity.RT_Plan entity = null;
for (int i = 0; i < writer.ThisCount; i++)
{
if (rtPlanList != null && i < rtPlanList.Count)
{
entity = rtPlanList[i];
DateTime timeNow = DateTime.Now;
foreach (PlcWriteItem item in writer.SchemaList)
{
item.ClearData();
item.EquipBrand = equipBrand; //设置PLC设备品牌
foreach (PropertyInfo pi in ps)
{
if (item.DataFieldName == pi.Name)
{
item.SetValue = pi.GetValue(entity, null);
break;
}
else if (item.EquipRunName == "DownLoad_MachineCode")
{
item.SetValue = "01";
break;
}
}
writeDataList.AddRange(item.WriteData());
}
}
}
}
if (writeDataList.Count < 200)
{
int fillNum = 200 - writeDataList.Count;
for (int i = 0; i < fillNum; i++)
{
writeDataList.AddRange(new short[] { 0 });
}
}
#endregion
#region 下传数据
object[] buff = new object[writeDataList.Count];
Array.Copy(writeDataList.ToArray(), buff, buff.Length);
ICSharpCode.Core.LoggingService<PlcPlanHelper>.Debug(String.Format("[{0}]开始下传计划信息...", writer.EquipRunName));
if (!BasePlcHelper.Instance.PlcWriteByRunName(writer.EquipRunName, buff))
{
string msg = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_Product_PptPlan_AutoAddPlan_logContent_5")); //HMI添加计划请求成功后计划列表写入PLC失败!
ICSharpCode.Core.LoggingService<PlcPlanHelper>.Error(msg);
}
else
{
reFlag = true;
}
ICSharpCode.Core.LoggingService<PlcPlanHelper>.Debug(String.Format("[{0}]计划列表信息下传完毕...", writer.EquipRunName));
#endregion
}
#endregion
return reFlag;
}
#endregion
#region 待计划数写入PLC
public static void WaitPlanNumToPLC()
{
try
{
int waitPlanNum = Product.PptPlan.PlanHelper.GetUnExePlanNum();
#region 向PLC写入等待计划数
object[] tempob = new object[1] { 0 };
tempob[0] = waitPlanNum;
bool result = BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.ChemicalWeighing_Scheduled_Num, tempob);
if (!result)
{
string msg = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_Product_PptPlan_AutoAddPlan_logContent_8")); //向PLC写入等待计划数失败!
ICSharpCode.Core.LoggingService<PlcPlanHelper>.Error(msg);
}
#endregion
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService<PlcPlanHelper>.Error("把等待计划数写入PLC异常" + ex.Message, ex);
}
}
#endregion
#region 终止计划
/// <summary>
/// 终止计划方法
/// </summary>
/// <param name="planSerial">要终止的计划序号</param>
/// <param name="planCommand">终止命令</param>
/// <param name="planNum">要终止的计划数</param>
/// <param name="unFinishedNum">要终止的计划剩余数</param>
/// <param name="isShowMsg">是否方法内显示消息窗口</param>
/// <param name="msgList">输出的消息列表</param>
/// <returns>成功返回true失败返回false</returns>
public static bool ExecStop(int planSerial, int planCommand, int planNum, int unFinishedNum, bool isShowMsg, List<string> msgList)
{
try
{
#region 1 定义变量
List<Int16> writeDataList = new List<Int16>();
#endregion
#region 2 验证处理-配置
List<PlcWriter> writerList = PlcSchemaHelper.Instance.GetPlcWriter("PlanModifyData");
if (writerList == null || writerList.Count == 0)
{
string msg = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_Product_PptPlan_ExecStop_logContent_1")); //终止计划数据结构获取失败!
ICSharpCode.Core.LoggingService<PlcPlanHelper>.Error(msg);
return false;
}
#endregion
#region 3 业务处理
foreach (PlcWriter writer in writerList)
{
writeDataList.Clear();
#region 验证处理
if (writer.EquipRunName != "Plan_To_Plc")
{
continue;
}
#endregion
#region 获取PLC品牌名称处理西门子与其他PLC差异
string equipBrand = Mesnac.Equips.Factory.Instance.GetEquipBrandByRunName(writer.EquipRunName);
writer.EquipBrand = equipBrand;
#endregion
#region 整理数据
if (writer.EquipRunName == "Plan_To_Plc")
{
for (int i = 0; i < writer.ThisCount; i++)
{
foreach (PlcWriteItem item in writer.SchemaList)
{
item.ClearData();
item.EquipBrand = equipBrand;
if (item.EquipRunName == "DownLoad_PlanSerial")
{
item.SetValue = planSerial;
}
else if (item.EquipRunName == "DownLoad_PlanCommand")
{
item.SetValue = planCommand;
}
else if (item.EquipRunName == "DownLoad_PlanNum")
{
item.SetValue = planNum;
}
else if (item.EquipRunName == "DownLoad_RealNum")
{
item.SetValue = unFinishedNum;
}
writeDataList.AddRange(item.WriteData());
}
}
}
#endregion
#region 下传数据
bool result = BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.ChemicalWeighing_Plan_Status_Com_ShakeHand, new object[] { 1 });
if (!result)
{
string msg = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_Product_PptPlan_ExecStop_logContent_3")); //向PLC下传终止计划命令失败!
ICSharpCode.Core.LoggingService<PlcPlanHelper>.Error(msg);
return false;
}
else
{
object[] buff = new object[writeDataList.Count];
Array.Copy(writeDataList.ToArray(), buff, buff.Length);
ICSharpCode.Core.LoggingService<PlcPlanHelper>.Debug(String.Format("[{0}]开始下传终止计划信息...", writer.EquipRunName));
if (!BasePlcHelper.Instance.PlcWriteByRunName(writer.EquipRunName, buff))
{
string msg = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_Product_PptPlan_ExecStop_logContent_2")); //终止计划写入PLC失败!
ICSharpCode.Core.LoggingService<PlcPlanHelper>.Error(msg);
return false;
}
else
{
bool result1 = BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.ChemicalWeighing_Plan_Status_Com_ShakeHand, new object[] { 2 });
if (!result1)
{
string msg = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_Product_PptPlan_ExecStop_logContent_4")); //向PLC下传终止计划命令及数据成功后反馈失败!
ICSharpCode.Core.LoggingService<PlcPlanHelper>.Error(msg);
return false;
}
else
{
int readCount = 0;
while (readCount < 5)
{
if (BasePlcHelper.Instance.ChemicalWeighing_Plan_Status_Com_FeedBack.NowValue.ToInt() == 1)
{
BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.ChemicalWeighing_Plan_Status_Com_ShakeHand, new object[] { 0 });
BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.ChemicalWeighing_Plan_Status_Com_FeedBack, new object[] { 0 });
break;
}
readCount++;
Thread.Sleep(200);
}
}
}
ICSharpCode.Core.LoggingService<PlcPlanHelper>.Debug(String.Format("[{0}]计划列表信息下传完毕...", writer.EquipRunName));
}
#endregion
}
#endregion
#region 4 关闭进度提示
Sys.FrmNotify.Instance.Value = 100;
Sys.FrmNotify.Instance.Destory();
#endregion
return true;
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService<PlcPlanHelper>.Error(ex.Message);
msgList.Add(ex.Message);
return false;
}
finally
{
Sys.FrmNotify.Instance.Destory();
}
}
#endregion
#region 修改计划数
/// <summary>
/// 修改计划数方法
/// </summary>
/// <param name="planID">要终止的计划编号</param>
/// <param name="newPlanNum">新的计划数</param>
/// <param name="isShowMsg">是否方法内显示消息窗口</param>
/// <param name="msgList">输出的消息列表</param>
/// <returns>成功返回true失败返回false</returns>
public static bool ExecModifyPlanNum(int planSerial, int planCommand, int newPlanNum, int unFinishedNum, bool isShowMsg, List<string> msgList)
{
try
{
#region 1 定义变量
List<Int16> writeDataList = new List<Int16>();
#endregion
#region 2 验证处理-配置
List<PlcWriter> writerList = PlcSchemaHelper.Instance.GetPlcWriter("PlanModifyData");
if (writerList == null || writerList.Count == 0)
{
string msg = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_Product_PptPlan_ExecModify_logContent_1")); //上位机修改计划数:数据结构获取失败!
ICSharpCode.Core.LoggingService<PlcPlanHelper>.Error(msg);
return false;
}
#endregion
#region 3 业务处理
foreach (PlcWriter writer in writerList)
{
writeDataList.Clear();
#region 验证处理
if (writer.EquipRunName != "Plan_To_Plc")
{
continue;
}
#endregion
#region 获取PLC品牌名称处理西门子与其他PLC差异
string equipBrand = Mesnac.Equips.Factory.Instance.GetEquipBrandByRunName(writer.EquipRunName);
writer.EquipBrand = equipBrand;
#endregion
#region 整理数据
if (writer.EquipRunName == "Plan_To_Plc")
{
for (int i = 0; i < writer.ThisCount; i++)
{
foreach (PlcWriteItem item in writer.SchemaList)
{
item.ClearData();
item.EquipBrand = equipBrand;
if (item.EquipRunName == "DownLoad_PlanSerial")
{
item.SetValue = planSerial;
}
else if (item.EquipRunName == "DownLoad_PlanCommand")
{
item.SetValue = planCommand;
}
else if (item.EquipRunName == "DownLoad_PlanNum")
{
item.SetValue = newPlanNum;
}
else if (item.EquipRunName == "DownLoad_RealNum")
{
item.SetValue = unFinishedNum;
}
writeDataList.AddRange(item.WriteData());
}
}
}
#endregion
#region 下传数据
object[] buff = new object[writeDataList.Count];
Array.Copy(writeDataList.ToArray(), buff, buff.Length);
ICSharpCode.Core.LoggingService<PlcPlanHelper>.Debug(String.Format("[{0}]开始下传终止计划信息...", writer.EquipRunName));
if (!BasePlcHelper.Instance.PlcWriteByRunName(writer.EquipRunName, buff))
{
string msg = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_Product_PptPlan_ExecModify_logContent_3")); //上位机修改计划数写入PLC失败!
ICSharpCode.Core.LoggingService<PlcPlanHelper>.Error(msg);
return false;
}
else
{
bool result1 = BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.ChemicalWeighing_Plan_Status_Com_ShakeHand, new object[] { 2 });
if (!result1)
{
string msg = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_Product_PptPlan_ExecModify_logContent_4")); //上位机修改计划数向PLC下传修改计划数成功后反馈失败!
ICSharpCode.Core.LoggingService<PlcPlanHelper>.Error(msg);
return false;
}
else
{
int readCount = 0;
while (readCount < 5)
{
if (BasePlcHelper.Instance.ChemicalWeighing_Plan_Status_Com_FeedBack.NowValue.ToInt() == 1)
{
BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.ChemicalWeighing_Plan_Status_Com_ShakeHand, new object[] { 0 });
BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.ChemicalWeighing_Plan_Status_Com_FeedBack, new object[] { 0 });
break;
}
readCount++;
Thread.Sleep(200);
}
}
}
ICSharpCode.Core.LoggingService<PlcPlanHelper>.Debug(String.Format("[{0}]计划列表信息下传完毕...", writer.EquipRunName));
#endregion
}
#endregion
#region 5 关闭进度提示
Sys.FrmNotify.Instance.Value = 100;
Sys.FrmNotify.Instance.Destory();
#endregion
return true;
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService<PlcPlanHelper>.Error(ex.Message);
msgList.Add(ex.Message);
return false;
}
finally
{
Sys.FrmNotify.Instance.Destory();
}
}
#endregion
#region 条码命令处理下传
/// <summary>
/// 条码命令处理下传方法
/// </summary>
/// <param name="codeStr">通过Socket取到的扫码枪条码字符串</param>
/// <returns>成功返回true失败返回false</returns>
public static bool ScanCodeDown(string codeStr)
{
try
{
#region 1 条码处理:取出查找物料等相关信息的字符串。
//string useStr = codeStr.Substring(Global.PublicVar.Instance.ScanCodeStrStart, Global.PublicVar.Instance.ScanCodeStrEnd);
string useStr = codeStr;
if (useStr == null || useStr == string.Empty || useStr == "")
{
bool resultHopper_No1 = BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.ChemicalWeighing_Hopper_No_103, new object[] { 0 });
bool resultScanCommand1 = BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.ChemicalWeighing_ScanCommand, new object[] { 0 });
if (!resultHopper_No1 || !resultScanCommand1)
{
string msg = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_ChemicalWeighingPlc_PlcPlanHelper_ScanCodeDown_Error1")); //没有获取到条码中的有效信息。
ICSharpCode.Core.LoggingService<PlcPlanHelper>.Error(msg);
}
return false;
}
#endregion
#region 2 根据从条码中取出的物料名称获取物料代码
string meterialId = Product.PptPlan.PlanHelper.GetPmt_materialID(useStr);
if (meterialId == null || meterialId == string.Empty || meterialId == "")
{
bool resultHopper_No2 = BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.ChemicalWeighing_Hopper_No_103, new object[] { 0 });
bool resultScanCommand2 = BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.ChemicalWeighing_ScanCommand, new object[] { 0 });
if (!resultHopper_No2 || !resultScanCommand2)
{
string msg = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_ChemicalWeighingPlc_PlcPlanHelper_ScanCodeDown_Error2")); //没有找到对应的物料代码。
ICSharpCode.Core.LoggingService<PlcPlanHelper>.Error(msg);
}
return false;
}
#endregion
#region 3 根据物料代码获取对应的料仓号
int binSerial = Product.PptPlan.PlanHelper.GetPmt_BinSerial(meterialId);
if (binSerial == 0)
{
bool resultHopper_No3 = BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.ChemicalWeighing_Hopper_No_103, new object[] { 0 });
bool resultScanCommand3 = BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.ChemicalWeighing_ScanCommand, new object[] { 0 });
if (!resultHopper_No3 || !resultScanCommand3)
{
string msg = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_ChemicalWeighingPlc_PlcPlanHelper_ScanCodeDown_Error3")); //没有找到对应的料仓号。
ICSharpCode.Core.LoggingService<PlcPlanHelper>.Error(msg);
}
return false;
}
#endregion
#region 4 下传料仓号和命令(失败反馈)
bool resultHopper_No4 = BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.ChemicalWeighing_Hopper_No_103, new object[] { binSerial });
bool resultScanCommand4 = BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.ChemicalWeighing_ScanCommand, new object[] { 1 });
bool resultFromPlc = BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.ChemicalWeighing_Open_door_Command_ShakeHand, new object[] { 2 });
if (!resultHopper_No4 || !resultScanCommand4 || !resultFromPlc)
{
string msg = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_ChemicalWeighingPlc_PlcPlanHelper_ScanCodeDown_Error4")); //料仓号获取成功,下传时失败。
ICSharpCode.Core.LoggingService<PlcPlanHelper>.Error(msg);
}
#endregion
#region 将扫码信息写入数据库报表
LR_BarcodeLog lR_BarcodeLog = new LR_BarcodeLog();
lR_BarcodeLog.Equip_Code = ConfigurationManager.AppSettings["EquipCode"];
lR_BarcodeLog.Scan_Time = DateTime.Now.ToString();
lR_BarcodeLog.Scan_Bar = useStr;
lR_BarcodeLog.Material = Product.PptPlan.PlanHelper.GetPmt_material(meterialId).materialName;
lR_BarcodeLog.Bin = binSerial;
lR_BarcodeLog.Scan_State = 1;
Report.ReportHelper.InsertLR_BarcodeLog(lR_BarcodeLog);
#endregion
return true;
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService<PlcPlanHelper>.Error(ex.Message);
return false;
}
finally
{
Sys.FrmNotify.Instance.Destory();
}
}
#endregion
#region 更新物料名称
/// <summary>
/// 更新物料名称
/// </summary>
public static void UpdateMaterialNameToPlc()
{
try
{
bool flag = false;
#region 更新物料名称及物料代码
if (BasePlcHelper.Instance.ChemicalWeighing_PLC_LoadingConfig.NowValue.ToInt() == 1 && BasePlcHelper.Instance.ChemicalWeighing_PC_LoadingConfig.NowValue.ToInt() == 0)
{
#region 下传物料名称
List<Pmt_Bin> pmt_Bins = Product.PptPlan.PlanHelper.GetAllPmt_Bin();
for (int i = 1; i <= 20; i++)
{
bool findFlag = false;
DataKeyValue ChemicalWeighing_MaterName = new DataKeyValue("MaterName_Bin" + Mesnac.Basic.DataProcessor.FillZero(i.ToString(), 2));
DataKeyValue ChemicalWeighing_MaterCode = new DataKeyValue("MaterCode_Bin" + Mesnac.Basic.DataProcessor.FillZero(i.ToString(), 2));
if (pmt_Bins != null && pmt_Bins.Count > 0)
{
for (int j = 0; j < pmt_Bins.Count; j++)
{
Base_MaterialInfo pmt_Material = Product.PptPlan.PlanHelper.GetPmt_material(pmt_Bins[j].Material_ID);
string binStr = null;
if (pmt_Bins[j] != null && pmt_Bins[j].Bin_Serial != null && pmt_Material != null && pmt_Material.materialName != null)
{
binStr = Mesnac.Basic.DataProcessor.FillZero(pmt_Bins[j].Bin_Serial.ToString(), 2);
if (ChemicalWeighing_MaterName.EquipRunName.Contains(binStr))
{
object[] oblist = Mesnac.Basic.DataProcessor.ToPLCDataArray(pmt_Material.materialName);
object[] oblistCode = Mesnac.Basic.DataProcessor.ToPLCDataArray(pmt_Material.materialId);
BasePlcHelper.Instance.PlcWriteByDataKey(ChemicalWeighing_MaterName, FullObj(oblist));
BasePlcHelper.Instance.PlcWriteByDataKey(ChemicalWeighing_MaterCode, FullObj(oblistCode));
findFlag = true;
break;
}
}
}
if (findFlag == false)
{
object[] oblist = new object[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
object[] oblistCode = new object[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
BasePlcHelper.Instance.PlcWriteByDataKey(ChemicalWeighing_MaterName, oblist);
BasePlcHelper.Instance.PlcWriteByDataKey(ChemicalWeighing_MaterCode, oblistCode);
}
}
}
flag = true;
#endregion
}
#endregion
#region 触发事件
if (flag)
{
#region 下传请求信号
bool result = BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.ChemicalWeighing_PC_LoadingConfig, new object[] { 1 });
if (!result)
{
string msg = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_ChemicalWeighingPlc_PlcPlanHelper_Mix_msg60")); //小料物料名称向PLC下传反馈信号失败!
ICSharpCode.Core.LoggingService<PlcPlanHelper>.Error(msg);
}
#endregion
}
#endregion
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService<PlcPlanHelper>.Error("把PLC的计划状态更新至数据库异常" + ex.Message, ex);
}
finally
{
Global.PublicVar.Instance.LocalPlanIsRefresh = false;
}
}
#endregion
#region 修改PCOnlin心跳信号
public static void HeartBeatChange()
{
if (BasePlcHelper.Instance.ChemicalWeighing_PLC_Online_Flag.NowValue.ToInt() != 0)
{
if (BasePlcHelper.Instance.ChemicalWeighing_PC_Online.NowValue.ToInt() == 1)
{
BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.ChemicalWeighing_PC_Online, new object[] { 0 });
}
else
{
BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.ChemicalWeighing_PC_Online, new object[] { 1 });
}
}
}
#endregion
#region 自动修改班次
public static void shiftIDAutoChange()
{
Global.PublicVar.Instance.globalShiftID = Product.ProductHelper.shiftIDAtuoRefresh();
}
#endregion
public static object[] FullObj(object[] ob)
{
object[] reOb = ob;
if (reOb.Count() < 20)
{
for (int z = reOb.Count(); z < 20; z++)
{
object[] tempo = new object[1] { 0 };
reOb = reOb.Concat(tempo).ToArray();
}
}
return reOb;
}
}
}