Main MRPT website > C++ reference for MRPT 1.9.9
CInterfaceFTDI.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2018, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #pragma once
10 
11 #include <mrpt/config.h>
12 #include <mrpt/io/CStream.h>
14 
15 #include <deque>
16 
17 namespace mrpt
18 {
19 namespace comms
20 {
21 /** A list of FTDI devices and their descriptors.
22  * \sa CInterfaceFTDI::ListAllDevices
23  * \ingroup mrpt_comms_grp
24  */
26 {
30 
34 
35 #if defined(MRPT_OS_LINUX) || defined(__APPLE__)
36  /** Only for Linux: the corresponding libusb's `libusb_device*` (or
37  * `usb_device*` for libftdi <1.2) */
38  void* usb_device_struct;
39 #endif
40 };
41 
42 /** Print out all the information of a FTDI device in textual form. */
43 std::ostream& operator<<(std::ostream& o, const TFTDIDevice& d);
44 
45 /** Used in CInterfaceFTDI::ListAllDevices */
46 using TFTDIDeviceList = std::deque<TFTDIDevice>;
47 
48 /** A definition of a CStream actually representing a USB connection to a FTDI
49  *chip.
50  *
51  * This class implements the communication with FT245BM / FT245RL chips.
52  * Using this class makes a program to depend on:
53  * - Windows: "FT2XX.DLL" and the device drivers (see FTDI website).
54  * - Linux: "libusb.so" (quite standard!), and "libftdi.so" only if linking
55  *against the dynamic library.
56  *
57  * If there is any error during the communications (or loading the Windows
58  *DLL), a std::exception will be raised.
59  *
60  * To write bulk data, use CStream::ReadBuffer and CStream::WriteBuffer.
61  *
62  * Warning: Avoid defining an object of this class in a global scope if you want
63  *to catch all potential
64  * exceptions during the constructors (like DLL not found, etc...)
65  *
66  * VERSIONS:
67  * - 11/APR/2005: Initial development. JLBC
68  * - 16/FEB/2007: Integration into the MRPT framework. Support for device
69  *serial numbers. JLBC
70  * - 15/APR/2008: Implemented for Linux using libftdi. JLBC
71  *
72  * \sa CStream
73  * \ingroup mrpt_comms_grp
74  */
76 {
77  public:
78  /** Constructor, which loads driver interface (the DLL under Windows).
79  */
81 
82  /** Destructor, which closes the connection with the chip and unloads the
83  * driver interface.
84  */
85  virtual ~CInterfaceFTDI();
86 
87  /** This object cannot be copied */
88  CInterfaceFTDI(const CInterfaceFTDI& o) = delete;
89 
90  /** This object cannot be copied */
91  CInterfaceFTDI& operator=(const CInterfaceFTDI& o) = delete;
92 
93  /** Checks whether the chip has been successfully open.
94  * \sa OpenBySerialNumber, OpenByDescription
95  */
96  bool isOpen();
97 
98  /** Open by device serial number
99  */
100  void OpenBySerialNumber(const std::string& serialNumber);
101 
102  /** Open by device description
103  */
104  void OpenByDescription(const std::string& description);
105 
106  /** Close the USB device */
107  void Close();
108 
109  /** Reset the USB device */
110  void ResetDevice();
111 
112  /** Purge the I/O buffers */
113  void Purge();
114 
115  /** Change the latency timer (in milliseconds) implemented on the FTDI chip:
116  * for a few ms, data is not sent to the PC waiting for possible more data,
117  * to save USB trafic. */
118  void SetLatencyTimer(unsigned char latency_ms);
119 
120  /** Change read & write timeouts, in milliseconds. */
121  void SetTimeouts(
122  unsigned long dwReadTimeout_ms, unsigned long dwWriteTimeout_ms);
123 
124  /** Generates a list with all FTDI devices connected right now.
125  */
126  void ListAllDevices(TFTDIDeviceList& outList);
127 
128  /** Tries to read, raising no exception if not all the bytes are available,
129  * but raising one if there is some communication error.
130  */
131  size_t ReadSync(void* Buffer, size_t Count) { return Read(Buffer, Count); }
132  /** Tries to write, raising no exception if not all the bytes are available,
133  * but raising one if there is some communication error.
134  */
135  size_t WriteSync(const void* Buffer, size_t Count)
136  {
137  return Write(Buffer, Count);
138  }
139 
140  /** Reads a block of bytes from the stream into Buffer, and returns the
141  *amound of bytes actually read, without waiting for more extra bytes to
142  *arrive (just those already enqued in the stream).
143  * In this class this method actually behaves as expected and does not
144  *fallback to ReadBuffer().
145  * \exception std::exception On any error, or if ZERO bytes are read.
146  */
147  virtual size_t ReadBufferImmediate(void* Buffer, size_t Count);
148 
149  /** Introduces a pure virtual method responsible for reading from the
150  * stream.
151  * It integrates a cache buffer to speed-up sequences of many, small
152  * readings.
153  */
154  size_t Read(void* Buffer, size_t Count);
155 
156  /** Introduces a pure virtual method responsible for writing to the stream.
157  * Write attempts to write up to Count bytes to Buffer, and returns the
158  * number of bytes actually written.
159  */
160  size_t Write(const void* Buffer, size_t Count);
161 
162  /** This virtual method does nothing in this class.
163  */
164  uint64_t Seek(int64_t Offset, CStream::TSeekOrigin Origin = sFromBeginning);
165 
166  /** This virtual method does nothing in this class.
167  */
169 
170  /** This virtual method does nothing in this class.
171  */
172  uint64_t getPosition() const;
173 
174  protected:
175  /** Used in Read */
177 
178  void ftdi_read(
179  void* lpvBuffer, unsigned long dwBuffSize,
180  unsigned long* lpdwBytesRead);
181  void ftdi_write(
182  const void* lpvBuffer, unsigned long dwBuffSize,
183  unsigned long* lpdwBytes);
184 
185 #if defined(_WIN32)
186  private:
187  void checkErrorAndRaise(int errorCode);
188 
189  void ftdi_open(void* pvDevice);
190  void ftdi_openEx(void* pArg1, unsigned long dwFlags);
191  void ftdi_listDevices(void* pArg1, void* pArg2, unsigned long dwFlags);
192  void ftdi_getQueueStatus(unsigned long* lpdwAmountInRxQueue);
193 
194  void* m_hmodule;
195  unsigned long m_ftHandle;
196 
197  void loadDriver();
198 
200 
201  typedef FT_STATUS(__stdcall* PtrToOpen)(void*, unsigned long*);
203 
204  typedef FT_STATUS(__stdcall* PtrToOpenEx)(
205  void*, unsigned long, unsigned long*);
207 
208  typedef FT_STATUS(__stdcall* PtrToListDevices)(void*, void*, unsigned long);
210 
211  typedef FT_STATUS(__stdcall* PtrToClose)(unsigned long);
213 
214  typedef FT_STATUS(__stdcall* PtrToRead)(
215  unsigned long, void*, unsigned long, unsigned long*);
217 
218  typedef FT_STATUS(__stdcall* PtrToWrite)(
219  unsigned long, const void*, unsigned long, unsigned long*);
221 
222  typedef FT_STATUS(__stdcall* PtrToResetDevice)(unsigned long);
224 
225  typedef FT_STATUS(__stdcall* PtrToPurge)(unsigned long, unsigned long);
227 
228  typedef FT_STATUS(__stdcall* PtrToSetTimeouts)(
229  unsigned long, unsigned long, unsigned long);
231 
232  typedef FT_STATUS(__stdcall* PtrToGetQueueStatus)(
233  unsigned long, unsigned long*);
235 
236  typedef FT_STATUS(__stdcall* PtrToSetLatencyTimer)(
237  unsigned long, unsigned char);
239 
240 #else
241  // Declarations for Linux:
242  void* m_ftdi_context;
243 
244  /** Process recursively a USB device and its children: */
245  void recursive_fill_list_devices(
246  void* usb_device_structure, TFTDIDeviceList& outList);
247 
248 #endif
249 
250 }; // end of class
251 
252 } // namespace comms
253 } // namespace mrpt
std::ostream & operator<<(std::ostream &o, const TFTDIDevice &d)
Print out all the information of a FTDI device in textual form.
PtrToResetDevice m_pResetDevice
FT_STATUS(__stdcall * PtrToPurge)(unsigned long, unsigned long)
bool isOpen()
Checks whether the chip has been successfully open.
virtual size_t ReadBufferImmediate(void *Buffer, size_t Count)
Reads a block of bytes from the stream into Buffer, and returns the amound of bytes actually read...
unsigned __int16 uint16_t
Definition: rptypes.h:44
void ftdi_getQueueStatus(unsigned long *lpdwAmountInRxQueue)
FT_STATUS(__stdcall * PtrToRead)(unsigned long, void *, unsigned long, unsigned long *)
void ftdi_write(const void *lpvBuffer, unsigned long dwBuffSize, unsigned long *lpdwBytes)
FT_STATUS(__stdcall * PtrToOpenEx)(void *, unsigned long, unsigned long *)
PtrToSetLatencyTimer m_pSetLatencyTimer
uint64_t getPosition() const
This virtual method does nothing in this class.
PtrToListDevices m_pListDevices
size_t Write(const void *Buffer, size_t Count)
Introduces a pure virtual method responsible for writing to the stream.
std::deque< TFTDIDevice > TFTDIDeviceList
Used in CInterfaceFTDI::ListAllDevices.
FT_STATUS(__stdcall * PtrToClose)(unsigned long)
mrpt::containers::circular_buffer< uint8_t > m_readBuffer
Used in Read.
virtual ~CInterfaceFTDI()
Destructor, which closes the connection with the chip and unloads the driver interface.
FT_STATUS(__stdcall * PtrToSetLatencyTimer)(unsigned long, unsigned char)
CInterfaceFTDI()
Constructor, which loads driver interface (the DLL under Windows).
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: io/CStream.h:30
unsigned char uint8_t
Definition: rptypes.h:41
size_t ReadSync(void *Buffer, size_t Count)
Tries to read, raising no exception if not all the bytes are available, but raising one if there is s...
void ftdi_listDevices(void *pArg1, void *pArg2, unsigned long dwFlags)
void ListAllDevices(TFTDIDeviceList &outList)
Generates a list with all FTDI devices connected right now.
CInterfaceFTDI & operator=(const CInterfaceFTDI &o)=delete
This object cannot be copied.
void ftdi_openEx(void *pArg1, unsigned long dwFlags)
uint64_t getTotalBytesCount() const
This virtual method does nothing in this class.
__int64 int64_t
Definition: rptypes.h:49
void Close()
Close the USB device.
void SetTimeouts(unsigned long dwReadTimeout_ms, unsigned long dwWriteTimeout_ms)
Change read & write timeouts, in milliseconds.
PtrToSetTimeouts m_pSetTimeouts
FT_STATUS(__stdcall * PtrToSetTimeouts)(unsigned long, unsigned long, unsigned long)
FT_STATUS(__stdcall * PtrToWrite)(unsigned long, const void *, unsigned long, unsigned long *)
void OpenBySerialNumber(const std::string &serialNumber)
Open by device serial number.
GLsizei const GLchar ** string
Definition: glext.h:4101
size_t Read(void *Buffer, size_t Count)
Introduces a pure virtual method responsible for reading from the stream.
void ResetDevice()
Reset the USB device.
FT_STATUS(__stdcall * PtrToResetDevice)(unsigned long)
FT_STATUS(__stdcall * PtrToOpen)(void *, unsigned long *)
size_t WriteSync(const void *Buffer, size_t Count)
Tries to write, raising no exception if not all the bytes are available, but raising one if there is ...
unsigned __int64 uint64_t
Definition: rptypes.h:50
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
void ftdi_read(void *lpvBuffer, unsigned long dwBuffSize, unsigned long *lpdwBytesRead)
uint64_t Seek(int64_t Offset, CStream::TSeekOrigin Origin=sFromBeginning)
This virtual method does nothing in this class.
A definition of a CStream actually representing a USB connection to a FTDI chip.
FT_STATUS(__stdcall * PtrToListDevices)(void *, void *, unsigned long)
void checkErrorAndRaise(int errorCode)
PtrToGetQueueStatus m_pGetQueueStatus
A list of FTDI devices and their descriptors.
void SetLatencyTimer(unsigned char latency_ms)
Change the latency timer (in milliseconds) implemented on the FTDI chip: for a few ms...
void OpenByDescription(const std::string &description)
Open by device description.
FT_STATUS(__stdcall * PtrToGetQueueStatus)(unsigned long, unsigned long *)
void Purge()
Purge the I/O buffers.



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at lun oct 28 00:14:14 CET 2019