sprint 1-alpha
sprint/io/ftdiserial.h
Go to the documentation of this file.
00001 /******************************************************************************
00002 *   sprint::ftdiserial
00003 *
00004 *   Copyright (C) 2005-2011  Paolo Medici <www.pmx.it>
00005 *
00006 *  This library is free software; you can redistribute it and/or
00007 *  modify it under the terms of the GNU Lesser General Public
00008 *  License as published by the Free Software Foundation; either
00009 *  version 2.1 of the License, or (at your option) any later version.
00010 *
00011 *  This library is distributed in the hope that it will be useful,
00012 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 *  Lesser General Public License for more details.
00015 *
00016 *  You should have received a copy of the GNU Lesser General Public
00017 *  License along with this library; if not, write to the Free Software
00018 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00019 *
00020 *******************************************************************************/
00021 
00022 #ifndef _SPRINT_FTDI_SERIAL_H
00023 #define _SPRINT_FTDI_SERIAL_H
00024 
00030 #define OUTPUTDEBUGSTRING(a) 
00031 
00032 #ifdef WIN32
00033 # include <windows.h>
00034 #endif
00035 
00036 #include <sprint/cpptraits.h>
00037 #include <sprint/log.h>
00038 
00039 #include <ftd2xx.h>
00040 #include <vector>
00041 #include <string>
00042 
00043 namespace sprint {
00044           
00046 template<bool handle>
00047 class Tftdiserial: public if_copiable<handle> {
00048 public:
00049   
00050         FT_HANDLE m_handle;
00051     FT_STATUS m_ftStatus;
00052 
00053   static  const char *ErrToStr(int err)
00054   {
00055   static const char *errs[]={
00056         "FT_OK",
00057         "FT_INVALID_HANDLE",
00058         "FT_DEVICE_NOT_FOUND",
00059         "FT_DEVICE_NOT_OPENED",
00060         "FT_IO_ERROR",
00061         "FT_INSUFFICIENT_RESOURCES",
00062         "FT_INVALID_PARAMETER",
00063         "FT_INVALID_BAUD_RATE",
00064 
00065         "FT_DEVICE_NOT_OPENED_FOR_ERASE",
00066         "FT_DEVICE_NOT_OPENED_FOR_WRITE",
00067         "FT_FAILED_TO_WRITE_DEVICE",
00068         "FT_EEPROM_READ_FAILED",
00069         "FT_EEPROM_WRITE_FAILED",
00070         "FT_EEPROM_ERASE_FAILED",
00071         "FT_EEPROM_NOT_PRESENT",
00072         "FT_EEPROM_NOT_PROGRAMMED",
00073         "FT_INVALID_ARGS",
00074         "FT_NOT_SUPPORTED",
00075         "FT_OTHER_ERROR",
00076         "FT_DEVICE_LIST_NOT_READY"};
00077         return errs[err]; }
00078 
00079 public:
00080         Tftdiserial() : m_handle(0) { }
00081         
00083         Tftdiserial(int index)
00084         {
00085       open(index);
00086     }
00087         
00088         ~Tftdiserial()
00089         {
00090        if(!handle)
00091          FT_Close(m_handle);    
00092     }
00093     
00094     bool open(int index)
00095     {
00096          m_ftStatus = FT_Open(index, &m_handle);
00097          return m_ftStatus == FT_OK;
00098     }
00099     
00100     bool set_timeout(int read, int write)
00101     {
00102          m_ftStatus = FT_SetTimeouts(m_handle, read, write);
00103          return m_ftStatus == FT_OK;
00104      }
00105      
00106     bool set_queue_lenght(int l)
00107     { 
00108          m_ftStatus = FT_SetUSBParameters(m_handle, l, 4096);
00109          return m_ftStatus == FT_OK;
00110     }
00111 
00112     unsigned int queue_lenght() 
00113     {
00114         DWORD dwQueueLenght;
00115         m_ftStatus = FT_GetQueueStatus(m_handle, &dwQueueLenght);
00116         return dwQueueLenght;
00117     }
00118     
00119     bool disable_flow_control()
00120     {
00121          m_ftStatus = FT_SetFlowControl(m_handle, FT_FLOW_NONE, 0, 0);
00122          return m_ftStatus == FT_OK;
00123     }
00124 
00125     bool set_flow_control(unsigned int flow_control, unsigned char xOn, unsigned char xOff)
00126     {
00127     }
00128     
00129     bool set_format(unsigned int word_lenght = FT_BITS_8, unsigned int stop_bits = FT_STOP_BITS_1, int parity = FT_PARITY_NONE)
00130     {
00131          m_ftStatus = FT_SetDataCharacteristics(m_handle, word_lenght, stop_bits, parity);
00132          return m_ftStatus == FT_OK;
00133     }
00134     
00135         bool set_baud_rate(unsigned int rate)
00136         {
00137          m_ftStatus = FT_SetBaudRate(m_handle, rate);
00138          return m_ftStatus == FT_OK;
00139         }
00140 
00141         inline void close()
00142                 {
00143         FT_Close(m_handle);
00144                 m_handle = 0;
00145                 }
00146 
00147   int read(char *buf, unsigned int size)
00148   {
00149       DWORD dwBytesReaded;
00150       m_ftStatus = FT_Read(m_handle, (LPVOID) buf, size, &dwBytesReaded);
00151       
00152       if(m_ftStatus!=FT_OK)
00153         {
00154         logPutsf(LOG_LEVEL_ERROR, "FT_Read error: return %u (%s)\n", m_ftStatus, ErrToStr(m_ftStatus) );
00155         return -1;
00156         }
00157       
00158       return dwBytesReaded;
00159   }
00160 
00161   int write(const char *buf, unsigned int size)
00162   {
00163       DWORD dwWritten;
00164       m_ftStatus = FT_Write(m_handle, (LPVOID) buf, size, &dwWritten);
00165       return dwWritten;
00166   }
00167   
00168   bool good() const { return m_ftStatus == FT_OK; }
00169 
00170 };
00171 
00173 typedef Tftdiserial<false> ftdiserial;
00174 
00176 typedef Tftdiserial<true> ftdiserialhandle;
00177 
00178 } // sprint
00179 
00180 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines