serialbase.cpp
Go to the documentation of this file.
1 //======================================================================
28 //======================================================================
29 
30 #include "sdhlibrary_settings.h"
31 
32 //----------------------------------------------------------------------
33 // System Includes - include with <>
34 //----------------------------------------------------------------------
35 
36 #include <fcntl.h>
37 #include <stdio.h>
38 #if ! SDH_USE_VCC
39 # include <termios.h>
40 # include <unistd.h>
41 # include <sys/select.h>
42 # include <sys/ioctl.h>
43 #endif
44 #include <errno.h>
45 //#include <string.h>
46 
47 #include <iostream>
48 #include <exception>
49 #include <stdarg.h>
50 #include <cstring> // needed in gcc-4.3 for prototypes like strcmp according to http://gcc.gnu.org/gcc-4.3/porting_to.html
51 
52 //----------------------------------------------------------------------
53 // Project Includes - include with ""
54 //----------------------------------------------------------------------
55 
56 #include "serialbase.h"
57 #include "simpletime.h"
58 
59 //----------------------------------------------------------------------
60 // Defines, enums, unions, structs,
61 //----------------------------------------------------------------------
62 
63 
64 //----------------------------------------------------------------------
65 // Global variables
66 //----------------------------------------------------------------------
67 
68 
69 //----------------------------------------------------------------------
70 // Function and class member implementation (function definitions)
71 //----------------------------------------------------------------------
72 
73 using namespace std;
75 
76 char* cSerialBase::readline( char* line, int size, char const* eol, bool return_on_less_data )
77 {
78  line[0] = '\0';
79  int len = 0;
80  int n;
81  char c;
82 
83  if (ungetch_valid)
84  {
85  line[len++] = ungetch;
86  ungetch_valid = false;
87  }
88 
90  long timeout_us;
91  if ( timeout >= 0.0 )
92  timeout_us = (long) (timeout*1000000.0);
93  else
94  timeout_us = -1L;
95  while (true)
96  {
97  // n = ::read(fd, line + len, 1);
98  n = Read( line + len, 1, timeout_us, return_on_less_data );
99 
100  if (n>0)
101  {
102  c = line[ len ];
103  len += n;
104 
105  //cout << "read '" << c << "' from device\n"; cout.flush();
106 
107  if (strchr( eol, c ) != NULL)
108  break;
109 
110 
111  if (size > 0 && len >= size-1)
112  break;
113  }
114  else
115  {
116  throw new cSerialBaseException( cMsg( "Timeout while reading line from device (timeout_us=%ld line=\"%s\")", timeout_us, line ) );
117  }
118  }
119  // mark end of line
120  line[ len ] = '\0';
121 
123  return line;
124 }
125 //----------------------------------------------------------------------
126 
128 {
129  static char return_msg[512];
130 #if SDH_USE_VCC
131  LPVOID lpMsgBuf;
132 
133  FormatMessageA( FORMAT_MESSAGE_ALLOCATE_BUFFER |
134  FORMAT_MESSAGE_FROM_SYSTEM |
135  FORMAT_MESSAGE_IGNORE_INSERTS,
136  NULL,
137  dw,
138  MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
139  (LPSTR) &lpMsgBuf,
140  0,
141  NULL );
142 
143  StringCchPrintfA( return_msg,
144  511,
145  "error 0x%x = %d = \"%s\"",
146  dw, dw, lpMsgBuf );
147  LocalFree(lpMsgBuf);
148 #else
149  snprintf( return_msg, 511, "error 0x%x = %d = \"%s\"", dw, dw, strerror( dw ) );
150 #endif
151  return return_msg;
152 }
153 //----------------------------------------------------------------------
154 
155 
156 //======================================================================
157 /*
158  Here are some settings for the emacs/xemacs editor (and can be safely ignored):
159  (e.g. to explicitely set C++ mode for *.h header files)
160 
161  Local Variables:
162  mode:C++
163  mode:ELSE
164  End:
165 */
166 //======================================================================
Interface of auxilliary utility functions for SDHLibrary-CPP.
#define NULL
Definition: getopt1.c:56
int tErrorCode
type of the error code, DWORD on windows and int on Linux/cygwin
Definition: serialbase.h:233
virtual char const * GetErrorMessage(tErrorCode dw)
Definition: serialbase.cpp:127
#define USING_NAMESPACE_SDH
UInt16 size
Definition: dsa.h:269
This file contains settings to make the SDHLibrary compile on differen systems:
Interface of class #SDH::cSerialBase, a virtal base class to access serial communication channels lik...
Derived exception class for low-level serial communication related exceptions.
Definition: serialbase.h:86
Class for short, fixed maximum length text messages.
Definition: sdhexception.h:77
virtual char * readline(char *line, int size, char const *eol="\, bool return_on_less_data=false)
Read a line from the device.
Definition: serialbase.cpp:76


sdhlibrary_cpp
Author(s): Dirk Osswald
autogenerated on Mon Feb 28 2022 23:41:51