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::comms
18 {
19 /** A list of FTDI devices and their descriptors.
20  * \sa CInterfaceFTDI::ListAllDevices
21  * \ingroup mrpt_comms_grp
22  */
24 {
28 
32 
33 #if defined(MRPT_OS_LINUX) || defined(__APPLE__)
34  /** Only for Linux: the corresponding libusb's `libusb_device*` (or
35  * `usb_device*` for libftdi <1.2) */
36  void* usb_device_struct;
37 #endif
38 };
39 
40 /** Print out all the information of a FTDI device in textual form. */
41 std::ostream& operator<<(std::ostream& o, const TFTDIDevice& d);
42 
43 /** Used in CInterfaceFTDI::ListAllDevices */
44 using TFTDIDeviceList = std::deque<TFTDIDevice>;
45 
46 /** A definition of a CStream actually representing a USB connection to a FTDI
47  *chip.
48  *
49  * This class implements the communication with FT245BM / FT245RL chips.
50  * Using this class makes a program to depend on:
51  * - Windows: "FT2XX.DLL" and the device drivers (see FTDI website).
52  * - Linux: "libusb.so" (quite standard!), and "libftdi.so" only if linking
53  *against the dynamic library.
54  *
55  * If there is any error during the communications (or loading the Windows
56  *DLL), a std::exception will be raised.
57  *
58  * To write bulk data, use CStream::ReadBuffer and CStream::WriteBuffer.
59  *
60  * Warning: Avoid defining an object of this class in a global scope if you want
61  *to catch all potential
62  * exceptions during the constructors (like DLL not found, etc...)
63  *
64  * VERSIONS:
65  * - 11/APR/2005: Initial development. JLBC
66  * - 16/FEB/2007: Integration into the MRPT framework. Support for device
67  *serial numbers. JLBC
68  * - 15/APR/2008: Implemented for Linux using libftdi. JLBC
69  *
70  * \sa CStream
71  * \ingroup mrpt_comms_grp
72  */
74 {
75  public:
76  /** Constructor, which loads driver interface (the DLL under Windows).
77  */
79 
80  /** Destructor, which closes the connection with the chip and unloads the
81  * driver interface.
82  */
83  virtual ~CInterfaceFTDI();
84 
85  /** This object cannot be copied */
86  CInterfaceFTDI(const CInterfaceFTDI& o) = delete;
87 
88  /** This object cannot be copied */
89  CInterfaceFTDI& operator=(const CInterfaceFTDI& o) = delete;
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 */
108  void ResetDevice();
109 
110  /** Purge the I/O buffers */
111  void Purge();
112 
113  /** Change the latency timer (in milliseconds) implemented on the FTDI chip:
114  * for a few ms, data is not sent to the PC waiting for possible more data,
115  * to save USB trafic. */
116  void SetLatencyTimer(unsigned char latency_ms);
117 
118  /** Change read & write timeouts, in milliseconds. */
119  void SetTimeouts(
120  unsigned long dwReadTimeout_ms, unsigned long dwWriteTimeout_ms);
121 
122  /** Generates a list with all FTDI devices connected right now.
123  */
124  void ListAllDevices(TFTDIDeviceList& outList);
125 
126  /** Tries to read, raising no exception if not all the bytes are available,
127  * but raising one if there is some communication error.
128  */
129  size_t ReadSync(void* Buffer, size_t Count) { return Read(Buffer, Count); }
130  /** Tries to write, raising no exception if not all the bytes are available,
131  * 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
139  *amound of bytes actually read, without waiting for more extra bytes to
140  *arrive (just those already enqued in the stream).
141  * In this class this method actually behaves as expected and does not
142  *fallback to ReadBuffer().
143  * \exception std::exception On any error, or if ZERO bytes are read.
144  */
145  virtual size_t ReadBufferImmediate(void* Buffer, size_t Count);
146 
147  /** Introduces a pure virtual method responsible for reading from the
148  * stream.
149  * It integrates a cache buffer to speed-up sequences of many, small
150  * readings.
151  */
152  size_t Read(void* Buffer, size_t Count);
153 
154  /** Introduces a pure virtual method responsible for writing to the stream.
155  * Write attempts to write up to Count bytes to Buffer, and returns the
156  * number of bytes actually written.
157  */
158  size_t Write(const void* Buffer, size_t Count);
159 
160  /** This virtual method does nothing in this class.
161  */
162  uint64_t Seek(int64_t Offset, CStream::TSeekOrigin Origin = sFromBeginning);
163 
164  /** This virtual method does nothing in this class.
165  */
167 
168  /** This virtual method does nothing in this class.
169  */
170  uint64_t getPosition() const;
171 
172  protected:
173  /** Used in Read */
175 
176  void ftdi_read(
177  void* lpvBuffer, unsigned long dwBuffSize,
178  unsigned long* lpdwBytesRead);
179  void ftdi_write(
180  const void* lpvBuffer, unsigned long dwBuffSize,
181  unsigned long* lpdwBytes);
182 
183 #if defined(_WIN32)
184  private:
185  void checkErrorAndRaise(int errorCode);
186 
187  void ftdi_open(void* pvDevice);
188  void ftdi_openEx(void* pArg1, unsigned long dwFlags);
189  void ftdi_listDevices(void* pArg1, void* pArg2, unsigned long dwFlags);
190  void ftdi_getQueueStatus(unsigned long* lpdwAmountInRxQueue);
191 
192  void* m_hmodule;
193  unsigned long m_ftHandle;
194 
195  void loadDriver();
196 
198 
199  typedef FT_STATUS(__stdcall* PtrToOpen)(void*, unsigned long*);
201 
202  typedef FT_STATUS(__stdcall* PtrToOpenEx)(
203  void*, unsigned long, unsigned long*);
205 
206  typedef FT_STATUS(__stdcall* PtrToListDevices)(void*, void*, unsigned long);
208 
209  typedef FT_STATUS(__stdcall* PtrToClose)(unsigned long);
211 
212  typedef FT_STATUS(__stdcall* PtrToRead)(
213  unsigned long, void*, unsigned long, unsigned long*);
215 
216  typedef FT_STATUS(__stdcall* PtrToWrite)(
217  unsigned long, const void*, unsigned long, unsigned long*);
219 
220  typedef FT_STATUS(__stdcall* PtrToResetDevice)(unsigned long);
222 
223  typedef FT_STATUS(__stdcall* PtrToPurge)(unsigned long, unsigned long);
225 
226  typedef FT_STATUS(__stdcall* PtrToSetTimeouts)(
227  unsigned long, unsigned long, unsigned long);
229 
230  typedef FT_STATUS(__stdcall* PtrToGetQueueStatus)(
231  unsigned long, unsigned long*);
233 
234  typedef FT_STATUS(__stdcall* PtrToSetLatencyTimer)(
235  unsigned long, unsigned char);
237 
238 #else
239  // Declarations for Linux:
240  void* m_ftdi_context;
241 
242  /** Process recursively a USB device and its children: */
243  void recursive_fill_list_devices(
244  void* usb_device_structure, TFTDIDeviceList& outList);
245 
246 #endif
247 
248 }; // end of class
249 
250 }
251 
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:28
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
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)
Serial and networking devices and utilities.
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: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020