Public Types | Public Member Functions | Private Member Functions | Private Attributes | List of all members
serialcom::SerialCom Class Reference

#include <SerialCom.h>

Public Types

enum  { NONE, EVEN, ODD }
 

Public Member Functions

int baudRate ()
 Get the current baud rate. More...
 
bool changebaudrate (int baudrate_)
 
void close ()
 Close the serial port. More...
 
bool config (int baudrate_, int databits_, int startbits_, int stopbits_, int parity_, int NLchar_)
 
int flush ()
 Wrapper around tcflush. More...
 
void open (const char *port_name, int baud_rate=115200)
 Open the serial port. More...
 
void pauseStream ()
 Pause streaming. More...
 
bool portOpen ()
 Check whether the port is open or not. More...
 
int read (char *data, int max_length, int timeout=-1)
 Read from the port. More...
 
bool readBetween (std::string *data, char start, char end, int timeout=-1)
 Read from the serial port between a start char and an end char. More...
 
int readbyte (uint8_t *read_byte)
 
int readBytes (char *data, int length, int timeout=-1)
 Read a fixed number of bytes from the serial port. More...
 
int readLine (char *data, int length, int timeout=-1)
 Read a line from the serial port. More...
 
bool readLine (std::string *data, int timeout=-1)
 Read a line from the serial port. More...
 
bool readLine (std::string &buffer)
 
void resumeStream ()
 Resume streaming. More...
 
tcflag_t selectbaud (int baudrate_)
 
 SerialCom ()
 Constructor. More...
 
bool startReadBetweenStream (boost::function< void(std::string *)> f, char start, char end)
 Start a stream of readBetween() More...
 
bool startReadLineStream (boost::function< void(std::string *)> f)
 Start a stream of readLine(std::string*, int) More...
 
bool startReadStream (boost::function< void(char *, int)> f)
 Start a stream of read() More...
 
void stopStream ()
 Stop streaming. More...
 
int write (const char *data, int length=-1)
 Write to the port. More...
 
bool writebyte (uint8_t byte)
 
 ~SerialCom ()
 Destructor. More...
 

Private Member Functions

void readBetweenThread (char start, char end)
 Thread for a stream of readBetween() More...
 
void readLineThread ()
 Thread for a stream of readLine(std::string*, int) More...
 
void readThread ()
 Thread for a stream of read() More...
 

Private Attributes

int baud_
 Baud rate. More...
 
int databits
 
int fd_
 File descriptor. More...
 
struct termios ComParams newParams
 
int parity
 
boost::function< void(std::string *)> readBetweenCallback
 Stream readBetween callback boost function. More...
 
boost::function< void(char *, int)> readCallback
 Stream read callback boost function. More...
 
boost::function< void(std::string *)> readLineCallback
 Stream readLine callback boost function. More...
 
int startbits
 
int stopbits
 
bool stream_paused_
 Whether streaming is paused or not. More...
 
bool stream_stopped_
 Whether streaming is stopped or not. More...
 
boost::thread * stream_thread_
 Stream thread. More...
 

Detailed Description

Definition at line 83 of file SerialCom.h.

Member Enumeration Documentation

anonymous enum
Enumerator
NONE 
EVEN 
ODD 

Definition at line 139 of file SerialCom.h.

Constructor & Destructor Documentation

serialcom::SerialCom::SerialCom ( )

Constructor.

Definition at line 66 of file SerialCom.cpp.

serialcom::SerialCom::~SerialCom ( )

Destructor.

Definition at line 71 of file SerialCom.cpp.

Member Function Documentation

int serialcom::SerialCom::baudRate ( )
inline

Get the current baud rate.

Definition at line 173 of file SerialCom.h.

bool serialcom::SerialCom::changebaudrate ( int  baudrate_)

Definition at line 215 of file SerialCom.cpp.

void serialcom::SerialCom::close ( )

Close the serial port.

This call essentially wraps close.

Definition at line 323 of file SerialCom.cpp.

bool serialcom::SerialCom::config ( int  baudrate_,
int  databits_,
int  startbits_,
int  stopbits_,
int  parity_,
int  NLchar_ 
)

Definition at line 110 of file SerialCom.cpp.

int serialcom::SerialCom::flush ( )

Wrapper around tcflush.

Definition at line 619 of file SerialCom.cpp.

void serialcom::SerialCom::open ( const char *  port_name,
int  baud_rate = 115200 
)

Open the serial port.

This opens the serial port for communication. Wrapper for open.

Parameters
port_nameA null terminated character array containing the name of the port.
baud_rateBaud rate of the serial port. Defaults to 115200.
Todo:
tcsetattr returns true if at least one attribute was set. Hence, we might not have set everything on success.

Definition at line 244 of file SerialCom.cpp.

void serialcom::SerialCom::pauseStream ( )

Pause streaming.

Definition at line 748 of file SerialCom.cpp.

bool serialcom::SerialCom::portOpen ( )
inline

Check whether the port is open or not.

Definition at line 170 of file SerialCom.h.

int serialcom::SerialCom::read ( char *  data,
int  max_length,
int  timeout = -1 
)

Read from the port.

This function allows to read data from the serial port. Simple wrapper for read.

Parameters
dataData coming from the serial port.
max_lengthMaximum length of the incoming data.
timeoutTimeout in milliseconds.
Returns
Number of bytes read.

Definition at line 386 of file SerialCom.cpp.

bool serialcom::SerialCom::readBetween ( std::string *  data,
char  start,
char  end,
int  timeout = -1 
)

Read from the serial port between a start char and an end char.

This function allows to read data from the serial port between a start and an end char.

Parameters
dataData coming from the serial port.
startStart character of the incoming data stream.
endEnd character of the incoming data stream.
timeoutTimeout in milliseconds.
Returns
Whether the read was successful or not.

Definition at line 571 of file SerialCom.cpp.

void serialcom::SerialCom::readBetweenThread ( char  start,
char  end 
)
private

Thread for a stream of readBetween()

Stream version of the readBetween function.

Parameters
startStart character of the incoming data stream.
endEnd character of the incoming data stream.

Definition at line 718 of file SerialCom.cpp.

int serialcom::SerialCom::readbyte ( uint8_t *  read_byte)

Definition at line 367 of file SerialCom.cpp.

int serialcom::SerialCom::readBytes ( char *  data,
int  length,
int  timeout = -1 
)

Read a fixed number of bytes from the serial port.

This function allows to read a fixed number of data bytes from the serial port, no more, no less.

Parameters
dataData coming from the serial port.
lengthFixed length of the incoming data.
timeoutTimeout in milliseconds.
See Also
read()
Returns
Number of bytes read.

Definition at line 410 of file SerialCom.cpp.

int serialcom::SerialCom::readLine ( char *  data,
int  length,
int  timeout = -1 
)

Read a line from the serial port.

This function allows to read a line from the serial port. Data is return as char*

Parameters
dataData coming from the serial port.
lengthLength of the incoming data.
timeoutTimeout in milliseconds.
See Also
readLine(std::string*, int)
Returns
Number of bytes read.

Definition at line 439 of file SerialCom.cpp.

bool serialcom::SerialCom::readLine ( std::string *  data,
int  timeout = -1 
)

Read a line from the serial port.

This function allows to read a line from the serial port. Data is return as std::string

Parameters
dataData coming from the serial port.
timeoutTimeout in milliseconds.
See Also
readLine(char*, int, int)
Returns
Whether the read was successful or not.

Definition at line 509 of file SerialCom.cpp.

bool serialcom::SerialCom::readLine ( std::string &  buffer)

Definition at line 472 of file SerialCom.cpp.

void serialcom::SerialCom::readLineThread ( )
private

Thread for a stream of readLine(std::string*, int)

Stream version of the readLine function.

Definition at line 681 of file SerialCom.cpp.

void serialcom::SerialCom::readThread ( )
private

Thread for a stream of read()

Stream version of the read function.

Definition at line 640 of file SerialCom.cpp.

void serialcom::SerialCom::resumeStream ( )

Resume streaming.

Definition at line 753 of file SerialCom.cpp.

tcflag_t serialcom::SerialCom::selectbaud ( int  baudrate_)

Definition at line 77 of file SerialCom.cpp.

bool serialcom::SerialCom::startReadBetweenStream ( boost::function< void(std::string *)>  f,
char  start,
char  end 
)

Start a stream of readBetween()

Stream version of the readBetween() function.

Parameters
fCallback boost function to receive the data.
startStart character of the incoming data stream.
endEnd character of the incoming data stream.
See Also
readBetween()
Returns
True if successful false if a stream is already running.

Definition at line 705 of file SerialCom.cpp.

bool serialcom::SerialCom::startReadLineStream ( boost::function< void(std::string *)>  f)

Start a stream of readLine(std::string*, int)

Stream version of the readLine(std::string*, int) function.

Parameters
fCallback boost function to receive the data.
See Also
readLine(std::string*, int)
Returns
True if successful false if a stream is already running.

Definition at line 668 of file SerialCom.cpp.

bool serialcom::SerialCom::startReadStream ( boost::function< void(char *, int)>  f)

Start a stream of read()

Stream version of the read function.

Parameters
fCallback boost function to receive the data.
See Also
read()
Returns
True if successful false if a stream is already running.

Definition at line 627 of file SerialCom.cpp.

void serialcom::SerialCom::stopStream ( )

Stop streaming.

Definition at line 739 of file SerialCom.cpp.

int serialcom::SerialCom::write ( const char *  data,
int  length = -1 
)

Write to the port.

This function allows to send data through the serial port. Wraper for write.

Parameters
dataData to send in a character array or null terminated c string.
lengthNumber of bytes being sent. Defaults to -1 if sending a c string.
Returns
Number of bytes writen.

Definition at line 335 of file SerialCom.cpp.

bool serialcom::SerialCom::writebyte ( uint8_t  byte)

Definition at line 350 of file SerialCom.cpp.

Member Data Documentation

int serialcom::SerialCom::baud_
private

Baud rate.

Definition at line 89 of file SerialCom.h.

int serialcom::SerialCom::databits
private

Definition at line 91 of file SerialCom.h.

int serialcom::SerialCom::fd_
private

File descriptor.

Definition at line 87 of file SerialCom.h.

struct termios ComParams serialcom::SerialCom::newParams
private

Definition at line 95 of file SerialCom.h.

int serialcom::SerialCom::parity
private

Definition at line 94 of file SerialCom.h.

boost::function<void(std::string*)> serialcom::SerialCom::readBetweenCallback
private

Stream readBetween callback boost function.

Definition at line 130 of file SerialCom.h.

boost::function<void(char*, int)> serialcom::SerialCom::readCallback
private

Stream read callback boost function.

Definition at line 126 of file SerialCom.h.

boost::function<void(std::string*)> serialcom::SerialCom::readLineCallback
private

Stream readLine callback boost function.

Definition at line 128 of file SerialCom.h.

int serialcom::SerialCom::startbits
private

Definition at line 92 of file SerialCom.h.

int serialcom::SerialCom::stopbits
private

Definition at line 93 of file SerialCom.h.

bool serialcom::SerialCom::stream_paused_
private

Whether streaming is paused or not.

Definition at line 133 of file SerialCom.h.

bool serialcom::SerialCom::stream_stopped_
private

Whether streaming is stopped or not.

Definition at line 135 of file SerialCom.h.

boost::thread* serialcom::SerialCom::stream_thread_
private

Stream thread.

Definition at line 123 of file SerialCom.h.


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


serialcom
Author(s): Ricardo Pascoal
autogenerated on Mon Mar 2 2015 01:32:52