CLPort.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. //-----------------------------------------------------------------------------
  2. // (c) 2008 by Basler Vision Technologies
  3. // Section: Vision Components
  4. // Project: GenApi
  5. // Author: Fritz Dierks
  6. // $Header$
  7. //
  8. // License: This file is published under the license of the EMVA GenICam Standard Group.
  9. // A text file describing the legal terms is included in your installation as 'GenICam_license.pdf'.
  10. // If for some reason you are missing this file please contact the EMVA or visit the website
  11. // (http://www.genicam.org) for a full copy.
  12. //
  13. // THIS SOFTWARE IS PROVIDED BY THE EMVA GENICAM STANDARD GROUP "AS IS"
  14. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  15. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE EMVA GENICAM STANDARD GROUP
  17. // OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18. // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  20. // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  21. // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  23. // POSSIBILITY OF SUCH DAMAGE.
  24. //-----------------------------------------------------------------------------
  25. /*!
  26. \file
  27. \brief C++ class wrapping CLSerialAll and CLProtocolDriver
  28. */
  29. #ifndef CLPROTOCOL_CLPORT_H
  30. #define CLPROTOCOL_CLPORT_H
  31. #include <Base\GCBase.h>
  32. #include <GenApi\PortImpl.h>
  33. #include <CLProtocol\CLException.h>
  34. #include <CLProtocol\CLAutoBuffer.h>
  35. #include <CLProtocol\CLProtocol.h>
  36. #include <CLProtocol\ISerial.h>
  37. #include <CLProtocol\ISerialAdapter.h>
  38. #include <CLProtocol\CLAllAdapter.h>
  39. #include <CLProtocol\DeviceID.h>
  40. #include <CLProtocol\CVersion.h>
  41. #include <CLProtocol\XMLID.h>
  42. #include <iostream>
  43. #include <fstream>
  44. #include <list>
  45. #include <algorithm>
  46. namespace CLProtocol
  47. {
  48. //! the magic GUID which indicates that the file is a CLProtocol registry file. Must be the first entry
  49. # define CLPORT_PERSISTENCE_MAGIC "{3DFD485D-0ADB-4d84-9956-C3766504A2DD}"
  50. #pragma warning (push )
  51. #pragma warning( disable:4275) // warning C4275: non dll-interface class 'GenApi::CPortImpl' used as base for dll-interface class 'CLProtocol::CCLPort'
  52. /*! \brief Wraps a CameraLink protocol DLL and implements the IPort and the ISerial interface
  53. \ingroup PublicInterfaces
  54. The class dynamically loads a protocol driver DLL and binds it to a given port.
  55. It implements the IPort and the ISerial interface.
  56. The class can enumerate
  57. - the PortIDs of all frame grabbers installed in the system
  58. - the DeviceID templates of all CLProtocol drivers installed in the system
  59. The class can probe a port and retrieve the DeviceID of the attached camera.
  60. The class can retrieve the GenApi XML file for a camera.
  61. */
  62. class CLPROTOCOL_DECL CCLPort : public GENAPI_NAMESPACE::CPortImpl, public ISerial
  63. {
  64. public:
  65. //! \name Device discovery and binding
  66. //!@{
  67. /*! \brief Retrieves a list of PortIDs which can be used to create a corresponding port object
  68. \param[out] PortIDs A list of PortIDs which each has the form 'ManufacturerName#PortName'
  69. */
  70. static void GetPortIDs( GENAPI_NAMESPACE::StringList_t &PortIDs );
  71. /*! \brief Gets a SerialAdapter port
  72. \param[in] PortID PortID of the serial port
  73. \return ISerialAdapter interface belonging to the PortID; NULL if PortID is not valid
  74. */
  75. static ISerialAdapter *GetPort( const GENICAM_NAMESPACE::gcstring &PortID );
  76. /*! \brief Retrieves a list of \ref DeviceID templates
  77. Retrieves all DLLs found in the subdirectories of $(GENICAM_CLPROTOCOL),
  78. loads the dlls, asks them for their supported DeviceIDTemplates, and returns a
  79. list of the combined DeviceIDTemplate strings of all dlls.
  80. \param[out] DeviceIDTemplates A list of DeviceID templates
  81. */
  82. static void GetDeviceIDTemplates( GENICAM_NAMESPACE::gcstring_vector &DeviceIDTemplates );
  83. /*! \brief Connects a camera and retrieves all information required for the DeviceID
  84. Connects the camera with 9600 Baud (= CL standard baud rate after power up) and
  85. identifies the camera attached to the port given by PortID. The DeviceIDTemplate
  86. helps with the identification whose result is returned as DeviceID.
  87. \param[in] PortID Identifies the port the camera to be probed is connected to
  88. \param[in] strDeviceIDTemplate The \ref DeviceID templates helping the protocol driver dll to identify the camera
  89. \param[in] SerialTimeout The timeout in milliseconds when probing the port (see \ref SetSerialTimeOut).
  90. \return A \ref DeviceID containing a unique description of the camera found. If the DeviceID is an empty string no device was found.
  91. */
  92. static GENICAM_NAMESPACE::gcstring ProbeDevice( const GENICAM_NAMESPACE::gcstring &PortID, const GENICAM_NAMESPACE::gcstring &strDeviceIDTemplate, const CLUINT32 SerialTimeout = 500);
  93. /*! \brief Register a custom serial port
  94. \param[in] PortName Identifies the port to use for connection.
  95. \param[in] pSerialAdapter An interface that allow CCLPort to access the serial port.
  96. \return PortID a portId of the form "Local#<PortName>"
  97. */
  98. static GENICAM_NAMESPACE::gcstring RegisterSerial( const GENICAM_NAMESPACE::gcstring &PortName, ISerialAdapter *pSerialAdapter);
  99. //! Unregister all resource related to serial port
  100. static void UnRegisterSerials( );
  101. //! Cleans up static variables
  102. static void Terminate( );
  103. /*! \brief Unregister serial serial port
  104. \param[in] PortID Identifies the port that need to be disconnected
  105. */
  106. static void UnRegisterSerial( const GENICAM_NAMESPACE::gcstring &PortID );
  107. /*! \brief Creates a port object for a given PortID and DeviceID
  108. \param[in] PortID Identifies the port to use for connection.
  109. \param[in] strDeviceID Identifies the device to connect. Can be optionally a DeviceID template.
  110. */
  111. void Connect( const GENICAM_NAMESPACE::gcstring &PortID, const GENICAM_NAMESPACE::gcstring &strDeviceID );
  112. /*! \brief Creates a port object for a given PortID taking the DeviceID from a internal cache
  113. \param[in] PortID Identifies the port to use for connection.
  114. \return false, if the cache doesn't contain a DeviceID for the PortID;
  115. true, if the cache contains a corresponding DeviceID and if a matching device is connected
  116. Throws an exception if there is a cache entry but no matching device is connected
  117. */
  118. bool Connect( const GENICAM_NAMESPACE::gcstring &PortID );
  119. /*! \brief Retrieves a list of \ref XMLID strings
  120. Retrieves a list of XML IDs from the file system and the CLProtocol.dll
  121. which in turn may ask the camera, e.g. by reading a Manifest register
  122. The list contains only XML ID which would match the DeviceID and
  123. whose schema version can be interpreted by the current GenICam version.
  124. The XML ID are order best matching first.
  125. \param[out] XMLIDs A list of XML IDs templates
  126. */
  127. void GetXMLIDs( GENICAM_NAMESPACE::gcstring_vector &XMLIDs );;
  128. /*! \brief Retrieves an GenApi XML description either from the DLL or the file system
  129. \param[in] XMLID The XML ID identifying the XML file whose data is to be received
  130. \param[in] UseMaxBaudrate if true the baud rate is set to maximum while downloading the XML file and reset afterwards
  131. \return GenApi compliant camera XML description code ready to instantiate a node map.
  132. */
  133. GENICAM_NAMESPACE::gcstring GetXML( GENICAM_NAMESPACE::gcstring XMLID, const bool UseMaxBaudrate = true );
  134. /*! \brief Delete the device xml file from the download folder if the file was downloaded for the camera.
  135. This can be use to remove a corrupted file.
  136. File can easily be corrupted if the download is aborted.
  137. \param[in] XMLID The XML ID identifying the XML file whose data is to be received
  138. */
  139. void DeleteXMLDownload( GENICAM_NAMESPACE::gcstring XMLID );
  140. /*! \brief Retrieves the best matching GenApi XML description either from the DLL or the file system
  141. \param[in] UseMaxBaudrate if true the baud rate is set to maximum while downloading the XML file and reset afterwards
  142. \return GenApi compliant camera description code ready to instantiate a node map.
  143. */
  144. GENICAM_NAMESPACE::gcstring GetXML( const bool UseMaxBaudrate = true );
  145. /*! \brief Creates a file URL for a given XML ID
  146. The URL has the following format: "file:///filepath.xml"
  147. \return file URL to a XML file as described in the GenTL module
  148. */
  149. GENICAM_NAMESPACE::gcstring MakeURLfromXMLID( GENICAM_NAMESPACE::gcstring &XMLID );
  150. //!@}
  151. //! \name Creation and parameterizing
  152. //!@{
  153. //! Public constructor
  154. CCLPort();
  155. //! Public destructor
  156. virtual ~CCLPort();
  157. //! Sets the timeout for operations on the serial port
  158. /*!
  159. \param[in] Timeout Timeout in [ms] for operations on the serial port.
  160. */
  161. void SetSerialTimeOut(const CLUINT32 Timeout);
  162. //! Gets the timeout for operations on the serial port
  163. CLUINT32 GetSerialTimeOut() const;
  164. //! Returns the Device ID the object has been created with
  165. GENICAM_NAMESPACE::gcstring GetDeviceID() const;
  166. //! Set device protocol log lever
  167. void SetProtocolLogLevel( const CLP_LOG_LEVEL_VALUE logLever);
  168. //! Set device bauderate using one of the CL_BAUDRATE_XXXX value.
  169. void SetDeviceBauderate( const CLUINT32 baudrate);
  170. //! Returns the current device bauderate, using one of the CL_BAUDRATE_XXXX value.
  171. CLUINT32 GetDeviceBauderate( );
  172. //! Sed supported bauderates of the combined device and host interface.
  173. CLUINT32 SupportedBaudrates();
  174. //! Set log level that is assigned to the device protocol when the dll is loaded.
  175. static void SetDefaultLogLevel( const CLP_LOG_LEVEL_VALUE logLever);
  176. //! Stores a PortID/DeviceID pair
  177. static void StorePortIDDeviceIDPairs( GENICAM_NAMESPACE::gcstring_vector &PortIDs, GENICAM_NAMESPACE::gcstring_vector &DeviceIDs );
  178. //! Retrieves a PortID/DeviceID pair
  179. static void RetrievePortIDDeviceIDPairs( GENICAM_NAMESPACE::gcstring_vector &PortIDs, GENICAM_NAMESPACE::gcstring_vector &DeviceIDs );
  180. //!@}
  181. //! \name IPort interface
  182. //@{
  183. //! Get the access mode of the node
  184. virtual GENAPI_NAMESPACE::EAccessMode GetAccessMode() const;
  185. //! Reads a chunk of bytes from the port
  186. virtual void Read(void * pBuffer, int64_t Address, int64_t Length);
  187. //! Writes a chunk of bytes to the port
  188. virtual void Write(const void * pBuffer, int64_t Address, int64_t Length);
  189. //! We are an IPort interface
  190. virtual GENAPI_NAMESPACE::EInterfaceType GetPrinicipalInterfaceType() const;
  191. //@}
  192. //! We can get event data
  193. CLINT32 GetEvent(
  194. void * buffer, //!< Points to a user-allocated buffer. Upon a successful call,
  195. //!< buffer contains the data read from the serial device.
  196. //!< Upon failure, this buffer is not affected.
  197. //!< if buffersize is too small result is CL_ERR_BUFFER_TOO_SMALL.
  198. CLUINT32 * bufferSize //!< input is the size of the buffer, output is the size of
  199. //!< event data in buffer.
  200. );
  201. //! \name ISerial interface
  202. //@{
  203. //! calls clSerialRead as defined by the camera link standard
  204. virtual CLINT32 CLPROTOCOL clSerialRead(
  205. CLINT8* buffer, //!< Points to a user-allocated buffer. Upon a successful call,
  206. //!< buffer contains the data read from the serial device.
  207. //!< Upon failure, this buffer is not affected.
  208. //!< Caller should ensure that buffer is at least numBytes in size.
  209. CLUINT32* bufferSize, //!< This is the number of bytes requested by the caller.
  210. CLUINT32 serialTimeOut //!< Indicates the timeout in milliseconds.
  211. );
  212. //! calls clSerialWrite as defined by the camera link standard
  213. virtual CLINT32 CLPROTOCOL clSerialWrite(
  214. CLINT8* buffer, //!< Contains data to write to the serial device.
  215. CLUINT32* bufferSize, //!< Contains the buffer size indicating
  216. //!< the number of bytes to be written.
  217. //!< Upon a successful call, bufferSize contains the number of
  218. //!< bytes written to the serial device.
  219. CLUINT32 serialTimeOut //!< Indicates the timeout in milliseconds.
  220. );
  221. //! returns the valid baud rates of the current interface.
  222. virtual CLINT32 CLPROTOCOL clGetSupportedBaudRates(
  223. CLUINT32 *baudRates //!< Bitfield that describes all supported baud rates of the serial port
  224. //!< as described by serialRefPtr. See Table B-2 of the Camera Link
  225. //!< documentation for more information on constants.
  226. );
  227. //@}
  228. private:
  229. //! Initialize the PortID list
  230. static void InitPortIDs(void);
  231. //! This function sets the baud rate for the serial port of the selected device.
  232. virtual CLINT32 CLPROTOCOL clSetBaudRate(
  233. CLUINT32 baudRate //!< The baud rate you want to use. This parameter expects the
  234. //!< values represented by the CL_BAUDRATE constants in table B-2.
  235. //!< By default, the baud rate for serial communication is 9600.;
  236. );
  237. //! Helper retrieving error messages
  238. void CheckError( CLINT32 ErrorCode ) const;
  239. //! helper retrieving error messages
  240. static void CheckLastError();
  241. //! loads the protocol driver DLL
  242. void LoadProtocolDriver( const GENICAM_NAMESPACE::gcstring &PortDriverDLLPath );
  243. //! unloads the driver DLL
  244. void UnLoadProtocolDriver( );
  245. //! Creates a CLProtocol driver and connects it a a port identified by PortID
  246. void CreatePort( const GENICAM_NAMESPACE::gcstring &PortID, const GENICAM_NAMESPACE::gcstring &PortDriverDLLPath );
  247. //! updates the DeviceID cache
  248. static void UpdateCache( const GENICAM_NAMESPACE::gcstring &PortID, const GENICAM_NAMESPACE::gcstring &DeviceID );
  249. //! A proxy to the serial port used by the CCLPort object
  250. ISerialAdapter *m_pSerialAdapter;
  251. //! A list of all available proxies in the system
  252. static SerialPortMap *s_pSerialList;
  253. //! A list of all available proxies registers manually
  254. static SerialPortMap *s_pPrivateSerialList;
  255. //! indicates if s_pSerialList is initialized
  256. static bool s_IsSerialListInitialized;
  257. //! handle to the port driver DLL loaded
  258. HMODULE m_hPortDriverDLL;
  259. //! Timeout in [ms]
  260. CLUINT32 m_SerialTimeOut;
  261. //! the \ref DeviceID of the connected device
  262. GENICAM_NAMESPACE::gcstring m_DeviceID;
  263. //! the \ref PortID of the connected device
  264. GENICAM_NAMESPACE::gcstring m_PortID;
  265. //! Cookie
  266. CLUINT32 m_Cookie;
  267. /*------------------------------------------------*/
  268. // functions of the CL protocol DLL
  269. /*------------------------------------------------*/
  270. //! Function pointer to clpInitLib
  271. CLINT32 (__cdecl *m_clpInitLib)( clp_logger_t logger, CLP_LOG_LEVEL_VALUE logLevel);
  272. //! Function pointer to clpCloseLib
  273. CLINT32 (__cdecl *m_clpCloseLib)( void );
  274. //! Function pointer to clpGetShortDeviceIDTemplates
  275. CLINT32 (__cdecl *m_clpGetShortDeviceIDTemplates) (CLINT8* pDeviceTemplates, CLUINT32* pBufferSize );
  276. //! Function pointer to clpProbeDevice
  277. CLINT32 (__cdecl *m_clpProbeDevice) (ISerial *pSerial, const CLINT8* pDeviceIDTemplate, CLINT8* pDeviceID, CLUINT32 *pBufferSize, CLUINT32 *pCookie, const CLUINT32 TimeOut );
  278. //! Function pointer to clpGetXMLIDs
  279. CLINT32 (__cdecl *m_clpGetXMLIDs) ( ISerial *pSerial, const CLUINT32 Cookie ,CLINT8* pXMLIDs, CLUINT32* pBufferSize, const CLUINT32 TimeOut );
  280. //! Function pointer to m_clpGetXMLDescription
  281. CLINT32 (__cdecl *m_clpGetXMLDescription) (ISerial *pSerial, const CLUINT32 Cookie, const CLINT8* pDeviceID, CLINT8* pXMLBuffer, CLUINT32* pBufferSize, const CLUINT32 TimeOut );
  282. //! Function pointer to clpReadRegister
  283. CLINT32 (__cdecl *m_clpReadRegister) (ISerial *pSerial, const CLUINT32 Cookie, const CLINT64 Address, CLINT8* pBuffer, const CLINT64 BufferSize, const CLUINT32 TimeOut );
  284. //! Function pointer to clpWriteRegister
  285. CLINT32 (__cdecl *m_clpWriteRegister) (ISerial *pSerial, const CLUINT32 Cookie, const CLINT64 Address, const CLINT8* pBuffer, const CLINT64 BufferSize, const CLUINT32 TimeOut );
  286. //! Function pointer to clpContinueWriteRegister
  287. CLINT32 (__cdecl *m_clpContinueWriteRegister) (ISerial *pSerial, const CLUINT32 Cookie, const BOOL8 ContinueWaiting, const CLUINT32 TimeOut );
  288. //! Function pointer to clpGetErrorText
  289. CLINT32 (__cdecl *m_clpGetErrorText) (const CLINT32 errorCode, CLINT8* errorText, CLUINT32* errorTextSize, const CLUINT32 Cookie );
  290. //! Function pointer to clpDisconnect
  291. CLINT32 (__cdecl *m_clpDisconnect) (const CLUINT32 Cookie );
  292. //! Function pointer to clpGetCLProtocolVersion
  293. CLINT32 (__cdecl *m_clpGetCLProtocolVersion) (CLUINT32 *pVersionMajor, CLUINT32 *pVersionMinor );
  294. //! Function pointer to clpGetParam
  295. CLINT32 (__cdecl *m_clpGetParam) (ISerial *pSerial, CLP_PARAMS param, const CLUINT32 Cookie, const CLINT8* pBuffer, const CLINT64 BufferSize, const CLUINT32 TimeOut);
  296. //! Function pointer to clpSetParam
  297. CLINT32 (__cdecl *m_clpSetParam) (ISerial *pSerial, CLP_PARAMS param, const CLUINT32 Cookie, const CLINT8* pBuffer, const CLINT64 BufferSize, const CLUINT32 TimeOut);
  298. //! Function pointer to clpIsParamSupported
  299. CLINT32 (__cdecl *m_clpIsParamSupported) (CLP_PARAMS param);
  300. //! Function pointer to clpgetEventData
  301. CLINT32 (__cdecl *m_clpGetEventData) (const CLUINT32 Cookie, CLINT8* pBuffer, CLUINT32* pBufferSize);
  302. // What 1.1 feature are supported by the device protocol?
  303. //! true if you can set baud rate via the CLProtocol's function interface
  304. bool m_protocolHasBaudratePrms;
  305. //! true if you can set the log level
  306. bool m_protocolHasLogLevelPrm;
  307. //! true if you can set the logger
  308. bool m_protocolHasLogggerPrm;
  309. //! Default log level
  310. static CLP_LOG_LEVEL_VALUE m_defaultloglevel;
  311. private:
  312. //! must not be used
  313. CCLPort( CCLPort& );
  314. //! must not be used
  315. void operator=( CCLPort& );
  316. };
  317. #pragma warning(pop)
  318. }
  319. #endif // CLPROTOCOL_CLPORT_H