/*------------------------------------------------------ * Browse Server * * ------------------------------------------------------*/ private SERVERPARAM[] BrowseServer(DEF_OPCDA opcDaVer, string hostName, out int nServerCnt) { nServerCnt = 0; try { // initialize IOPCServerList svrList = (IOPCServerList)CreateInstance(CLSID_SERVERLIST, hostName); // convert to GUID Guid catid; switch (opcDaVer) { case DEF_OPCDA.VER_30: catid = CLSID_DA_30; break; case DEF_OPCDA.VER_10: case DEF_OPCDA.VER_20: default: catid = CLSID_DA_20; break; } // Get class id SERVERPARAM[] svrPrms = GetServerParam(svrList, catid, out nServerCnt); return(svrPrms); } catch (Exception exc) { MessageBox.Show(exc.ToString(), "BrowseServer"); return(null); } }
/*------------------------------------------------------ * Browse Server List * * ------------------------------------------------------*/ public void BrowseServerNameList(DEF_OPCDA opcDaVer, string hostName, ref List <string> listServerName) { listServerName.Clear(); int nServerCnt; SERVERPARAM[] svrprmList = BrowseServer(opcDaVer, hostName, out nServerCnt); for (int n = 0; n < nServerCnt; n++) { listServerName.Add(svrprmList[n].progID); } return; }
/*------------------------------------------------------ * Connect OPC Server * * (ret) True OK * False NG * ------------------------------------------------------*/ public bool Connect(DEF_OPCDA OpcdaVer, string sNodeName, string sSvrName, string sGrpName, int iUpdateRate) { if (m_OPCServer != null) { return(true); } m_OpcdaVer = OpcdaVer; try { // instantiate the serverlist using CoCreateInstanceEx. IOPCServerList svrList = (IOPCServerList)CreateInstance(CLSID_SERVERLIST, sNodeName); Guid clsidList; svrList.CLSIDFromProgID(sSvrName, out clsidList); m_OPCServer = (IOPCServer)CreateInstance(clsidList, sNodeName); if (m_OPCServer != null) { // 2011/11/14 シャットダウンイベントを受けれるようにする ( IConnectionPointContainer OPCConnPointCntnr = (IConnectionPointContainer)m_OPCServer; Guid guidShutdown = Marshal.GenerateGuidForType(typeof(IOPCShutdown)); OPCConnPointCntnr.FindConnectionPoint(ref guidShutdown, out m_OpcShutdownConnectionPoint); m_OpcShutdownConnectionPoint.Advise(this, out m_iShutdownConnectionCookie); // 2011/11/14 シャットダウンイベントを受けれるようにする ) if (AddGroup(sGrpName, iUpdateRate)) { IOPCCommon m_com = (IOPCCommon)m_OPCServer; m_com.SetClientName("TestClient"); m_bConnect = true; Marshal.ReleaseComObject(svrList); // 1.0.0.5 10/06/21 Kishimoto Release Com Object svrList = null; // 1.0.0.5 10/06/21 Kishimoto Release Com Object return(true); } } Marshal.ReleaseComObject(svrList); // 1.0.0.5 10/06/21 Kishimoto Release Com Object svrList = null; // 1.0.0.5 10/06/21 Kishimoto Release Com Object MessageBox.Show("Cannot connect OPC Server.", "Connect"); return(false); } catch (Exception exc) { MessageBox.Show(exc.ToString(), "Connect"); return(false); } }