using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Web; using OPCAutomation; using Webservice; namespace WebService { public class OPCHelper { #region 变量 public string strHostIP; public string strHostName; public OPCServer opcServer; public OPCGroups opcGroups; public OPCGroup opcGroup1; public OPCItems opcItems1; public OPCItem[] opcItemm1; public OPCGroup opcGroup2; public OPCItems opcItems2; public OPCItem[] opcItemm2; public OPCGroup opcGroup3; public OPCItems opcItems3; public OPCItem[] opcItemm3; public OPCGroup opcGroup4; public OPCItems opcItems4; public OPCItem[] opcItemm4; public OPCGroup opcGroup5; public OPCItems opcItems5; public OPCItem[] opcItemm5; public string[] ItemIDs; object ItemValues; object Qualities; object TimeStamps; public bool Connected = false; #endregion public bool CreateServer() { try { opcServer = new OPCServer(); } catch (Exception ex) { LogHelper.WriteErrLogBase(ex.ToString(), MethodBase.GetCurrentMethod().Name); return false; } return true; } public bool ConnectServer(string strHostIP, string strHostName) { try { opcServer.Connect(strHostName, strHostIP); } catch (Exception ex) { LogHelper.WriteErrLogBase(ex.ToString(), MethodBase.GetCurrentMethod().Name); return false; } return true; } public void SetGroupProperty(OPCGroup opcGroup, int updateRate) { opcGroup.IsActive = true; opcGroup.DeadBand = 0; opcGroup.UpdateRate = updateRate; opcGroup.IsSubscribed = true; } public object ReadSingleValueFromOPC(OPCItem[] item, int i) { object ItemValues; object Qualities; object TimeStamps; item[i].Read(1, out ItemValues, out Qualities, out TimeStamps); if ((ItemValues != null) && (Qualities.ToString() != "Good")) { return ItemValues.ToString(); } return null; } public bool Disconnect() { try { opcServer.OPCGroups.RemoveAll(); opcServer.Disconnect(); return true; } catch (Exception ex) { LogHelper.WriteErrLogBase(ex.ToString(), MethodBase.GetCurrentMethod().Name); return false; } } } }