电工电气网

当前位置:首页
>>
>>
正文

自己编写的C访问OPC库自动化接口

自己编写的C#访问OPC库(自动化接口)

系统:Windows 7旗舰版软件:Visual Studio2010 SIMATIC_NET_8.2OPC库文件源代码:using System;using System.Collections.Generic;using System.Linq;using System.Text;using OPCAutomation;using System.Net;namespace OPCHelper{ class OPCHelper癫痫病的起因:IDisposable { #region Version 3 #region 变量 private string strHostIP; private string strHostName; private OPCServer opcServer; private OPCGroups opcGroups; private OPCGroup opcGroup; private List<int> itemHandleClient = new List<int>(); private List<int> itemHandleServer = new List<int>(); private List<string> itemNames = new List<string>(); private OPCItems opcItems; private OPCItem opcItem; private string itemNamePreAppend; private Dictionary<string, string> itemValues = new Dictionary<string, string>(); public bool Connected = false; #endregion #region 构造函数 public OPCHelper(string strHostIP, string strHostName, int updateRate) { this.strHostIP = strHostIP; this.strHostName = strHostName; if (!CreatServer()) return; if (!ConnectServer(strHostIP, strHostName)) return; Connected = true; opcGroups = opcServer.OPCGroups; opcGroup = opcGroups.Add("OPCDOTNETGROUP"); SetGroupProperty(opcGroup, updateRate); opcGroup.DataChange += new DIOPCGroupEvent_DataChangeEventHandler(opcGroup_DataChange); opcGroup.AsyncWriteComplete += new DIOPCGroupEvent_AsyncWriteCompleteEventHandler(opcGroup_AsyncWriteComplete); opcItems = opcGroup.OPCItems; } #endregion #region 私有方法 private bool CreatServer() { try { opcServer = new OPCServer(); } catch { return false; } return true; } private bool ConnectServer(string strHostIP, string strHostName) { try { opcServer.Connect(strHostName, strHostIP); } catch { return false; } return true; } private void SetGroupProperty(OPCGroup opcGroup, int updateRate) { opcGroup.IsActive = Convert.ToBoolean(true); opcGroup.DeadBand = 0; opcGroup.UpdateRate = updateRate; opcGroup.IsSubscribed = Convert.ToBoolean(true); } #endregion #region 公有方法 public bool Contains(string itemNameContains) { foreach (string key in itemValues.Keys) { if (key == itemNamePreAppend + itemNameContains) return true; } return false; } public void AddItems(string itemNamePreAppend, string[] itemNamesAdded) { this.itemNamePreAppend = itemNamePreAppend; for (int i = 0; i < itemNamesAdded.Length; i++) { this.itemNames.Add(this.itemNamePreAppend + itemNamesAdded[i]); itemValues.Add(this.itemNamePreAppend + itemNamesAdded[i], ""); } for (int i = 0; i < itemNamesAdded.Length; i++) { itemHandleClient.Add(itemHandleClient.Count != 0 ? itemHandleClient[item呼和浩特癫痫哪里治疗的好啊HandleClient.Count - 1] + 1 : 1); opcItem = opcItems.AddItem(this.itemNamePreAppend + itemNamesAdded[i], itemHandleClient[itemHandleClient.Count - 1]); itemHandleServer.Add(opcItem.ServerHandle); } } public string[] GetItemValues(string[] getValuesItemNames) { string[] getedValues = new string[getValuesItemNames.Length]; for (int i = 0; i < getValuesItemNames.Length; i++) { if (Contains(getValuesItemNames[i])) itemValues.TryGetValue(itemNamePreAppend + getValuesItemNames[i], out getedValues[i]); } return getedValues; } public void AsyncWrite(string[] writeItemNames, string[] writeItemValues) { OPCItem[] bItem = new OPCItem[writeItemNames.Length]; for (int i = 0; i < writeItemNames.Length; i++) { for (int j = 0; j < itemNames.Count; j++) { if (itemNames[j] == itemNamePreAppend + writeItemNames[i]) { bItem[i] = opcItems.GetOPCItem(itemHandleServer[j]); break; } } } int[] temp = new int[writeItemNames.Length + 1]; temp[0] = 0; for (int i = 1; i < writeItemNames.Length + 1; i++) { temp[i] = bItem[i - 1].ServerHandle; } Array serverHandles = (Array)temp; object[] valueTemp = new object[writeItemNames.Length + 1]; valueTemp[0] = ""; for (int i = 1; i < writeItemNames.Length + 1; i++) { valueTemp[i] = writeItemValues[i - 1]; } Array values = (Array)valueTemp; Array Errors; int cancelID; opcGroup.AsyncWrite(writeItemNames.Length, ref serverHandles, ref values, out Errors, 2009, out cancelID); GC.Collect(); } public void SyncWrite(string[] writeItemNames, string[] writeItemValues) { OPCItem[] bItem = new OPCItem[writeItemNames.Length]; for (int i = 0; i < writeItemNames.Length; i++) { for (int j = 0; j < itemNames.Count; j++) { if (itemNames[j] == itemNamePreAppend + writeItemNames[i]) { bItem[i] = opcItems.GetOPCItem(itemHandleServer[j]); break; } } } int[] temp = new int[writeItemNames.Length + 1]; temp[0] = 0; for (int i = 1; i < writeItemNames.Length + 1; i++) { temp[i] = bItem[i - 1].ServerHandle; } Array serverHandles = (Array)temp; object[] valueTemp = new object[writeItemNames.Length + 1]; valueTemp[0] = ""; for (int i = 1; i < writeItemNames.Length + 1; i++) { valueTemp[i] = writeItemValues[i - 1]; } Array values = (Array)valueTemp; Array Errors; opcGroup.SyncWrite(writeItemNames.Length, ref serverHandles, ref values, out Errors); GC.Collect(); } public void Dispose() { if (opcGroup != null) { opcGroup.DataChange -= new DIOPCGroupEvent_DataChangeEventHandler(opcGroup_DataChange); opcGroup.AsyncWriteComplete -= new DIOPCGroupEvent_AsyncWriteCompleteEventHandler(opcGroup_AsyncWriteComplete); } if (opcServer != null) { opcServer.Disconnect(); opcServer = null; } Connected = false; } #endregion #region 事件 void opcGroup_AsyncWriteComplete(int TransactionID, int NumItems, ref Array ClientHandles, ref Array Errors) { // } void opcGroup_DataChange(int TransactionID, int NumItems, ref Array ClientHandles, ref Array ItemValues, ref Array Qualities, ref Array TimeStamps) { for (int i = 1; i <= NumItems; i++) { itemValues[itemNames[Convert.ToInt32(ClientHandles.GetValue(i)) - 1]] = ItemValues.GetValue(i).ToString(); } } #endregion #endregion }}测试图片:

测试代码:using System;using System.Collections.Generic;using Sys汕尾癫痫病医院tem.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Net;namespace OPCHelper{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } OPCHelper oPCHelper; private void Form1_Load(object sender, EventArgs e) { IPHostEntry IPHost = Dns.Resolve(Environment.MachineName); string strIP; strIP = IPHost.AddressList[0].ToString(); oPCHelper = new OPCHelper(strIP, "OPC.SimaticNET",10); oPCHelper.AddItems("S7:[S7 Connection_1]", new string[] {"Start", "Stop", "Start2", "Pump1", "Pump2"}); oPCHelper.AddItems("S7:[S7 Connection_1]", new string[] { "Num1", "Num2", "Num3", "Num4" }); oPCHelper.AddItems("S7:[S7 Connection_1]", new string[] { "DB1,INT266", "DB1,INT268", "DB1,INT270", "DB1,INT272" }); } private void timer1_Tick(object sender, EventArgs e) { if (oPCHelper.Connected) { string str1, str2; str1 = oPCHelper.GetItemValues(new string[] { "Pump1" })[0]; str2 = oPCHelper.GetItemValues(new string[] { "Pump2" })[0]; btn_Pump1.BackColor = str1!=null&&str1.ToLower() == "true" ? Color.Green: Color.Gray; btn_Pump2.BackColor = str2 != null && str2.ToLower() == "true" ? Color.Green : Color.Gray; string[] str3 = oPCHelper.GetItemValues(new string[] { "Num1", "Num2", "Num3", "Num4", "DB1,INT266", "DB1,INT268", "DB1,INT270", "DB1,INT272" }); txt_RNum1.Text=str3[0]; txt_RNum2.Text=str3[1]; txt_RNum3.Text=str3[2]; txt_RNum4.Text=str3[3]; txt_RNum5.Text = 成年人得癫痫的原因str3[4]; txt_RNum6.Text = str3[5]; txt_RNum7.Text = str3[6]; txt_RNum8.Text = str3[7]; } } private void btn_Start_MouseDown(object sender, MouseEventArgs e) { oPCHelper.SyncWrite(new string[] { "Start" }, new string[] { "1" }); } private void btn_Start_MouseUp(object sender, MouseEventArgs e) { oPCHelper.SyncWrite(new string[] { "Start" }, new string[] { "0" }); } private void btn_Stop_MouseDown(object sender, MouseEventArgs e) { oPCHelper.SyncWrite(new string[] { "Stop" }, new string[] { "1" }); } private void btn_Stop_MouseUp(object sender, MouseEventArgs e) { oPCHelper.SyncWrite(new string[] { "Stop" }, new string[] { "0" }); } private void btn_Start2_Click(object sender, EventArgs e) { if (oPCHelper.Contains("Start2")) { string str; str = oPCHelper.GetItemValues(new string[] { "Start2" })[0]; if (Convert.ToBoolean(str)) oPCHelper.SyncWrite(new string[] { "Start2" }, new string[] { "0" }); else oPCHelper.SyncWrite(new string[] { "Start2" }, new string[] { "1" }); } } private void btn_Write_Click(object sender, EventArgs e) { oPCHelper.SyncWrite(new string[] { "Num1", "Num2", "Num3", "Num4" }, new string[] { txt_WNum1.Text, txt_WNum2.Text, txt_WNum3.Text, txt_WNum4.Text }); } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { oPCHelper.Dispose(); } }}