InstantInterface.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. //------------------------------------------------------------------------------
  2. // Basler pylon SDK
  3. // Copyright (c) 2019-2021 Basler AG
  4. // http://www.baslerweb.com
  5. // Author: DV
  6. //------------------------------------------------------------------------------
  7. #pragma once
  8. #include <pylon/Platform.h>
  9. #ifdef _MSC_VER
  10. # pragma pack(push, PYLON_PACKING)
  11. #endif /* _MSC_VER */
  12. #include <pylon/stdinclude.h>
  13. #include <pylon/PylonBase.h>
  14. #include <pylon/InterfaceInfo.h>
  15. #include <pylon/DeviceInfo.h>
  16. #include <pylon/TransportLayer.h>
  17. #include <pylon/Interface.h>
  18. #include <pylon/_BaslerUniversalInterfaceParams.h>
  19. namespace Pylon
  20. {
  21. /*!
  22. \class CInstantInterface
  23. \brief Provides convenient access to an interface. An interface is used to represent a frame grabber board, a network card, etc.
  24. <ul>
  25. <li> Establishes a single access point for accessing interface functionality.
  26. <li> Handles %Pylon interface lifetime.
  27. <li> Handles opening and closing of a %Pylon interface automatically.
  28. </ul>
  29. \note Currently, this object type is mainly used for the pylon GenTL Consumer Transport Layer, e.g., for CoaXPress.
  30. All other pylon transport layers currently return one default interface.
  31. */
  32. template <typename T>
  33. class CInstantInterface : public T, public IInterface
  34. {
  35. public:
  36. /*!
  37. \brief Constructor. Creates a CInstantInterface object from a CDeviceInfo or CInterfaceInfo object.
  38. The following steps are taken:
  39. <ul>
  40. <li> The DeviceClass property is used to create the transport layer.
  41. <li> If the DeviceClass property is not available, the TlType property is used to create the first matching transport layer.
  42. <li> If the DeviceClass and TlType properties are not be available, an error will be thrown.
  43. <li> If the provided argument is of CInterfaceInfo type, this is used to create the interface.
  44. <li> If the provided argument is of CDeviceInfo type, use the InterfaceID property to create the interface.
  45. <li> If the above steps fail, the first interface is created.
  46. </ul>
  47. \error
  48. May throw an exception if the passed argument cannot be used to create an interface object.
  49. */
  50. CInstantInterface( const Pylon::CInfoBase& info ) : T()
  51. , m_pTransportLayer( NULL )
  52. , m_pInterface( NULL )
  53. {
  54. if (info.IsDeviceClassAvailable())
  55. {
  56. // find transport layer by DeviceClass
  57. CTlFactory& theFactory = CTlFactory::GetInstance();
  58. m_pTransportLayer = theFactory.CreateTl( info.GetDeviceClass() );
  59. }
  60. else if (info.IsTLTypeAvailable())
  61. {
  62. // find transport layer by TlType (choose first)
  63. TlInfoList_t tlList;
  64. CTlFactory& theFactory = Pylon::CTlFactory::GetInstance();
  65. theFactory.EnumerateTls( tlList );
  66. for (TlInfoList_t::const_iterator it = tlList.begin(); it != tlList.end(); ++it)
  67. {
  68. const CTlInfo& tinfo = *it;
  69. if (tinfo.GetTLType() == info.GetTLType())
  70. {
  71. m_pTransportLayer = theFactory.CreateTl( tinfo.GetDeviceClass() );
  72. break;
  73. }
  74. }
  75. }
  76. else
  77. {
  78. throw RUNTIME_EXCEPTION( "Not enough information to create the desired transport layer." );
  79. }
  80. if (m_pTransportLayer == NULL)
  81. {
  82. throw RUNTIME_EXCEPTION( "Cannot create transport layer." );
  83. }
  84. const CInterfaceInfo* pInterfaceInfo = dynamic_cast<const CInterfaceInfo*>(&info);
  85. const CDeviceInfo* pDeviceInfo = dynamic_cast<const CDeviceInfo*>(&info);
  86. if (pInterfaceInfo != NULL)
  87. {
  88. // parameter might be CInterfaceInfo
  89. m_pInterface = m_pTransportLayer->CreateInterface( *pInterfaceInfo );
  90. }
  91. else if ((pDeviceInfo != NULL) && (pDeviceInfo->IsInterfaceIDAvailable()))
  92. {
  93. // parameter might be CDeviceInfo
  94. CInterfaceInfo ii;
  95. ii.SetDeviceClass( info.GetDeviceClass() );
  96. ii.SetInterfaceID( pDeviceInfo->GetInterfaceID() );
  97. m_pInterface = m_pTransportLayer->CreateInterface( ii );
  98. }
  99. else
  100. {
  101. // open first interface
  102. InterfaceInfoList infoList;
  103. m_pTransportLayer->EnumerateInterfaces( infoList );
  104. m_pInterface = m_pTransportLayer->CreateInterface( infoList.at( 0 ) );
  105. }
  106. if (m_pInterface == NULL)
  107. {
  108. throw RUNTIME_EXCEPTION( "Cannot create interface." );
  109. }
  110. }
  111. /*!
  112. \brief Destructor. All created objects are destroyed correctly.
  113. */
  114. ~CInstantInterface()
  115. {
  116. if (m_pInterface != NULL)
  117. {
  118. if (IsOpen())
  119. {
  120. Close();
  121. }
  122. m_pTransportLayer->DestroyInterface( m_pInterface );
  123. }
  124. if (m_pTransportLayer != NULL)
  125. {
  126. CTlFactory& theFactory = CTlFactory::GetInstance();
  127. theFactory.ReleaseTl( m_pTransportLayer );
  128. }
  129. }
  130. // IInterface
  131. /*!
  132. \brief Opens the attached %Pylon interface.
  133. This call is neccessary to work with all parameters.
  134. \post
  135. <ul>
  136. <li> The interface is opened.
  137. <li> The nodemap and parameters are available and accessable.
  138. </ul>
  139. \error
  140. May throw C++ exceptions.
  141. */
  142. virtual void Open()
  143. {
  144. m_pInterface->Open();
  145. this->_Initialize( m_pInterface->GetNodeMap() );
  146. }
  147. /*!
  148. \brief Checks if the interface is open.
  149. \note The 'open' status of an interface instance won't change even if an attached camera is used, e.g., opened or closed.
  150. \return Returns true if the interface is open.
  151. \error
  152. May throw C++ exceptions.
  153. */
  154. virtual bool IsOpen() const
  155. {
  156. bool isOpen = false;
  157. if (m_pInterface != NULL)
  158. {
  159. isOpen = m_pInterface->IsOpen();
  160. }
  161. return isOpen;
  162. }
  163. /*!
  164. \brief Closes an interface.
  165. \post
  166. <ul>
  167. <li> The interface is closed.
  168. <li> Any previously acquired objects and references have been deleted and must not be used any longer.
  169. </ul>
  170. \error
  171. May throw C++ exceptions.
  172. */
  173. virtual void Close()
  174. {
  175. if (m_pInterface != NULL)
  176. {
  177. m_pInterface->Close();
  178. }
  179. this->_Initialize( NULL );
  180. }
  181. /*!
  182. \brief Returns the interface info object storing information like the Interface ID property.
  183. This information is available at all times regardless of whether the interface is open or closed.
  184. \return A reference to the interface info object.
  185. \error
  186. May throw C++ exceptions.
  187. */
  188. virtual const CInterfaceInfo& GetInterfaceInfo() const
  189. {
  190. return m_pInterface->GetInterfaceInfo();
  191. }
  192. /*!
  193. \brief Returns the GenApi node map used for accessing parameters provided by the interface.
  194. \note The default interface object does not provide a node map.
  195. \note Interfaces will only provide a nodemap after calling Open().
  196. \return Returns the GenApi node map used for accessing parameters provided by the interface.
  197. If no parameters are available, NULL is returned.
  198. \error
  199. May throw C++ exceptions.
  200. */
  201. virtual GenApi::INodeMap* GetNodeMap()
  202. {
  203. return m_pInterface->GetNodeMap();
  204. }
  205. // IDeviceFactory
  206. /// @copydoc IDeviceFactory::EnumerateDevices(DeviceInfoList_t&,bool)
  207. /// \error
  208. /// May throw C++ exceptions.
  209. virtual int EnumerateDevices( DeviceInfoList_t& list, bool addToList = false )
  210. {
  211. return m_pInterface->EnumerateDevices( list, addToList );
  212. }
  213. /// @copydoc IDeviceFactory::EnumerateDevices(DeviceInfoList_t&,const DeviceInfoList_t&,bool)
  214. /// \error
  215. /// May throw C++ exceptions.
  216. virtual int EnumerateDevices( DeviceInfoList_t& list, const DeviceInfoList_t& filter, bool addToList = false )
  217. {
  218. return m_pInterface->EnumerateDevices( list, filter, addToList );
  219. }
  220. /// @copydoc IDeviceFactory::CreateDevice(const CDeviceInfo&)
  221. /// \error
  222. /// May throw C++ exceptions.
  223. virtual IPylonDevice* CreateDevice( const CDeviceInfo& di )
  224. {
  225. return m_pInterface->CreateDevice( di );
  226. }
  227. /// @copydoc IDeviceFactory::CreateFirstDevice(const CDeviceInfo&)
  228. /// \error
  229. /// May throw C++ exceptions.
  230. virtual IPylonDevice* CreateFirstDevice( const CDeviceInfo& di = CDeviceInfo() )
  231. {
  232. return m_pInterface->CreateFirstDevice( di );
  233. }
  234. /// @copydoc IDeviceFactory::CreateDevice(const CDeviceInfo&,const StringList_t&)
  235. /// \error
  236. /// May throw C++ exceptions.
  237. virtual IPylonDevice* CreateDevice( const CDeviceInfo& di, const StringList_t& InjectedXmlStrings )
  238. {
  239. return m_pInterface->CreateDevice( di, InjectedXmlStrings );
  240. }
  241. /// @copydoc IDeviceFactory::CreateFirstDevice(const CDeviceInfo&,const StringList_t&)
  242. /// \error
  243. /// May throw C++ exceptions.
  244. virtual IPylonDevice* CreateFirstDevice( const CDeviceInfo& di, const StringList_t& InjectedXmlStrings )
  245. {
  246. return m_pInterface->CreateFirstDevice( di, InjectedXmlStrings );
  247. }
  248. /// @copydoc IDeviceFactory::CreateDevice(const String_t&)
  249. /// \error
  250. /// May throw C++ exceptions.
  251. virtual IPylonDevice* CreateDevice( const String_t& s )
  252. {
  253. return m_pInterface->CreateDevice( s );
  254. }
  255. /// @copydoc IDeviceFactory::DestroyDevice(IPylonDevice*)
  256. /// \error
  257. /// May throw C++ exceptions.
  258. virtual void DestroyDevice( IPylonDevice* pDevice )
  259. {
  260. return m_pInterface->DestroyDevice( pDevice );
  261. }
  262. /// @copydoc IDeviceFactory::IsDeviceAccessible(const CDeviceInfo&,AccessModeSet,EDeviceAccessiblityInfo*)
  263. /// \error
  264. /// May throw C++ exceptions.
  265. virtual bool IsDeviceAccessible( const CDeviceInfo& deviceInfo, AccessModeSet mode = Control, EDeviceAccessiblityInfo* pAccessibilityInfo = NULL )
  266. {
  267. return m_pInterface->IsDeviceAccessible( deviceInfo, mode, pAccessibilityInfo );
  268. }
  269. private:
  270. // Do not use
  271. CInstantInterface();
  272. ITransportLayer* m_pTransportLayer;
  273. IInterface* m_pInterface;
  274. };
  275. typedef CInstantInterface<Basler_UniversalInterfaceParams::CUniversalInterfaceParams_Params> CUniversalInstantInterface;
  276. } // namespace Pylon
  277. #ifdef _MSC_VER
  278. # pragma pack(pop)
  279. #endif /* _MSC_VER */