MRPT  2.0.0
CInterfaceFTDI.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #pragma once
10 
11 #include <mrpt/config.h>
13 #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 {
25  std::string ftdi_manufacturer;
26  std::string ftdi_description;
27  std::string ftdi_serial;
28 
29  uint16_t usb_idVendor;
30  uint16_t usb_idProduct;
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  ~CInterfaceFTDI() override;
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  size_t ReadBufferImmediate(void* Buffer, size_t Count) override;
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) override;
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) override;
159 
160  /** This virtual method does nothing in this class.
161  */
162  uint64_t Seek(
163  int64_t Offset, CStream::TSeekOrigin Origin = sFromBeginning) override;
164 
165  /** This virtual method does nothing in this class.
166  */
167  uint64_t getTotalBytesCount() const override;
168 
169  /** This virtual method does nothing in this class.
170  */
171  uint64_t getPosition() const override;
172 
173  protected:
174  /** Used in Read */
176 
177  void ftdi_read(
178  void* lpvBuffer, unsigned long dwBuffSize,
179  unsigned long* lpdwBytesRead);
180  void ftdi_write(
181  const void* lpvBuffer, unsigned long dwBuffSize,
182  unsigned long* lpdwBytes);
183 
184 #if defined(_WIN32)
185  private:
186  void checkErrorAndRaise(int errorCode);
187 
188  void ftdi_open(void* pvDevice);
189  void ftdi_openEx(void* pArg1, unsigned long dwFlags);
190  void ftdi_listDevices(void* pArg1, void* pArg2, unsigned long dwFlags);
191  void ftdi_getQueueStatus(unsigned long* lpdwAmountInRxQueue);
192 
193  void* m_hmodule;
194  unsigned long m_ftHandle;
195 
196  void loadDriver();
197 
199 
200  typedef FT_STATUS(__stdcall* PtrToOpen)(void*, unsigned long*);
202 
203  typedef FT_STATUS(__stdcall* PtrToOpenEx)(
204  void*, unsigned long, unsigned long*);
206 
207  typedef FT_STATUS(__stdcall* PtrToListDevices)(void*, void*, unsigned long);
209 
210  typedef FT_STATUS(__stdcall* PtrToClose)(unsigned long);
212 
213  typedef FT_STATUS(__stdcall* PtrToRead)(
214  unsigned long, void*, unsigned long, unsigned long*);
216 
217  typedef FT_STATUS(__stdcall* PtrToWrite)(
218  unsigned long, const void*, unsigned long, unsigned long*);
220 
221  typedef FT_STATUS(__stdcall* PtrToResetDevice)(unsigned long);
223 
224  typedef FT_STATUS(__stdcall* PtrToPurge)(unsigned long, unsigned long);
226 
227  typedef FT_STATUS(__stdcall* PtrToSetTimeouts)(
228  unsigned long, unsigned long, unsigned long);
230 
231  typedef FT_STATUS(__stdcall* PtrToGetQueueStatus)(
232  unsigned long, unsigned long*);
234 
235  typedef FT_STATUS(__stdcall* PtrToSetLatencyTimer)(
236  unsigned long, unsigned char);
238 
239 #else
240  // Declarations for Linux:
241  void* m_ftdi_context;
242 
243  /** Process recursively a USB device and its children: */
244  void recursive_fill_list_devices(
245  void* usb_device_structure, TFTDIDeviceList& outList);
246 
247 #endif
248 
249 }; // end of class
250 
251 } // namespace mrpt::comms
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.
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
PtrToListDevices m_pListDevices
std::deque< TFTDIDevice > TFTDIDeviceList
Used in CInterfaceFTDI::ListAllDevices.
size_t Write(const void *Buffer, size_t Count) override
Introduces a pure virtual method responsible for writing to the stream.
FT_STATUS(__stdcall * PtrToClose)(unsigned long)
mrpt::containers::circular_buffer< uint8_t > m_readBuffer
Used in Read.
uint64_t Seek(int64_t Offset, CStream::TSeekOrigin Origin=sFromBeginning) override
This virtual method does nothing in this class.
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
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.
size_t Read(void *Buffer, size_t Count) override
Introduces a pure virtual method responsible for reading from the stream.
void ftdi_openEx(void *pArg1, unsigned long dwFlags)
void Close()
Close the USB device.
void SetTimeouts(unsigned long dwReadTimeout_ms, unsigned long dwWriteTimeout_ms)
Change read & write timeouts, in milliseconds.
uint64_t getPosition() const override
This virtual method does nothing in this class.
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.
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 ...
void ftdi_read(void *lpvBuffer, unsigned long dwBuffSize, unsigned long *lpdwBytesRead)
size_t ReadBufferImmediate(void *Buffer, size_t Count) override
Reads a block of bytes from the stream into Buffer, and returns the amound of bytes actually read...
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)
uint64_t getTotalBytesCount() const override
This virtual method does nothing in this class.
Serial and networking devices and utilities.
PtrToGetQueueStatus m_pGetQueueStatus
~CInterfaceFTDI() override
Destructor, which closes the connection with the chip and unloads the driver interface.
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 2.0.0 Git: b38439d21 Tue Mar 31 19:58:06 2020 +0200 at miƩ abr 1 00:50:30 CEST 2020