Classes | Public Types | Public Member Functions | Public Attributes | Protected Attributes | List of all members
cSerialBase Class Referenceabstract

Low-level communication class to access a serial port. More...

#include <serialbase.h>

Inheritance diagram for cSerialBase:
Inheritance graph
[legend]

Classes

class  cSetTimeoutTemporarily
 helper class to set timeout of _serial_base on construction and reset to previous value on destruction. (RAII-idiom) More...
 

Public Types

typedef int tErrorCode
 type of the error code, DWORD on windows and int on Linux/cygwin More...
 

Public Member Functions

virtual void Close (void)=0
 Close the previously opened communication channel. More...
 
 cSerialBase ()
 ctor More...
 
virtual char const * GetErrorMessage (tErrorCode dw)
 
virtual tErrorCode GetErrorNumber ()
 
char const * GetLastErrorMessage (void)
 return the last error message as string. The string returned will be overwritten by the next call to the function More...
 
virtual double GetTimeout ()
 get the timeout for next readline() calls (negative value means: no timeout, wait for ever) More...
 
virtual bool IsOpen (void)=0 throw ()
 Return true if communication channel is open. More...
 
virtual void Open (void)=0
 Open rs232 port port. More...
 
virtual ssize_t Read (void *data, ssize_t size, long timeout_us, bool return_on_less_data)=0
 
virtual char * readline (char *line, int size, char const *eol="\n", bool return_on_less_data=false)
 Read a line from the device. More...
 
virtual void SetTimeout (double _timeout)
 set the timeout for next readline() calls (negative value means: no timeout, wait for ever) More...
 
virtual bool UseCRC16 ()
 
virtual int write (char const *ptr, int len=0)=0
 Write data to a previously opened port. More...
 
virtual ~cSerialBase (void)
 dtor More...
 

Public Attributes

cDBG dbg
 A stream object to print colored debug messages. More...
 

Protected Attributes

double timeout
 timeout in seconds More...
 
char ungetch
 an already read data byte of the next line More...
 
bool ungetch_valid
 Flag, true if ungetch is valid. More...
 

Detailed Description

Low-level communication class to access a serial port.

(This is an abstract base class with pure virtual functions)

Definition at line 105 of file serialbase.h.

Member Typedef Documentation

◆ tErrorCode

type of the error code, DWORD on windows and int on Linux/cygwin

Definition at line 233 of file serialbase.h.

Constructor & Destructor Documentation

◆ cSerialBase()

cSerialBase::cSerialBase ( )
inline

ctor

Definition at line 120 of file serialbase.h.

◆ ~cSerialBase()

virtual cSerialBase::~cSerialBase ( void  )
inlinevirtual

dtor

Definition at line 132 of file serialbase.h.

Member Function Documentation

◆ Close()

virtual void cSerialBase::Close ( void  )
pure virtual

Close the previously opened communication channel.

Implemented in cCANSerial_ESD, cCANSerial_PEAK, cRS232, cTCPSerial, and cRS232.

◆ GetErrorMessage()

char const * cSerialBase::GetErrorMessage ( tErrorCode  dw)
virtual

Helper function that returns an error message for error code dw.

  • On windows FormatMessageA() is used
  • On cygwin/linux this uses errno
Remarks
The string returned will be overwritten by the next call to the function

Reimplemented in cCANSerial_ESD, and cCANSerial_PEAK.

Definition at line 127 of file serialbase.cpp.

◆ GetErrorNumber()

virtual tErrorCode cSerialBase::GetErrorNumber ( )
inlinevirtual

Helper function that returns the last error number.

  • On windows GetLastError() is used
  • On cygwin/linux this uses errno

Reimplemented in cCANSerial_ESD, cCANSerial_PEAK, and cTCPSerial.

Definition at line 241 of file serialbase.h.

◆ GetLastErrorMessage()

char const* cSerialBase::GetLastErrorMessage ( void  )
inline

return the last error message as string. The string returned will be overwritten by the next call to the function

Definition at line 260 of file serialbase.h.

◆ GetTimeout()

virtual double cSerialBase::GetTimeout ( )
inlinevirtual

get the timeout for next readline() calls (negative value means: no timeout, wait for ever)

Definition at line 157 of file serialbase.h.

◆ IsOpen()

virtual bool cSerialBase::IsOpen ( void  )
throw (
)
pure virtual

Return true if communication channel is open.

Implemented in cCANSerial_ESD, cCANSerial_PEAK, cRS232, cTCPSerial, and cRS232.

◆ Open()

virtual void cSerialBase::Open ( void  )
pure virtual

Open rs232 port port.

Open the device with the parameters provided in the constructor

Implemented in cCANSerial_ESD, cCANSerial_PEAK, cRS232, cTCPSerial, and cRS232.

◆ Read()

virtual ssize_t cSerialBase::Read ( void *  data,
ssize_t  size,
long  timeout_us,
bool  return_on_less_data 
)
pure virtual

Read data from device. This function waits until max_time_us us passed or the expected number of bytes are received via serial line. if (return_on_less_data is true (default value), the number of bytes that have been received are returned and the data is stored in data If the return_on_less_data is false, data is only read from serial line, if at least size bytes are available.

Implemented in cCANSerial_ESD, cCANSerial_PEAK, cRS232, cTCPSerial, and cRS232.

◆ readline()

USING_NAMESPACE_SDH char * cSerialBase::readline ( char *  line,
int  size,
char const *  eol = "\n",
bool  return_on_less_data = false 
)
virtual

Read a line from the device.

A line is terminated with one of the end-of-line (eol) characters ('
' by default) or until timeout. Up to size-1 bytes are read and a '\0' char is appended.

Parameters
line- ptr to where to store the read line
size- space available in line (bytes)
eol- a string containing all the chars that mark an end of line
return_on_less_data- if (return_on_less_data is true (default value), the number of bytes that have been received are returned and the data is stored in data If the return_on_less_data is false, data is only read from serial line, if at least size bytes are available.

A pointer to the line read is returned.

Definition at line 76 of file serialbase.cpp.

◆ SetTimeout()

virtual void cSerialBase::SetTimeout ( double  _timeout)
inlinevirtual

set the timeout for next readline() calls (negative value means: no timeout, wait for ever)

Reimplemented in cCANSerial_ESD, cCANSerial_PEAK, cTCPSerial, and cRS232.

Definition at line 151 of file serialbase.h.

◆ UseCRC16()

virtual bool cSerialBase::UseCRC16 ( )
inlinevirtual

function that returns true if a CRC16 is used to protect binary communication.

The default is false since only RS232 communication needs this.

Reimplemented in cRS232, and cRS232.

Definition at line 270 of file serialbase.h.

◆ write()

virtual int cSerialBase::write ( char const *  ptr,
int  len = 0 
)
pure virtual

Write data to a previously opened port.

Write len bytes from *ptr to the rs232 device

Parameters
ptr- pointer the byte array to send in memory
len- number of bytes to send
Returns
the number of bytes actually written

Implemented in cCANSerial_ESD, cCANSerial_PEAK, cRS232, cTCPSerial, and cRS232.

Member Data Documentation

◆ dbg

cDBG cSerialBase::dbg

A stream object to print colored debug messages.

Definition at line 227 of file serialbase.h.

◆ timeout

double cSerialBase::timeout
protected

timeout in seconds

Definition at line 116 of file serialbase.h.

◆ ungetch

char cSerialBase::ungetch
protected

an already read data byte of the next line

Definition at line 110 of file serialbase.h.

◆ ungetch_valid

bool cSerialBase::ungetch_valid
protected

Flag, true if ungetch is valid.

Definition at line 113 of file serialbase.h.


The documentation for this class was generated from the following files:


sdhlibrary_cpp
Author(s): Dirk Osswald
autogenerated on Wed Mar 2 2022 01:00:59