serialbase.h
Go to the documentation of this file.
00001 //======================================================================
00028 //======================================================================
00029 
00030 #ifndef SERIALBASE_H_
00031 #define SERIALBASE_H_
00032 
00033 #include "sdhlibrary_settings.h"
00034 
00035 #if SDH_USE_VCC
00036 # pragma warning(disable : 4290)
00037 #endif
00038 
00039 //----------------------------------------------------------------------
00040 // System Includes - include with <>
00041 //----------------------------------------------------------------------
00042 
00043 #if SDH_USE_VCC
00044 # include <windows.h>
00045 # include <strsafe.h>
00046 #else
00047 # include <errno.h>
00048 #endif
00049 
00050 //----------------------------------------------------------------------
00051 // Project Includes - include with ""
00052 //----------------------------------------------------------------------
00053 
00054 #include "sdhexception.h"
00055 #include "dbg.h"
00056 #include "sdhbase.h" // for g_sdh_debug_log
00057 
00058 //----------------------------------------------------------------------
00059 // Defines, enums, unions, structs,
00060 //----------------------------------------------------------------------
00061 
00062 NAMESPACE_SDH_START
00063 
00064 typedef void* tDeviceHandle;  
00065 
00066 #if SDH_USE_VCC
00067 # define snprintf _snprintf
00068 #endif
00069 
00070 //----------------------------------------------------------------------
00071 // Global variables
00072 //----------------------------------------------------------------------
00073 
00074 
00075 //----------------------------------------------------------------------
00076 // Function and class member declarations
00077 //----------------------------------------------------------------------
00078 
00079 
00080 
00081 
00082 
00086 class VCC_EXPORT cSerialBaseException: public cSDHErrorCommunication
00087 {
00088  public:
00089     cSerialBaseException( cMsg const & _msg )
00090         : cSDHErrorCommunication( "cSerialBaseException", _msg )
00091     {}
00092 
00093     cSerialBaseException( char const* _type, cMsg const & _msg )
00094         : cSDHErrorCommunication( _type, _msg )
00095     {}
00096 };
00097 //======================================================================
00098 
00099 
00105 class VCC_EXPORT cSerialBase
00106 {
00107  protected:
00108 
00110     char ungetch;
00111 
00113     bool ungetch_valid;
00114 
00116     double timeout;
00117 
00118  public:
00120     cSerialBase()
00121     : // init members
00122         ungetch('\0'),
00123         ungetch_valid(false),
00124         // setting the timeout does not make sense here. should be left to virtual SetTimeout()
00125         dbg( false, "cyan", g_sdh_debug_log )
00126     {
00127         // nothing more to do
00128     }
00129 
00131     virtual ~cSerialBase( void )
00132     {
00134     }
00135 
00137 
00140     virtual void Open( void )
00141         throw (cSerialBaseException*) = 0;
00142 
00144     virtual bool IsOpen( void )
00145         throw() = 0;
00146 
00148     virtual void Close( void )
00149         throw (cSerialBaseException*) = 0;
00150 
00152     virtual void SetTimeout( double _timeout )
00153         throw (cSerialBaseException*)
00154     {
00155         timeout = _timeout;
00156     }
00157 
00159     virtual double GetTimeout()
00160     {
00161         return timeout;
00162     }
00163 
00165     class cSetTimeoutTemporarily
00166     {
00167         cSerialBase* serial_base;
00168         double       old_timeout;
00169     public:
00171         cSetTimeoutTemporarily( cSerialBase* _serial_base, double new_timeout )
00172         : serial_base(_serial_base),
00173           old_timeout( serial_base->GetTimeout() )
00174         {
00175             if ( new_timeout != old_timeout )
00176                 serial_base->SetTimeout( new_timeout );
00177         }
00178 
00180         ~cSetTimeoutTemporarily()
00181         {
00182             if ( old_timeout != serial_base->GetTimeout() )
00183                 serial_base->SetTimeout( old_timeout );
00184         }
00185     };
00186 
00187 
00189 
00197     virtual int write( char const *ptr, int len=0 )
00198         throw (cSerialBaseException*) = 0;
00199 
00208     virtual ssize_t Read( void *data, ssize_t size, long timeout_us, bool return_on_less_data )
00209         throw (cSerialBaseException*) = 0;
00210 
00212 
00228     virtual char* readline( char* line, int size, char const* eol = "\n", bool return_on_less_data = false )
00229         throw (cSerialBaseException*);
00230 
00232     cDBG dbg;
00233 
00235 #if SDH_USE_VCC
00236     typedef DWORD tErrorCode;
00237 #else
00238     typedef int tErrorCode;
00239 #endif
00240 
00246     virtual tErrorCode GetErrorNumber()
00247     {
00248 #if SDH_USE_VCC
00249         return GetLastError();
00250 #else
00251         return errno;
00252 #endif
00253     }
00254 
00262     virtual char const* GetErrorMessage( tErrorCode dw );
00263 
00265     char const* GetLastErrorMessage( void )
00266     {
00267         return GetErrorMessage( GetErrorNumber() );
00268     }
00269 
00275     virtual bool UseCRC16()
00276     {
00277         return false;
00278     }
00279     //----------------------------------------------------------------------
00280 
00281 };
00282 //======================================================================
00283 
00284 NAMESPACE_SDH_END
00285 
00286 #endif
00287 
00288 
00289 //======================================================================
00290 /*
00291   Here are some settings for the emacs/xemacs editor (and can be safely ignored):
00292   (e.g. to explicitely set C++ mode for *.h header files)
00293 
00294   Local Variables:
00295   mode:C++
00296   mode:ELSE
00297   End:
00298 */
00299 //======================================================================


schunk_sdh
Author(s): Florian Weisshardt
autogenerated on Mon Oct 6 2014 07:29:15