PageRenderTime 29ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/SimpleSim/OPCdotNETLib/OPC_Data_Srv.cs

http://automationtools.codeplex.com
C# | 514 lines | 351 code | 135 blank | 28 comment | 61 complexity | 9ff63c14979a454ff7251a77aed07858 MD5 | raw file
  1. /*=====================================================================
  2. File: OPC_Data_Srv.cs
  3. Summary: OPC DA server interfaces wrapper class
  4. -----------------------------------------------------------------------
  5. This file is part of the Viscom OPC Code Samples.
  6. Copyright(c) 2001 Viscom (www.viscomvisual.com) All rights reserved.
  7. THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  8. KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  9. IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  10. PARTICULAR PURPOSE.
  11. ======================================================================*/
  12. using System;
  13. using System.Text;
  14. using System.Collections;
  15. using System.Runtime.InteropServices;
  16. using System.Reflection;
  17. using OPC.Common;
  18. using OPC.Data.Interface;
  19. namespace OPC.Data
  20. {
  21. // ------------- managed side only structs ----------------------
  22. public class OPCProperty // QueryAvailableProperties
  23. {
  24. public int PropertyID;
  25. public string Description;
  26. public VarEnum DataType;
  27. public override string ToString()
  28. {
  29. return "ID:" + PropertyID + " '" + Description + "' T:" + DUMMY_VARIANT.VarEnumToString( DataType );
  30. }
  31. }
  32. public class OPCPropertyData // GetItemProperties
  33. {
  34. public int PropertyID;
  35. public int Error;
  36. public object Data;
  37. public override string ToString()
  38. {
  39. if( Error == HRESULTS.S_OK )
  40. return "ID:" + PropertyID + " Data:" + Data.ToString();
  41. else
  42. return "ID:" + PropertyID + " Error:" + Error.ToString();
  43. }
  44. }
  45. public class OPCPropertyItem // LookupItemIDs
  46. {
  47. public int PropertyID;
  48. public int Error;
  49. public string newItemID;
  50. public override string ToString()
  51. {
  52. if( Error == HRESULTS.S_OK )
  53. return "ID:" + PropertyID + " newID:" + newItemID;
  54. else
  55. return "ID:" + PropertyID + " Error:" + Error.ToString();
  56. }
  57. }
  58. // ----------------- event argument+handler ------------------------
  59. // IOPCShutdown
  60. public class ShutdownRequestEventArgs : EventArgs
  61. {
  62. public ShutdownRequestEventArgs( string shutdownReasonp )
  63. {
  64. shutdownReason = shutdownReasonp;
  65. }
  66. public string shutdownReason;
  67. }
  68. public delegate void ShutdownRequestEventHandler( object sender, ShutdownRequestEventArgs e );
  69. // --------------------------- OpcServer ------------------------
  70. [ComVisible(true)]
  71. public class OpcServer : IOPCShutdown
  72. {
  73. public OpcServer()
  74. {
  75. }
  76. ~OpcServer()
  77. { Disconnect(); }
  78. public void Connect( string clsidOPCserver )
  79. {
  80. Disconnect();
  81. Type typeofOPCserver = Type.GetTypeFromProgID( clsidOPCserver );
  82. if( typeofOPCserver == null )
  83. Marshal.ThrowExceptionForHR( HRESULTS.OPC_E_NOTFOUND );
  84. OPCserverObj = Activator.CreateInstance( typeofOPCserver );
  85. ifServer = (IOPCServer) OPCserverObj;
  86. if( ifServer == null )
  87. Marshal.ThrowExceptionForHR( HRESULTS.CONNECT_E_NOCONNECTION );
  88. // connect all interfaces
  89. ifCommon = (IOPCCommon) OPCserverObj;
  90. ifBrowse = (IOPCBrowseServerAddressSpace) ifServer;
  91. ifItmProps = (IOPCItemProperties) ifServer;
  92. cpointcontainer = (UCOMIConnectionPointContainer) OPCserverObj;
  93. AdviseIOPCShutdown();
  94. }
  95. public void Disconnect()
  96. {
  97. if( ! (shutdowncpoint == null) )
  98. {
  99. if( shutdowncookie != 0 )
  100. {
  101. shutdowncpoint.Unadvise( shutdowncookie );
  102. shutdowncookie = 0;
  103. }
  104. int rc = Marshal.ReleaseComObject( shutdowncpoint );
  105. shutdowncpoint = null;
  106. }
  107. cpointcontainer = null;
  108. ifBrowse = null;
  109. ifItmProps = null;
  110. ifCommon = null;
  111. ifServer = null;
  112. if( ! (OPCserverObj == null) )
  113. {
  114. int rc = Marshal.ReleaseComObject( OPCserverObj );
  115. OPCserverObj = null;
  116. }
  117. }
  118. public void GetStatus( out SERVERSTATUS serverStatus )
  119. {
  120. ifServer.GetStatus( out serverStatus );
  121. }
  122. public string GetErrorString( int errorCode, int localeID )
  123. {
  124. string errorres;
  125. ifServer.GetErrorString( errorCode, localeID, out errorres );
  126. return errorres;
  127. }
  128. public OpcGroup AddGroup( string groupName, bool setActive, int requestedUpdateRate )
  129. {
  130. return AddGroup( groupName, setActive, requestedUpdateRate, null, null, 0 );
  131. }
  132. public OpcGroup AddGroup( string groupName, bool setActive, int requestedUpdateRate,
  133. int[] biasTime, float[] percentDeadband, int localeID )
  134. {
  135. if( ifServer == null )
  136. Marshal.ThrowExceptionForHR( HRESULTS.E_ABORT );
  137. OpcGroup grp = new OpcGroup( ref ifServer, false, groupName, setActive, requestedUpdateRate );
  138. grp.internalAdd( biasTime, percentDeadband, localeID );
  139. return grp;
  140. }
  141. // --------------------------------- IOPCServerPublicGroups (indirect) -----------------
  142. public OpcGroup GetPublicGroup( string groupName )
  143. {
  144. if( ifServer == null )
  145. Marshal.ThrowExceptionForHR( HRESULTS.E_ABORT );
  146. OpcGroup grp = new OpcGroup( ref ifServer, true, groupName, false, 1000 );
  147. grp.internalAdd( null, null, 0 );
  148. return grp;
  149. }
  150. // --------------------------------- IOPCCommon -------------------------
  151. public void SetLocaleID( int lcid )
  152. {
  153. ifCommon.SetLocaleID( lcid );
  154. }
  155. public void GetLocaleID( out int lcid )
  156. {
  157. ifCommon.GetLocaleID( out lcid );
  158. }
  159. public void QueryAvailableLocaleIDs( out int[] lcids )
  160. {
  161. lcids = null;
  162. int count;
  163. IntPtr ptrIds;
  164. int hresult = ifCommon.QueryAvailableLocaleIDs( out count, out ptrIds );
  165. if( HRESULTS.Failed( hresult ) )
  166. Marshal.ThrowExceptionForHR( hresult );
  167. if( ((int) ptrIds) == 0 )
  168. return;
  169. if( count < 1 )
  170. { Marshal.FreeCoTaskMem( ptrIds ); return; }
  171. lcids = new int[ count ];
  172. Marshal.Copy( ptrIds, lcids, 0, count );
  173. Marshal.FreeCoTaskMem( ptrIds );
  174. }
  175. public void SetClientName( string name )
  176. {
  177. ifCommon.SetClientName( name );
  178. }
  179. // ------------------------ IOPCBrowseServerAddressSpace ---------------
  180. public OPCNAMESPACETYPE QueryOrganization()
  181. {
  182. OPCNAMESPACETYPE ns;
  183. ifBrowse.QueryOrganization( out ns );
  184. return ns;
  185. }
  186. public void ChangeBrowsePosition( OPCBROWSEDIRECTION direction, string name )
  187. {
  188. ifBrowse.ChangeBrowsePosition( direction, name );
  189. }
  190. public void BrowseOPCItemIDs( OPCBROWSETYPE filterType, string filterCriteria,
  191. VarEnum dataTypeFilter, OPCACCESSRIGHTS accessRightsFilter,
  192. out UCOMIEnumString stringEnumerator )
  193. {
  194. stringEnumerator = null;
  195. object enumtemp;
  196. ifBrowse.BrowseOPCItemIDs( filterType, filterCriteria, (short) dataTypeFilter, accessRightsFilter, out enumtemp );
  197. stringEnumerator = (UCOMIEnumString) enumtemp;
  198. enumtemp = null;
  199. }
  200. public string GetItemID( string itemDataID )
  201. {
  202. string itemid;
  203. ifBrowse.GetItemID( itemDataID, out itemid );
  204. return itemid;
  205. }
  206. public void BrowseAccessPaths( string itemID, out UCOMIEnumString stringEnumerator )
  207. {
  208. stringEnumerator = null;
  209. object enumtemp;
  210. ifBrowse.BrowseAccessPaths( itemID, out enumtemp );
  211. stringEnumerator = (UCOMIEnumString) enumtemp;
  212. enumtemp = null;
  213. }
  214. // extra helper
  215. public void Browse( OPCBROWSETYPE typ, out ArrayList lst )
  216. {
  217. lst = null;
  218. UCOMIEnumString enumerator;
  219. BrowseOPCItemIDs( typ, "", VarEnum.VT_EMPTY, 0, out enumerator );
  220. if( enumerator == null )
  221. return;
  222. lst = new ArrayList( 500 );
  223. int cft;
  224. string[] strF = new string[100];
  225. int hresult;
  226. do
  227. {
  228. cft = 0;
  229. hresult = enumerator.Next( 100, strF, out cft );
  230. if( cft > 0 )
  231. {
  232. for( int i = 0; i < cft; i++ )
  233. lst.Add( strF[i] );
  234. }
  235. }
  236. while( hresult == HRESULTS.S_OK );
  237. int rc = Marshal.ReleaseComObject( enumerator );
  238. enumerator = null;
  239. lst.TrimToSize();
  240. }
  241. // ------------------------ IOPCItemProperties ---------------
  242. public void QueryAvailableProperties( string itemID, out OPCProperty[] opcProperties )
  243. {
  244. opcProperties = null;
  245. int count = 0;
  246. IntPtr ptrID;
  247. IntPtr ptrDesc;
  248. IntPtr ptrTyp;
  249. ifItmProps.QueryAvailableProperties( itemID, out count, out ptrID, out ptrDesc, out ptrTyp );
  250. if( (count == 0) || (count > 10000) )
  251. return;
  252. int runID = (int) ptrID;
  253. int runDesc = (int) ptrDesc;
  254. int runTyp = (int) ptrTyp;
  255. if( (runID == 0) || (runDesc == 0) || (runTyp == 0) )
  256. Marshal.ThrowExceptionForHR( HRESULTS.E_ABORT );
  257. opcProperties = new OPCProperty[ count ];
  258. IntPtr ptrString;
  259. for( int i = 0; i < count; i++ )
  260. {
  261. opcProperties[i] = new OPCProperty();
  262. opcProperties[i].PropertyID = Marshal.ReadInt32( (IntPtr) runID );
  263. runID += 4;
  264. ptrString = (IntPtr) Marshal.ReadInt32( (IntPtr) runDesc );
  265. runDesc += 4;
  266. opcProperties[i].Description = Marshal.PtrToStringUni( ptrString );
  267. Marshal.FreeCoTaskMem( ptrString );
  268. opcProperties[i].DataType = (VarEnum) Marshal.ReadInt16( (IntPtr) runTyp );
  269. runTyp += 2;
  270. }
  271. Marshal.FreeCoTaskMem( ptrID );
  272. Marshal.FreeCoTaskMem( ptrDesc );
  273. Marshal.FreeCoTaskMem( ptrTyp );
  274. }
  275. public bool GetItemProperties( string itemID, int[] propertyIDs, out OPCPropertyData[] propertiesData )
  276. {
  277. propertiesData = null;
  278. int count = propertyIDs.Length;
  279. if( count < 1 )
  280. return false;
  281. IntPtr ptrDat;
  282. IntPtr ptrErr;
  283. int hresult = ifItmProps.GetItemProperties( itemID, count, propertyIDs, out ptrDat, out ptrErr );
  284. if( HRESULTS.Failed( hresult ) )
  285. Marshal.ThrowExceptionForHR( hresult );
  286. int runDat = (int) ptrDat;
  287. int runErr = (int) ptrErr;
  288. if( (runDat == 0) || (runErr == 0) )
  289. Marshal.ThrowExceptionForHR( HRESULTS.E_ABORT );
  290. propertiesData = new OPCPropertyData[ count ];
  291. for( int i = 0; i < count; i++ )
  292. {
  293. propertiesData[i] = new OPCPropertyData();
  294. propertiesData[i].PropertyID = propertyIDs[i];
  295. propertiesData[i].Error = Marshal.ReadInt32( (IntPtr) runErr );
  296. runErr += 4;
  297. if( propertiesData[i].Error == HRESULTS.S_OK )
  298. {
  299. propertiesData[i].Data = Marshal.GetObjectForNativeVariant( (IntPtr) runDat );
  300. DUMMY_VARIANT.VariantClear( (IntPtr) runDat );
  301. }
  302. else
  303. propertiesData[i].Data = null;
  304. runDat += DUMMY_VARIANT.ConstSize;
  305. }
  306. Marshal.FreeCoTaskMem( ptrDat );
  307. Marshal.FreeCoTaskMem( ptrErr );
  308. return hresult == HRESULTS.S_OK;
  309. }
  310. public bool LookupItemIDs( string itemID, int[] propertyIDs, out OPCPropertyItem[] propertyItems )
  311. {
  312. propertyItems = null;
  313. int count = propertyIDs.Length;
  314. if( count < 1 )
  315. return false;
  316. IntPtr ptrErr;
  317. IntPtr ptrIds;
  318. int hresult = ifItmProps.LookupItemIDs( itemID, count, propertyIDs, out ptrIds, out ptrErr );
  319. if( HRESULTS.Failed( hresult ) )
  320. Marshal.ThrowExceptionForHR( hresult );
  321. int runIds = (int) ptrIds;
  322. int runErr = (int) ptrErr;
  323. if( (runIds == 0) || (runErr == 0) )
  324. Marshal.ThrowExceptionForHR( HRESULTS.E_ABORT );
  325. propertyItems = new OPCPropertyItem[ count ];
  326. IntPtr ptrString;
  327. for( int i = 0; i < count; i++ )
  328. {
  329. propertyItems[i] = new OPCPropertyItem();
  330. propertyItems[i].PropertyID = propertyIDs[i];
  331. propertyItems[i].Error = Marshal.ReadInt32( (IntPtr) runErr );
  332. runErr += 4;
  333. if( propertyItems[i].Error == HRESULTS.S_OK )
  334. {
  335. ptrString = (IntPtr) Marshal.ReadInt32( (IntPtr) runIds );
  336. propertyItems[i].newItemID = Marshal.PtrToStringUni( ptrString );
  337. Marshal.FreeCoTaskMem( ptrString );
  338. }
  339. else
  340. propertyItems[i].newItemID = null;
  341. runIds += 4;
  342. }
  343. Marshal.FreeCoTaskMem( ptrIds );
  344. Marshal.FreeCoTaskMem( ptrErr );
  345. return hresult == HRESULTS.S_OK;
  346. }
  347. // ------------------------ IOPCShutdown --------------- COMMON CALLBACK
  348. void IOPCShutdown.ShutdownRequest( string shutdownReason )
  349. {
  350. ShutdownRequestEventArgs e = new ShutdownRequestEventArgs( shutdownReason );
  351. if( ShutdownRequested != null )
  352. ShutdownRequested( this, e );
  353. }
  354. // -------------------------- event ---------------------
  355. public event ShutdownRequestEventHandler ShutdownRequested;
  356. // -------------------------- private ---------------------
  357. private void AdviseIOPCShutdown()
  358. {
  359. Type sinktype = typeof( IOPCShutdown );
  360. Guid sinkguid = sinktype.GUID;
  361. cpointcontainer.FindConnectionPoint( ref sinkguid, out shutdowncpoint );
  362. if( shutdowncpoint == null )
  363. return;
  364. shutdowncpoint.Advise( this, out shutdowncookie );
  365. }
  366. private object OPCserverObj = null;
  367. private IOPCServer ifServer = null;
  368. private IOPCCommon ifCommon = null;
  369. private IOPCBrowseServerAddressSpace ifBrowse = null;
  370. private IOPCItemProperties ifItmProps = null;
  371. private UCOMIConnectionPointContainer cpointcontainer = null;
  372. private UCOMIConnectionPoint shutdowncpoint = null;
  373. private int shutdowncookie = 0;
  374. } // class OpcServer
  375. } // namespace OPC.Data