serialbase.h
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2008 SCHUNK GmbH & Co. KG
00003  * Copyright (c) 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *   http://www.apache.org/licenses/LICENSE-2.0
00010 
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 //======================================================================
00045 //======================================================================
00046 
00047 #ifndef SERIALBASE_H_
00048 #define SERIALBASE_H_
00049 
00050 #include "sdhlibrary_settings.h"
00051 
00052 #if SDH_USE_VCC
00053 # pragma warning(disable : 4290)
00054 #endif
00055 
00056 //----------------------------------------------------------------------
00057 // System Includes - include with <>
00058 //----------------------------------------------------------------------
00059 
00060 #if SDH_USE_VCC
00061 # include <windows.h>
00062 # include <strsafe.h>
00063 #else
00064 # include <errno.h>
00065 #endif
00066 
00067 //----------------------------------------------------------------------
00068 // Project Includes - include with ""
00069 //----------------------------------------------------------------------
00070 
00071 #include "sdhexception.h"
00072 #include "dbg.h"
00073 #include "sdhbase.h" // for g_sdh_debug_log
00074 
00075 //----------------------------------------------------------------------
00076 // Defines, enums, unions, structs,
00077 //----------------------------------------------------------------------
00078 
00079 NAMESPACE_SDH_START
00080 
00081 typedef void* tDeviceHandle;  
00082 
00083 #if SDH_USE_VCC
00084 # define snprintf _snprintf
00085 #endif
00086 
00087 //----------------------------------------------------------------------
00088 // Global variables
00089 //----------------------------------------------------------------------
00090 
00091 
00092 //----------------------------------------------------------------------
00093 // Function and class member declarations
00094 //----------------------------------------------------------------------
00095 
00096 
00097 
00098 
00099 
00103 class VCC_EXPORT cSerialBaseException: public cSDHErrorCommunication
00104 {
00105 public:
00106   cSerialBaseException(cMsg const & _msg)
00107     : cSDHErrorCommunication("cSerialBaseException", _msg)
00108   {}
00109 
00110   cSerialBaseException(char const* _type, cMsg const & _msg)
00111     : cSDHErrorCommunication(_type, _msg)
00112   {}
00113 };
00114 //======================================================================
00115 
00116 
00122 class VCC_EXPORT cSerialBase
00123 {
00124 protected:
00125 
00127   char ungetch;
00128 
00130   bool ungetch_valid;
00131 
00133   double timeout;
00134 
00135 public:
00137   cSerialBase()
00138     : // init members
00139     ungetch('\0'),
00140     ungetch_valid(false),
00141     // setting the timeout does not make sense here. should be left to virtual SetTimeout()
00142     dbg(false, "cyan", g_sdh_debug_log)
00143   {
00144     // nothing more to do
00145   }
00146 
00148   virtual ~cSerialBase(void)
00149   {
00151   }
00152 
00154 
00157   virtual void Open(void)
00158   throw (cSerialBaseException*) = 0;
00159 
00161   virtual bool IsOpen(void)
00162   throw() = 0;
00163 
00165   virtual void Close(void)
00166   throw (cSerialBaseException*) = 0;
00167 
00169   virtual void SetTimeout(double _timeout)
00170   throw (cSerialBaseException*)
00171   {
00172     timeout = _timeout;
00173   }
00174 
00176   virtual double GetTimeout()
00177   {
00178     return timeout;
00179   }
00180 
00182   class cSetTimeoutTemporarily
00183   {
00184     cSerialBase* serial_base;
00185     double       old_timeout;
00186   public:
00188     cSetTimeoutTemporarily(cSerialBase* _serial_base, double new_timeout)
00189       : serial_base(_serial_base),
00190         old_timeout(serial_base->GetTimeout())
00191     {
00192       if (new_timeout != old_timeout)
00193         serial_base->SetTimeout(new_timeout);
00194     }
00195 
00197     ~cSetTimeoutTemporarily()
00198     {
00199       if (old_timeout != serial_base->GetTimeout())
00200         serial_base->SetTimeout(old_timeout);
00201     }
00202   };
00203 
00204 
00206 
00214   virtual int write(char const *ptr, int len = 0)
00215   throw (cSerialBaseException*) = 0;
00216 
00225   virtual ssize_t Read(void *data, ssize_t size, long timeout_us, bool return_on_less_data)
00226   throw (cSerialBaseException*) = 0;
00227 
00229 
00245   virtual char* readline(char* line, int size, char const* eol = "\n", bool return_on_less_data = false)
00246   throw (cSerialBaseException*);
00247 
00249   cDBG dbg;
00250 
00252 #if SDH_USE_VCC
00253   typedef DWORD tErrorCode;
00254 #else
00255   typedef int tErrorCode;
00256 #endif
00257 
00263   virtual tErrorCode GetErrorNumber()
00264   {
00265 #if SDH_USE_VCC
00266     return GetLastError();
00267 #else
00268     return errno;
00269 #endif
00270   }
00271 
00279   virtual char const* GetErrorMessage(tErrorCode dw);
00280 
00282   char const* GetLastErrorMessage(void)
00283   {
00284     return GetErrorMessage(GetErrorNumber());
00285   }
00286 
00292   virtual bool UseCRC16()
00293   {
00294     return false;
00295   }
00296   //----------------------------------------------------------------------
00297 
00298 };
00299 //======================================================================
00300 
00301 NAMESPACE_SDH_END
00302 
00303 #endif
00304 
00305 
00306 //======================================================================
00307 /*
00308   Here are some settings for the emacs/xemacs editor (and can be safely ignored):
00309   (e.g. to explicitely set C++ mode for *.h header files)
00310 
00311   Local Variables:
00312   mode:C++
00313   mode:ELSE
00314   End:
00315 */
00316 //======================================================================


schunk_sdh
Author(s): Mathias Luedtke , Florian Weisshardt
autogenerated on Sat Jun 8 2019 20:25:21