40 #define _CRT_SECURE_NO_WARNINGS 1 63 #define SDH_RS232_VCC_DEBUG 1 65 #if SDH_RS232_VCC_DEBUG 94 cRS232::cRS232(
int _port,
unsigned long _baudrate,
double _timeout,
char const* _device_format_string )
98 _hCOM(INVALID_HANDLE_VALUE),
104 SetTimeout( _timeout );
109 if (
_hCOM != INVALID_HANDLE_VALUE)
119 char device[] =
"\\\\.\\COM00";
120 sprintf(device,
"\\\\.\\COM%d",
port+1);
121 DBG(
dbg <<
"cRS232-vcc::Open: Opening RS232 device '" << device <<
"', baudrate: " <<
baudrate <<
"\n" );
123 _hCOM = CreateFileA(device,
124 GENERIC_READ | GENERIC_WRITE,
129 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
131 FILE_ATTRIBUTE_NORMAL,
134 if (
_hCOM == INVALID_HANDLE_VALUE )
137 #if SDH_RS232_VCC_ASYNC 138 memset(&o, 0,
sizeof(OVERLAPPED));
142 dcbInitState.DCBlength =
sizeof( DCB );
144 rc = GetCommState(
_hCOM, &dcbInitState);
147 dcbInitState.DCBlength =
sizeof( DCB );
149 dcbInitState.fBinary = 1;
150 dcbInitState.fParity = 0;
151 dcbInitState.fOutxCtsFlow = 0;
152 dcbInitState.fOutxDsrFlow = 0;
153 dcbInitState.fDtrControl = DTR_CONTROL_DISABLE;
154 dcbInitState.fDsrSensitivity = 0;
155 dcbInitState.fTXContinueOnXoff = 1;
156 dcbInitState.fOutX = 0;
157 dcbInitState.fInX = 0;
158 dcbInitState.fErrorChar = 0;
159 dcbInitState.fNull = 0;
160 dcbInitState.fRtsControl = RTS_CONTROL_DISABLE;
161 dcbInitState.fAbortOnError = 0;
162 dcbInitState.ByteSize = 8;
163 dcbInitState.Parity = NOPARITY;
164 dcbInitState.StopBits = ONESTOPBIT;
165 rc = SetCommState(
_hCOM, &dcbInitState);
180 _hCOM = INVALID_HANDLE_VALUE;
185 DBG(
dbg <<
"cRS232-vcc::SetTimeout( " << _timeout <<
")\n" );
186 if (
_hCOM != INVALID_HANDLE_VALUE )
191 if ( _timeout < 0.0 )
198 else if ( _timeout == 0.0 )
209 comm_timeouts.ReadTotalTimeoutConstant = DWORD( _timeout * 1000.0 );
220 ssize_t
cRS232::Read(
void *data, ssize_t
size,
long timeout_us,
bool return_on_less_data )
222 char* buffer = (
char*) data;
232 #if SDH_RS232_VCC_ASYNC 233 memset(&o, 0,
sizeof(OVERLAPPED));
234 o.hEvent = CreateEvent(
NULL, FALSE, FALSE,
NULL);
235 rc = ReadFile(
_hCOM, buffer + offset,
size-offset, &bytes_read, &o);
237 rc = ReadFile(
_hCOM, buffer + offset,
size-offset, &bytes_read,
NULL );
241 #if SDH_RS232_VCC_ASYNC 242 DWORD last_error = GetLastError();
243 if ( last_error == ERROR_IO_PENDING )
246 if(return_on_less_data)
247 nRetVal = WaitForSingleObject(o.hEvent, 10);
249 nRetVal = WaitForSingleObject(o.hEvent, 5000);
255 DBG(
dbg <<
"cRS232-vcc::Read: WAIT_TIMEOUT bytes_read = " << bytes_read <<
"\n" );
256 CloseHandle(o.hEvent);
262 std::cerr <<
"error " << last_error <<
" from ReadFile / GetLastError: " <<
GetLastErrorMessage() <<
"\n";
268 DBG(
dbg <<
"cRS232-vcc::Read: Read " << bytes_read <<
"/" <<
size <<
" bytes (hex): " <<
cHexByteString( buffer + offset, bytes_read ) <<
"\n" );
269 if ( bytes_read == 0 )
271 if ( timeout_us == 0 )
275 DBG(
dbg <<
"cRS232-vcc::Read timeout! bytes_read = 0\n" );
276 #if SDH_RS232_VCC_ASYNC 277 CloseHandle(o.hEvent);
281 #if SDH_RS232_VCC_ASYNC 282 CloseHandle(o.hEvent);
284 offset += bytes_read;
287 }
while( offset < (DWORD)
size );
289 return (ssize_t) offset;
294 memset(line, 0, size*
sizeof(
char));
300 #if SDH_RS232_VCC_ASYNC 301 memset(&o, 0,
sizeof(OVERLAPPED));
302 o.hEvent = CreateEvent(
NULL, FALSE, FALSE,
NULL);
303 rc = ReadFile(
_hCOM, &line[offset], 1, &bytes_read, &o);
305 rc = ReadFile(
_hCOM, &line[offset], 1, &bytes_read,
NULL);
309 #if SDH_RS232_VCC_ASYNC 310 DWORD last_error = GetLastError();
311 if ( last_error == ERROR_IO_PENDING )
314 if(return_on_less_data)
315 nRetVal = WaitForSingleObject(o.hEvent, 10);
317 nRetVal = WaitForSingleObject(o.hEvent, 5000);
328 std::cerr <<
"error " << last_error <<
" from ReadFile / GetLastError: " <<
GetLastErrorMessage() <<
"\n";
334 if ( bytes_read == 0 )
337 #if SDH_RS232_VCC_ASYNC 338 CloseHandle(o.hEvent);
340 offset += bytes_read;
343 }
while(line[offset-1] != eol[0]);
351 len =
static_cast<int>(strlen(ptr));
355 #if SDH_RS232_VCC_ASYNC 357 o.hEvent = CreateEvent(
NULL, FALSE, FALSE,
NULL);
358 rc = WriteFile(
_hCOM, (LPCVOID)ptr, len, &dwWritten, &o);
360 rc = WriteFile(
_hCOM, (LPCVOID)ptr, len, &dwWritten,
NULL );
365 DBG(
dbg <<
"cRS232::write wrote " << len <<
"/" << dwWritten <<
" bytes (hex): " <<
cHexByteString( ptr, len ) <<
"\n" );
388 std::cout <<
"EOL size=" << strlen(eol) << std::endl;
389 if(return_on_less_data)
392 return "cRS232::readline";
399 std::cout <<
">>> " << ptr << std::endl;
ssize_t Read(void *data, ssize_t size, long timeout_us, bool return_on_less_data)
Interface of auxilliary utility functions for SDHLibrary-CPP.
char * readline(char *line, int size, char *eol, bool return_on_less_data)
dummy class for (debug) stream output of bytes as list of hex values
virtual void SetTimeout(double _timeout)
set the timeout for next readline() calls (negative value means: no timeout, wait for ever) ...
void Close(void)
Close the previously opened rs232 port.
This file contains interface and implementation of class #SDH::cDBG, a class for colorfull debug mess...
helper class to set timeout of _serial_base on construction and reset to previous value on destructio...
Low-level communication class to access a serial port.
virtual void SetTimeout(double _timeout)
set the timeout for next readline() calls (negative value means: no timeout, wait for ever) ...
char const * GetLastErrorMessage(void)
return the last error message as string. The string returned will be overwritten by the next call to ...
int write(char const *ptr, int len=0)
Write data to a previously opened port.
Interface of auxilliary utility functions for SDHLibrary-CPP.
COMMTIMEOUTS comm_timeouts
cRS232(int _port, unsigned long _baudrate, double _timeout, char const *_device_format_string="/dev/ttyS%d")
#define SDH_RS232_VCC_ASYNC
#define USING_NAMESPACE_SDH
int port
the RS232 portnumber to use
double timeout
timeout in seconds
This file contains settings to make the SDHLibrary compile on differen systems:
unsigned long baudrate
the baudrate in bit/s
Derived exception class for low-level RS232 related exceptions.
Class for short, fixed maximum length text messages.
Implementation of class #SDH::cRS232, a class to access serial RS232 port with VCC compiler on Window...
cDBG dbg
A stream object to print colored debug messages.