using System; using System.Collections.Generic; using System.Linq; using System.Web; using QMAPP.FJC.Entity.Basic; using System.Configuration; using OPCPLC; using QMAPP.FJC.Entity; using System.Threading; using OPCAutomation; using QMFrameWork.Common.Serialization; using QMAPP.FJC.DAL.Basic; using QMAPP.FJC.BLL.Process; using QMFrameWork.Data; using QMAPP.FJC.Entity.Operation; using OpcProcessWinService.WebServiceForOpc; namespace OpcProcessWinService { public static class ParaInit { #region 变量 //用来存储工控机发起连接服务器建立双通道连接的设备信息 public List connectMachineList; //用来存储生产设备信息 public List machineList; //用来存储OPC配置参数与服务端句柄的对应关系 //key:OPC配置参数,样式为 设备号+":"+模具号+":"+数据库字段 //value:服务端句柄 public Dictionary parameterValueDict = new Dictionary(); /// /// 用来存储客户端句柄与设备参数对应关系 /// key:客户端句柄值 /// value:OPC配置参数,样式为设备号+":"+模具号+":"+数据库字段 /// public Dictionary clientHandleDict = new Dictionary(); //OPC配置参数列表静态变量 public List paraConfigList; //设备模具参数信息静态变量 public List moldList; //OPC连接字典静态变量 public OPCPLCAutomation opcplcConnection; public OPCPLCAutomation MonitorConnection; //客户端句柄参数 private int clientHandleValue = 0; private OpcServiceClient client; #endregion #region 初始化 /// /// 初始化相关信息 /// public static void InitParameter() { moldList = new List(); machineList = new List(); MonitorList = new List(); //opcServer服务器IP string opcServerIP = ConfigurationManager.AppSettings.Get("OPCServerIP"); //opcServer服务名称 string opcServerName = ConfigurationManager.AppSettings.Get("OPCServerName"); try { #region 获取数据库中所有的生产设备信息 client = new OpcServiceClient(); string maincheStr = client.GetMachineList(); //获取所有的设备信息 machineList = JsonConvertHelper.GetDeserialize>(maincheStr); //过滤掉搪塑设备的配置信息 machineList = machineList .Where(o => o.PROCESSTYPE != EnumGeter.ProcessType.tangsu.GetHashCode().ToString()) .ToList(); #endregion #region 获取所有的配置信息 string paraConfigStr = client.GetParaConfigList(); paraConfigList = JsonConvertHelper.GetDeserialize>(paraConfigStr); //过滤掉搪塑设备的配置信息 paraConfigList = paraConfigList .Where(o => o.PROCESSTYPE != EnumGeter.ProcessType.tangsu.GetHashCode().ToString()) .ToList(); #endregion #region 获取设备模块信息 //对加工参数配置信息按照设备及模具信息进行分组 var objlist = from pc in paraConfigList group pc by new { pc.MACHINECODDE, pc.MOLDNUMBER } into g select new ParameterConfig { MACHINECODDE = g.Key.MACHINECODDE, MOLDNUMBER = g.Key.MOLDNUMBER }; //添加到静态变量中 foreach (var ob in objlist) { moldList.Add(new ParameterConfig() { MACHINECODDE = ob.MACHINECODDE, MOLDNUMBER = ob.MOLDNUMBER }); } //按照设备进行排序 moldList = moldList.OrderBy(o => o.MACHINECODDE).ToList(); #endregion #region 建立opc服务连接 opcplcConnection = new OPCPLC.OPCPLCAutomation(); opcplcConnection.GetListOPCServers(opcServerIP); opcplcConnection.ConnectRemoteServer(opcServerName, opcServerIP); MonitorConnection = new OPCPLC.OPCPLCAutomation(); MonitorConnection.GetListOPCServers(opcServerIP); MonitorConnection.ConnectRemoteServer(opcServerName, opcServerIP); MonitorConnection.CreateGroup("Monitor"); MonitorConnection.KepGroups.DefaultGroupIsActive = true;//激活组。 MonitorConnection.KepGroups.DefaultGroupDeadband = 0;// 死区值,设为0时,服务器端该组内任何数据变化都通知组。 MonitorConnection.KepGroups.DefaultGroupUpdateRate = 50;//默认组群的刷新频率为200ms MonitorConnection.KepGroup.UpdateRate = 50;//刷新频率为1秒。 MonitorConnection.KepGroup.IsSubscribed = true;//使用订阅功能,即可以异步,默认false //绑定监控事件 MonitorConnection.KepGroup.DataChange += new DIOPCGroupEvent_DataChangeEventHandler(KepGroup_DataChange); #endregion #region 加载模块下的加工参数配置信息 //变量所有的模具信息 foreach (var m in moldList) { MachineInfo machineEntity = machineList.First(o => o.MACHINECODDE == m.MACHINECODDE); #region 获取加工参数组 //获取该模块下所有的加工参数信息 List paraList = paraConfigList .Where(o => o.MACHINECODDE == m.MACHINECODDE && o.MOLDNUMBER == m.MOLDNUMBER && o.COLUMNTYPE == OpcEnumGeter.COLUMNTYPE.PARAMETER.GetHashCode().ToString() ) .ToList(); if (paraList.Count > 0) { //为该模块创建参数组 opcplcConnection.CreateGroup(m.MACHINECODDE + ":" + m.MOLDNUMBER + ":GetData"); if (opcplcConnection.KepGroups.DefaultGroupUpdateRate == 1000) { opcplcConnection.KepGroups.DefaultGroupUpdateRate = 200; } //变该模块下的所有参数信息 foreach (var pc in paraList) { //初始化字典名称: 设备名称+模块编号+字段名称 string dicKeyStr = machineEntity.MACHINECODDE + ":" + m.MOLDNUMBER + ":" + pc.COLUMNCODE; try { //初始化字段信息值为0 parameterValueDict.Add(dicKeyStr, 0); //获取opc中tagName string itemName = pc.CONNECTIONSTRING; clientHandleValue++; clientHandleDict.Add(clientHandleValue, dicKeyStr); //将字典中的与PLC内存地址向绑定 parameterValueDict[dicKeyStr] = opcplcConnection.AddKepItem(itemName, clientHandleValue); } catch (Exception ex) { //Console.WriteLine(ex.Message); //Console.WriteLine(dicKeyStr); continue; } } } #endregion #region 写入组 //包括写人设备可操作标记和获取加工参数完成标记 List writeList = paraConfigList .Where(o => o.MACHINECODDE == m.MACHINECODDE && o.MOLDNUMBER == m.MOLDNUMBER && ( o.COLUMNTYPE == OpcEnumGeter.COLUMNTYPE.OPERATEFLAG.GetHashCode().ToString() || o.COLUMNTYPE == OpcEnumGeter.COLUMNTYPE.PARAMETERREADED.GetHashCode().ToString() ) ) .ToList(); if (writeList.Count > 0) { if (m.MACHINECODDE == "CASTING") { int i = 1; } opcplcConnection.CreateGroup(m.MACHINECODDE + ":" + m.MOLDNUMBER + ":WriteData"); //变该模块下的所有参数信息 foreach (var pc in writeList) { //初始化字典名称: 设备名称+模块编号+字段名称 string dicKeyStr = machineEntity.MACHINECODDE + ":" + m.MOLDNUMBER + ":" + pc.COLUMNCODE; try { //初始化字段信息值为0 parameterValueDict.Add(dicKeyStr, 0); //获取opc中tagName string itemName = pc.CONNECTIONSTRING; clientHandleValue++; clientHandleDict.Add(clientHandleValue, dicKeyStr); //将字典中的与PLC内存地址向绑定 parameterValueDict[dicKeyStr] = opcplcConnection.AddKepItem(itemName, clientHandleValue); } catch (Exception ex) { //Console.WriteLine(ex.Message); //Console.WriteLine(dicKeyStr); continue; } } } #endregion #region 条码信息 List barcodePara = paraConfigList .Where(o => o.MACHINECODDE == m.MACHINECODDE && o.MOLDNUMBER == m.MOLDNUMBER && o.COLUMNTYPE == OpcEnumGeter.COLUMNTYPE.BARCODE.GetHashCode().ToString()) .ToList(); if (barcodePara.Count > 0) { //创建该设备该模块下的条码组 opcplcConnection.CreateGroup(m.MACHINECODDE + ":" + m.MOLDNUMBER + ":BarCodePara"); //变该模块下的所有参数信息 foreach (var pc in barcodePara) { //初始化字典名称: 设备名称+模块编号+字段名称 string dicKeyStr = machineEntity.MACHINECODDE + ":" + m.MOLDNUMBER + ":" + pc.COLUMNCODE; try { //初始化字段信息值为0 parameterValueDict.Add(dicKeyStr, 0); //获取opc中tagName string itemName = pc.CONNECTIONSTRING; clientHandleValue++; clientHandleDict.Add(clientHandleValue, dicKeyStr); //将字典中的与PLC内存地址向绑定 parameterValueDict[dicKeyStr] = opcplcConnection.AddKepItem(itemName, clientHandleValue); } catch (Exception ex) { //Console.WriteLine(ex.Message); //Console.WriteLine(dicKeyStr); continue; } } } #endregion #region 监控组 //获取设备扫描条码完成标记和设备加工完成标记 List monitorList = paraConfigList .Where(o => o.MACHINECODDE == m.MACHINECODDE && o.MOLDNUMBER == m.MOLDNUMBER && ( o.COLUMNTYPE == OpcEnumGeter.COLUMNTYPE.EQUIPSCANFLAG.GetHashCode().ToString() || o.COLUMNTYPE == OpcEnumGeter.COLUMNTYPE.COMPLETEFLAG.GetHashCode().ToString() ) ) .ToList(); if (monitorList.Count > 0) { //OPCPLCAutomation mouldConnection = new OPCPLCAutomation(); //mouldConnection.GetListOPCServers(opcServerIP); //mouldConnection.ConnectRemoteServer(opcServerName, opcServerIP); //mouldConnection.CreateGroup(m.MACHINECODDE + ":" + m.MOLDNUMBER + ":Monitor"); //mouldConnection.KepGroups.DefaultGroupIsActive = true;//激活组。 //mouldConnection.KepGroups.DefaultGroupDeadband = 0;// 死区值,设为0时,服务器端该组内任何数据变化都通知组。 //mouldConnection.KepGroups.DefaultGroupUpdateRate = 200;//默认组群的刷新频率为200ms //mouldConnection.KepGroup.UpdateRate = 200;//刷新频率为1秒。 //mouldConnection.KepGroup.IsSubscribed = true;//使用订阅功能,即可以异步,默认false ////绑定监控事件 //mouldConnection.KepGroup.DataChange += new DIOPCGroupEvent_DataChangeEventHandler(KepGroup_DataChange); //MonitorConnection.CreateGroup(m.MACHINECODDE + ":" + m.MOLDNUMBER + ":Monitor"); //变该模块下的所有参数信息 foreach (var pc in monitorList) { //初始化字典名称: 设备名称+模块编号+字段名称 string dicKeyStr = machineEntity.MACHINECODDE + ":" + m.MOLDNUMBER + ":" + pc.COLUMNCODE; //Console.WriteLine(dicKeyStr); try { //初始化字段信息值为0 parameterValueDict.Add(dicKeyStr, 0); //获取opc中tagName string itemName = pc.CONNECTIONSTRING; clientHandleValue++; clientHandleDict.Add(clientHandleValue, dicKeyStr); //将字典中的与PLC内存地址向绑定 parameterValueDict[dicKeyStr] = MonitorConnection.AddKepItem(itemName, clientHandleValue); } catch (Exception ex) { //Console.WriteLine(ex.Message); //Console.WriteLine(dicKeyStr); continue; } } //MonitorList.Add(mouldConnection); } #endregion } #endregion } catch (Exception ex) { WriteLog.Write(ex.Message); throw ex; } } #endregion private static void KepGroup_DataChange(int TransactionID, int NumItems, ref Array ClientHandles, ref Array ItemValues, ref Array Qualities, ref Array TimeStamps) { try { for (int i = 1; i <= NumItems; i++) { try { #region 获取被获取的获取数据类型 //获取写入的值 var itemValue = ItemValues.GetValue(i); //获取客户端句柄 int chvalue = Convert.ToInt32(ClientHandles.GetValue(i)); //获取服务端字典key值 string dicKeyStr = clientHandleDict[chvalue]; //获取设备信息 string machCode = dicKeyStr.Substring(0, dicKeyStr.IndexOf(':')); string moldAndColumn = dicKeyStr.Substring(dicKeyStr.IndexOf(':') + 1); //获取模块信息 string moldNumber = moldAndColumn.Substring(0, moldAndColumn.IndexOf(':')); //获取字段名称 string columnCode = dicKeyStr.Substring(dicKeyStr.LastIndexOf(':') + 1); //获取该标记信息 ParameterConfig currentPC = paraConfigList.First(o => o.MACHINECODDE == machCode && o.MOLDNUMBER == moldNumber && o.COLUMNCODE == columnCode); //Console.WriteLine(currentPC.MACHINECODDE + ":" + currentPC.MOLDNUMBER + ":" + currentPC.COLUMNCODE.ToString()); #endregion #region 捕获完成标记 if (currentPC.COLUMNTYPE == OpcEnumGeter.COLUMNTYPE.COMPLETEFLAG.GetHashCode().ToString()) { if (itemValue == null) { continue; } //如果完成标记表示更新了加工参数 //获取所有的加工参数 if ((Boolean)itemValue == true) { //设置延迟2秒 //因为plc刷新频率 //个别情况获取不到值可以适当的延长等待时间 Thread.Sleep(3000); //Console.WriteLine("Catch " + currentPC.MACHINECODDE + ":" + currentPC.MOLDNUMBER + ":" + currentPC.COLUMNCODE + " Finish"); List resultList = new List(); WriteLog.Write("Get Parameter " + System.DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss")); #region 获取该模块下的加工参数 List getParaList = new List(); //获取加工参数和公用参数 List paraList = paraConfigList .Where(o => o.MACHINECODDE == machCode && o.MOLDNUMBER == moldNumber && ( o.COLUMNTYPE == OpcEnumGeter.COLUMNTYPE.PARAMETER.GetHashCode().ToString() ) ) .ToList(); //确定组信息 if (paraList.Count > 0) { opcplcConnection.KepGroup = opcplcConnection.KepGroups.GetOPCGroup(currentPC.MACHINECODDE + ":" + currentPC.MOLDNUMBER.ToString() + ":GetData"); opcplcConnection.KepItems = opcplcConnection.KepGroup.OPCItems; } //获取所有的加工参数 foreach (var parameter in paraList) { try { #region 获取参数 ParameterConfig paraConfig = GetResultParameterConfig(parameter); string item = parameter.MACHINECODDE + ":" + parameter.MOLDNUMBER + ":" + parameter.COLUMNCODE; int paraValue = 0; try { paraValue = parameterValueDict[item]; } catch (Exception ex) { WriteLog.Write("GetParameter Error! parameterValueDict[item]" + item); throw ex; } try { var result = opcplcConnection.ReadtagValue(paraValue); if (result != null) { paraConfig.PARAVALUE = result; } WriteLog.Write(paraConfig.MACHINECODDE + ":" + paraConfig.MOLDNUMBER + ":" + paraConfig.COLUMNCODE + " " + ((result == null) ? " " : result.ToString())); } catch (Exception ex) { WriteLog.Write("GetParameter Error! result" + item); WriteLog.Write(ex.Message); throw ex; } resultList.Add(paraConfig); #endregion //Console.WriteLine(paraConfig.MACHINECODDE + ":" + paraConfig.MOLDNUMBER + ":" + paraConfig.COLUMNCODE + " " + ((result == null) ? " " : result.ToString())); } catch (Exception ex) { WriteLog.Write("GetParameter Error!"); continue; } } #endregion #region 获取该模块下的公共参数 List publicDataList = paraConfigList .Where(o => o.MACHINECODDE == machCode && ( o.COLUMNTYPE == OpcEnumGeter.COLUMNTYPE.PUBLICDATA.GetHashCode().ToString() ) ) .ToList(); if (publicDataList.Count > 0) { opcplcConnection.KepGroup = opcplcConnection.KepGroups.GetOPCGroup(publicDataList[0].MACHINECODDE + ":" + publicDataList[0].MOLDNUMBER.ToString() + ":CommonPara"); opcplcConnection.KepItems = opcplcConnection.KepGroup.OPCItems; foreach (var parameter in publicDataList) { #region 获取参数 ParameterConfig paraConfig = GetResultParameterConfig(parameter); string item = parameter.MACHINECODDE + ":" + parameter.MOLDNUMBER + ":" + parameter.COLUMNCODE; var result = opcplcConnection.ReadtagValue(parameterValueDict[item]); if (result != null) { paraConfig.PARAVALUE = result; } resultList.Add(paraConfig); #endregion //Console.WriteLine(paraConfig.MACHINECODDE + ":" + paraConfig.MOLDNUMBER + ":" + paraConfig.COLUMNCODE + " " + ((result == null) ? " " : result.ToString())); WriteLog.Write(paraConfig.MACHINECODDE + ":" + paraConfig.MOLDNUMBER + ":" + paraConfig.COLUMNCODE + " " + ((result == null) ? " " : result.ToString())); } } #endregion #region 获取条码信息 #endregion #region 处理已经读取完加工参数标记 //目前主要针对浇注设备 //浇注设备读取完加工参数后要将加工参数读取完成标记位修改为true if (paraConfigList.Count(o => o.MACHINECODDE == currentPC.MACHINECODDE && o.MOLDNUMBER == currentPC.MOLDNUMBER && o.COLUMNTYPE == OpcEnumGeter.COLUMNTYPE.PARAMETERREADED.GetHashCode().ToString() ) > 0) { List collectedList = paraConfigList.Where(o => o.MACHINECODDE == currentPC.MACHINECODDE && o.MOLDNUMBER == currentPC.MOLDNUMBER && o.COLUMNTYPE == OpcEnumGeter.COLUMNTYPE.PARAMETERREADED.GetHashCode().ToString()) .ToList(); opcplcConnection.KepGroup = opcplcConnection.KepGroups.GetOPCGroup(collectedList[0].MACHINECODDE + ":" + collectedList[0].MOLDNUMBER.ToString() + ":WriteData"); opcplcConnection.KepItems = opcplcConnection.KepGroup.OPCItems; foreach (var p in collectedList) { //获取对应的OPC操作对象 //获取对应的服务端句柄 //向服务端句柄写入 opcplcConnection.WritetagValue(parameterValueDict[p.MACHINECODDE + ":" + p.MOLDNUMBER + ":" + p.COLUMNCODE], true); //Console.WriteLine("set Collected " + p.MACHINECODDE + ":" + p.MOLDNUMBER + ":" + p.COLUMNCODE + " true"); WriteLog.Write("set Collected " + p.MACHINECODDE + ":" + p.MOLDNUMBER + ":" + p.COLUMNCODE + " true"); } } #endregion #region 冲切设备设置 if (currentPC.PROCESSTYPE == EnumGeter.ProcessType.chongqie.GetHashCode().ToString()) { if (paraConfigList.Count(o => (o.MACHINECODDE == currentPC.MACHINECODDE && o.MOLDNUMBER == currentPC.MOLDNUMBER && o.COLUMNTYPE == EnumGeter.COLUMNTYPE.OPERATEFLAG.GetHashCode().ToString()) && (o.COLUMNCODE == "BARCODEOK" || o.COLUMNCODE == "BARCODENOK")) > 0) { List okList = paraConfigList.Where(o => (o.MACHINECODDE == currentPC.MACHINECODDE && o.MOLDNUMBER == currentPC.MOLDNUMBER && o.COLUMNTYPE == EnumGeter.COLUMNTYPE.OPERATEFLAG.GetHashCode().ToString()) && (o.COLUMNCODE == "BARCODEOK" || o.COLUMNCODE == "BARCODENOK")).ToList(); opcplcConnection.KepGroup = opcplcConnection.KepGroups.GetOPCGroup(currentPC.MACHINECODDE + ":" + currentPC.MOLDNUMBER.ToString() + ":WriteData"); opcplcConnection.KepItems = opcplcConnection.KepGroup.OPCItems; foreach (var p in okList) { //Console.WriteLine("set " + p.MACHINECODDE + ":" + p.MOLDNUMBER + ":" + p.COLUMNCODE + " false"); //向服务端句柄写入 opcplcConnection.WritetagValue(parameterValueDict[p.MACHINECODDE + ":" + p.MOLDNUMBER + ":" + p.COLUMNCODE], false); } } } #endregion #region 将参数信息传递到web服务中 string machineNo = resultList[0].MACHINECODDE; //异步调用服务 client.SubmitParameters(JsonConvertHelper.GetSerializes(resultList)); #endregion #region 加工步骤返回工控机信息 //定义参数信息 ChatEventArgs e = new ChatEventArgs(); //设备编号 e.MachineCode = machCode; //传输参数的类别 e.MessageType = OpcEnumGeter.MESSAGETYPE.PROCESSFINISH.GetHashCode().ToString(); //传输参数的内容 e.MessageContent = ""; OpcService opcService = new OpcService(); opcService.ReturnProductCodeToMachine(e); #endregion } else { //如果完成标记为false //FRIM设备中的铣削设备要手动清除参数读取完成标记 //SB的FRIM工程师 #region 铣削和冷刀设备的处理 if (currentPC.PROCESSTYPE == EnumGeter.ProcessType.xixiao.GetHashCode().ToString() || currentPC.PROCESSTYPE == EnumGeter.ProcessType.lengdaoruohua.GetHashCode().ToString()) { //获取对应的OPC操作对象 opcplcConnection.KepGroup = opcplcConnection.KepGroups.GetOPCGroup( currentPC.MACHINECODDE + ":" + currentPC.MOLDNUMBER + ":WriteData" ); opcplcConnection.KepItems = opcplcConnection.KepGroup.OPCItems; List paraReadList = paraConfigList .Where(o => o.MACHINECODDE == machCode && o.MOLDNUMBER == moldNumber && ( o.COLUMNTYPE == OpcEnumGeter.COLUMNTYPE.PARAMETERREADED.GetHashCode().ToString() || o.COLUMNTYPE == OpcEnumGeter.COLUMNTYPE.OPERATEFLAG.GetHashCode().ToString() ) ) .ToList(); foreach (var p in paraReadList) { //Console.WriteLine("get finish false, set" + p.MACHINECODDE + ":" + p.MOLDNUMBER + ":" + p.COLUMNCODE + " false"); opcplcConnection.WritetagValue(parameterValueDict[p.MACHINECODDE + ":" + p.MOLDNUMBER + ":" + p.COLUMNCODE], false); } } #endregion #region 冲切设备的处理 if (currentPC.PROCESSTYPE == EnumGeter.ProcessType.chongqie.GetHashCode().ToString()) { //获取对应的OPC操作对象 opcplcConnection.KepGroup = opcplcConnection.KepGroups.GetOPCGroup( currentPC.MACHINECODDE + ":" + currentPC.MOLDNUMBER + ":WriteData" ); opcplcConnection.KepItems = opcplcConnection.KepGroup.OPCItems; List paraReadList = paraConfigList .Where(o => o.MACHINECODDE == machCode && o.MOLDNUMBER == moldNumber && ( o.COLUMNTYPE == OpcEnumGeter.COLUMNTYPE.PARAMETERREADED.GetHashCode().ToString() && o.COLUMNCODE == "TRACEDELETE" ) ) .ToList(); foreach (var p in paraReadList) { opcplcConnection.WritetagValue(parameterValueDict[p.MACHINECODDE + ":" + p.MOLDNUMBER + ":" + p.COLUMNCODE], false); } } #endregion } } #endregion #region 捕获条码扫描完成标记 if (currentPC.COLUMNTYPE == OpcEnumGeter.COLUMNTYPE.EQUIPSCANFLAG.GetHashCode().ToString()) { #region 提示扫描条码完成 if (itemValue == null) { continue; } //如果完成标记表示更新了加工参数 //获取所有的加工参数 if ((Boolean)itemValue == true) { Thread.Sleep(1000); //Console.WriteLine("Catch" + currentPC.MACHINECODDE + ":" + currentPC.MOLDNUMBER + ":" + currentPC.COLUMNCODE + " BarcodeCheck"); #region 获取条码信息 bool ishaveBarcode = true; //获取该设备模块下的条码配置信息 List barCodeParaList = paraConfigList .Where(o => o.MACHINECODDE == machCode && o.MOLDNUMBER == moldNumber && o.COLUMNTYPE == OpcEnumGeter.COLUMNTYPE.BARCODE.GetHashCode().ToString() ) .OrderBy(o => o.COLUMNCODE) .ToList(); //确定组信息 if (barCodeParaList.Count > 0) { opcplcConnection.KepGroup = opcplcConnection.KepGroups.GetOPCGroup(currentPC.MACHINECODDE + ":" + currentPC.MOLDNUMBER.ToString() + ":BarCodePara"); opcplcConnection.KepItems = opcplcConnection.KepGroup.OPCItems; } //获取的信息集合 List getParaList = new List(); WriteLog.Write("Get Barcode " + System.DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss")); //循环获取条码信息 foreach (var parameter in barCodeParaList) { try { string item = parameter.MACHINECODDE + ":" + parameter.MOLDNUMBER + ":" + parameter.COLUMNCODE; var result = opcplcConnection.ReadtagValue(parameterValueDict[item]); if (result != null) { parameter.PARAVALUE = result; } //Console.WriteLine("get Barcode " + " " + parameter.MACHINECODDE + ":" + parameter.MOLDNUMBER + ":" + parameter.COLUMNCODE + " " + ((result == null) ? " " : result.ToString())); WriteLog.Write(parameter.MACHINECODDE + ":" + parameter.MOLDNUMBER + ":" + parameter.COLUMNCODE + " " + ((result == null) ? " " : result.ToString())); getParaList.Add(parameter); } catch (Exception ex) { //Console.WriteLine("获取" + parameter.COLUMNCODE + "值异常"); continue; } } if (ishaveBarcode == false) { //Console.WriteLine("Fail to get barcode!"); continue; } #endregion #region 在双攻通信的条件下将扫描的条码信息传送到工控机上 try { //定义参数信息 ChatEventArgs e = new ChatEventArgs(); //设备编号 e.MachineCode = machCode; //传输参数的类别 e.MessageType = OpcEnumGeter.MESSAGETYPE.PRODUCTCODE.GetHashCode().ToString(); //传输参数的内容 e.MessageContent = JsonConvertHelper.GetSerializes(getParaList); OpcService opcService = new OpcService(); opcService.ReturnProductCodeToMachine(e); } catch (Exception ex) { //Console.WriteLine("无法连接设备" + currentPC.MACHINECODDE + "的工控机!"); WriteLog.Write("无法连接设备" + currentPC.MACHINECODDE + "的工控机!"); } #endregion } else { #region 冲切设备做额外的处理 if (currentPC.PROCESSTYPE == EnumGeter.ProcessType.chongqie.GetHashCode().ToString()) { try { List paraWriteList = paraConfigList .Where(o => o.MACHINECODDE == currentPC.MACHINECODDE && o.MOLDNUMBER == currentPC.MOLDNUMBER && ( o.COLUMNTYPE == EnumGeter.COLUMNTYPE.OPERATEFLAG.GetHashCode().ToString() ) ) .ToList(); //获取对应的OPC操作对象 opcplcConnection.KepGroup = opcplcConnection.KepGroups.GetOPCGroup( currentPC.MACHINECODDE + ":" + currentPC.MOLDNUMBER + ":WriteData" ); opcplcConnection.KepItems = opcplcConnection.KepGroup.OPCItems; foreach (var configdata in paraWriteList) { ////Console.WriteLine("set " + configdata.MACHINECODDE + ":" + configdata.MOLDNUMBER + ":" + configdata.COLUMNCODE + " " + configdata.PARAVALUE.ToString()); //向服务端句柄写入 opcplcConnection.WritetagValue(parameterValueDict[configdata.MACHINECODDE + ":" + configdata.MOLDNUMBER + ":" + configdata.COLUMNCODE], false); } } catch (Exception ex) { throw; } } #endregion } #endregion } #endregion #region 中断 if (currentPC.COLUMNTYPE == OpcEnumGeter.COLUMNTYPE.INTERRUPT.GetHashCode().ToString()) { //捕获停止事件 if (itemValue == null) { continue; } if ((Boolean)itemValue == true) { #region 获取该模块下的加工参数 List resultList = new List(); //获取加工参数和公用参数 List paraList = paraConfigList .Where(o => o.MACHINECODDE == machCode && o.MOLDNUMBER == moldNumber && ( o.COLUMNTYPE == OpcEnumGeter.COLUMNTYPE.PARAMETER.GetHashCode().ToString() ) ) .ToList(); //确定组信息 if (paraList.Count > 0) { opcplcConnection.KepGroup = opcplcConnection.KepGroups.GetOPCGroup(currentPC.MACHINECODDE + ":" + currentPC.MOLDNUMBER.ToString() + ":GetData"); opcplcConnection.KepItems = opcplcConnection.KepGroup.OPCItems; } //获取所有的加工参数 foreach (var parameter in paraList) { #region 获取参数 ParameterConfig paraConfig = GetResultParameterConfig(parameter); string item = parameter.MACHINECODDE + ":" + parameter.MOLDNUMBER + ":" + parameter.COLUMNCODE; var result = opcplcConnection.ReadtagValue(parameterValueDict[item]); if (result != null) { paraConfig.PARAVALUE = result; } resultList.Add(paraConfig); #endregion //Console.WriteLine(paraConfig.MACHINECODDE + ":" + paraConfig.MOLDNUMBER + ":" + paraConfig.COLUMNCODE + " " + ((result == null) ? " " : result.ToString())); WriteLog.Write(paraConfig.MACHINECODDE + ":" + paraConfig.MOLDNUMBER + ":" + paraConfig.COLUMNCODE + " " + ((result == null) ? " " : result.ToString())); } #endregion //获取加工结果信息 List getResultList = resultList .Where(o => o.MACHINECODDE == machCode && o.MOLDNUMBER == moldNumber && o.COLUMNTYPE == OpcEnumGeter.COLUMNTYPE.PARAMETER.GetHashCode().ToString() && o.COLUMNCODE.ToUpper() == "RESULT" ) .ToList(); if (getResultList.Count > 0) { string interruptResult = getResultList[0].PARAVALUE.ToString(); if (interruptResult != "OK") { //异步调用服务提交信息 client.SubmitInterruptParameters(JsonConvertHelper.GetSerializes(resultList)); #region 铣削冷刀设备红外焊接的处理 if (currentPC.PROCESSTYPE == EnumGeter.ProcessType.xixiao.GetHashCode().ToString() || currentPC.PROCESSTYPE == EnumGeter.ProcessType.lengdaoruohua.GetHashCode().ToString() || currentPC.PROCESSTYPE == EnumGeter.ProcessType.hongwaihanjie.GetHashCode().ToString()) { //获取对应的OPC操作对象 opcplcConnection.KepGroup = opcplcConnection.KepGroups.GetOPCGroup( currentPC.MACHINECODDE + ":" + currentPC.MOLDNUMBER + ":WriteData" ); opcplcConnection.KepItems = opcplcConnection.KepGroup.OPCItems; List paraReadList = paraConfigList .Where(o => o.MACHINECODDE == machCode && o.MOLDNUMBER == moldNumber && ( o.COLUMNTYPE == OpcEnumGeter.COLUMNTYPE.PARAMETERREADED.GetHashCode().ToString() || o.COLUMNTYPE == OpcEnumGeter.COLUMNTYPE.OPERATEFLAG.GetHashCode().ToString() ) ) .ToList(); foreach (var p in paraReadList) { //Console.WriteLine("get finish false, set" + p.MACHINECODDE + ":" + p.MOLDNUMBER + ":" + p.COLUMNCODE + " false"); opcplcConnection.WritetagValue(parameterValueDict[p.MACHINECODDE + ":" + p.MOLDNUMBER + ":" + p.COLUMNCODE], false); } } #endregion #region 返回信息 //定义参数信息 ChatEventArgs e = new ChatEventArgs(); //设备编号 e.MachineCode = machCode; //传输参数的类别 e.MessageType = OpcEnumGeter.MESSAGETYPE.INTERRUPT.GetHashCode().ToString(); //传输参数的内容 e.MessageContent = ""; OpcService opcService = new OpcService(); opcService.ReturnProductCodeToMachine(e); #endregion } else { #region 铣削冷刀设备红外焊接的处理 if (currentPC.PROCESSTYPE == EnumGeter.ProcessType.xixiao.GetHashCode().ToString() || currentPC.PROCESSTYPE == EnumGeter.ProcessType.lengdaoruohua.GetHashCode().ToString() || currentPC.PROCESSTYPE == EnumGeter.ProcessType.hongwaihanjie.GetHashCode().ToString()) { //获取对应的OPC操作对象 opcplcConnection.KepGroup = opcplcConnection.KepGroups.GetOPCGroup( currentPC.MACHINECODDE + ":" + currentPC.MOLDNUMBER + ":WriteData" ); opcplcConnection.KepItems = opcplcConnection.KepGroup.OPCItems; List paraReadList = paraConfigList .Where(o => o.MACHINECODDE == machCode && o.MOLDNUMBER == moldNumber && ( o.COLUMNTYPE == OpcEnumGeter.COLUMNTYPE.OPERATEFLAG.GetHashCode().ToString() ) ) .ToList(); foreach (var p in paraReadList) { if (p.COLUMNCODE == "BARCODEOK") { opcplcConnection.WritetagValue(parameterValueDict[p.MACHINECODDE + ":" + p.MOLDNUMBER + ":" + p.COLUMNCODE], true); } else { opcplcConnection.WritetagValue(parameterValueDict[p.MACHINECODDE + ":" + p.MOLDNUMBER + ":" + p.COLUMNCODE], false); } } } #endregion } } } } #endregion } catch (Exception ex) { WriteLog.Write(ex.Message); continue; } System.GC.Collect(); } } catch (Exception ex) { WriteLog.Write(ex.Message); } } /// /// 向设备发送可以操作的指令信息 /// /// public static void SendOperateReadyOrder(List configList) { try { //获取对应的OPC操作对象 opcplcConnection.KepGroup = opcplcConnection.KepGroups.GetOPCGroup( configList[0].MACHINECODDE + ":" + configList[0].MOLDNUMBER + ":WriteData" ); opcplcConnection.KepItems = opcplcConnection.KepGroup.OPCItems; foreach (var configdata in configList) { //Console.WriteLine("set " + configdata.MACHINECODDE + ":" + configdata.MOLDNUMBER + ":" + configdata.COLUMNCODE + " " + configdata.PARAVALUE.ToString()); //向服务端句柄写入 opcplcConnection.WritetagValue(parameterValueDict[configdata.MACHINECODDE + ":" + configdata.MOLDNUMBER + ":" + configdata.COLUMNCODE], configdata.PARAVALUE); } } catch (Exception ex) { //Console.WriteLine(ex.Message); //throw; } } /// /// 获取参数信息 /// /// /// private static ParameterConfig GetResultParameterConfig(ParameterConfig parameter) { ParameterConfig paraConfig = new ParameterConfig(); #region 属性复制 paraConfig.COLUMNCODE = parameter.COLUMNCODE; paraConfig.COLUMNTYPE = parameter.COLUMNTYPE; paraConfig.DATATYPE = parameter.DATATYPE; paraConfig.DBNUMBER = parameter.DBNUMBER; paraConfig.DEALTYPE = parameter.DEALTYPE; paraConfig.MACHINECODDE = parameter.MACHINECODDE; paraConfig.MOLDNUMBER = parameter.MOLDNUMBER; paraConfig.OPERATETYPE = parameter.OPERATETYPE; paraConfig.PROCESSTYPE = parameter.PROCESSTYPE; #endregion return paraConfig; } } }