libhomegear-base  0.7
Base library for Homegear and Homegear family modules.
BaseLib::SerialReaderWriter Class Reference

#include <SerialReaderWriter.h>

Inheritance diagram for BaseLib::SerialReaderWriter:
BaseLib::IEventsEx

Classes

class  ISerialReaderWriterEventSink
 

Public Types

enum  CharacterSize : tcflag_t { CharacterSize::Five = CS5, CharacterSize::Six = CS6, CharacterSize::Seven = CS7, CharacterSize::Eight = CS8 }
 

Public Member Functions

 SerialReaderWriter (BaseLib::SharedObjects *baseLib, std::string device, int32_t baudrate, int32_t flags, bool createLockFile, int32_t readThreadPriority, bool writeOnly=false)
 Constructor. More...
 
virtual ~SerialReaderWriter ()
 Destructor. More...
 
bool isOpen ()
 
std::shared_ptr< FileDescriptorfileDescriptor ()
 
void openDevice (bool parity, bool oddParity, bool events=true, CharacterSize characterSize=CharacterSize::Eight, bool twoStopBits=false)
 Opens the serial device. More...
 
void closeDevice ()
 Closes the serial device. More...
 
int32_t readLine (std::string &data, uint32_t timeout=500000, char splitChar='\n')
 SerialReaderWriter can either be used through events (by implementing ISerialReaderWriterEventSink and usage of addEventHandler) or by polling using this method. More...
 
int32_t readChar (char &data, uint32_t timeout=500000)
 SerialReaderWriter can either be used through events (by implementing ISerialReaderWriterEventSink and usage of addEventHandler) or by polling using this method. More...
 
void writeLine (std::string &data)
 Writes one line of data. More...
 
void writeData (const std::vector< char > &data)
 Writes binary data to the serial device. More...
 
void writeData (const std::vector< uint8_t > &data)
 Writes binary data to the serial device. More...
 
void writeChar (char data)
 Writes one character to the serial device. More...
 
- Public Member Functions inherited from BaseLib::IEventsEx
 IEventsEx ()
 
virtual ~IEventsEx ()
 
virtual PEventHandler addEventHandler (IEventSinkBase *eventHandler)
 
virtual std::vector< PEventHandleraddEventHandlers (EventHandlers eventHandlers)
 
virtual void removeEventHandler (PEventHandler eventHandler)
 
virtual EventHandlers getEventHandlers ()
 

Protected Member Functions

void readThread (bool parity, bool oddParity, CharacterSize characterSize, bool twoStopBits)
 

Protected Attributes

BaseLib::SharedObjects_bl = nullptr
 
std::shared_ptr< FileDescriptor_fileDescriptor
 
std::string _device
 
bool _writeOnly = false
 
struct termios _termios
 
int32_t _baudrate = 0
 
int32_t _flags = 0
 
int32_t _readThreadPriority = 0
 
int32_t _handles = 0
 
std::atomic_bool _stopReadThread
 
std::mutex _readThreadMutex
 
std::thread _readThread
 
std::mutex _sendMutex
 
std::mutex _openDeviceThreadMutex
 
std::thread _openDeviceThread
 
- Protected Attributes inherited from BaseLib::IEventsEx
int32_t _currentId = 0
 
std::mutex _eventHandlerMutex
 
EventHandlers _eventHandlers
 

Member Enumeration Documentation

§ CharacterSize

Enumerator
Five 
Six 
Seven 
Eight 

Constructor & Destructor Documentation

§ SerialReaderWriter()

BaseLib::SerialReaderWriter::SerialReaderWriter ( BaseLib::SharedObjects baseLib,
std::string  device,
int32_t  baudrate,
int32_t  flags,
bool  createLockFile,
int32_t  readThreadPriority,
bool  writeOnly = false 
)

Constructor.

Parameters
baseLibThe base library object.
deviceThe device to use (e. g. "/dev/ttyUSB0")
baudrateThe baudrate (e. g. 115200)
flagsFlags passed to the C function "open". 0 should be fine for most cases. "O_NDELAY" is always added by the constructor. By default "O_RDWR | O_NOCTTY | O_NDELAY" is used.
createLockFileDummy parameter for compatibility.
readThreadPriorityThe priority of the read thread between 0 and 99. Set to -1 to not prioritize the thread. Only relevent when "events" are enabled in "openDevice()".
writeOnlyOpen the device for writing only.

§ ~SerialReaderWriter()

BaseLib::SerialReaderWriter::~SerialReaderWriter ( )
virtual

Destructor.

Member Function Documentation

§ closeDevice()

void BaseLib::SerialReaderWriter::closeDevice ( )

Closes the serial device.

§ fileDescriptor()

std::shared_ptr<FileDescriptor> BaseLib::SerialReaderWriter::fileDescriptor ( )
inline

§ isOpen()

bool BaseLib::SerialReaderWriter::isOpen ( )
inline

§ openDevice()

void BaseLib::SerialReaderWriter::openDevice ( bool  parity,
bool  oddParity,
bool  events = true,
CharacterSize  characterSize = CharacterSize::Eight,
bool  twoStopBits = false 
)

Opens the serial device.

Parameters
evenParityEnable parity checking using an even parity bit.
oddParityEnable parity checking using an odd parity bit. "evenParity" and "oddParity" are mutually exclusive.
eventsEnable events. This starts a thread which calls "lineReceived()" in a derived class for each received packet.
characterSizeSet the character Size.
twoStopBitsEnable two stop bits instead of one.

§ readChar()

int32_t BaseLib::SerialReaderWriter::readChar ( char &  data,
uint32_t  timeout = 500000 
)

SerialReaderWriter can either be used through events (by implementing ISerialReaderWriterEventSink and usage of addEventHandler) or by polling using this method.

Parameters
dataThe variable to write the returned character into.
timeoutThe maximum amount of time to wait in microseconds before the function returns (default: 500000).
Returns
Returns "0" on success, "1" on timeout or "-1" on error.

§ readLine()

int32_t BaseLib::SerialReaderWriter::readLine ( std::string &  data,
uint32_t  timeout = 500000,
char  splitChar = '\n' 
)

SerialReaderWriter can either be used through events (by implementing ISerialReaderWriterEventSink and usage of addEventHandler) or by polling using this method.

Parameters
dataThe variable to write the returned line into.
timeoutThe maximum amount of time to wait in microseconds before the function returns (default: 500000).
splitCharThe character to split at (default: '
')
Returns
Returns "0" on success, "1" on timeout or "-1" on error.

§ readThread()

void BaseLib::SerialReaderWriter::readThread ( bool  parity,
bool  oddParity,
CharacterSize  characterSize,
bool  twoStopBits 
)
protected

§ writeChar()

void BaseLib::SerialReaderWriter::writeChar ( char  data)

Writes one character to the serial device.

Parameters
dataThe (binary) character to write.

§ writeData() [1/2]

void BaseLib::SerialReaderWriter::writeData ( const std::vector< char > &  data)

Writes binary data to the serial device.

Parameters
dataThe data to write. It is written as is without any modification.

§ writeData() [2/2]

void BaseLib::SerialReaderWriter::writeData ( const std::vector< uint8_t > &  data)

Writes binary data to the serial device.

Parameters
dataThe data to write. It is written as is without any modification.

§ writeLine()

void BaseLib::SerialReaderWriter::writeLine ( std::string &  data)

Writes one line of data.

Parameters
dataThe data to write. If data is not terminated by a new line character, it is appended.

Member Data Documentation

§ _baudrate

int32_t BaseLib::SerialReaderWriter::_baudrate = 0
protected

§ _bl

BaseLib::SharedObjects* BaseLib::SerialReaderWriter::_bl = nullptr
protected

§ _device

std::string BaseLib::SerialReaderWriter::_device
protected

§ _fileDescriptor

std::shared_ptr<FileDescriptor> BaseLib::SerialReaderWriter::_fileDescriptor
protected

§ _flags

int32_t BaseLib::SerialReaderWriter::_flags = 0
protected

§ _handles

int32_t BaseLib::SerialReaderWriter::_handles = 0
protected

§ _openDeviceThread

std::thread BaseLib::SerialReaderWriter::_openDeviceThread
protected

§ _openDeviceThreadMutex

std::mutex BaseLib::SerialReaderWriter::_openDeviceThreadMutex
protected

§ _readThread

std::thread BaseLib::SerialReaderWriter::_readThread
protected

§ _readThreadMutex

std::mutex BaseLib::SerialReaderWriter::_readThreadMutex
protected

§ _readThreadPriority

int32_t BaseLib::SerialReaderWriter::_readThreadPriority = 0
protected

§ _sendMutex

std::mutex BaseLib::SerialReaderWriter::_sendMutex
protected

§ _stopReadThread

std::atomic_bool BaseLib::SerialReaderWriter::_stopReadThread
protected

§ _termios

struct termios BaseLib::SerialReaderWriter::_termios
protected

§ _writeOnly

bool BaseLib::SerialReaderWriter::_writeOnly = false
protected

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