CLProtocol  3.5.1
CLAllAdapter.h
1 //-----------------------------------------------------------------------------
2 // (c) 2009 by DALSA
3 // Section: Digital Imaging
4 // Project: CLProtocol
5 // Author: Eric Bourbonnais
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 //-----------------------------------------------------------------------------
30 #ifndef CLPROTOCOL_CLALLADAPTER_H
31 #define CLPROTOCOL_CLALLADAPTER_H
32 
33 #include <Base/GCBase.h>
34 #include <CLProtocol/ClAllSerial.h>
35 #include <CLProtocol/CLException.h>
36 #include <CLProtocol/CLProtocol.h>
37 #include <CLProtocol/ISerialAdapter.h>
38 #include <map>
39 
40 #if defined(_WIN32)
41 #include <windows.h>
42 #else
43 #include <dlfcn.h>
44 #include <errno.h>
45 #endif
46 
47 #if defined (_WIN32)
48 typedef HMODULE lib_handle_t;
49 #else
50 typedef void * lib_handle_t;
51 #endif
52 
53 // manufacturer shared libraries are defined with __cdecl ...; taken from DLLLoad.h
54 #if defined (_MSC_VER)
55 // nothing
56 #else
57 # if !defined(__x86_64) && !defined(__arm__) && !defined(__aarch64__) && !defined(VXWORKS)
58 # define __cdecl __attribute__((cdecl))
59 # else
60 # define __cdecl /* use default calling convention */
61 # endif
62 #endif
63 
64 namespace CLProtocol
65 {
67  typedef std::map<GENICAM_NAMESPACE::gcstring, ISerialAdapter *> SerialPortMap;
68 
70  class CLPROTOCOL_DECL CCLAllAdapter : public ISerialAdapter
71  {
72  public:
73 
75  static void Destroy( ISerialAdapter * pAdapter );
76 
78 
79  virtual CLINT32 CLPROTOCOL clSerialRead( CLINT8*,CLUINT32*, CLUINT32);
80  virtual CLINT32 CLPROTOCOL clSerialWrite(CLINT8 *,CLUINT32 *,CLUINT32);
81  virtual CLINT32 CLPROTOCOL clGetSupportedBaudRates(CLUINT32 *);
82  virtual CLINT32 CLPROTOCOL clSetBaudRate(CLUINT32);
83  virtual CLINT32 SerialInit( void);
84  virtual void SerialClose( void);
85  virtual void CheckError( CLINT32 ErrorCode );
87 
89  static void GetPortIDs(GENICAM_NAMESPACE::gcstring_vector &TheVector);
91  static ISerialAdapter *GetSerialAdapter(GENICAM_NAMESPACE::gcstring PortID);
92 
93  private:
94  /*------------------------------------------------*/
95  // functions of the CLAllSerial DLL
96  /*------------------------------------------------*/
97  struct CLAllProxy
98  {
100  CLINT32 (CLALLSERIALCC *m_clGetErrorText)( const CLINT8*, CLINT32, CLINT8*, CLUINT32*);
102  CLINT32 (CLALLSERIALCC *m_clGetNumPorts) ( CLUINT32* Ports);
104  CLINT32 (CLALLSERIALCC *m_clGetPortInfo ) ( CLUINT32 serialIndex, CLINT8* manufacturerName, CLUINT32* nameBytes, CLINT8* portID, CLUINT32* IDBytes, CLUINT32* version);
106  CLINT32 (CLALLSERIALCC *m_clGetSupportedBaudRates) ( hSerRef serialRef, CLUINT32* baudRates);
108  void (CLALLSERIALCC *m_clSerialClose) ( hSerRef serialRef);
110  CLINT32 (CLALLSERIALCC *m_clSerialInit) ( CLUINT32 serialIndex, hSerRef* serialRefPtr);
112  CLINT32 (CLALLSERIALCC *m_clSerialRead) ( hSerRef serialRef, CLINT8* buffer, CLUINT32* bufferSize, CLUINT32 serialTimeout);
114  CLINT32 (CLALLSERIALCC *m_clSerialWrite) ( hSerRef serialRef, CLINT8* buffer, CLUINT32* bufferSize, CLUINT32 serialTimeout);
116  CLINT32 (CLALLSERIALCC *m_clSetBaudRate) ( hSerRef serialRef, CLUINT32 baudRate);
117  };
118 
119  static void CheckError( CLINT32 ErrorCode, CLUINT32 SerialIndex, CLAllProxy & );
120 
122  CCLAllAdapter( CLUINT32, CLAllProxy &);
123 
125  virtual ~CCLAllAdapter();
126 
128  hSerRef m_SerialRef;
129 
130 #ifdef _DEBUG // Allow changing serial index to test error situations.
131  public:
132 #endif
133  CLUINT32 m_SerialIndex;
135 
136  private:
137 
139  static void InternalGetPortMap(SerialPortMap &);
140 
141  static SerialPortMap *g_pSerialPortMap;
142 
144  lib_handle_t m_ClAlldllHandle;
145 
147  CLAllProxy m_clallproxy;
148 
150  CLUINT32 m_PreviousBaudRate;
151  };
152 
154  inline void GetPortMap(SerialPortMap &StdMap)
155  {
156  GENICAM_NAMESPACE::gcstring_vector PortIDs;
157  CCLAllAdapter::GetPortIDs(PortIDs);
158  for (GENICAM_NAMESPACE::gcstring_vector::iterator it = PortIDs.begin(); it != PortIDs.end(); it++)
159  {
160  StdMap[*it] = CCLAllAdapter::GetSerialAdapter(*it);
161  }
162  }
163 
164  inline void Destroy(SerialPortMap &portList)
165  {
166  for (SerialPortMap::iterator it = portList.begin(); it != portList.end(); it++)
167  CCLAllAdapter::Destroy(it->second);
168  }
169 
170 }
171 #endif //CLPROTOCOL_CLALLADAPTER_H
static void GetPortIDs(GENICAM_NAMESPACE::gcstring_vector &TheVector)
used to copy the content of the SerialPort ma to a std::map
Definition: CLAllAdapter.h:64
Wraps the CLAllSerial.dll.
Definition: CLAllAdapter.h:70
CLSERIALEXPORT CLINT32 CLSERIALCC clSetBaudRate(hSerRef serialRef, CLUINT32 baudRate)
This function sets the baud rate for the serial port of the selected device. Use clGetSupportedBaudRa...
static void Destroy(ISerialAdapter *pAdapter)
Destroy the factory and all its objects.
CLSERIALEXPORT CLINT32 CLSERIALCC clSerialRead(hSerRef serialRef, CLINT8 *buffer, CLUINT32 *bufferSize, CLUINT32 serialTimeOut)
This function reads numBytes from the serial device referred to by serialRef. clSerialRead will retur...
static ISerialAdapter * GetSerialAdapter(GENICAM_NAMESPACE::gcstring PortID)
used to copy the content of the SerialPort ma to a std::map
CLSERIALEXPORT CLINT32 CLSERIALCC clSerialWrite(hSerRef serialRef, CLINT8 *buffer, CLUINT32 *bufferSize, CLUINT32 serialTimeOut)
This function writes the data in the buffer to the serial device referenced by serialRef.
CLSERIALEXPORT CLINT32 CLSERIALCC clGetSupportedBaudRates(hSerRef serialRef, CLUINT32 *baudRates)
This function returns the valid baud rates of the current interface.