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
mrpt::comms::CInterfaceFTDI::Purge
void Purge()
Purge the I/O buffers.
Definition: CInterfaceFTDI_WIN.cpp:337
mrpt::comms::CInterfaceFTDI::ListAllDevices
void ListAllDevices(TFTDIDeviceList &outList)
Generates a list with all FTDI devices connected right now.
Definition: CInterfaceFTDI_WIN.cpp:242
mrpt::comms::CInterfaceFTDI::PtrToSetLatencyTimer
FT_STATUS(__stdcall * PtrToSetLatencyTimer)(unsigned long, unsigned char)
Definition: CInterfaceFTDI.h:236
mrpt::comms::CInterfaceFTDI::Seek
uint64_t Seek(int64_t Offset, CStream::TSeekOrigin Origin=sFromBeginning)
This virtual method does nothing in this class.
Definition: CInterfaceFTDI_common.cpp:61
mrpt::comms::CInterfaceFTDI::PtrToListDevices
FT_STATUS(__stdcall * PtrToListDevices)(void *, void *, unsigned long)
Definition: CInterfaceFTDI.h:208
mrpt::comms::CInterfaceFTDI::PtrToClose
FT_STATUS(__stdcall * PtrToClose)(unsigned long)
Definition: CInterfaceFTDI.h:211
mrpt::comms::CInterfaceFTDI::ftdi_listDevices
void ftdi_listDevices(void *pArg1, void *pArg2, unsigned long dwFlags)
Definition: CInterfaceFTDI_WIN.cpp:274
uint16_t
unsigned __int16 uint16_t
Definition: rptypes.h:44
mrpt::comms::CInterfaceFTDI::ftdi_open
void ftdi_open(void *pvDevice)
Definition: CInterfaceFTDI_WIN.cpp:217
mrpt::comms::CInterfaceFTDI::PtrToResetDevice
FT_STATUS(__stdcall * PtrToResetDevice)(unsigned long)
Definition: CInterfaceFTDI.h:222
mrpt::comms::CInterfaceFTDI::m_pResetDevice
PtrToResetDevice m_pResetDevice
Definition: CInterfaceFTDI.h:223
mrpt::comms::CInterfaceFTDI::~CInterfaceFTDI
virtual ~CInterfaceFTDI()
Destructor, which closes the connection with the chip and unloads the driver interface.
Definition: CInterfaceFTDI_WIN.cpp:166
mrpt::comms::TFTDIDeviceList
std::deque< TFTDIDevice > TFTDIDeviceList
Used in CInterfaceFTDI::ListAllDevices.
Definition: CInterfaceFTDI.h:46
mrpt::comms::CInterfaceFTDI::getTotalBytesCount
uint64_t getTotalBytesCount() const
This virtual method does nothing in this class.
Definition: CInterfaceFTDI_common.cpp:66
mrpt::containers::circular_buffer< uint8_t >
mrpt::comms::CInterfaceFTDI::m_pOpenEx
PtrToOpenEx m_pOpenEx
Definition: CInterfaceFTDI.h:206
mrpt::comms::CInterfaceFTDI::m_pOpen
PtrToOpen m_pOpen
Definition: CInterfaceFTDI.h:202
mrpt::comms::CInterfaceFTDI::PtrToOpenEx
FT_STATUS(__stdcall * PtrToOpenEx)(void *, unsigned long, unsigned long *)
Definition: CInterfaceFTDI.h:204
mrpt::comms::CInterfaceFTDI::isOpen
bool isOpen()
Checks whether the chip has been successfully open.
Definition: CInterfaceFTDI_WIN.cpp:179
mrpt::comms::CInterfaceFTDI::PtrToPurge
FT_STATUS(__stdcall * PtrToPurge)(unsigned long, unsigned long)
Definition: CInterfaceFTDI.h:225
mrpt::comms::CInterfaceFTDI::m_hmodule
void * m_hmodule
Definition: CInterfaceFTDI.h:194
mrpt::comms::CInterfaceFTDI::FT_STATUS
FT_STATUS
Definition: CInterfaceFTDI.h:199
int64_t
__int64 int64_t
Definition: rptypes.h:49
mrpt::comms::CInterfaceFTDI::Read
size_t Read(void *Buffer, size_t Count)
Introduces a pure virtual method responsible for reading from the stream.
Definition: CInterfaceFTDI_common.cpp:18
mrpt::comms::CInterfaceFTDI::m_pSetTimeouts
PtrToSetTimeouts m_pSetTimeouts
Definition: CInterfaceFTDI.h:230
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
uint8_t
unsigned char uint8_t
Definition: rptypes.h:41
mrpt::comms::CInterfaceFTDI::m_pListDevices
PtrToListDevices m_pListDevices
Definition: CInterfaceFTDI.h:209
mrpt::comms::CInterfaceFTDI::ResetDevice
void ResetDevice()
Reset the USB device.
Definition: CInterfaceFTDI_WIN.cpp:325
mrpt::comms::CInterfaceFTDI::ftdi_openEx
void ftdi_openEx(void *pArg1, unsigned long dwFlags)
Definition: CInterfaceFTDI_WIN.cpp:228
mrpt::comms::CInterfaceFTDI::dummy
@ dummy
Definition: CInterfaceFTDI.h:199
mrpt::comms::TFTDIDevice::ftdi_description
std::string ftdi_description
Definition: CInterfaceFTDI.h:28
mrpt::comms::CInterfaceFTDI::OpenByDescription
void OpenByDescription(const std::string &description)
Open by device description.
Definition: CInterfaceFTDI_WIN.cpp:437
mrpt::comms::CInterfaceFTDI::m_pGetQueueStatus
PtrToGetQueueStatus m_pGetQueueStatus
Definition: CInterfaceFTDI.h:234
mrpt::comms::CInterfaceFTDI::ReadBufferImmediate
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,...
Definition: CInterfaceFTDI_common.cpp:68
mrpt::comms::CInterfaceFTDI::getPosition
uint64_t getPosition() const
This virtual method does nothing in this class.
Definition: CInterfaceFTDI_common.cpp:67
mrpt::comms::CInterfaceFTDI::loadDriver
void loadDriver()
Definition: CInterfaceFTDI_WIN.cpp:180
mrpt::comms::CInterfaceFTDI::PtrToSetTimeouts
FT_STATUS(__stdcall * PtrToSetTimeouts)(unsigned long, unsigned long, unsigned long)
Definition: CInterfaceFTDI.h:228
mrpt::comms::CInterfaceFTDI::m_pRead
PtrToRead m_pRead
Definition: CInterfaceFTDI.h:216
mrpt::comms::CInterfaceFTDI
A definition of a CStream actually representing a USB connection to a FTDI chip.
Definition: CInterfaceFTDI.h:75
mrpt::comms::CInterfaceFTDI::PtrToRead
FT_STATUS(__stdcall * PtrToRead)(unsigned long, void *, unsigned long, unsigned long *)
Definition: CInterfaceFTDI.h:214
circular_buffer.h
comms
Definition: CInterfaceFTDI_WIN.cpp:19
mrpt::io::CStream::sFromBeginning
@ sFromBeginning
Definition: io/CStream.h:36
mrpt::comms::TFTDIDevice::usb_idVendor
uint16_t usb_idVendor
Definition: CInterfaceFTDI.h:31
mrpt::comms::TFTDIDevice
A list of FTDI devices and their descriptors.
Definition: CInterfaceFTDI.h:25
uint64_t
unsigned __int64 uint64_t
Definition: rptypes.h:50
mrpt::comms::CInterfaceFTDI::Close
void Close()
Close the USB device.
Definition: CInterfaceFTDI_WIN.cpp:285
mrpt::comms::CInterfaceFTDI::WriteSync
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 ...
Definition: CInterfaceFTDI.h:135
mrpt::comms::CInterfaceFTDI::m_readBuffer
mrpt::containers::circular_buffer< uint8_t > m_readBuffer
Used in Read.
Definition: CInterfaceFTDI.h:176
CStream.h
mrpt::comms::CInterfaceFTDI::CInterfaceFTDI
CInterfaceFTDI()
Constructor, which loads driver interface (the DLL under Windows).
Definition: CInterfaceFTDI_WIN.cpp:153
mrpt::comms::CInterfaceFTDI::m_pPurge
PtrToPurge m_pPurge
Definition: CInterfaceFTDI.h:226
mrpt::comms::CInterfaceFTDI::Write
size_t Write(const void *Buffer, size_t Count)
Introduces a pure virtual method responsible for writing to the stream.
Definition: CInterfaceFTDI_common.cpp:54
mrpt::comms::TFTDIDevice::usb_serialNumber
uint8_t usb_serialNumber
Definition: CInterfaceFTDI.h:33
mrpt::comms::CInterfaceFTDI::operator=
CInterfaceFTDI & operator=(const CInterfaceFTDI &o)=delete
This object cannot be copied.
mrpt::comms::CInterfaceFTDI::m_pClose
PtrToClose m_pClose
Definition: CInterfaceFTDI.h:212
mrpt::comms::CInterfaceFTDI::m_pSetLatencyTimer
PtrToSetLatencyTimer m_pSetLatencyTimer
Definition: CInterfaceFTDI.h:238
mrpt::comms::CInterfaceFTDI::ReadSync
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...
Definition: CInterfaceFTDI.h:131
mrpt::comms::CInterfaceFTDI::m_ftHandle
unsigned long m_ftHandle
Definition: CInterfaceFTDI.h:195
mrpt::comms::CInterfaceFTDI::m_pWrite
PtrToWrite m_pWrite
Definition: CInterfaceFTDI.h:220
mrpt::comms::CInterfaceFTDI::ftdi_read
void ftdi_read(void *lpvBuffer, unsigned long dwBuffSize, unsigned long *lpdwBytesRead)
Definition: CInterfaceFTDI_WIN.cpp:301
mrpt::comms::CInterfaceFTDI::ftdi_getQueueStatus
void ftdi_getQueueStatus(unsigned long *lpdwAmountInRxQueue)
Definition: CInterfaceFTDI_WIN.cpp:361
mrpt::comms::CInterfaceFTDI::PtrToOpen
FT_STATUS(__stdcall * PtrToOpen)(void *, unsigned long *)
Definition: CInterfaceFTDI.h:201
mrpt::comms::CInterfaceFTDI::PtrToGetQueueStatus
FT_STATUS(__stdcall * PtrToGetQueueStatus)(unsigned long, unsigned long *)
Definition: CInterfaceFTDI.h:232
mrpt::comms::TFTDIDevice::ftdi_manufacturer
std::string ftdi_manufacturer
Definition: CInterfaceFTDI.h:27
string
GLsizei const GLchar ** string
Definition: glext.h:4101
mrpt::comms::CInterfaceFTDI::OpenBySerialNumber
void OpenBySerialNumber(const std::string &serialNumber)
Open by device serial number.
Definition: CInterfaceFTDI_WIN.cpp:425
mrpt::comms::TFTDIDevice::usb_idProduct
uint16_t usb_idProduct
Definition: CInterfaceFTDI.h:32
mrpt::comms::CInterfaceFTDI::SetLatencyTimer
void SetLatencyTimer(unsigned char latency_ms)
Change the latency timer (in milliseconds) implemented on the FTDI chip: for a few ms,...
Definition: CInterfaceFTDI_WIN.cpp:371
mrpt::comms::CInterfaceFTDI::SetTimeouts
void SetTimeouts(unsigned long dwReadTimeout_ms, unsigned long dwWriteTimeout_ms)
Change read & write timeouts, in milliseconds.
Definition: CInterfaceFTDI_WIN.cpp:349
mrpt::comms::CInterfaceFTDI::checkErrorAndRaise
void checkErrorAndRaise(int errorCode)
Definition: CInterfaceFTDI_WIN.cpp:384
mrpt::comms::CInterfaceFTDI::ftdi_write
void ftdi_write(const void *lpvBuffer, unsigned long dwBuffSize, unsigned long *lpdwBytes)
Definition: CInterfaceFTDI_WIN.cpp:313
mrpt::comms::TFTDIDevice::ftdi_serial
std::string ftdi_serial
Definition: CInterfaceFTDI.h:29
mrpt::comms::operator<<
std::ostream & operator<<(std::ostream &o, const TFTDIDevice &d)
Print out all the information of a FTDI device in textual form.
Definition: CInterfaceFTDI_WIN.cpp:449
mrpt::comms::CInterfaceFTDI::PtrToWrite
FT_STATUS(__stdcall * PtrToWrite)(unsigned long, const void *, unsigned long, unsigned long *)
Definition: CInterfaceFTDI.h:218
mrpt::io::CStream
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: io/CStream.h:30



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST