实例介绍
自己项目中关于C#与OPC的通讯
【实例截图】
【核心代码】
using System; using System.Collections.Generic; using System.Text; using OPCAutomation; using System.Runtime.InteropServices; using System.Windows.Forms; namespace WindowsApplication1 { sealed class OPC { [DllImport("kernel32.dll")] public static extern int GetTickCount(); #region OPC连接的变量 string ServerName = "OPC.SimaticNET"; // ‘OPC的类型 //string NodeName = "soft"; //string ip = "";//'结点名,即计算机名 //string OPCWay = "S7:[ccc1|VFD1|S7ONLINE]"; //'Step7 用 string OPCWay = "S7:[S7 connection_1]"; // Simatic用 const int RI50 = 170; const int RI51 = 232; const int RI52 = 40; const int RI53 = 52; const int RI54 = 150; const int RI55 = 120; const int RI56 = 80; const int RI57 = 120; const int RI58 = 200; const int RI59 = 120; const int RI61 = 90; const int RI63 = 364; const int RI65 = 8; const int RI66 = 50; const int RI90 = 340; //static object DBD; int i; ///OPCServer MyOPCServer; //public OPCGroups MyOPCGroups; OPCGroup OPCGroup50; OPCGroup OPCGroup51; OPCGroup OPCGroup52; OPCGroup OPCGroup53; OPCGroup OPCGroup54; OPCGroup OPCGroup55; OPCGroup OPCGroup56; OPCGroup OPCGroup57; OPCGroup OPCGroup58; OPCGroup OPCGroup59; OPCGroup OPCGroup61; OPCGroup OPCGroup63; OPCGroup OPCGroup65; OPCGroup OPCGroup66; OPCGroup OPCGroup90; public static OPCItems OPCItemColl50; public static OPCItems OPCItemColl51; public static OPCItems OPCItemColl52; public static OPCItems OPCItemColl53; public static OPCItems OPCItemColl54; public static OPCItems OPCItemColl55; public static OPCItems OPCItemColl56; public static OPCItems OPCItemColl57; public static OPCItems OPCItemColl58; public static OPCItems OPCItemColl59; public static OPCItems OPCItemColl61; public static OPCItems OPCItemColl63; public static OPCItems OPCItemColl65; public static OPCItems OPCItemColl66; public static OPCItems OPCItemColl90; //Array ServerHandlesOut; //Array ErrorsOut; // Array ReadItemLb50 = Array.CreateInstance(typeof(String), new int[] { RI50 }, new int[] { 1 }); string[] ReadItemLbl50 = new string[RI50 1]; string[] ReadItemLbl51 = new string[RI51 1]; string[] ReadItemLbl52 = new string[RI52 1]; string[] ReadItemLbl53 = new string[RI53 1]; string[] ReadItemLbl54 = new string[RI54 1]; string[] ReadItemLbl55 = new string[RI55 1]; string[] ReadItemLbl56 = new string[RI56 1]; string[] ReadItemLbl57 = new string[RI57 1]; string[] ReadItemLbl58 = new string[RI58 1]; string[] ReadItemLbl59 = new string[RI59 1]; string[] ReadItemLbl61 = new string[RI61 1]; string[] ReadItemLbl63 = new string[RI63 1]; string[] ReadItemLbl65 = new string[RI65 1]; string[] ReadItemLbl66 = new string[RI66 1]; string[] ReadItemLbl90 = new string[RI90 1]; //Array ClientHandles50=Array.CreateInstance(typeof(string), new int[]{RI50}, new int[]{1}); int[] ClientHandles50 = new int[RI50 1]; int[] ClientHandles51 = new int[RI51 1]; int[] ClientHandles52 = new int[RI52 1]; int[] ClientHandles53 = new int[RI53 1]; int[] ClientHandles54 = new int[RI54 1]; int[] ClientHandles55 = new int[RI55 1]; int[] ClientHandles56 = new int[RI56 1]; int[] ClientHandles57 = new int[RI57 1]; int[] ClientHandles58 = new int[RI58 1]; int[] ClientHandles59 = new int[RI59 1]; int[] ClientHandles61 = new int[RI61 1]; int[] ClientHandles63 = new int[RI63 1]; int[] ClientHandles65 = new int[RI65 1]; int[] ClientHandles66 = new int[RI66 1]; int[] ClientHandles90 = new int[RI90 1]; #endregion #region 程序方法实现初始化部分 private void InitRItemLbl50() { int i = 0; int DBXH = 0; int DBXL = 0; do { ClientHandles50[i] = i; i = i 1; ReadItemLbl50[i] = OPCWay "DB50," DBXH "." DBXL; DBXL = DBXL 1; if (DBXL > 7) { DBXL = 0; DBXH = DBXH 1; } //splash.ProgressBar.Value = i; } while (i < RI50); } private void InitRItemLbl51() { int i = 0; int DBXH = 0; int DBXL = 0; do { ClientHandles51[i] = i; i = i 1; ReadItemLbl51[i] = OPCWay "DB51," DBXH "." DBXL; DBXL = DBXL 1; if (DBXL > 7) { DBXL = 0; DBXH = DBXH 1; } // splash.ProgressBar.Value = i; } while (i < RI51); } private void InitRItemLbl52() { int i = 0; int DBXH = 0; int DBXL = 0; do { ClientHandles52[i] = i; i = i 1; ReadItemLbl52[i] = OPCWay "DB52," DBXH "." DBXL; DBXL = DBXL 1; if (DBXL > 7) { DBXL = 0; DBXH = DBXH 1; } //splash.ProgressBar.Value = i; } while (i < RI52); } private void InitRItemLbl53() { int i = 0; int DBD = 0; do { ClientHandles53[i] = i; i = i 1; ReadItemLbl53[i] = OPCWay "DB53,W" DBD; DBD = DBD 2; //splash.ProgressBar.Value = i; } while (i < RI53); } private void InitRItemLbl54() { int i = 0; int DBD = 0; do { ClientHandles54[i] = i; i = i 1; ReadItemLbl54[i] = OPCWay "DB54,REAL" DBD; DBD = DBD 4; //splash.ProgressBar.Value = i; } while (i < 65); //ReadItemLbl54[i] = OPCWay "DB54,DINT" DBD - 4; i = 65; DBD = 258; do { ClientHandles54[i] = i; i = i 1; ReadItemLbl54[i] = OPCWay "DB54,REAL" DBD; DBD = DBD 4; //splash.ProgressBar.Value = i; } while (i < RI54); } private void InitRItemLbl55() { int i = 0; int DBD = 0; do { ClientHandles55[i] = i; i = i 1; ReadItemLbl55[i] = OPCWay "DB55,REAL" DBD; DBD = DBD 4; //splash.ProgressBar.Value = i; } while (i < RI55); } private void InitRItemLbl56() { int i = 0; int DBD = 0; do { ClientHandles56[i] = i; i = i 1; ReadItemLbl56[i] = OPCWay "DB56,REAL" DBD; DBD = DBD 4; //splash.ProgressBar.Value = i; } while (i < RI56); } private void InitRItemLbl57() { int i = 0; int DBD = 0; do { ClientHandles57[i] = i; i = i 1; ReadItemLbl57[i] = OPCWay "DB57,REAL" DBD; DBD = DBD 4; // splash.ProgressBar.Value = i; } while (i < RI57); } private void InitRItemLbl58() { int i = 0; int DBD = 0; do { ClientHandles58[i] = i; i = i 1; ReadItemLbl58[i] = OPCWay "DB58,REAL"; DBD = DBD 4; //splash.ProgressBar.Value = i; } while (i < RI58); } private void InitRItemLbl59() { int i = 0; int DBD = 0; do { ClientHandles59[i] = i; i = i 1; ReadItemLbl59[i] = OPCWay "DB59,REAL" DBD; DBD = DBD 4; //splash.ProgressBar.Value = i; } while (i < RI59); } private void InitRItemLbl61() { int i = 0; int DBXH = 0; int DBXL = 0; do { ClientHandles61[i] = i; i = i 1; ReadItemLbl61[i] = OPCWay "DB61," DBXH "." DBXL; DBXL = DBXL 1; if (DBXL > 7) { DBXL = 0; DBXH = DBXH 1; } // splash.ProgressBar.Value = i; } while (i < RI61); } private void InitRItemLbl63() { int i = 0; int DBXH = 0; int DBXL = 0; do { ClientHandles63[i] = i; i = i 1; ReadItemLbl63[i] = OPCWay "DB63," DBXH "." DBXL; DBXL = DBXL 1; if (DBXL > 7) { DBXL = 0; DBXH = DBXH 1; } // splash.ProgressBar.Value = i; } while (i < RI63); } private void InitRItemLbl65() { int i = 0; int DBD = 0; do { ClientHandles65[i] = i; i = i 1; ReadItemLbl65[i] = OPCWay "DB65,REAL" DBD; DBD = DBD 4; //splash.ProgressBar.Value = i; } while (i < RI65); } private void InitRItemLbl66() { int i = 0; int DBXH = 0; int DBXL = 0; do { ClientHandles66[i] = i; i = i 1; ReadItemLbl66[i] = OPCWay "DB66," DBXH "." DBXL; DBXL = DBXL 1; if (DBXL > 7) { DBXL = 0; DBXH = DBXH 1; } // splash.ProgressBar.Value = i; } while (i < RI66); } private void InitRItemLbl90() { int i = 0; int DBXH = 0; int DBXL = 0; do { ClientHandles90[i] = i; i = i 1; ReadItemLbl90[i] = OPCWay "DB90," DBXH "." DBXL; DBXL = DBXL 1; if (DBXL > 7) { DBXL = 0; DBXH = DBXH 1; } //splash.ProgressBar.Value = i; } while (i < RI90); } #endregion #region method public void ReadOPC() { //int ItemNum; //long TarnscationID; //long CanceID; //检测一段代码运行时间间隔用的 long Btime; long Etime; //TarnscationID = 1; //初始化所有 //Formsplash splash = new Formsplash(); // splash.timer1.Enabled = true; // splash.ProgressBar.Minimum = 1; // splash.ProgressBar.Maximum = 400; // splash.ProgressBar.Value = 2; InitRItemLbl50(); InitRItemLbl51(); InitRItemLbl52(); InitRItemLbl53(); InitRItemLbl54(); InitRItemLbl55(); InitRItemLbl56(); InitRItemLbl57(); InitRItemLbl58(); InitRItemLbl59(); InitRItemLbl61(); // InitRItemLbl63 InitRItemLbl65(); InitRItemLbl66(); InitRItemLbl90(); //生成OPC对象, OPCServer MyOPCServer; OPCGroups MyOPCGroups; long serverstate; MyOPCServer = new OPCServer(); MyOPCServer.Connect(ServerName, null); serverstate = MyOPCServer.ServerState; MessageBox.Show("the server state is " serverstate); MyOPCGroups = MyOPCServer.OPCGroups; //MyOPCGroups = (OPCGroups)tmpg; //OPCGroups MyOPCGroups = (OPCGroups)Marshal.CreateWrapperOfType(tmpg, typeof(OPCGroups)); //tmpg = null; // MyOPCGroups=MyOPCServer.OPCGroups; // MyOPCGroups = new OPCGroups(); MyOPCServer.OPCGroups.DefaultGroupIsActive = true; OPCGroup50 = MyOPCGroups.Add("OPCGroup50"); OPCGroup51 = MyOPCGroups.Add("OPCGroup51"); OPCGroup52 = MyOPCGroups.Add("OPCGroup52"); OPCGroup53 = MyOPCGroups.Add("OPCGroup53"); OPCGroup54 = MyOPCGroups.Add("OPCGroup54"); OPCGroup55 = MyOPCGroups.Add("OPCGroup55"); OPCGroup56 = MyOPCGroups.Add("OPCGroup56"); OPCGroup57 = MyOPCGroups.Add("OPCGroup57"); OPCGroup58 = MyOPCGroups.Add("OPCGroup58"); OPCGroup59 = MyOPCGroups.Add("OPCGroup59"); OPCGroup61 = MyOPCGroups.Add("OPCGroup61"); OPCGroup63 = MyOPCGroups.Add("OPCGroup63"); OPCGroup65 = MyOPCGroups.Add("OPCGroup65"); OPCGroup66 = MyOPCGroups.Add("OPCGroup66"); OPCGroup90 = MyOPCGroups.Add("OPCGroup90"); OPCItemColl50 = OPCGroup50.OPCItems; OPCItemColl51 = OPCGroup51.OPCItems; OPCItemColl52 = OPCGroup52.OPCItems; OPCItemColl53 = OPCGroup53.OPCItems; OPCItemColl54 = OPCGroup54.OPCItems; OPCItemColl55 = OPCGroup55.OPCItems; OPCItemColl56 = OPCGroup56.OPCItems; OPCItemColl57 = OPCGroup57.OPCItems; OPCItemColl58 = OPCGroup58.OPCItems; OPCItemColl59 = OPCGroup59.OPCItems; OPCItemColl61 = OPCGroup61.OPCItems; OPCItemColl63 = OPCGroup63.OPCItems; OPCItemColl65 = OPCGroup65.OPCItems; OPCItemColl66 = OPCGroup66.OPCItems; OPCItemColl90 = OPCGroup90.OPCItems; //byte[] myBytes = Array.CreateInstance(typeof(byte), new int[]{32}, new int[]{1}); Btime = GetTickCount(); // OPCGroup50.IsSubscribed = true; //OPCGroup50.IsActive = true; // OPCItemColl50.AddItems(RI50, ref ReadItemLbl50, ref ClientHandles50, out ServerHandlesOut, out ErrorsOut,"VT_EMPTY",""); /* OPCItemColl51.AddItems(RI51, ref ReadItemLbl51, ref ClientHandles51, out ServerHandlesOut, out ErrorsOut, "VT_EMPTY", ""); OPCGroup51.IsSubscribed = true; OPCItemColl52.AddItems(RI52, ref ReadItemLbl52, ref ClientHandles52, out ServerHandlesOut, out ErrorsOut, "VT_EMPTY", ""); OPCGroup52.IsSubscribed = true; OPCItemColl53.AddItems(RI53, ref ReadItemLbl53, ref ClientHandles53, out ServerHandlesOut, out ErrorsOut, "VT_EMPTY", ""); OPCGroup53.IsSubscribed = true; OPCItemColl54.AddItems(RI54, ref ReadItemLbl54, ref ClientHandles54, out ServerHandlesOut, out ErrorsOut, "VT_EMPTY", ""); OPCGroup54.IsSubscribed = true; OPCItemColl55.AddItems(RI55, ref ReadItemLbl55, ref ClientHandles55, out ServerHandlesOut, out ErrorsOut, "VT_EMPTY", ""); OPCGroup55.IsSubscribed = true; OPCItemColl56.AddItems(RI56, ref ReadItemLbl56, ref ClientHandles56, out ServerHandlesOut, out ErrorsOut, "VT_EMPTY", ""); OPCGroup56.IsSubscribed = true; OPCItemColl57.AddItems(RI57, ref ReadItemLbl57, ref ClientHandles57, out ServerHandlesOut, out ErrorsOut, "VT_EMPTY", ""); OPCGroup57.IsSubscribed = true; OPCItemColl58.AddItems(RI58, ref ReadItemLbl58, ref ClientHandles58, out ServerHandlesOut, out ErrorsOut, "VT_EMPTY", ""); OPCGroup58.IsSubscribed = true; OPCItemColl59.AddItems(RI59, ref ReadItemLbl59, ref ClientHandles59, out ServerHandlesOut, out ErrorsOut, "VT_EMPTY", ""); OPCGroup59.IsSubscribed = true; OPCItemColl61.AddItems(RI61, ref ReadItemLbl61, ref ClientHandles61, out ServerHandlesOut, out ErrorsOut, "VT_EMPTY", ""); OPCGroup61.IsSubscribed = true; OPCItemColl63.AddItems(RI63, ref ReadItemLbl63, ref ClientHandles63, out ServerHandlesOut, out ErrorsOut, "VT_EMPTY", ""); OPCGroup63.IsSubscribed = true; OPCItemColl65.AddItems(RI65, ref ReadItemLbl65, ref ClientHandles65, out ServerHandlesOut, out ErrorsOut, "VT_EMPTY", ""); OPCGroup65.IsSubscribed = true; OPCItemColl66.AddItems(RI66, ref ReadItemLbl66, ref ClientHandles66, out ServerHandlesOut, out ErrorsOut, "VT_EMPTY", ""); OPCGroup66.IsSubscribed = true; OPCItemColl90.AddItems(RI90, ref ReadItemLbl90, ref ClientHandles90, out ServerHandlesOut, out ErrorsOut, "VT_EMPTY", ""); OPCGroup90.IsSubscribed = true; *///激活组 OPCGroup50.IsSubscribed = true; OPCGroup50.IsActive = true; OPCGroup51.IsSubscribed = true; OPCGroup51.IsActive = true; OPCGroup52.IsActive = true; OPCGroup52.IsSubscribed = true; OPCGroup53.IsActive = true; OPCGroup53.IsSubscribed = true; OPCGroup54.IsSubscribed = true; OPCGroup54.IsSubscribed = true; OPCGroup55.IsActive = true; OPCGroup55.IsSubscribed = true; OPCGroup56.IsActive = true; OPCGroup56.IsSubscribed = true; OPCGroup57.IsActive = true; OPCGroup57.IsSubscribed = true; OPCGroup58.IsActive = true; OPCGroup58.IsSubscribed = true; OPCGroup59.IsActive = true; OPCGroup59.IsSubscribed = true; OPCGroup61.IsActive = true; OPCGroup61.IsSubscribed = true; OPCGroup63.IsActive = true; OPCGroup63.IsSubscribed = true; OPCGroup65.IsActive = true; OPCGroup65.IsSubscribed = true; OPCGroup66.IsActive = true; OPCGroup66.IsSubscribed = true; OPCGroup90.IsActive = true; OPCGroup90.IsSubscribed = true; for (int i = 1; i < RI50; i ) { OPCItemColl50.AddItem(ReadItemLbl50[i], ClientHandles50[i]); } //初始化OCP连接 for (int i = 1; i < RI51; i ) { OPCItemColl51.AddItem(ReadItemLbl51[i], ClientHandles51[i]); } Etime = GetTickCount(); if (Etime - Btime > 5000) { goto HANDLEeRROR; } for (int i = 1; i < RI52; i ) { OPCItemColl52.AddItem(ReadItemLbl52[i], ClientHandles52[i]); } for (int i = 1; i < RI53; i ) { OPCItemColl53.AddItem(ReadItemLbl53[i], ClientHandles53[i]); } for (int i = 1; i < RI54; i ) { OPCItemColl54.AddItem(ReadItemLbl54[i], ClientHandles54[i]); } for (int i = 1; i < RI55; i ) { OPCItemColl55.AddItem(ReadItemLbl55[i], ClientHandles55[i]); } for (i = 1; i > RI56; i ) { OPCItemColl56.AddItem(ReadItemLbl56[i], ClientHandles56[i]); } for (int i = 1; i < RI57; i ) { OPCItemColl57.AddItem(ReadItemLbl57[i], ClientHandles57[i]); } for (i = 1; i > RI58; i ) { OPCItemColl58.AddItem(ReadItemLbl58[i], ClientHandles58[i]); } for (int i = 1; i < RI59; i ) { OPCItemColl59.AddItem(ReadItemLbl59[i], ClientHandles59[i]); } for (int i = 1; i > RI61; i ) { OPCItemColl61.AddItem(ReadItemLbl61[i], ClientHandles61[i]); } for (i = 1; i > RI63; i ) { OPCItemColl63.AddItem(ReadItemLbl63[i], ClientHandles63[i]); } for (int i = 1; i < RI65; i ) { OPCItemColl65.AddItem(ReadItemLbl65[i], ClientHandles65[i]); } for (int i = 1; i < RI66; i ) { OPCItemColl66.AddItem(ReadItemLbl66[i], ClientHandles66[i]); } for (int i = 1; i < RI90; i ) { OPCItemColl90.AddItem(ReadItemLbl90[i], ClientHandles90[i]); } MessageBox.Show("complete OK!"); return; //splash.ProgressBar.Maximum = 400; HANDLEeRROR: { //splash.ProgressBar.Maximum = 400; return; } } public void QuanlitiConnection() { if (GetQualityText(OPCItemColl51.Item(15).Quality) != "GOOD") //取的转盘变频器运行点的质量 { //FormMain main = new FormMain(); // main.AniLineConnection.Visible = true; } else { //F//ormMain main = new FormMain(); // main.AniLineConnection.Visible = false; } // if (1 != 1) { // FormMain main = new FormMain(); // main.AniLineConnection.Visible =true; // return; // } } } public string GetQualityText(int Quality) { string quality; switch (Quality) { case 0: quality = "BAD! Check You Connection"; break; case 64: quality = "UNCERTAIN"; break; case 192: quality = "GOOD"; break; case 8: quality = "NOT_CONNECTED"; break; case 13: quality = "DEVICE_FAILURE"; break; case 16: quality = "SENSOR_FAILURE"; break; case 20: quality = "LAST_KNOWN"; break; case 24: quality = "COMM_FAILURE"; break; case 28: quality = "OUT_OF_SERVICE"; break; case 132: quality = "LAST_USABLE"; break; case 144: quality = "SENSOR_CAL"; break; case 148: quality = "EGU_EXCEEDED"; break; case 152: quality = "SUB_NORMAL"; break; case 216: quality = "LOCAL_OVERRIDE"; break; default: quality = "UNKNOWN ERROR"; break; } return quality; } #endregion } }
标签:
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论