Main MRPT website > C++ reference
MRPT logo
CInterfaceFTDI.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | The Mobile Robot Programming Toolkit (MRPT) C++ library |
3  | |
4  | http://www.mrpt.org/ |
5  | |
6  | Copyright (C) 2005-2012 University of Malaga |
7  | |
8  | This software was written by the Machine Perception and Intelligent |
9  | Robotics Lab, University of Malaga (Spain). |
10  | Contact: Jose-Luis Blanco <jlblanco@ctima.uma.es> |
11  | |
12  | This file is part of the MRPT project. |
13  | |
14  | MRPT is free software: you can redistribute it and/or modify |
15  | it under the terms of the GNU General Public License as published by |
16  | the Free Software Foundation, either version 3 of the License, or |
17  | (at your option) any later version. |
18  | |
19  | MRPT is distributed in the hope that it will be useful, |
20  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
21  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22  | GNU General Public License for more details. |
23  | |
24  | You should have received a copy of the GNU General Public License |
25  | along with MRPT. If not, see <http://www.gnu.org/licenses/>. |
26  | |
27  +---------------------------------------------------------------------------+ */
28 
29 #ifndef CInterfaceFTDI_H
30 #define CInterfaceFTDI_H
31 
32 #include <mrpt/config.h>
33 #include <mrpt/utils/CStream.h>
35 
37 
38 namespace mrpt
39 {
40  namespace hwdrivers
41  {
42 
43  /** A list of FTDI devices and their descriptors.
44  * \sa CInterfaceFTDI::ListAllDevices
45  * \ingroup mrpt_hwdrivers_grp
46  */
48  {
49  std::string ftdi_manufacturer;
50  std::string ftdi_description;
51  std::string ftdi_serial;
52 
53  uint16_t usb_idVendor;
54  uint16_t usb_idProduct;
56 
57  #if defined(MRPT_OS_LINUX) || defined(MRPT_OS_APPLE)
58  /** Only for Linux: the corresponding libusb's "usb_device*" */
59  void *usb_device_struct;
60  #endif
61  };
62 
63  /** Print out all the information of a FTDI device in textual form. */
64  std::ostream HWDRIVERS_IMPEXP &operator << ( std::ostream &o, const TFTDIDevice &d);
65 
66  /** Used in CInterfaceFTDI::ListAllDevices */
67  typedef std::deque<TFTDIDevice> TFTDIDeviceList;
68 
69  /** A definition of a CStream actually representing a USB connection to a FTDI chip.
70  *
71  * This class implements the communication with FT245BM / FT245RL chips.
72  * Using this class makes a program to depend on:
73  * - Windows: "FT2XX.DLL" and the device drivers (see FTDI website).
74  * - Linux: "libusb.so" (quite standard!), and "libftdi.so" only if linking against the dynamic library.
75  *
76  * If there is any error during the communications (or loading the Windows DLL), a std::exception will be raised.
77  *
78  * To write bulk data, use CStream::ReadBuffer and CStream::WriteBuffer. See also the derived classes for
79  * higher level communication: CInterfaceFTDIMessages
80  *
81  * Warning: Avoid defining an object of this class in a global scope if you want to catch all potential
82  * exceptions during the constructors (like DLL not found, etc...)
83  *
84  * VERSIONS:
85  * - 11/APR/2005: Initial development. JLBC
86  * - 16/FEB/2007: Integration into the MRPT framework. Support for device serial numbers. JLBC
87  * - 15/APR/2008: Implemented for Linux using libftdi. JLBC
88  *
89  * \sa CInterfaceFTDIMessages, CStream
90  * \ingroup mrpt_hwdrivers_grp
91  */
93  {
94  public:
95  /** Constructor, which loads driver interface (the DLL under Windows).
96  */
98 
99  /** Destructor, which closes the connection with the chip and unloads the driver interface.
100  */
101  virtual ~CInterfaceFTDI();
102 
103  /** This object cannot be copied */
104  CInterfaceFTDI(const CInterfaceFTDI &o);
105 
106  /** This object cannot be copied */
107  CInterfaceFTDI& operator =(const CInterfaceFTDI &o);
108 
109  /** Checks whether the chip has been successfully open.
110  * \sa OpenBySerialNumber, OpenByDescription
111  */
112  bool isOpen();
113 
114  /** Open by device serial number
115  */
116  void OpenBySerialNumber( const std::string &serialNumber );
117 
118  /** Open by device description
119  */
120  void OpenByDescription( const std::string &description );
121 
122  /** Close the USB device */
123  void Close();
124 
125  /** Reset the USB device */
126  void ResetDevice();
127 
128  /** Purge the I/O buffers */
129  void Purge();
130 
131  /** Change the latency timer (in milliseconds) implemented on the FTDI chip: for a few ms, data is not sent to the PC waiting for possible more data, to save USB trafic. */
132  void SetLatencyTimer (unsigned char latency_ms);
133 
134  /** Change read & write timeouts, in milliseconds. */
135  void SetTimeouts(unsigned long dwReadTimeout_ms, unsigned long dwWriteTimeout_ms);
136 
137 
138  /** Generates a list with all FTDI devices connected right now.
139  */
140  void ListAllDevices( TFTDIDeviceList &outList );
141 
142  /** Tries to read, raising no exception if not all the bytes are available, but raising one if there is some communication error.
143  */
144  size_t ReadSync(void *Buffer, size_t Count)
145  {
146  return Read(Buffer,Count);
147  }
148 
149  /** Tries to write, raising no exception if not all the bytes are available, but raising one if there is some communication error.
150  */
151  size_t WriteSync(const void *Buffer, size_t Count)
152  {
153  return Write(Buffer,Count);
154  }
155 
156  /** Reads a block of bytes from the stream into Buffer, and returns the amound of bytes actually read, without waiting for more extra bytes to arrive (just those already enqued in the stream).
157  * In this class this method actually behaves as expected and does not fallback to ReadBuffer().
158  * \exception std::exception On any error, or if ZERO bytes are read.
159  */
160  virtual size_t ReadBufferImmediate(void *Buffer, size_t Count);
161 
162  protected:
163  /** Introduces a pure virtual method responsible for reading from the stream.
164  * It integrates a cache buffer to speed-up sequences of many, small readings.
165  */
166  size_t Read(void *Buffer, size_t Count);
167 
169 
170  /** Introduces a pure virtual method responsible for writing to the stream.
171  * Write attempts to write up to Count bytes to Buffer, and returns the number of bytes actually written.
172  */
173  size_t Write(const void *Buffer, size_t Count);
174 
175  /** This virtual method does nothing in this class.
176  */
177  uint64_t Seek(long Offset, CStream::TSeekOrigin Origin = sFromBeginning);
178 
179  /** This virtual method does nothing in this class.
180  */
181  uint64_t getTotalBytesCount();
182 
183  /** This virtual method does nothing in this class.
184  */
185  uint64_t getPosition();
186 
187 
188  void ftdi_read(void *lpvBuffer, unsigned long dwBuffSize, unsigned long *lpdwBytesRead);
189  void ftdi_write(const void *lpvBuffer, unsigned long dwBuffSize, unsigned long *lpdwBytes);
190 
191  #if defined(MRPT_OS_WINDOWS)
192  private:
193  void checkErrorAndRaise(int errorCode);
194 
195  void ftdi_open(void* pvDevice);
196  void ftdi_openEx(void* pArg1, unsigned long dwFlags);
197  void ftdi_listDevices(void *pArg1, void *pArg2, unsigned long dwFlags);
198  void ftdi_getQueueStatus(unsigned long *lpdwAmountInRxQueue);
199 
200  void *m_hmodule;
201  unsigned long m_ftHandle;
202 
203  void loadDriver();
204 
205  enum FT_STATUS
206  {
207  dummy
208  };
209 
210  typedef FT_STATUS (__stdcall *PtrToOpen)(void*, unsigned long *);
211  PtrToOpen m_pOpen;
212 
213  typedef FT_STATUS (__stdcall *PtrToOpenEx)(void*, unsigned long, unsigned long *);
214  PtrToOpenEx m_pOpenEx;
215 
216  typedef FT_STATUS (__stdcall *PtrToListDevices)(void*, void*, unsigned long);
217  PtrToListDevices m_pListDevices;
218 
219  typedef FT_STATUS (__stdcall *PtrToClose)(unsigned long );
220  PtrToClose m_pClose;
221 
222  typedef FT_STATUS (__stdcall *PtrToRead)(unsigned long , void *, unsigned long, unsigned long *);
223  PtrToRead m_pRead;
224 
225  typedef FT_STATUS (__stdcall *PtrToWrite)(unsigned long , const void *, unsigned long, unsigned long *);
226  PtrToWrite m_pWrite;
227 
228  typedef FT_STATUS (__stdcall *PtrToResetDevice)(unsigned long );
229  PtrToResetDevice m_pResetDevice;
230 
231  typedef FT_STATUS (__stdcall *PtrToPurge)(unsigned long , unsigned long);
232  PtrToPurge m_pPurge;
233 
234  typedef FT_STATUS (__stdcall *PtrToSetTimeouts)(unsigned long , unsigned long, unsigned long);
235  PtrToSetTimeouts m_pSetTimeouts;
236 
237  typedef FT_STATUS (__stdcall *PtrToGetQueueStatus)(unsigned long , unsigned long *);
238  PtrToGetQueueStatus m_pGetQueueStatus;
239 
240  typedef FT_STATUS (__stdcall *PtrToSetLatencyTimer )(unsigned long , unsigned char);
241  PtrToSetLatencyTimer m_pSetLatencyTimer;
242 
243  #else
244  // Declarations for Linux:
246 
247  /** Process recursively a USB device and its children: */
248  void recursive_fill_list_devices( void *usb_device_structure , TFTDIDeviceList &outList );
249 
250 
251  #endif
252 
253 
254  }; // end of class
255 
256  } // end of namespace
257 } // end of namespace
258 
259 #endif



Page generated by Doxygen 1.8.3 for MRPT 0.9.6 SVN: at Fri Feb 15 22:05:02 EST 2013