SerialPort.h
Go to the documentation of this file.
/***************************************************************************
* Copyright (C) 2004 by Manish Pagey *
* crayzeewulf@users.sourceforge.net
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef _SerialPort_h_
#define _SerialPort_h_
#include <string>
#include <vector>
#include <stdexcept>
#include <termios.h>
class SerialPort
{
public:
enum BaudRate {
BAUD_50 = B50,
BAUD_75 = B75,
BAUD_110 = B110,
BAUD_134 = B134,
BAUD_150 = B150,
BAUD_200 = B200,
BAUD_300 = B300,
BAUD_600 = B600,
BAUD_1200 = B1200,
BAUD_1800 = B1800,
BAUD_2400 = B2400,
BAUD_4800 = B4800,
BAUD_9600 = B9600,
BAUD_19200 = B19200,
BAUD_38400 = B38400,
BAUD_57600 = B57600,
BAUD_115200 = B115200,
BAUD_230400 = B230400,
//
// Bug#1318912: B460800 is defined on Linux but not on Mac OS
// X. What about other operating systems ?
//
#ifdef __linux__
BAUD_460800 = B460800,
#endif
BAUD_DEFAULT = BAUD_57600
} ;
enum CharacterSize {
CHAR_SIZE_5 = CS5,
CHAR_SIZE_6 = CS6,
CHAR_SIZE_7 = CS7,
CHAR_SIZE_8 = CS8,
CHAR_SIZE_DEFAULT = CHAR_SIZE_8
} ;
enum StopBits {
STOP_BITS_1,
STOP_BITS_2,
STOP_BITS_DEFAULT = STOP_BITS_1
} ;
enum Parity {
PARITY_EVEN,
PARITY_ODD,
PARITY_NONE,
PARITY_DEFAULT = PARITY_NONE
} ;
enum FlowControl {
FLOW_CONTROL_HARD,
FLOW_CONTROL_SOFT,
FLOW_CONTROL_NONE,
FLOW_CONTROL_DEFAULT = FLOW_CONTROL_NONE
} ;
class NotOpen : public std::logic_error
{
public:
NotOpen(const std::string& whatArg) :
logic_error(whatArg) { }
} ;
class OpenFailed : public std::runtime_error
{
public:
OpenFailed(const std::string& whatArg) :
runtime_error(whatArg) { }
} ;
class AlreadyOpen : public std::logic_error
{
public:
AlreadyOpen( const std::string& whatArg ) :
logic_error(whatArg) { }
} ;
class UnsupportedBaudRate : public std::runtime_error
{
public:
UnsupportedBaudRate( const std::string& whatArg ) :
runtime_error(whatArg) { }
} ;
class ReadTimeout : public std::runtime_error
{
public:
ReadTimeout() : runtime_error( "Read timeout" ) { }
} ;
explicit SerialPort( const std::string& serialPortName ) ;
virtual ~SerialPort() throw() ;
void
Open( const BaudRate baudRate = BAUD_DEFAULT,
const CharacterSize charSize = CHAR_SIZE_DEFAULT,
const Parity parityType = PARITY_DEFAULT,
const StopBits stopBits = STOP_BITS_DEFAULT,
const FlowControl flowControl = FLOW_CONTROL_DEFAULT )
throw( AlreadyOpen,
OpenFailed,
UnsupportedBaudRate,
std::invalid_argument ) ;
bool
IsOpen() const ;
void
Close()
throw(NotOpen) ;
void
SetBaudRate( const BaudRate baudRate )
throw( UnsupportedBaudRate,
NotOpen,
std::invalid_argument ) ;
BaudRate
GetBaudRate() const
throw( NotOpen,
std::runtime_error ) ;
void
SetCharSize( const CharacterSize charSize )
throw( NotOpen,
std::invalid_argument ) ;
CharacterSize
GetCharSize() const
throw(NotOpen) ;
void
SetParity( const Parity parityType )
throw( NotOpen,
std::invalid_argument ) ;
Parity
GetParity() const
throw(NotOpen) ;
void
SetNumOfStopBits( const StopBits numOfStopBits )
throw( NotOpen,
std::invalid_argument ) ;
StopBits
GetNumOfStopBits() const
throw(NotOpen) ;
void
SetFlowControl( const FlowControl flowControl )
throw( NotOpen,
std::invalid_argument ) ;
FlowControl
GetFlowControl() const
throw( NotOpen ) ;
bool
IsDataAvailable() const
throw(NotOpen) ;
unsigned char
ReadByte( const unsigned int msTimeout = 0 )
throw( NotOpen,
ReadTimeout,
std::runtime_error ) ;
typedef std::vector<unsigned char> DataBuffer ;
void
Read( DataBuffer& dataBuffer,
const unsigned int numOfBytes = 0,
const unsigned int msTimeout = 0 )
throw( NotOpen,
ReadTimeout,
std::runtime_error ) ;
const std::string
ReadLine( const unsigned int msTimeout = 0,
const char lineTerminator = '\n' )
throw( NotOpen,
ReadTimeout,
std::runtime_error ) ;
void
WriteByte(const unsigned char dataByte)
throw( NotOpen,
std::runtime_error ) ;
void
Write(const DataBuffer& dataBuffer)
throw( NotOpen,
std::runtime_error ) ;
void
Write(const std::string& dataString)
throw( NotOpen,
std::runtime_error ) ;
void
SetDtr( const bool dtrState = true )
throw( NotOpen,
std::runtime_error ) ;
bool
GetDtr() const
throw( NotOpen,
std::runtime_error ) ;
void
SetRts( const bool rtsState = true )
throw( NotOpen,
std::runtime_error ) ;
bool
GetRts() const
throw( NotOpen,
std::runtime_error ) ;
//void
//SetCts( const bool ctsState = true )
// throw( NotOpen,
// std::runtime_error ) ;
bool
GetCts() const
throw( NotOpen,
std::runtime_error ) ;
//void
//SetDsr( const bool dsrState = true )
// throw( NotOpen,
// std::runtime_error ) ;
bool
GetDsr() const
throw( NotOpen,
std::runtime_error ) ;
private:
SerialPort( const SerialPort& otherSerialPort ) ;
SerialPort& operator=(const SerialPort& otherSerialPort ) ;
/*
* Forward declaration of the implementation class folowing the
* PImpl idiom.
*/
class SerialPortImpl ;
SerialPortImpl* mSerialPortImpl ;
} ;
#endif // #ifndef _SerialPort_h_
SerialPort::Parity
Parity
Definition: SerialPort.h:99
SerialPort::AlreadyOpen::AlreadyOpen
AlreadyOpen(const std::string &whatArg)
Definition: SerialPort.h:130
SerialPort::BAUD_38400
Definition: SerialPort.h:71
SerialPort::ReadTimeout
Definition: SerialPort.h:141
SerialPort::BAUD_230400
Definition: SerialPort.h:74
SerialPort::Close
void Close()
Close the serial port.
SerialPort::BAUD_134
Definition: SerialPort.h:60
SerialPort::DataBuffer
std::vector< unsigned char > DataBuffer
Read the specified number of bytes from the serial port.
Definition: SerialPort.h:359
SerialPort
Definition: SerialPort.h:50
SerialPort::UnsupportedBaudRate::UnsupportedBaudRate
UnsupportedBaudRate(const std::string &whatArg)
Definition: SerialPort.h:137
SerialPort::NotOpen
Definition: SerialPort.h:113
SerialPort::BAUD_110
Definition: SerialPort.h:59
SerialPort::ReadByte
unsigned char ReadByte(const unsigned int msTimeout=0)
Read a single byte from the serial port.
SerialPort::STOP_BITS_2
1 stop bit.
Definition: SerialPort.h:95
SerialPort::OpenFailed::OpenFailed
OpenFailed(const std::string &whatArg)
Definition: SerialPort.h:123
SerialPort::GetFlowControl
FlowControl GetFlowControl() const
Get the current flow control setting.
SerialPort::FLOW_CONTROL_DEFAULT
Definition: SerialPort.h:110
SerialPort::BAUD_1200
Definition: SerialPort.h:65
SerialPort::GetParity
Parity GetParity() const
Get the parity type for the serial port.
SerialPort::CHAR_SIZE_DEFAULT
Definition: SerialPort.h:90
SerialPort::Read
void Read(DataBuffer &dataBuffer, const unsigned int numOfBytes=0, const unsigned int msTimeout=0)
SerialPort::ReadTimeout::ReadTimeout
ReadTimeout()
Definition: SerialPort.h:144
SerialPort::FLOW_CONTROL_SOFT
Definition: SerialPort.h:108
SerialPort::UnsupportedBaudRate
Definition: SerialPort.h:134
SerialPort::BAUD_57600
Definition: SerialPort.h:72
SerialPort::GetBaudRate
BaudRate GetBaudRate() const
Get the current baud rate for the serial port.
SerialPort::SetRts
void SetRts(const bool rtsState=true)
Set the RTS line to the specified value.
SerialPort::IsOpen
bool IsOpen() const
Check if the serial port is open for I/O.
SerialPort::BAUD_115200
Definition: SerialPort.h:73
SerialPort::BAUD_75
Definition: SerialPort.h:58
SerialPort::Write
void Write(const DataBuffer &dataBuffer)
Write the data from the specified vector to the serial port.
SerialPort::CharacterSize
CharacterSize
Definition: SerialPort.h:85
SerialPort::CHAR_SIZE_7
7 bit characters.
Definition: SerialPort.h:88
SerialPort::BAUD_1800
Definition: SerialPort.h:66
SerialPort::AlreadyOpen
Definition: SerialPort.h:127
SerialPort::OpenFailed
Definition: SerialPort.h:120
SerialPort::GetDtr
bool GetDtr() const
Get the status of the DTR line.
SerialPort::FLOW_CONTROL_HARD
Definition: SerialPort.h:107
SerialPort::BAUD_19200
Definition: SerialPort.h:70
SerialPort::PARITY_DEFAULT
Definition: SerialPort.h:103
SerialPort::PARITY_NONE
No parity i.e. parity checking disabled.
Definition: SerialPort.h:102
SerialPort::GetRts
bool GetRts() const
Get the status of the RTS line.
SerialPort::SetBaudRate
void SetBaudRate(const BaudRate baudRate)
Set the baud rate for the serial port to the specified value (baudRate).
SerialPort::CHAR_SIZE_5
5 bit characters.
Definition: SerialPort.h:86
SerialPort::FLOW_CONTROL_NONE
Definition: SerialPort.h:109
SerialPort::Open
void Open(const BaudRate baudRate=BAUD_DEFAULT, const CharacterSize charSize=CHAR_SIZE_DEFAULT, const Parity parityType=PARITY_DEFAULT, const StopBits stopBits=STOP_BITS_DEFAULT, const FlowControl flowControl=FLOW_CONTROL_DEFAULT)
Open the serial port with the specified settings.
SerialPort::BAUD_9600
Definition: SerialPort.h:69
SerialPort::SetDtr
void SetDtr(const bool dtrState=true)
Set the DTR line to the specified value.
SerialPort::BAUD_600
Definition: SerialPort.h:64
SerialPort::GetCharSize
CharacterSize GetCharSize() const
Get the current character size for the serial port.
SerialPort::CHAR_SIZE_8
8 bit characters.
Definition: SerialPort.h:89
SerialPort::SetNumOfStopBits
void SetNumOfStopBits(const StopBits numOfStopBits)
Set the number of stop bits to be used with the serial port.
SerialPort::SetFlowControl
void SetFlowControl(const FlowControl flowControl)
Set flow control.
SerialPort::CHAR_SIZE_6
6 bit characters.
Definition: SerialPort.h:87
SerialPort::GetNumOfStopBits
StopBits GetNumOfStopBits() const
Get the number of stop bits currently being used by the serial port.
SerialPort::STOP_BITS_DEFAULT
2 stop bits.
Definition: SerialPort.h:96
SerialPort::StopBits
StopBits
Definition: SerialPort.h:93
SerialPort::BaudRate
BaudRate
The allowed set of baud rates.
Definition: SerialPort.h:56
SerialPort::BAUD_150
Definition: SerialPort.h:61
SerialPort::BAUD_300
Definition: SerialPort.h:63
SerialPort::GetCts
bool GetCts() const
SerialPort::BAUD_50
Definition: SerialPort.h:57
SerialPort::SerialPort
SerialPort(const std::string &serialPortName)
Constructor for a serial port.
SerialPort::~SerialPort
virtual ~SerialPort()
Destructor.
SerialPort::BAUD_200
Definition: SerialPort.h:62
SerialPort::WriteByte
void WriteByte(const unsigned char dataByte)
Send a single byte to the serial port.
SerialPort::NotOpen::NotOpen
NotOpen(const std::string &whatArg)
Definition: SerialPort.h:116
SerialPort::IsDataAvailable
bool IsDataAvailable() const
Check if data is available at the input of the serial port.
SerialPort::PARITY_EVEN
Even parity.
Definition: SerialPort.h:100
SerialPort::FlowControl
FlowControl
Definition: SerialPort.h:106
SerialPort::BAUD_DEFAULT
Definition: SerialPort.h:82
SerialPort::SetCharSize
void SetCharSize(const CharacterSize charSize)
Set the character size for the serial port.
SerialPort::SetParity
void SetParity(const Parity parityType)
Set the parity type for the serial port.
SerialPort::BAUD_2400
Definition: SerialPort.h:67
SerialPort::STOP_BITS_1
Definition: SerialPort.h:94
SerialPort::BAUD_4800
Definition: SerialPort.h:68
SerialPort::GetDsr
bool GetDsr() const
SerialPort::mSerialPortImpl
SerialPortImpl * mSerialPortImpl
Pointer to implementation class instance.
Definition: SerialPort.h:474
SerialPort::ReadLine
const std::string ReadLine(const unsigned int msTimeout=0, const char lineTerminator= '\n')
Read a line of characters from the serial port.
SerialPort::PARITY_ODD
Odd parity.
Definition: SerialPort.h:101