Low-level communication class to access a serial port. More...
#include <serialbase.h>
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 | |
Public Member Functions | |
cSerialBase () | |
ctor | |
virtual | ~cSerialBase (void) |
dtor | |
virtual void | Open (void)=0 throw (cSerialBaseException*) |
Open rs232 port port. | |
virtual bool | IsOpen (void)=0 throw () |
Return true if communication channel is open. | |
virtual void | Close (void)=0 throw (cSerialBaseException*) |
Close the previously opened communication channel. | |
virtual void | SetTimeout (double _timeout) throw (cSerialBaseException*) |
set the timeout for next readline() calls (negative value means: no timeout, wait for ever) | |
virtual double | GetTimeout () |
get the timeout for next readline() calls (negative value means: no timeout, wait for ever) | |
virtual int | write (char const *ptr, int len=0)=0 throw (cSerialBaseException*) |
Write data to a previously opened port. | |
virtual ssize_t | Read (void *data, ssize_t size, long timeout_us, bool return_on_less_data)=0 throw (cSerialBaseException*) |
virtual char * | readline (char *line, int size, char const *eol="\n", bool return_on_less_data=false) throw (cSerialBaseException*) |
Read a line from the device. | |
virtual tErrorCode | GetErrorNumber () |
virtual char const * | GetErrorMessage (tErrorCode dw) |
char const * | GetLastErrorMessage (void) |
return the last error message as string. The string returned will be overwritten by the next call to the function | |
virtual bool | UseCRC16 () |
Public Attributes | |
cDBG | dbg |
A stream object to print colored debug messages. | |
Protected Attributes | |
char | ungetch |
an already read data byte of the next line | |
bool | ungetch_valid |
Flag, true if ungetch is valid. | |
double | timeout |
timeout in seconds |
Low-level communication class to access a serial port.
(This is an abstract base class with pure virtual functions)
typedef int SDH::cSerialBase::tErrorCode |
type of the error code, DWORD on windows and int on Linux/cygwin
SDH::cSerialBase::cSerialBase | ( | ) | [inline] |
ctor
virtual SDH::cSerialBase::~cSerialBase | ( | void | ) | [inline, virtual] |
dtor
virtual void SDH::cSerialBase::Close | ( | void | ) | throw (cSerialBaseException*) [pure virtual] |
Close the previously opened communication channel.
Implemented in SDH::cCANSerial_ESD, SDH::cCANSerial_PEAK, SDH::cRS232, SDH::cRS232, and SDH::cTCPSerial.
char const * cSerialBase::GetErrorMessage | ( | tErrorCode | dw | ) | [virtual] |
Helper function that returns an error message for error code dw.
Reimplemented in SDH::cCANSerial_ESD, and SDH::cCANSerial_PEAK.
virtual tErrorCode SDH::cSerialBase::GetErrorNumber | ( | ) | [inline, virtual] |
Helper function that returns the last error number.
Reimplemented in SDH::cCANSerial_ESD, SDH::cCANSerial_PEAK, and SDH::cTCPSerial.
char const* SDH::cSerialBase::GetLastErrorMessage | ( | void | ) | [inline] |
return the last error message as string. The string returned will be overwritten by the next call to the function
virtual double SDH::cSerialBase::GetTimeout | ( | ) | [inline, virtual] |
get the timeout for next readline() calls (negative value means: no timeout, wait for ever)
virtual bool SDH::cSerialBase::IsOpen | ( | void | ) | throw () [pure virtual] |
Return true if communication channel is open.
Implemented in SDH::cCANSerial_ESD, SDH::cCANSerial_PEAK, SDH::cRS232, SDH::cRS232, and SDH::cTCPSerial.
virtual void SDH::cSerialBase::Open | ( | void | ) | throw (cSerialBaseException*) [pure virtual] |
Open rs232 port port.
Open the device with the parameters provided in the constructor
Implemented in SDH::cCANSerial_ESD, SDH::cCANSerial_PEAK, SDH::cRS232, SDH::cRS232, and SDH::cTCPSerial.
virtual ssize_t SDH::cSerialBase::Read | ( | void * | data, |
ssize_t | size, | ||
long | timeout_us, | ||
bool | return_on_less_data | ||
) | throw (cSerialBaseException*) [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 SDH::cCANSerial_ESD, SDH::cCANSerial_PEAK, SDH::cRS232, SDH::cRS232, and SDH::cTCPSerial.
char * cSerialBase::readline | ( | char * | line, |
int | size, | ||
char const * | eol = "\n" , |
||
bool | return_on_less_data = false |
||
) | throw (cSerialBaseException*) [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.
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.
virtual void SDH::cSerialBase::SetTimeout | ( | double | _timeout | ) | throw (cSerialBaseException*) [inline, virtual] |
set the timeout for next readline() calls (negative value means: no timeout, wait for ever)
Reimplemented in SDH::cCANSerial_ESD, SDH::cCANSerial_PEAK, SDH::cRS232, and SDH::cTCPSerial.
virtual bool SDH::cSerialBase::UseCRC16 | ( | ) | [inline, virtual] |
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 SDH::cRS232, and SDH::cRS232.
virtual int SDH::cSerialBase::write | ( | char const * | ptr, |
int | len = 0 |
||
) | throw (cSerialBaseException*) [pure virtual] |
Write data to a previously opened port.
Write len bytes from *ptr to the rs232 device
ptr | - pointer the byte array to send in memory |
len | - number of bytes to send |
Implemented in SDH::cCANSerial_ESD, SDH::cCANSerial_PEAK, SDH::cRS232, SDH::cRS232, and SDH::cTCPSerial.
A stream object to print colored debug messages.
double SDH::cSerialBase::timeout [protected] |
timeout in seconds
char SDH::cSerialBase::ungetch [protected] |
an already read data byte of the next line
bool SDH::cSerialBase::ungetch_valid [protected] |
Flag, true if ungetch is valid.