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-2017, 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/utils/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(MRPT_OS_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 typedef std::deque<TFTDIDevice> TFTDIDeviceList;
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 */
89 
90  /** This object cannot be copied */
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  protected:
150  /** Introduces a pure virtual method responsible for reading from the
151  * stream.
152  * It integrates a cache buffer to speed-up sequences of many, small
153  * readings.
154  */
155  size_t Read(void* Buffer, size_t Count);
156 
157  /** Used in Read */
159 
160  /** Introduces a pure virtual method responsible for writing to the stream.
161  * Write attempts to write up to Count bytes to Buffer, and returns the
162  * number of bytes actually written.
163  */
164  size_t Write(const void* Buffer, size_t Count);
165 
166  /** This virtual method does nothing in this class.
167  */
168  uint64_t Seek(
169  uint64_t Offset, CStream::TSeekOrigin Origin = sFromBeginning);
170 
171  /** This virtual method does nothing in this class.
172  */
174 
175  /** This virtual method does nothing in this class.
176  */
178 
179  void ftdi_read(
180  void* lpvBuffer, unsigned long dwBuffSize,
181  unsigned long* lpdwBytesRead);
182  void ftdi_write(
183  const void* lpvBuffer, unsigned long dwBuffSize,
184  unsigned long* lpdwBytes);
185 
186 #if defined(MRPT_OS_WINDOWS)
187  private:
188  void checkErrorAndRaise(int errorCode);
189 
190  void ftdi_open(void* pvDevice);
191  void ftdi_openEx(void* pArg1, unsigned long dwFlags);
192  void ftdi_listDevices(void* pArg1, void* pArg2, unsigned long dwFlags);
193  void ftdi_getQueueStatus(unsigned long* lpdwAmountInRxQueue);
194 
195  void* m_hmodule;
196  unsigned long m_ftHandle;
197 
198  void loadDriver();
199 
200  enum FT_STATUS{dummy};
201 
202  typedef FT_STATUS(__stdcall* PtrToOpen)(void*, unsigned long*);
203  PtrToOpen m_pOpen;
204 
205  typedef FT_STATUS(__stdcall* PtrToOpenEx)(
206  void*, unsigned long, unsigned long*);
207  PtrToOpenEx m_pOpenEx;
208 
209  typedef FT_STATUS(__stdcall* PtrToListDevices)(void*, void*, unsigned long);
210  PtrToListDevices m_pListDevices;
211 
212  typedef FT_STATUS(__stdcall* PtrToClose)(unsigned long);
213  PtrToClose m_pClose;
214 
215  typedef FT_STATUS(__stdcall* PtrToRead)(
216  unsigned long, void*, unsigned long, unsigned long*);
217  PtrToRead m_pRead;
218 
219  typedef FT_STATUS(__stdcall* PtrToWrite)(
220  unsigned long, const void*, unsigned long, unsigned long*);
221  PtrToWrite m_pWrite;
222 
223  typedef FT_STATUS(__stdcall* PtrToResetDevice)(unsigned long);
224  PtrToResetDevice m_pResetDevice;
225 
226  typedef FT_STATUS(__stdcall* PtrToPurge)(unsigned long, unsigned long);
227  PtrToPurge m_pPurge;
228 
229  typedef FT_STATUS(__stdcall* PtrToSetTimeouts)(
230  unsigned long, unsigned long, unsigned long);
231  PtrToSetTimeouts m_pSetTimeouts;
232 
233  typedef FT_STATUS(__stdcall* PtrToGetQueueStatus)(
234  unsigned long, unsigned long*);
235  PtrToGetQueueStatus m_pGetQueueStatus;
236 
237  typedef FT_STATUS(__stdcall* PtrToSetLatencyTimer)(
238  unsigned long, unsigned char);
239  PtrToSetLatencyTimer m_pSetLatencyTimer;
240 
241 #else
242  // Declarations for Linux:
244 
245  /** Process recursively a USB device and its children: */
247  void* usb_device_structure, TFTDIDeviceList& outList);
248 
249 #endif
250 
251 }; // end of class
252 
253 } // end of namespace
254 } // end of namespace
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 ResetDevice()
Reset the USB device.
size_t Write(const void *Buffer, size_t Count)
Introduces a pure virtual method responsible for writing to the stream.
void OpenBySerialNumber(const std::string &serialNumber)
Open by device serial number.
void recursive_fill_list_devices(void *usb_device_structure, TFTDIDeviceList &outList)
Process recursively a USB device and its children:
uint64_t getTotalBytesCount()
This virtual method does nothing in this class.
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 Close()
Close the USB device.
void OpenByDescription(const std::string &description)
Open by device description.
mrpt::utils::circular_buffer< uint8_t > m_readBuffer
Used in Read.
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:41
void SetTimeouts(unsigned long dwReadTimeout_ms, unsigned long dwWriteTimeout_ms)
Change read & write timeouts, in milliseconds.
void ListAllDevices(TFTDIDeviceList &outList)
Generates a list with all FTDI devices connected right now.
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.
std::deque< TFTDIDevice > TFTDIDeviceList
Used in CInterfaceFTDI::ListAllDevices.
uint64_t getPosition()
This virtual method does nothing in this class.
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.
CInterfaceFTDI & operator=(const CInterfaceFTDI &o)
This object cannot be copied.
std::ostream & operator<<(std::ostream &o, const TFTDIDevice &d)
Print out all the information of a FTDI device in textual form.
A definition of a CStream actually representing a USB connection to a FTDI chip.
CInterfaceFTDI()
Constructor, which loads driver interface (the DLL under Windows).
void ftdi_read(void *lpvBuffer, unsigned long dwBuffSize, unsigned long *lpdwBytesRead)
void Purge()
Purge the I/O buffers.
void ftdi_write(const void *lpvBuffer, unsigned long dwBuffSize, unsigned long *lpdwBytes)
virtual ~CInterfaceFTDI()
Destructor, which closes the connection with the chip and unloads the driver interface.
void SetLatencyTimer(unsigned char latency_ms)
Change the latency timer (in milliseconds) implemented on the FTDI chip: for a few ms...
A list of FTDI devices and their descriptors.
uint64_t Seek(uint64_t Offset, CStream::TSeekOrigin Origin=sFromBeginning)
This virtual method does nothing in this class.



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019