public bool BindToPLC() { opcServer = new OPCServer(); if (!opcServer.Connect()) { return(false); } if (!opcServer.AddGroup()) { return(false); } int client = 1; if (commandDB != null) { OpcRcw.Da.OPCITEMDEF[] commandItem = new OPCITEMDEF[commandDB.Length]; for (int i = 0; i < commandDB.Length; i++) { commandItem[i].szAccessPath = ""; commandItem[i].bActive = 1; commandItem[i].hClient = client; commandItem[i].dwBlobSize = 1; commandItem[i].pBlob = IntPtr.Zero; commandItem[i].vtRequestedDataType = (int)VarEnum.VT_BSTR; commandItem[i].szItemID = string.Format("S7:[S7 connection_1]{0}", commandDB[i]); client++; } if (!opcServer.AddItems(commandItem, commandHandle)) { return(false); } } isBindToPLC = true; return(isBindToPLC); }
/// <summary> /// 初始化,绑定PLC /// </summary> public void BindToPLC() { OpcRcw.Da.OPCITEMDEF[] Items = new OPCITEMDEF[1]; Items[0].szAccessPath = ""; Items[0].bActive = 1; Items[0].hClient = 1; Items[0].dwBlobSize = 1; Items[0].pBlob = IntPtr.Zero; Items[0].vtRequestedDataType = (int)VarEnum.VT_BSTR; if (commandDB.Length > 2) { Items[0].szItemID = string.Format("S7:[S7 connection_1]{0}", commandDB); //状态DB opcServer.AddItems(Items, commandHandle); } if (returnDB.Length > 2) { Items[0].szItemID = string.Format("S7:[S7 connection_1]{0}", returnDB); //状态DB opcServer.AddItems(Items, returnHandle); } if (controlDB.Length > 2) { Items[0].vtRequestedDataType = (int)VarEnum.VT_UI2; Items[0].szItemID = string.Format("S7:[S7 connection_1]{0}", controlDB); //状态DB opcServer.AddItems(Items, controlHandle); } if (failureDB.Length > 2) { Items[0].szItemID = string.Format("S7:[S7 connection_1]{0}", failureDB); //状态DB opcServer.AddItems(Items, failureHandle); } if (loadDB.Length > 2) { if (statusType == "MJTD") { Items[0].vtRequestedDataType = (int)VarEnum.VT_UI2; } else { Items[0].vtRequestedDataType = (int)VarEnum.VT_BSTR; } Items[0].szItemID = string.Format("S7:[S7 connection_1]{0}", loadDB); //状态DB opcServer.AddItems(Items, loadHandle); } isBindToPLC = true; }
public override bool BindToPLC() { //同步OPC opcServer = new OPCServer(); if (!opcServer.Connect()) { return(false); } if (!opcServer.AddGroup()) { return(false); } int client = 1; if (controlDB != null) { OpcRcw.Da.OPCITEMDEF[] controlitem = new OPCITEMDEF[controlDB.Length]; for (int i = 0; i < controlDB.Length; i++) { controlitem[i].szAccessPath = ""; controlitem[i].bActive = 1; controlitem[i].hClient = client; controlitem[i].dwBlobSize = 1; controlitem[i].pBlob = IntPtr.Zero; controlitem[i].vtRequestedDataType = (int)VarEnum.VT_BSTR; controlitem[i].szItemID = string.Format("S7:[S7 connection_1]{0}", controlDB[i]); //状态DB client++; } if (!opcServer.AddItems(controlitem, controlHandle)) { return(false); } } //if (failureDB != null) //{ // OpcRcw.Da.OPCITEMDEF[] failureitem = new OPCITEMDEF[failureDB.Length]; // for (int i = 0; i < failureDB.Length; i++) // { // failureitem[i].szAccessPath = ""; // failureitem[i].bActive = 1; // failureitem[i].hClient = client; // failureitem[i].dwBlobSize = 1; // failureitem[i].pBlob = IntPtr.Zero; // failureitem[i].vtRequestedDataType = (int)VarEnum.VT_BSTR; // failureitem[i].szItemID = string.Format("S7:[S7 connection_1]{0}", failureDB[i]); //状态DB // client++; // } // if (!opcServer.AddItems(failureitem, failureHandle)) return false; //} //异步OPC if (!BindToPLCAsyn()) { return(false); } isBindToPLC = true; return(isBindToPLC); }
public Int64 lastRefreshTicks; //保存上次刷新的时间,当两次刷新时间间隔较小(小于100ms)时,不进行刷新。 public virtual bool BindToPLC() { opcServer = new OPCServer(); if (!opcServer.Connect()) { return(false); } if (!opcServer.AddGroup()) { return(false); } int client = 1; if (commandDB != null) { OpcRcw.Da.OPCITEMDEF[] commandItem = new OPCITEMDEF[commandDB.Length]; for (int i = 0; i < commandDB.Length; i++) { commandItem[i].szAccessPath = ""; commandItem[i].bActive = 1; commandItem[i].hClient = client; commandItem[i].dwBlobSize = 1; commandItem[i].pBlob = IntPtr.Zero; commandItem[i].vtRequestedDataType = (int)VarEnum.VT_BSTR; commandItem[i].szItemID = string.Format("S7:[S7 connection_1]{0}", commandDB[i]); //状态DB client++; } if (!opcServer.AddItems(commandItem, commandHandle)) { return(false); } } if (returnDB != null) { OpcRcw.Da.OPCITEMDEF[] returnitem = new OPCITEMDEF[returnDB.Length]; for (int i = 0; i < returnDB.Length; i++) { returnitem[i].szAccessPath = ""; returnitem[i].bActive = 1; returnitem[i].hClient = client; returnitem[i].dwBlobSize = 1; returnitem[i].pBlob = IntPtr.Zero; returnitem[i].vtRequestedDataType = (int)VarEnum.VT_BSTR; returnitem[i].szItemID = string.Format("S7:[S7 connection_1]{0}", returnDB[i]); //状态DB client++; } if (!opcServer.AddItems(returnitem, returnHandle)) { return(false); } } if (controlDB != null) { OpcRcw.Da.OPCITEMDEF[] controlitem = new OPCITEMDEF[controlDB.Length]; for (int i = 0; i < controlDB.Length; i++) { controlitem[i].szAccessPath = ""; controlitem[i].bActive = 1; controlitem[i].hClient = client; controlitem[i].dwBlobSize = 1; controlitem[i].pBlob = IntPtr.Zero; controlitem[i].vtRequestedDataType = (int)VarEnum.VT_BSTR; controlitem[i].szItemID = string.Format("S7:[S7 connection_1]{0}", controlDB[i]); //状态DB client++; } if (!opcServer.AddItems(controlitem, controlHandle)) { return(false); } } if (statusDB != null) { OpcRcw.Da.OPCITEMDEF[] statusitem = new OPCITEMDEF[statusDB.Length]; for (int i = 0; i < statusDB.Length; i++) { statusitem[i].szAccessPath = ""; statusitem[i].bActive = 1; statusitem[i].hClient = client; statusitem[i].dwBlobSize = 1; statusitem[i].pBlob = IntPtr.Zero; statusitem[i].vtRequestedDataType = (int)VarEnum.VT_BSTR; statusitem[i].szItemID = string.Format("S7:[S7 connection_1]{0}", statusDB[i]); //状态DB client++; } if (!opcServer.AddItems(statusitem, statusHandle)) { return(false); } } if (failureDB != null) { OpcRcw.Da.OPCITEMDEF[] failureitem = new OPCITEMDEF[failureDB.Length]; for (int i = 0; i < failureDB.Length; i++) { failureitem[i].szAccessPath = ""; failureitem[i].bActive = 1; failureitem[i].hClient = client; failureitem[i].dwBlobSize = 1; failureitem[i].pBlob = IntPtr.Zero; failureitem[i].vtRequestedDataType = (int)VarEnum.VT_BSTR; failureitem[i].szItemID = string.Format("S7:[S7 connection_1]{0}", failureDB[i]); //状态DB client++; } if (!opcServer.AddItems(failureitem, failureHandle)) { return(false); } } isBindToPLC = true; return(isBindToPLC); }