12 #ifdef MRPT_OS_WINDOWS 28 #define FTD2XX_API __declspec(dllexport) 30 #define FTD2XX_API __declspec(dllimport) 34 typedef unsigned long FT_HANDLE;
40 #define FT_OPEN_BY_SERIAL_NUMBER 1 41 #define FT_OPEN_BY_DESCRIPTION 2 47 #define FT_LIST_NUMBER_ONLY 0x80000000 48 #define FT_LIST_BY_INDEX 0x40000000 49 #define FT_LIST_ALL 0x20000000 51 #define FT_LIST_MASK (FT_LIST_NUMBER_ONLY|FT_LIST_BY_INDEX|FT_LIST_ALL) 57 #define FT_BAUD_300 300 58 #define FT_BAUD_600 600 59 #define FT_BAUD_1200 1200 60 #define FT_BAUD_2400 2400 61 #define FT_BAUD_4800 4800 62 #define FT_BAUD_9600 9600 63 #define FT_BAUD_14400 14400 64 #define FT_BAUD_19200 19200 65 #define FT_BAUD_38400 38400 66 #define FT_BAUD_57600 57600 67 #define FT_BAUD_115200 115200 68 #define FT_BAUD_230400 230400 69 #define FT_BAUD_460800 460800 70 #define FT_BAUD_921600 921600 76 #define FT_BITS_8 (unsigned char) 8 77 #define FT_BITS_7 (unsigned char) 7 78 #define FT_BITS_6 (unsigned char) 6 79 #define FT_BITS_5 (unsigned char) 5 85 #define FT_STOP_BITS_1 (unsigned char) 0 86 #define FT_STOP_BITS_1_5 (unsigned char) 1 87 #define FT_STOP_BITS_2 (unsigned char) 2 93 #define FT_PARITY_NONE (unsigned char) 0 94 #define FT_PARITY_ODD (unsigned char) 1 95 #define FT_PARITY_EVEN (unsigned char) 2 96 #define FT_PARITY_MARK (unsigned char) 3 97 #define FT_PARITY_SPACE (unsigned char) 4 103 #define FT_FLOW_NONE 0x0000 104 #define FT_FLOW_RTS_CTS 0x0100 105 #define FT_FLOW_DTR_DSR 0x0200 106 #define FT_FLOW_XON_XOFF 0x0400 111 #define FT_PURGE_RX 1 112 #define FT_PURGE_TX 2 118 typedef void (*PFT_EVENT_HANDLER)(
unsigned long,
unsigned long);
120 #define FT_EVENT_RXCHAR 1 121 #define FT_EVENT_MODEM_STATUS 2 127 #define FT_DEFAULT_RX_TIMEOUT 300 128 #define FT_DEFAULT_TX_TIMEOUT 300 141 using namespace mrpt;
150 CInterfaceFTDI::CInterfaceFTDI() :
166 if(m_hmodule != NULL)
172 FreeLibrary( (HMODULE)m_hmodule );
198 return m_ftHandle!=0;
204 void CInterfaceFTDI::loadDriver()
210 m_hmodule = ::LoadLibraryA(
"Ftd2xx.dll");
211 if(m_hmodule == NULL)
THROW_EXCEPTION(
"Error: Cannot load Ftd2xx.dll");
213 m_pWrite = (PtrToWrite)GetProcAddress((HMODULE)m_hmodule,
"FT_Write");
214 m_pRead = (PtrToRead)GetProcAddress((HMODULE)m_hmodule,
"FT_Read");
215 m_pOpen = (PtrToOpen)GetProcAddress((HMODULE)m_hmodule,
"FT_Open");
216 m_pOpenEx = (PtrToOpenEx)GetProcAddress((HMODULE)m_hmodule,
"FT_OpenEx");
217 m_pListDevices = (PtrToListDevices)GetProcAddress((HMODULE)m_hmodule,
"FT_ListDevices");
218 m_pClose = (PtrToClose)GetProcAddress((HMODULE)m_hmodule,
"FT_Close");
219 m_pResetDevice = (PtrToResetDevice)GetProcAddress((HMODULE)m_hmodule,
"FT_ResetDevice");
220 m_pPurge = (PtrToPurge)GetProcAddress((HMODULE)m_hmodule,
"FT_Purge");
221 m_pSetTimeouts = (PtrToSetTimeouts)GetProcAddress((HMODULE)m_hmodule,
"FT_SetTimeouts");
222 m_pGetQueueStatus = (PtrToGetQueueStatus)GetProcAddress((HMODULE)m_hmodule,
"FT_GetQueueStatus");
223 m_pSetLatencyTimer = (PtrToSetLatencyTimer)GetProcAddress((HMODULE)m_hmodule,
"FT_SetLatencyTimer");
225 if( !m_pWrite || !m_pRead || !m_pOpen ||
226 !m_pOpenEx || !m_pListDevices || !m_pClose ||
227 !m_pResetDevice || !m_pPurge || !m_pSetTimeouts ||
228 !m_pGetQueueStatus || !m_pSetLatencyTimer)
237 void CInterfaceFTDI::ftdi_open(
void* pvDevice)
243 checkErrorAndRaise( (*m_pOpen)(pvDevice, &m_ftHandle ) );
248 void CInterfaceFTDI::ftdi_openEx(
void*pArg1,
unsigned long dwFlags)
254 checkErrorAndRaise( (*m_pOpenEx)(pArg1,dwFlags,&m_ftHandle) );
268 unsigned long nConectedDevices;
272 ftdi_listDevices(&nConectedDevices,NULL, 0x80000000);
274 for (
size_t i=0;i<nConectedDevices;i++)
279 ftdi_listDevices( (
void*)(i),(
void*)str, (
unsigned long)(0x40000000 | 1));
283 ftdi_listDevices( (
void*)(i),(
void*)str, (
unsigned long)(0x40000000 | 2));
286 outList.push_back(newEntry);
293 void CInterfaceFTDI::ftdi_listDevices(
void*pArg1,
void*pArg2,
unsigned long dwFlags)
298 checkErrorAndRaise( (*m_pListDevices)(pArg1, pArg2, dwFlags) );
310 (*m_pClose)( m_ftHandle );
324 checkErrorAndRaise( (*m_pRead)( m_ftHandle, lpvBuffer, dwBuffSize, lpdwBytesRead ) );
334 checkErrorAndRaise( (*m_pWrite)( m_ftHandle, lpvBuffer, dwBuffSize, lpdwBytes ) );
344 checkErrorAndRaise( (*m_pResetDevice) ( m_ftHandle ));
356 unsigned long dwMask = FT_PURGE_RX | FT_PURGE_TX;
357 checkErrorAndRaise( (*m_pPurge)( m_ftHandle, dwMask ) );
368 checkErrorAndRaise( (*m_pSetTimeouts)( m_ftHandle, dwReadTimeout_ms,dwWriteTimeout_ms ) );
373 void CInterfaceFTDI::ftdi_getQueueStatus(
unsigned long *lpdwAmountInRxQueue)
378 checkErrorAndRaise( (*m_pGetQueueStatus) ( m_ftHandle, lpdwAmountInRxQueue ) );
388 checkErrorAndRaise( (*m_pSetLatencyTimer)( m_ftHandle, latency_ms ) );
396 void CInterfaceFTDI::checkErrorAndRaise(
int errorCode)
416 case 5:
THROW_EXCEPTION(
"*** FTD2XX ERROR ***: FT_INSUFFICIENT_RESOURCES");
418 default:
THROW_EXCEPTION(
"*** FTD2XX ERROR ***: Invalid error code!?!?!?");
431 ftdi_openEx( (
void*)serialNumber.c_str(), FT_OPEN_BY_SERIAL_NUMBER);
443 ftdi_openEx( (
void*)description.c_str(), FT_OPEN_BY_DESCRIPTION);
void SetTimeouts(unsigned long dwReadTimeout_ms, unsigned long dwWriteTimeout_ms)
Change read & write timeouts, in milliseconds.
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
std::ostream HWDRIVERS_IMPEXP & operator<<(std::ostream &o, const TFTDIDevice &d)
Print out all the information of a FTDI device in textual form.
void ftdi_write(const void *lpvBuffer, unsigned long dwBuffSize, unsigned long *lpdwBytes)
void clear()
Delete all the stored data, if any.
#define THROW_EXCEPTION(msg)
void OpenBySerialNumber(const std::string &serialNumber)
Open by device serial number.
bool isOpen()
Checks whether the chip has been successfully open.
Contains classes for various device interfaces.
void Purge()
Purge the I/O buffers.
CInterfaceFTDI()
Constructor, which loads driver interface (the DLL under Windows).
std::string ftdi_description
CInterfaceFTDI & operator=(const CInterfaceFTDI &o)
This object cannot be copied.
std::string BASE_IMPEXP format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
void ftdi_read(void *lpvBuffer, unsigned long dwBuffSize, unsigned long *lpdwBytesRead)
GLsizei const GLchar ** string
void OpenByDescription(const std::string &description)
Open by device description.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
std::string ftdi_manufacturer
void SetLatencyTimer(unsigned char latency_ms)
Change the latency timer (in milliseconds) implemented on the FTDI chip: for a few ms...
typedef void(APIENTRYP PFNGLBLENDCOLORPROC)(GLclampf red
void ListAllDevices(TFTDIDeviceList &outList)
Generates a list with all FTDI devices connected right now.
std::deque< TFTDIDevice > TFTDIDeviceList
Used in CInterfaceFTDI::ListAllDevices.
A list of FTDI devices and their descriptors.
A definition of a CStream actually representing a USB connection to a FTDI chip.
void ResetDevice()
Reset the USB device.
mrpt::utils::circular_buffer< uint8_t > m_readBuffer
Used in Read.
virtual ~CInterfaceFTDI()
Destructor, which closes the connection with the chip and unloads the driver interface.
void Close()
Close the USB device.