using System; using System.Collections.Generic; using System.Linq; using System.Text; using QMAPP.FJC.Entity.Basic; using OPCPLC; using QMAPP.FJC.DAL.Basic; using QMAPP.FJC.Entity; using System.Configuration; using OPCAutomation; using System.Threading; namespace EQUIPINTERFACETEST { public class WeldTest { //用来存储生产设备信息 public List machineList; //用来存储设备加工参数信息 //key值样式为:设备号+模具号+读写状态+操作类别+数据库字段 public Dictionary parameterValueDict; private Dictionary clientHandleDict = new Dictionary(); private Dictionary colunmConDict = new Dictionary(); private int clientHandleValue = 0; //加工参数列表静态变量 public List paraConfigList; //设备参数信息 public List moldList; //OPC零件信息列表静态变量 public Dictionary opcplcConnectionList; public void InitParameter() { parameterValueDict = new Dictionary(); moldList = new List(); paraConfigList = new List(); machineList = new List(); //获取数据库中所有的生产设备信息 opcplcConnectionList = new Dictionary(); try { #region 获取数据库中所有的生产设备信息 MachineInfoDAL machineDal = new MachineInfoDAL(); machineList = machineDal.GetList(new MachineInfo() { PROCESSTYPE = EnumGeter.ProcessType.hongwaihanjie.GetHashCode().ToString() }); #endregion #region 获取所有的配置信息 ParameterConfigDAL paraConDal = new QMAPP.FJC.DAL.Basic.ParameterConfigDAL(); paraConfigList = paraConDal.GetList(new ParameterConfig() { MACHINECODDE = machineList[0].MACHINECODDE }); #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).OrderBy(o => o.MOLDNUMBER).ToList(); #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 == "0") .ToList(); if (paraList.Count > 0) { OPCPLCAutomation gtxConnection = new OPCPLC.OPCPLCAutomation(); //opcServer服务器IP string opcServerIP = ConfigurationManager.AppSettings.Get("OPCServerIP"); //opcServer服务名称 string opcServerName = ConfigurationManager.AppSettings.Get("OPCServerName"); gtxConnection.GetListOPCServers(opcServerIP); //连接Opc服务 gtxConnection.ConnectRemoteServer(opcServerName, opcServerIP); gtxConnection.CreateGroup(m.MACHINECODDE + "-" + m.MOLDNUMBER + "-GetData"); //变该模块下的所有参数信息 foreach (var pc in paraList) { try { //初始化字典名称: 设备名称+模块编号+字段名称 string dicKeyStr = machineEntity.MACHINECODDE + "-" + m.MOLDNUMBER + "-" + pc.COLUMNCODE; //初始化字段信息值为0 parameterValueDict.Add(dicKeyStr, 0); //获取opc中tagName string itemName = pc.CONNECTIONSTRING; clientHandleValue++; clientHandleDict.Add(clientHandleValue, dicKeyStr); //将字典中的与PLC内存地址向绑定 parameterValueDict[dicKeyStr] = gtxConnection.AddKepItem(itemName, clientHandleValue); colunmConDict.Add(dicKeyStr, m.MACHINECODDE + "-" + m.MOLDNUMBER + "-GetData"); } catch (Exception ex) { throw; } } //添加PLC连接信息 opcplcConnectionList.Add(m.MACHINECODDE + "-" + m.MOLDNUMBER + "-GetData", gtxConnection); } #endregion #region 写入组 List writeList = paraConfigList .Where( o => o.MACHINECODDE == m.MACHINECODDE && o.MOLDNUMBER == m.MOLDNUMBER && (o.COLUMNTYPE == EQUIPINTERFACETESTCommon.COLUMNTYPE.OPERATEFLAG.GetHashCode().ToString() || o.COLUMNTYPE == EQUIPINTERFACETESTCommon.COLUMNTYPE.PARAMETERREADED.GetHashCode().ToString() ) ) .ToList(); if (writeList.Count > 0) { OPCPLCAutomation gtxConnection = new OPCPLC.OPCPLCAutomation(); //opcServer服务器IP string opcServerIP = ConfigurationManager.AppSettings.Get("OPCServerIP"); //opcServer服务名称 string opcServerName = ConfigurationManager.AppSettings.Get("OPCServerName"); gtxConnection.GetListOPCServers(opcServerIP); //连接Opc服务 gtxConnection.ConnectRemoteServer(opcServerName, opcServerIP); gtxConnection.CreateGroup(m.MACHINECODDE + "-" + m.MOLDNUMBER + "-WriteData"); //变该模块下的所有参数信息 foreach (var pc in writeList) { //初始化字典名称: 设备名称+模块编号+字段名称 string dicKeyStr = machineEntity.MACHINECODDE + "-" + m.MOLDNUMBER + "-" + pc.COLUMNCODE; //初始化字段信息值为0 parameterValueDict.Add(dicKeyStr, 0); //获取opc中tagName string itemName = pc.CONNECTIONSTRING; clientHandleValue++; clientHandleDict.Add(clientHandleValue, dicKeyStr); //将字典中的与PLC内存地址向绑定 parameterValueDict[dicKeyStr] = gtxConnection.AddKepItem(itemName, clientHandleValue); colunmConDict.Add(dicKeyStr, m.MACHINECODDE + "-" + m.MOLDNUMBER + "-WriteData"); } //添加PLC连接信息 opcplcConnectionList.Add(m.MACHINECODDE + "-" + m.MOLDNUMBER + "-WriteData", gtxConnection); } #endregion #region 条码信息 List barcodePara = paraConfigList .Where(o => o.MACHINECODDE == m.MACHINECODDE && o.MOLDNUMBER == m.MOLDNUMBER && o.COLUMNTYPE == EQUIPINTERFACETESTCommon.COLUMNTYPE.BARCODE.GetHashCode().ToString()) .ToList(); if (barcodePara.Count > 0) { OPCPLCAutomation gtxConnection = new OPCPLC.OPCPLCAutomation(); //opcServer服务器IP string opcServerIP = ConfigurationManager.AppSettings.Get("OPCServerIP"); //opcServer服务名称 string opcServerName = ConfigurationManager.AppSettings.Get("OPCServerName"); gtxConnection.GetListOPCServers(opcServerIP); //连接Opc服务 gtxConnection.ConnectRemoteServer(opcServerName, opcServerIP); gtxConnection.CreateGroup(m.MACHINECODDE + "-" + m.MOLDNUMBER + "-BarCodePara"); //变该模块下的所有参数信息 foreach (var pc in barcodePara) { try { //初始化字典名称: 设备名称+模块编号+字段名称 string dicKeyStr = machineEntity.MACHINECODDE + "-" + m.MOLDNUMBER + "-" + pc.COLUMNCODE; //初始化字段信息值为0 parameterValueDict.Add(dicKeyStr, 0); //获取opc中tagName string itemName = pc.CONNECTIONSTRING; clientHandleValue++; clientHandleDict.Add(clientHandleValue, dicKeyStr); //将字典中的与PLC内存地址向绑定 parameterValueDict[dicKeyStr] = gtxConnection.AddKepItem(itemName, clientHandleValue); colunmConDict.Add(dicKeyStr, m.MACHINECODDE + "-" + m.MOLDNUMBER + "-BarCodePara"); } catch (Exception ex) { Console.WriteLine(ex.Message); throw; } } //添加PLC连接信息 opcplcConnectionList.Add(m.MACHINECODDE + "-" + m.MOLDNUMBER + "-BarCodePara", gtxConnection); } #endregion #region 公共参数组 List commonPara = paraConfigList .Where(o => o.MACHINECODDE == m.MACHINECODDE && o.MOLDNUMBER == m.MOLDNUMBER && o.COLUMNTYPE == EQUIPINTERFACETESTCommon.COLUMNTYPE.PUBLICDATA.GetHashCode().ToString()) .ToList(); if (commonPara.Count > 0) { OPCPLCAutomation gtxConnection = new OPCPLC.OPCPLCAutomation(); //opcServer服务器IP string opcServerIP = ConfigurationManager.AppSettings.Get("OPCServerIP"); //opcServer服务名称 string opcServerName = ConfigurationManager.AppSettings.Get("OPCServerName"); gtxConnection.GetListOPCServers(opcServerIP); //连接Opc服务 gtxConnection.ConnectRemoteServer(opcServerName, opcServerIP); gtxConnection.CreateGroup(m.MACHINECODDE + "-" + m.MOLDNUMBER + "-CommonPara"); //变该模块下的所有参数信息 foreach (var pc in commonPara) { try { //初始化字典名称: 设备名称+模块编号+字段名称 string dicKeyStr = machineEntity.MACHINECODDE + "-" + m.MOLDNUMBER + "-" + pc.COLUMNCODE; //初始化字段信息值为0 parameterValueDict.Add(dicKeyStr, 0); //获取opc中tagName string itemName = pc.CONNECTIONSTRING; clientHandleValue++; clientHandleDict.Add(clientHandleValue, dicKeyStr); //将字典中的与PLC内存地址向绑定 parameterValueDict[dicKeyStr] = gtxConnection.AddKepItem(itemName, clientHandleValue); colunmConDict.Add(dicKeyStr, m.MACHINECODDE + "-" + m.MOLDNUMBER + "-CommonPara"); } catch (Exception ex) { Console.WriteLine(ex.Message); throw; } } //添加PLC连接信息 opcplcConnectionList.Add(m.MACHINECODDE + "-" + m.MOLDNUMBER + "-CommonPara", gtxConnection); } #endregion #region 监控组 //获取设备扫描条码完成标记和设备加工完成标记 List monitorList = paraConfigList .Where(o => o.MACHINECODDE == m.MACHINECODDE && o.MOLDNUMBER == m.MOLDNUMBER && ( o.COLUMNTYPE == EQUIPINTERFACETESTCommon.COLUMNTYPE.EQUIPSCANFLAG.GetHashCode().ToString() || o.COLUMNTYPE == EQUIPINTERFACETESTCommon.COLUMNTYPE.COMPLETEFLAG.GetHashCode().ToString() || o.COLUMNTYPE == EQUIPINTERFACETESTCommon.COLUMNTYPE.INTERRUPT.GetHashCode().ToString())) .ToList(); if (monitorList.Count > 0) { OPCPLCAutomation gtxConnection = new OPCPLC.OPCPLCAutomation(); //opcServer服务器IP string opcServerIP = ConfigurationManager.AppSettings.Get("OPCServerIP"); //opcServer服务名称 string opcServerName = ConfigurationManager.AppSettings.Get("OPCServerName"); gtxConnection.GetListOPCServers(opcServerIP); //连接Opc服务 gtxConnection.ConnectRemoteServer(opcServerName, opcServerIP); gtxConnection.CreateGroup(m.MACHINECODDE + "-" + m.MOLDNUMBER + "-Monitor"); //变该模块下的所有参数信息 foreach (var pc in monitorList) { //初始化字典名称: 设备名称+模块编号+字段名称 string dicKeyStr = machineEntity.MACHINECODDE + "-" + m.MOLDNUMBER + "-" + pc.COLUMNCODE; //初始化字段信息值为0 parameterValueDict.Add(dicKeyStr, 0); //获取opc中tagName string itemName = pc.CONNECTIONSTRING; clientHandleValue++; clientHandleDict.Add(clientHandleValue, dicKeyStr); //将字典中的与PLC内存地址向绑定 parameterValueDict[dicKeyStr] = gtxConnection.AddKepItem(itemName, clientHandleValue); colunmConDict.Add(dicKeyStr, m.MACHINECODDE + "-" + m.MOLDNUMBER + "-Monitor"); } gtxConnection.KepGroups.DefaultGroupIsActive = true;//激活组。 gtxConnection.KepGroups.DefaultGroupDeadband = 0;// 死区值,设为0时,服务器端该组内任何数据变化都通知组。 gtxConnection.KepGroups.DefaultGroupUpdateRate = 200;//默认组群的刷新频率为200ms gtxConnection.KepGroup.UpdateRate = 200;//刷新频率为1秒。 gtxConnection.KepGroup.IsSubscribed = true;//使用订阅功能,即可以异步,默认false gtxConnection.KepGroup.DataChange += new DIOPCGroupEvent_DataChangeEventHandler(KepGroup_DataChange); //添加PLC连接信息 opcplcConnectionList.Add(m.MACHINECODDE + "-" + m.MOLDNUMBER + "-Monitor", gtxConnection); } #endregion } #endregion Console.WriteLine("Finish loading basedata!"); } catch (Exception ex) { } } private void KepGroup_DataChange(int TransactionID, int NumItems, ref Array ClientHandles, ref Array ItemValues, ref Array Qualities, ref Array TimeStamps) { try { //延迟一秒 Thread.Sleep(1000); //为了测试,所以加了控制台的输出,来查看事物ID号 //Console.WriteLine("********"+TransactionID.ToString()+"*********"); for (int i = 1; i <= NumItems; i++) { //获取写入的值 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); //完成标记 if (currentPC.COLUMNTYPE == EQUIPINTERFACETESTCommon.COLUMNTYPE.COMPLETEFLAG.GetHashCode().ToString()) { #region try { if (itemValue == null) { Console.WriteLine(currentPC.COLUMNCODE.ToString() + " value is null"); WriteLog.Write(currentPC.COLUMNCODE.ToString() + " value is null"); continue; } //如果完成标记表示更新了加工参数 //获取所有的加工参数 if ((Boolean)itemValue == true) { Console.WriteLine(System.DateTime.Now.ToLongTimeString() + " catch FINISH,the value is true"); WriteLog.Write(System.DateTime.Now.ToLongTimeString() + " catch FINISH,the value is true"); List paraList = paraConfigList .Where(o => o.MACHINECODDE == machCode && o.MOLDNUMBER == moldNumber && ( o.COLUMNTYPE == OpcHostEnumGeter.COLUMNTYPE.PARAMETER.GetHashCode().ToString() ) ) .OrderBy(o => o.COLUMNCODE) .ToList(); foreach (var parameter in paraList) { try { string item = parameter.MACHINECODDE + "-" + parameter.MOLDNUMBER + "-" + parameter.COLUMNCODE; string conListKey = colunmConDict[item]; OPCPLCAutomation con = opcplcConnectionList[conListKey]; var result = con.ReadtagValue(parameterValueDict[item]); if (parameter.COLUMNCODE.Contains("HOUR") || parameter.COLUMNCODE.ToUpper().Contains("MINUTE")) { Console.WriteLine(parameter.COLUMNCODE + ":" + ConvertClass.DecToHex(result.ToString())); WriteLog.Write(parameter.COLUMNCODE + ":" + ConvertClass.DecToHex(result.ToString())); } else { Console.WriteLine(parameter.COLUMNCODE + ":" + result.ToString()); WriteLog.Write(parameter.COLUMNCODE + ":" + result.ToString()); } } catch (Exception) { Console.WriteLine("获取" + parameter.COLUMNCODE + "值异常"); WriteLog.Write("获取" + parameter.COLUMNCODE + "值异常"); continue; } } List paraReadList = paraConfigList .Where(o => o.MACHINECODDE == machCode && o.MOLDNUMBER == moldNumber && ( o.COLUMNTYPE == OpcHostEnumGeter.COLUMNTYPE.PARAMETERREADED.GetHashCode().ToString() ) ) .ToList(); foreach (var p in paraReadList) { Console.WriteLine(System.DateTime.Now.ToLongTimeString() + " set Data_Colleted,the value is true"); WriteLog.Write(System.DateTime.Now.ToLongTimeString() + " set Data_Colleted,the value is true"); opcplcConnectionList["WELDING-1-WriteData"].WritetagValue(parameterValueDict[p.MACHINECODDE + "-" + p.MOLDNUMBER + "-" + p.COLUMNCODE], true); } } } catch (Exception ex) { WriteLog.Write(ex.Message ); continue; } #endregion } else if (currentPC.COLUMNTYPE == EQUIPINTERFACETESTCommon.COLUMNTYPE.EQUIPSCANFLAG.GetHashCode().ToString()) { #region 提示扫描条码完成 if (itemValue == null) { Console.WriteLine(currentPC.COLUMNCODE.ToString() + "值为null"); WriteLog.Write(currentPC.COLUMNCODE.ToString() + "值为null"); continue; } //如果完成标记表示更新了加工参数 //获取所有的加工参数 if ((Boolean)itemValue == true) { bool ishaveBarcode = true; Console.WriteLine("--------------Start to Weld-----------------"); WriteLog.Write("---------------Start to Weld----------------"); Console.WriteLine(System.DateTime.Now.ToLongTimeString() + " Three barcode scaned!"); WriteLog.Write(System.DateTime.Now.ToLongTimeString() + " Three barcode scaned!"); List paraList = paraConfigList .Where(o => o.MACHINECODDE == machCode && o.MOLDNUMBER == moldNumber && ( o.COLUMNTYPE == EQUIPINTERFACETESTCommon.COLUMNTYPE.BARCODE.GetHashCode().ToString() ) ) .OrderBy(o=>o.COLUMNCODE) .ToList(); foreach (var parameter in paraList) { try { string item = parameter.MACHINECODDE + "-" + parameter.MOLDNUMBER + "-" + parameter.COLUMNCODE; string conListKey = colunmConDict[item]; OPCPLCAutomation con = opcplcConnectionList[conListKey]; var result = con.ReadtagValue(parameterValueDict[item]); Console.WriteLine(parameter.COLUMNCODE + ":" + result.ToString()); WriteLog.Write(parameter.COLUMNCODE + ":" + result.ToString()); if (string.IsNullOrEmpty(result.ToString().Trim())) { ishaveBarcode = false; } } catch (Exception ex) { Console.WriteLine("获取" + parameter.COLUMNCODE + "值异常"); WriteLog.Write("获取" + parameter.COLUMNCODE + "值异常"); continue; } } if (ishaveBarcode == false) { Console.WriteLine("Fail to get barcode!"); WriteLog.Write("Fail to get barcode!"); continue; } List paraWriteList = paraConfigList .Where(o => o.MACHINECODDE == machCode && o.MOLDNUMBER == moldNumber && ( o.COLUMNTYPE == OpcHostEnumGeter.COLUMNTYPE.OPERATEFLAG.GetHashCode().ToString() ) ) .ToList(); string resultOk = ConfigurationManager.AppSettings["ResultValue"]; //resultOk = "NOK"; if (resultOk == "OK") { ParameterConfig p = paraWriteList.First(o => o.COLUMNCODE == "BARCODEOK"); Console.WriteLine("set BARCODEOK value true"); WriteLog.Write("set BARCODEOK value true"); opcplcConnectionList["WELDING-1-WriteData"].WritetagValue(parameterValueDict[p.MACHINECODDE + "-" + p.MOLDNUMBER + "-" + p.COLUMNCODE], true); ParameterConfig p2 = paraWriteList.First(o => o.COLUMNCODE == "BARCODENOK"); Console.WriteLine("set BARCODENOK value false"); WriteLog.Write("set BARCODEOK value false"); opcplcConnectionList["WELDING-1-WriteData"].WritetagValue(parameterValueDict[p2.MACHINECODDE + "-" + p2.MOLDNUMBER + "-" + p2.COLUMNCODE], false); } else { Console.WriteLine("welding interupted last time"); WriteLog.Write("welding interupted last time"); ParameterConfig p = paraWriteList.First(o => o.COLUMNCODE == "BARCODEOK"); Console.WriteLine("set BARCODEOK value false"); WriteLog.Write("set BARCODEOK value false"); opcplcConnectionList["WELDING-1-WriteData"].WritetagValue(parameterValueDict[p.MACHINECODDE + "-" + p.MOLDNUMBER + "-" + p.COLUMNCODE], false); ParameterConfig p2 = paraWriteList.First(o => o.COLUMNCODE == "BARCODENOK"); Console.WriteLine("set BARCODENOK value true"); WriteLog.Write("set BARCODEOK value true"); opcplcConnectionList["WELDING-1-WriteData"].WritetagValue(parameterValueDict[p2.MACHINECODDE + "-" + p2.MOLDNUMBER + "-" + p2.COLUMNCODE], true); } } #endregion } else { //捕获停止事件 if (itemValue == null) { Console.WriteLine(currentPC.COLUMNCODE.ToString() + "值为null"); WriteLog.Write(currentPC.COLUMNCODE.ToString() + "值为null"); continue; } if ((Boolean)itemValue == true) { Console.WriteLine("catch INTERUPT,the value is true"); WriteLog.Write("catch INTERUPT,the value is true"); var reuslt = opcplcConnectionList["WELDING-1-GetData"].ReadtagValue(parameterValueDict["WELDING-1-RESULT"]); List paraWriteList = paraConfigList .Where(o => o.MACHINECODDE == machCode && o.MOLDNUMBER == moldNumber && ( o.COLUMNTYPE == OpcHostEnumGeter.COLUMNTYPE.OPERATEFLAG.GetHashCode().ToString() ) ) .ToList(); ParameterConfig p = paraWriteList.First(o => o.COLUMNCODE == "BARCODEOK"); ParameterConfig p2 = paraWriteList.First(o => o.COLUMNCODE == "BARCODENOK"); if (reuslt.ToString().Trim() == "OK") { ConfigurationManager.AppSettings.Set("ResultValue", "OK"); Console.WriteLine("get RESULT ,the value is OK"); WriteLog.Write("get RESULT ,the value is OK"); } else { ConfigurationManager.AppSettings.Set("ResultValue", "NOK"); Console.WriteLine("get RESULT ,the value is NOK"); WriteLog.Write("get RESULT ,the value is NOK"); } } } } } catch (Exception ex) { Console.WriteLine("系统出现异常:" + ex.Message); } } public void Test() { //hour = ConvertClass.DecToHex(result.ToString()); List list = paraConfigList .Where(o => o.COLUMNTYPE == "8") .ToList(); foreach (var parameter in list) { try { string item = parameter.MACHINECODDE + "-" + parameter.MOLDNUMBER + "-" + parameter.COLUMNCODE; //string conListKey = colunmConDict[item]; OPCPLCAutomation con = new OPCPLCAutomation(); if (parameter.COLUMNTYPE == "0") { con = opcplcConnectionList["WELDING-1-GetData"]; } else { con = opcplcConnectionList["WELDING-1-BarCodePara"]; } var result = con.ReadtagValue(parameterValueDict[item]); if (result != null) { string a = result.ToString().Trim(); Console.WriteLine(item.ToString() + "值为" + result.ToString().Trim()); if (parameter.COLUMNCODE.Contains("HOUR")) { Console.WriteLine(item.ToString() + "值为" + ConvertClass.DecToHex(result.ToString())); } } else { Console.WriteLine(item.ToString() + ":"); } } catch (Exception ex) { Console.WriteLine("Exception:" + parameter.COLUMNCODE); Console.WriteLine(ex.Message); continue; } } } } }