public OPCPLCCMD(string plcnum, OPCServer KepServer) { PLCNUM = plcnum; if (CMD.ContainsKey(plcnum)) { OPCItemPLC_r = CMD[plcnum].OPCItemPLC_r; OPCItemPLC_c = CMD[plcnum].OPCItemPLC_c; KepGroup = CMD[plcnum].KepGroup; } else { OPCGroups KepGroups = KepServer.OPCGroups; KepGroup = KepGroups.Add("Group" + plcnum); KepGroup.UpdateRate = 250; KepGroup.IsActive = true; KepGroup.IsSubscribed = true; OPCItemPLC_r[0] = KepGroup.OPCItems.AddItem("!UI4,PCR_" + plcnum + ",Plc.PVL,Application.USERVARGLOBAL.JXSStatus", 1); OPCItemPLC_r[1] = KepGroup.OPCItems.AddItem("!UI4,PCR_" + plcnum + ",Plc.PVL,Application.USERVARGLOBAL.r_cphigh", 2); OPCItemPLC_r[2] = KepGroup.OPCItems.AddItem("!UI4,PCR_" + plcnum + ",Plc.PVL,Application.USERVARGLOBAL.r_cpnoid", 3); OPCItemPLC_r[3] = KepGroup.OPCItems.AddItem("!UI4,PCR_" + plcnum + ",Plc.PVL,Application.USERVARGLOBAL.r_cpnoid_back", 4); OPCItemPLC_r[4] = KepGroup.OPCItems.AddItem("!UI4,PCR_" + plcnum + ",Plc.PVL,Application.USERVARGLOBAL.r_cpzik", 5); OPCItemPLC_r[5] = KepGroup.OPCItems.AddItem("!UI4,PCR_" + plcnum + ",Plc.PVL,Application.USERVARGLOBAL.r_hang", 6); OPCItemPLC_r[6] = KepGroup.OPCItems.AddItem("!UI4,PCR_" + plcnum + ",Plc.PVL,Application.USERVARGLOBAL.r_lie", 7); OPCItemPLC_r[7] = KepGroup.OPCItems.AddItem("!UI4,PCR_" + plcnum + ",Plc.PVL,Application.USERVARGLOBAL.r_high", 8); OPCItemPLC_r[8] = KepGroup.OPCItems.AddItem("!UI4,PCR_" + plcnum + ",Plc.PVL,Application.USERVARGLOBAL.r_typeid", 9); OPCItemPLC_r[9] = KepGroup.OPCItems.AddItem("!BOOL,PCR_" + plcnum + ",Plc.PVL,Application.USERVARGLOBAL.ruku_in", 10); OPCItemPLC_c[0] = KepGroup.OPCItems.AddItem("!UI4,PCR_" + plcnum + ",Plc.PVL,Application.USERVARGLOBAL.c_cphigh", 1); OPCItemPLC_c[1] = KepGroup.OPCItems.AddItem("!UI4,PCR_" + plcnum + ",Plc.PVL,Application.USERVARGLOBAL.c_orderid", 2); OPCItemPLC_c[2] = KepGroup.OPCItems.AddItem("!UI4,PCR_" + plcnum + ",Plc.PVL,Application.USERVARGLOBAL.c_orderid_back", 3); OPCItemPLC_c[3] = KepGroup.OPCItems.AddItem("!UI4,PCR_" + plcnum + ",Plc.PVL,Application.USERVARGLOBAL.c_cpzik", 4); OPCItemPLC_c[4] = KepGroup.OPCItems.AddItem("!UI4,PCR_" + plcnum + ",Plc.PVL,Application.USERVARGLOBAL.c_hang", 5); OPCItemPLC_c[5] = KepGroup.OPCItems.AddItem("!UI4,PCR_" + plcnum + ",Plc.PVL,Application.USERVARGLOBAL.c_lie", 6); OPCItemPLC_c[6] = KepGroup.OPCItems.AddItem("!UI4,PCR_" + plcnum + ",Plc.PVL,Application.USERVARGLOBAL.c_high", 7); OPCItemPLC_c[7] = KepGroup.OPCItems.AddItem("!UI4,PCR_" + plcnum + ",Plc.PVL,Application.USERVARGLOBAL.c_cpnum", 8); OPCItemPLC_c[8] = KepGroup.OPCItems.AddItem("!UI4,PCR_" + plcnum + ",Plc.PVL,Application.USERVARGLOBAL.c_typeid", 9); OPCItemPLC_c[9] = KepGroup.OPCItems.AddItem("!I2,PCR_" + plcnum + ",Plc.PVL,Application.USERVARGLOBAL.ischuk_lk", 10); OPCItemPLC_c[10] = KepGroup.OPCItems.AddItem("!UI4,PCR_" + plcnum + ",Plc.PVL,Application.USERVARGLOBAL.JXSStatus", 11); CMD.Add(plcnum, this); } }
private void Form1_Load(object sender, EventArgs e) { try { String serIp = "localhost"; //服务器的IP地址 String serverName = "OPC.IwSCP"; //OPC服务器名称 KepServer = new OPCServer(); //连接OPC服务器,opc服务名,ip KepServer.Connect(serverName, serIp); //判断连接状态 if (KepServer.ServerState == (int)OPCServerState.OPCRunning) { textBox1.Text = "已连接到-" + KepServer.ServerName + " "; } else { //这里你可以根据返回的状态来自定义显示信息,请查看自动化接口API文档 textBox1.Text = "状态:" + KepServer.ServerState.ToString() + " "; return; } /*time.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed); * time.Interval = 1000; * time.Start();*/ /*t1 = new NiThread(outtyre, null, "outtyre", 500); * t1.Start();*/ object ItemValues, Qualities, TimeStamps; KepGroups = KepServer.OPCGroups; KepGroup = KepGroups.Add("Group0"); KepGroup.UpdateRate = 250; KepGroup.IsActive = true; KepGroup.IsSubscribed = true; // object ItemValues, Qualities, TimeStamps; //KepGroups = KepServer.OPCGroups; KepGroup2 = KepGroups.Add("Group2"); KepGroup2.UpdateRate = 250; KepGroup2.IsActive = true; KepGroup2.IsSubscribed = true; KepGroup3 = KepGroups.Add("Group3"); KepGroup3.UpdateRate = 250; KepGroup3.IsActive = true; KepGroup3.IsSubscribed = true; KepGroup4 = KepGroups.Add("Group4"); KepGroup4.UpdateRate = 250; KepGroup4.IsActive = true; KepGroup4.IsSubscribed = true; KepItems2 = KepGroup2.OPCItems; //当KepGroup中数据发生改变的触发事件 // KepGroup.DataChange += new DIOPCGroupEvent_DataChangeEventHandler(KepGroup_DataChange); KepItems = KepGroup.OPCItems; KepItems3 = KepGroup3.OPCItems; KepItems4 = KepGroup4.OPCItems; // t = new Thread(outtyre); //t.Start(); /*int[] temp = new int[3]; * temp[0] = 0; * KepItems.AddItem("123456:OPCAE", 1); * KepItems.AddItem("123456:lishile", 2);*/ //OPCItem bItem = KepItems.Item(2); try{ MyItem = new OPCItem[10]; MyItem[0] = KepItems.AddItem("!UI4,PCR_LM2,Plc.PVL,Application.USERVARGLOBAL.JXSStatus", 1); MyItem[1] = KepItems.AddItem("!UI4,PCR_LM2,Plc.PVL,Application.USERVARGLOBAL.r_cphigh", 2); MyItem[2] = KepItems.AddItem("!UI4,PCR_LM2,Plc.PVL,Application.USERVARGLOBAL.r_cpnoid", 3); MyItem[3] = KepItems.AddItem("!UI4,PCR_LM2,Plc.PVL,Application.USERVARGLOBAL.r_cpnoid_back", 4); MyItem[4] = KepItems.AddItem("!UI4,PCR_LM2,Plc.PVL,Application.USERVARGLOBAL.r_cpzik", 5); MyItem[5] = KepItems.AddItem("!UI4,PCR_LM2,Plc.PVL,Application.USERVARGLOBAL.r_hang", 6); MyItem[6] = KepItems.AddItem("!UI4,PCR_LM2,Plc.PVL,Application.USERVARGLOBAL.r_lie", 7); MyItem[7] = KepItems.AddItem("!UI4,PCR_LM2,Plc.PVL,Application.USERVARGLOBAL.r_high", 8); MyItem[8] = KepItems.AddItem("!UI4,PCR_LM2,Plc.PVL,Application.USERVARGLOBAL.r_typeid", 9); MyItem[9] = KepItems.AddItem("!BOOL,PCR_LM2,Plc.PVL,Application.USERVARGLOBAL.ruku_in", 10); MyItem2 = new OPCItem[11]; MyItem2[0] = KepItems2.AddItem("!UI4,PCR_LM2,Plc.PVL,Application.USERVARGLOBAL.c_cphigh", 1); MyItem2[1] = KepItems2.AddItem("!UI4,PCR_LM2,Plc.PVL,Application.USERVARGLOBAL.c_orderid", 2); MyItem2[2] = KepItems2.AddItem("!UI4,PCR_LM2,Plc.PVL,Application.USERVARGLOBAL.c_orderid_back", 3); MyItem2[3] = KepItems2.AddItem("!UI4,PCR_LM2,Plc.PVL,Application.USERVARGLOBAL.c_cpzik", 4); MyItem2[4] = KepItems2.AddItem("!UI4,PCR_LM2,Plc.PVL,Application.USERVARGLOBAL.c_hang", 5); MyItem2[5] = KepItems2.AddItem("!UI4,PCR_LM2,Plc.PVL,Application.USERVARGLOBAL.c_lie", 6); MyItem2[6] = KepItems2.AddItem("!UI4,PCR_LM2,Plc.PVL,Application.USERVARGLOBAL.c_high", 7); MyItem2[7] = KepItems2.AddItem("!UI4,PCR_LM2,Plc.PVL,Application.USERVARGLOBAL.c_cpnum", 8); MyItem2[8] = KepItems2.AddItem("!UI4,PCR_LM2,Plc.PVL,Application.USERVARGLOBAL.c_typeid", 9); MyItem2[9] = KepItems2.AddItem("!I2,PCR_LM2,Plc.PVL,Application.USERVARGLOBAL.ischuk_lk", 10); MyItem2[10] = KepItems2.AddItem("!UI4,PCR_LM2,Plc.PVL,Application.USERVARGLOBAL.JXSStatus", 11); MyItem3 = new OPCItem[10]; MyItem3[0] = KepItems3.AddItem("!UI4,PCR_LM4,Plc.PVL,Application.USERVARGLOBAL.JXSStatus", 1); MyItem3[1] = KepItems3.AddItem("!UI4,PCR_LM4,Plc.PVL,Application.USERVARGLOBAL.r_cphigh", 2); MyItem3[2] = KepItems3.AddItem("!UI4,PCR_LM4,Plc.PVL,Application.USERVARGLOBAL.r_cpnoid", 3); MyItem3[3] = KepItems3.AddItem("!UI4,PCR_LM4,Plc.PVL,Application.USERVARGLOBAL.r_cpnoid_back", 4); MyItem3[4] = KepItems3.AddItem("!UI4,PCR_LM4,Plc.PVL,Application.USERVARGLOBAL.r_cpzik", 5); MyItem3[5] = KepItems3.AddItem("!UI4,PCR_LM4,Plc.PVL,Application.USERVARGLOBAL.r_hang", 6); MyItem3[6] = KepItems3.AddItem("!UI4,PCR_LM4,Plc.PVL,Application.USERVARGLOBAL.r_lie", 7); MyItem3[7] = KepItems3.AddItem("!UI4,PCR_LM4,Plc.PVL,Application.USERVARGLOBAL.r_high", 8); MyItem3[8] = KepItems3.AddItem("!UI4,PCR_LM4,Plc.PVL,Application.USERVARGLOBAL.r_typeid", 9); MyItem3[9] = KepItems3.AddItem("!BOOL,PCR_LM4,Plc.PVL,Application.USERVARGLOBAL.ruku_in", 10); MyItem4 = new OPCItem[11]; MyItem4[0] = KepItems4.AddItem("!UI4,PCR_LM4,Plc.PVL,Application.USERVARGLOBAL.c_cphigh", 1); MyItem4[1] = KepItems4.AddItem("!UI4,PCR_LM4,Plc.PVL,Application.USERVARGLOBAL.c_orderid", 2); MyItem4[2] = KepItems4.AddItem("!UI4,PCR_LM4,Plc.PVL,Application.USERVARGLOBAL.c_orderid_back", 3); MyItem4[3] = KepItems4.AddItem("!UI4,PCR_LM4,Plc.PVL,Application.USERVARGLOBAL.c_cpzik", 4); MyItem4[4] = KepItems4.AddItem("!UI4,PCR_LM4,Plc.PVL,Application.USERVARGLOBAL.c_hang", 5); MyItem4[5] = KepItems4.AddItem("!UI4,PCR_LM4,Plc.PVL,Application.USERVARGLOBAL.c_lie", 6); MyItem4[6] = KepItems4.AddItem("!UI4,PCR_LM4,Plc.PVL,Application.USERVARGLOBAL.c_high", 7); MyItem4[7] = KepItems4.AddItem("!UI4,PCR_LM4,Plc.PVL,Application.USERVARGLOBAL.c_cpnum", 8); MyItem4[8] = KepItems4.AddItem("!UI4,PCR_LM4,Plc.PVL,Application.USERVARGLOBAL.c_typeid", 9); MyItem4[9] = KepItems4.AddItem("!I2,PCR_LM4,Plc.PVL,Application.USERVARGLOBAL.ischuk_lk", 10); MyItem4[10] = KepItems4.AddItem("!UI4,PCR_LM4,Plc.PVL,Application.USERVARGLOBAL.JXSStatus", 11); } catch (Exception ex) { Trace.WriteLine(ex); } /* MyItem[2].Read(1, out ItemValues, out Qualities, out TimeStamps); * aaa = int.Parse(ItemValues.ToString()); * MyItem[3].Read(1, out ItemValues, out Qualities, out TimeStamps); * bbb = int.Parse(ItemValues.ToString()); * if (aaa == 0) * { * aaa = bbb; * } * else * { * bbb = aaa; * } * if (aaa == 0) * { * aaa = 1; * }*/ // lie1 = 5 + (aaa - 1) / 4; //maduo = (aaa - 1) / 4; DataSet myds2 = new DataSet(); string dstr2 = "select * from luntaiweihu where flag = 1"; myds2 = sql.GetDataSet(dstr2, "luntaiweihu"); zikou = int.Parse(myds2.Tables[0].Rows[0]["zikou"].ToString()); higher1 = int.Parse(myds2.Tables[0].Rows[0]["high1"].ToString()); higher2 = int.Parse(myds2.Tables[0].Rows[0]["high2"].ToString()); higher3 = int.Parse(myds2.Tables[0].Rows[0]["high3"].ToString()); higher4 = int.Parse(myds2.Tables[0].Rows[0]["high4"].ToString()); zhuangxiangnumber = int.Parse(myds2.Tables[0].Rows[0]["zhuangxiangnumber"].ToString()); fullnumber = int.Parse(myds2.Tables[0].Rows[0]["number"].ToString()); DataSet myds = new DataSet(); string dstr = "select * from ruku"; myds = sql.GetDataSet(dstr, "ruku"); hang1 = int.Parse(myds.Tables[0].Rows[0]["hang"].ToString()); lie11 = int.Parse(myds.Tables[0].Rows[0]["lie"].ToString()); aaa1 = int.Parse(myds.Tables[0].Rows[0]["aaa"].ToString()); tyrenumber = aaa1 % fullnumber; if (tyrenumber == 0) { tyrenumber = fullnumber; } DataSet myds1 = new DataSet(); string dstr1 = "select * from ruku3"; myds1 = sql.GetDataSet(dstr1, "ruku3"); hang = int.Parse(myds1.Tables[0].Rows[0]["hang"].ToString()); lie1 = int.Parse(myds1.Tables[0].Rows[0]["lie"].ToString()); aaa = int.Parse(myds1.Tables[0].Rows[0]["aaa"].ToString()); tyrenumber1 = aaa % fullnumber; if (tyrenumber1 == 0) { tyrenumber1 = fullnumber; } KepGroup.DataChange += new DIOPCGroupEvent_DataChangeEventHandler(KepGroup_DataChange); KepGroup2.DataChange += new DIOPCGroupEvent_DataChangeEventHandler(KepGroup_DataChange2); KepGroup3.DataChange += new DIOPCGroupEvent_DataChangeEventHandler(KepGroup_DataChange3); KepGroup4.DataChange += new DIOPCGroupEvent_DataChangeEventHandler(KepGroup_DataChange4); } catch (Exception ex) { textBox1.Text = ex.Message; } }