serialbase.h
Go to the documentation of this file.
1 //======================================================================
28 //======================================================================
29 
30 #ifndef SERIALBASE_H_
31 #define SERIALBASE_H_
32 
33 #include "sdhlibrary_settings.h"
34 
35 #if SDH_USE_VCC
36 # pragma warning(disable : 4290)
37 #endif
38 
39 //----------------------------------------------------------------------
40 // System Includes - include with <>
41 //----------------------------------------------------------------------
42 
43 #if SDH_USE_VCC
44 # include <windows.h>
45 # include <strsafe.h>
46 #else
47 # include <errno.h>
48 #endif
49 
50 //----------------------------------------------------------------------
51 // Project Includes - include with ""
52 //----------------------------------------------------------------------
53 
54 #include "sdhexception.h"
55 #include "dbg.h"
56 #include "sdhbase.h" // for g_sdh_debug_log
57 
58 //----------------------------------------------------------------------
59 // Defines, enums, unions, structs,
60 //----------------------------------------------------------------------
61 
63 
64 typedef void* tDeviceHandle;
65 
66 #if SDH_USE_VCC
67 # define snprintf _snprintf
68 #endif
69 
70 //----------------------------------------------------------------------
71 // Global variables
72 //----------------------------------------------------------------------
73 
74 
75 //----------------------------------------------------------------------
76 // Function and class member declarations
77 //----------------------------------------------------------------------
78 
79 
80 
81 
82 
87 {
88  public:
89  cSerialBaseException( cMsg const & _msg )
90  : cSDHErrorCommunication( "cSerialBaseException", _msg )
91  {}
92 
93  cSerialBaseException( char const* _type, cMsg const & _msg )
94  : cSDHErrorCommunication( _type, _msg )
95  {}
96 };
97 //======================================================================
98 
99 
105 class VCC_EXPORT cSerialBase
106 {
107  protected:
108 
110  char ungetch;
111 
114 
116  double timeout;
117 
118  public:
121  : // init members
122  ungetch('\0'),
123  ungetch_valid(false),
124  timeout( -1.0 ),
125  // setting the timeout does not make sense here. should be left to virtual SetTimeout()
126  dbg( false, "magenta", g_sdh_debug_log )
127  {
128  // nothing more to do
129  }
130 
132  virtual ~cSerialBase( void )
133  {
135  }
136 
138 
141  virtual void Open( void ) = 0;
142 
144  virtual bool IsOpen( void )
145  throw() = 0;
146 
148  virtual void Close( void ) = 0;
149 
151  virtual void SetTimeout( double _timeout )
152  {
153  timeout = _timeout;
154  }
155 
157  virtual double GetTimeout()
158  {
159  return timeout;
160  }
161 
164  {
166  double old_timeout;
167  public:
169  cSetTimeoutTemporarily( cSerialBase* _serial_base, double new_timeout )
170  : serial_base(_serial_base),
171  old_timeout( serial_base->GetTimeout() )
172  {
173  if ( new_timeout != old_timeout )
174  serial_base->SetTimeout( new_timeout );
175  }
176 
179  {
180  if ( old_timeout != serial_base->GetTimeout() )
181  serial_base->SetTimeout( old_timeout );
182  }
183  };
184 
185 
187 
195  virtual int write( char const *ptr, int len=0 ) = 0;
196 
205  virtual ssize_t Read( void *data, ssize_t size, long timeout_us, bool return_on_less_data ) = 0;
206 
208 
224  virtual char* readline( char* line, int size, char const* eol = "\n", bool return_on_less_data = false );
225 
228 
230 #if SDH_USE_VCC
231  typedef DWORD tErrorCode;
232 #else
233  typedef int tErrorCode;
234 #endif
235 
241  virtual tErrorCode GetErrorNumber()
242  {
243 #if SDH_USE_VCC
244  return GetLastError();
245 #else
246  return errno;
247 #endif
248  }
249 
257  virtual char const* GetErrorMessage( tErrorCode dw );
258 
260  char const* GetLastErrorMessage( void )
261  {
262  return GetErrorMessage( GetErrorNumber() );
263  }
264 
270  virtual bool UseCRC16()
271  {
272  return false;
273  }
274  //----------------------------------------------------------------------
275 
276 };
277 //======================================================================
278 
280 
281 #endif
282 
283 
284 //======================================================================
285 /*
286  Here are some settings for the emacs/xemacs editor (and can be safely ignored):
287  (e.g. to explicitely set C++ mode for *.h header files)
288 
289  Local Variables:
290  mode:C++
291  mode:ELSE
292  End:
293 */
294 //======================================================================
cSerialBase()
ctor
Definition: serialbase.h:120
A class to print colored debug messages.
Definition: dbg.h:113
virtual double GetTimeout()
get the timeout for next readline() calls (negative value means: no timeout, wait for ever) ...
Definition: serialbase.h:157
cSerialBaseException(char const *_type, cMsg const &_msg)
Definition: serialbase.h:93
virtual void SetTimeout(double _timeout)
set the timeout for next readline() calls (negative value means: no timeout, wait for ever) ...
Definition: serialbase.h:151
This file contains interface and implementation of class #SDH::cDBG, a class for colorfull debug mess...
int tErrorCode
type of the error code, DWORD on windows and int on Linux/cygwin
Definition: serialbase.h:233
Interface of the exception base class #SDH::cSDHLibraryException and #SDH::cMsg.
helper class to set timeout of _serial_base on construction and reset to previous value on destructio...
Definition: serialbase.h:163
Low-level communication class to access a serial port.
Definition: serialbase.h:105
#define NAMESPACE_SDH_START
virtual ~cSerialBase(void)
dtor
Definition: serialbase.h:132
~cSetTimeoutTemporarily()
DTOR: restore the remembered timeout.
Definition: serialbase.h:178
char const * GetLastErrorMessage(void)
return the last error message as string. The string returned will be overwritten by the next call to ...
Definition: serialbase.h:260
Interface of class #SDH::cSDHBase.
UInt16 size
Definition: dsa.h:269
virtual bool UseCRC16()
Definition: serialbase.h:270
double timeout
timeout in seconds
Definition: serialbase.h:116
#define NAMESPACE_SDH_END
Derived exception class for exceptions related to communication between the SDHLibrary and the SDH...
Definition: sdhexception.h:206
This file contains settings to make the SDHLibrary compile on differen systems:
char ungetch
an already read data byte of the next line
Definition: serialbase.h:110
Derived exception class for low-level serial communication related exceptions.
Definition: serialbase.h:86
bool ungetch_valid
Flag, true if ungetch is valid.
Definition: serialbase.h:113
USING_NAMESPACE_SDH NAMESPACE_SDH_START std::ostream * g_sdh_debug_log
Definition: sdhbase.cpp:55
Class for short, fixed maximum length text messages.
Definition: sdhexception.h:77
cDBG dbg
A stream object to print colored debug messages.
Definition: serialbase.h:227
NAMESPACE_SDH_START typedef void * tDeviceHandle
generic device handle for CAN devices
Definition: serialbase.h:64
cSetTimeoutTemporarily(cSerialBase *_serial_base, double new_timeout)
CTOR: remember current timeout of _serial_base and set its timeout to new_timeout, but only if current timeout and new_timeout differ.
Definition: serialbase.h:169
virtual tErrorCode GetErrorNumber()
Definition: serialbase.h:241
cSerialBaseException(cMsg const &_msg)
Definition: serialbase.h:89


sdhlibrary_cpp
Author(s): Dirk Osswald
autogenerated on Sun Aug 18 2019 03:42:20