Main MRPT website > C++ reference for MRPT 1.4.0
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-2016, 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
10#ifndef CInterfaceFTDI_H
11#define CInterfaceFTDI_H
12
13#include <mrpt/config.h>
14#include <mrpt/utils/CStream.h>
16
18#include <deque>
19
20namespace mrpt
21{
22 namespace hwdrivers
23 {
24
25 /** A list of FTDI devices and their descriptors.
26 * \sa CInterfaceFTDI::ListAllDevices
27 * \ingroup mrpt_hwdrivers_grp
28 */
30 {
31 std::string ftdi_manufacturer;
32 std::string ftdi_description;
33 std::string ftdi_serial;
34
38
39 #if defined(MRPT_OS_LINUX) || defined(MRPT_OS_APPLE)
40 /** Only for Linux: the corresponding libusb's `libusb_device*` (or `usb_device*` for libftdi <1.2) */
41 void *usb_device_struct;
42 #endif
43 };
44
45 /** Print out all the information of a FTDI device in textual form. */
46 std::ostream HWDRIVERS_IMPEXP &operator << ( std::ostream &o, const TFTDIDevice &d);
47
48 /** Used in CInterfaceFTDI::ListAllDevices */
49 typedef std::deque<TFTDIDevice> TFTDIDeviceList;
50
51 /** A definition of a CStream actually representing a USB connection to a FTDI chip.
52 *
53 * This class implements the communication with FT245BM / FT245RL chips.
54 * Using this class makes a program to depend on:
55 * - Windows: "FT2XX.DLL" and the device drivers (see FTDI website).
56 * - Linux: "libusb.so" (quite standard!), and "libftdi.so" only if linking against the dynamic library.
57 *
58 * If there is any error during the communications (or loading the Windows DLL), a std::exception will be raised.
59 *
60 * To write bulk data, use CStream::ReadBuffer and CStream::WriteBuffer. See also the derived classes for
61 * higher level communication: CInterfaceFTDIMessages
62 *
63 * Warning: Avoid defining an object of this class in a global scope if you want 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 serial numbers. JLBC
69 * - 15/APR/2008: Implemented for Linux using libftdi. JLBC
70 *
71 * \sa CInterfaceFTDIMessages, CStream
72 * \ingroup mrpt_hwdrivers_grp
73 */
75 {
76 public:
77 /** Constructor, which loads driver interface (the DLL under Windows).
78 */
80
81 /** Destructor, which closes the connection with the chip and unloads the driver interface.
82 */
83 virtual ~CInterfaceFTDI();
84
85 /** This object cannot be copied */
87
88 /** This object cannot be copied */
89 CInterfaceFTDI& operator =(const CInterfaceFTDI &o);
90
91 /** Checks whether the chip has been successfully open.
92 * \sa OpenBySerialNumber, OpenByDescription
93 */
94 bool isOpen();
95
96 /** Open by device serial number
97 */
98 void OpenBySerialNumber( const std::string &serialNumber );
99
100 /** Open by device description
101 */
102 void OpenByDescription( const std::string &description );
103
104 /** Close the USB device */
105 void Close();
106
107 /** Reset the USB device */
109
110 /** Purge the I/O buffers */
111 void Purge();
112
113 /** 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. */
114 void SetLatencyTimer (unsigned char latency_ms);
115
116 /** Change read & write timeouts, in milliseconds. */
117 void SetTimeouts(unsigned long dwReadTimeout_ms, unsigned long dwWriteTimeout_ms);
118
119
120 /** Generates a list with all FTDI devices connected right now.
121 */
123
124 /** Tries to read, raising no exception if not all the bytes are available, but raising one if there is some communication error.
125 */
126 size_t ReadSync(void *Buffer, size_t Count)
127 {
128 return Read(Buffer,Count);
129 }
130
131 /** Tries to write, raising no exception if not all the bytes are available, but raising one if there is some communication error.
132 */
133 size_t WriteSync(const void *Buffer, size_t Count)
134 {
135 return Write(Buffer,Count);
136 }
137
138 /** 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).
139 * In this class this method actually behaves as expected and does not fallback to ReadBuffer().
140 * \exception std::exception On any error, or if ZERO bytes are read.
141 */
142 virtual size_t ReadBufferImmediate(void *Buffer, size_t Count);
143
144 protected:
145 /** Introduces a pure virtual method responsible for reading from the stream.
146 * It integrates a cache buffer to speed-up sequences of many, small readings.
147 */
148 size_t Read(void *Buffer, size_t Count);
149
151
152 /** Introduces a pure virtual method responsible for writing to the stream.
153 * Write attempts to write up to Count bytes to Buffer, and returns the number of bytes actually written.
154 */
155 size_t Write(const void *Buffer, size_t Count);
156
157 /** This virtual method does nothing in this class.
158 */
159 uint64_t Seek(uint64_t Offset, CStream::TSeekOrigin Origin = sFromBeginning);
160
161 /** This virtual method does nothing in this class.
162 */
164
165 /** This virtual method does nothing in this class.
166 */
167 uint64_t getPosition();
168
169
170 void ftdi_read(void *lpvBuffer, unsigned long dwBuffSize, unsigned long *lpdwBytesRead);
171 void ftdi_write(const void *lpvBuffer, unsigned long dwBuffSize, unsigned long *lpdwBytes);
172
173 #if defined(MRPT_OS_WINDOWS)
174 private:
175 void checkErrorAndRaise(int errorCode);
176
177 void ftdi_open(void* pvDevice);
178 void ftdi_openEx(void* pArg1, unsigned long dwFlags);
179 void ftdi_listDevices(void *pArg1, void *pArg2, unsigned long dwFlags);
180 void ftdi_getQueueStatus(unsigned long *lpdwAmountInRxQueue);
181
182 void *m_hmodule;
183 unsigned long m_ftHandle;
184
185 void loadDriver();
186
187 enum FT_STATUS
188 {
189 dummy
190 };
191
192 typedef FT_STATUS (__stdcall *PtrToOpen)(void*, unsigned long *);
193 PtrToOpen m_pOpen;
194
195 typedef FT_STATUS (__stdcall *PtrToOpenEx)(void*, unsigned long, unsigned long *);
196 PtrToOpenEx m_pOpenEx;
197
198 typedef FT_STATUS (__stdcall *PtrToListDevices)(void*, void*, unsigned long);
199 PtrToListDevices m_pListDevices;
200
201 typedef FT_STATUS (__stdcall *PtrToClose)(unsigned long );
202 PtrToClose m_pClose;
203
204 typedef FT_STATUS (__stdcall *PtrToRead)(unsigned long , void *, unsigned long, unsigned long *);
205 PtrToRead m_pRead;
206
207 typedef FT_STATUS (__stdcall *PtrToWrite)(unsigned long , const void *, unsigned long, unsigned long *);
208 PtrToWrite m_pWrite;
209
210 typedef FT_STATUS (__stdcall *PtrToResetDevice)(unsigned long );
211 PtrToResetDevice m_pResetDevice;
212
213 typedef FT_STATUS (__stdcall *PtrToPurge)(unsigned long , unsigned long);
214 PtrToPurge m_pPurge;
215
216 typedef FT_STATUS (__stdcall *PtrToSetTimeouts)(unsigned long , unsigned long, unsigned long);
217 PtrToSetTimeouts m_pSetTimeouts;
218
219 typedef FT_STATUS (__stdcall *PtrToGetQueueStatus)(unsigned long , unsigned long *);
220 PtrToGetQueueStatus m_pGetQueueStatus;
221
222 typedef FT_STATUS (__stdcall *PtrToSetLatencyTimer )(unsigned long , unsigned char);
223 PtrToSetLatencyTimer m_pSetLatencyTimer;
224
225 #else
226 // Declarations for Linux:
228
229 /** Process recursively a USB device and its children: */
230 void recursive_fill_list_devices( void *usb_device_structure , TFTDIDeviceList &outList );
231
232
233 #endif
234
235
236 }; // end of class
237
238 } // end of namespace
239} // end of namespace
240
241#endif
A definition of a CStream actually representing a USB connection to a FTDI chip.
bool isOpen()
Checks whether the chip has been successfully open.
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 ...
void ftdi_write(const void *lpvBuffer, unsigned long dwBuffSize, unsigned long *lpdwBytes)
void SetLatencyTimer(unsigned char latency_ms)
Change the latency timer (in milliseconds) implemented on the FTDI chip: for a few ms,...
void OpenBySerialNumber(const std::string &serialNumber)
Open by device serial number.
mrpt::utils::circular_buffer< uint8_t > m_readBuffer
Used in Read.
void SetTimeouts(unsigned long dwReadTimeout_ms, unsigned long dwWriteTimeout_ms)
Change read & write timeouts, in milliseconds.
void Purge()
Purge the I/O buffers.
CInterfaceFTDI(const CInterfaceFTDI &o)
This object cannot be copied.
uint64_t Seek(uint64_t Offset, CStream::TSeekOrigin Origin=sFromBeginning)
This virtual method does nothing in this class.
uint64_t getPosition()
This virtual method does nothing in this class.
void recursive_fill_list_devices(void *usb_device_structure, TFTDIDeviceList &outList)
Process recursively a USB device and its children:
void OpenByDescription(const std::string &description)
Open by device description.
void ResetDevice()
Reset the USB device.
void ftdi_read(void *lpvBuffer, unsigned long dwBuffSize, unsigned long *lpdwBytesRead)
void Close()
Close the USB device.
size_t Read(void *Buffer, size_t Count)
Introduces a pure virtual method responsible for reading from the stream.
CInterfaceFTDI()
Constructor, which loads driver interface (the DLL under Windows).
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...
virtual ~CInterfaceFTDI()
Destructor, which closes the connection with the chip and unloads the driver interface.
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,...
uint64_t getTotalBytesCount()
This virtual method does nothing in this class.
void ListAllDevices(TFTDIDeviceList &outList)
Generates a list with all FTDI devices connected right now.
size_t Write(const void *Buffer, size_t Count)
Introduces a pure virtual method responsible for writing to the stream.
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:39
A circular buffer of fixed size (defined at construction-time), implemented with a std::vector as the...
#define HWDRIVERS_IMPEXP
std::ostream HWDRIVERS_IMPEXP & operator<<(std::ostream &o, const TFTDIDevice &d)
Print out all the information of a FTDI device in textual form.
std::deque< TFTDIDevice > TFTDIDeviceList
Used in CInterfaceFTDI::ListAllDevices.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
unsigned int uint16_t
Definition: pstdint.h:170
unsigned char uint8_t
Definition: pstdint.h:143
A list of FTDI devices and their descriptors.



Page generated by Doxygen 1.9.6 for MRPT 1.4.0 SVN: at Wed Mar 22 09:54:56 UTC 2023