nidas  v1.2-1520
Public Member Functions | Static Public Member Functions | Private Types | Private Member Functions | Private Attributes | List of all members
nidas::util::SerialPort Class Reference

#include <SerialPort.h>

Public Member Functions

 SerialPort ()
 
 SerialPort (const SerialPort &)
 Copy constructor. More...
 
 SerialPort (const std::string &name)
 Constructor, given a device name. More...
 
 SerialPort (const std::string &name, int fd) throw (IOException)
 For serial port that is already open (stdin for example). More...
 
virtual ~SerialPort ()
 close the file descriptor if is is open. More...
 
Termiostermios ()
 Writable reference to the SerialPort's Termios. More...
 
const TermiosgetTermios () const
 Readonly reference to Termios. More...
 
void applyTermios () throw (IOException)
 Apply the Termios settings to an opened serial port. More...
 
const std::string & getName () const
 Get device name of the SerialPort. More...
 
void setName (const std::string &val)
 Set device name of the SerialPort. More...
 
virtual int open (int mode=O_RDONLY) throw (IOException)
 open the serial port. More...
 
void close ()
 close the file descriptor. More...
 
int getFd () const
 
int getModemStatus () throw (IOException)
 Get the current state of the modem bits. More...
 
void setModemStatus (int val) throw (IOException)
 Set the current state of the modem bits. More...
 
void clearModemBits (int val) throw (IOException)
 Clear the indicated modem bits. More...
 
void setModemBits (int val) throw (IOException)
 Set the indicated modem bits. More...
 
bool getCarrierDetect () throw (IOException)
 
void setBlocking (bool val) throw (IOException)
 
bool getBlocking () throw (IOException)
 
void drain () throw (IOException)
 Do a tcdrain() system call on the device. More...
 
void flushInput () throw (IOException)
 Do a tcflush() system call on the device. More...
 
void flushOutput () throw (IOException)
 Do a tcflush() system call on the device. More...
 
void flushBoth () throw (IOException)
 
int timeoutOrEOF () const
 
virtual int readUntil (char *buf, int len, char term) throw (IOException)
 Read bytes until either the term character is read, or len-1 number of characters have been read. More...
 
virtual int readLine (char *buf, int len) throw (IOException)
 Do a readUntil with a newline terminator. More...
 
virtual int read (char *buf, int len) throw (IOException)
 
virtual char readchar () throw (IOException)
 Do a buffered read and return character read. More...
 
virtual int write (const void *buf, int len) throw (IOException)
 

Static Public Member Functions

static std::string modemFlagsToString (int modem)
 
static int createPtyLink (const std::string &linkname) throw (IOException)
 Static utility that creates a pseudo-terminal, returning the file descriptor of the master side and creating a symbolic link with the given name to the slave side. More...
 

Private Types

enum  state { OK, TIMEOUT_OR_EOF }
 

Private Member Functions

SerialPortoperator= (const SerialPort &)
 No assignment. More...
 

Private Attributes

Termios _termios
 
int _fd
 
std::string _name
 
enum nidas::util::SerialPort::state _state
 
char * _savep
 
char * _savebuf
 
int _savelen
 
int _savealloc
 
bool _blocking
 

Member Enumeration Documentation

Enumerator
OK 
TIMEOUT_OR_EOF 

Constructor & Destructor Documentation

SerialPort::SerialPort ( )
SerialPort::SerialPort ( const SerialPort x)

Copy constructor.

The attributes of the port are copied, but if the original is opened, the copy will not be opened.

SerialPort::SerialPort ( const std::string &  name)

Constructor, given a device name.

The device is NOT opened, mainly just to avoid throwing an exception in the constructor. Perhaps that should be changed.

SerialPort::SerialPort ( const std::string &  name,
int  fd 
)
throw (IOException
)

For serial port that is already open (stdin for example).

SerialPort::~SerialPort ( )
virtual

close the file descriptor if is is open.

References _savebuf, and close().

Member Function Documentation

void SerialPort::applyTermios ( )
throw (IOException
)

Apply the Termios settings to an opened serial port.

References _fd, _name, _termios, and nidas::util::Termios::apply().

void SerialPort::clearModemBits ( int  val)
throw (IOException
)
void SerialPort::close ( )

close the file descriptor.

References _fd, getName(), and ILOG.

Referenced by Garmin::run(), GPS_SetClock::run(), and ~SerialPort().

int SerialPort::createPtyLink ( const std::string &  linkname)
throw (IOException
)
static

Static utility that creates a pseudo-terminal, returning the file descriptor of the master side and creating a symbolic link with the given name to the slave side.

Parameters
linkname,:Name of symbolic link to be created that links to the slave side of the pseudo-terminal. If a symbolic link already exists with that name it will be removed and re-created. If linkname already exists and it isn't a symbolic link, an error will be returned.
Returns
The file descriptor of the master side of the pseudo-terminal.

Note: the symbolic link should be deleted when the file descriptor to the master pseudo-terminal is closed. Otherwise, because of the way the system recycles pseudo-terminal devices, the link may at some time point to a different pseudo-terminal, probably created by a different process, like sshd. Opening and reading/writing to the symbolic link would then effect the other process, if the open was permitted.

References fd.

Referenced by SensorSimApp::main(), TeeTTy::run(), and TeeI2C::run().

void SerialPort::drain ( )
throw (IOException
)

Do a tcdrain() system call on the device.

According to the tcdrain man page, it "waits until all output written to the object referred to by fd has been transmitted".

References _fd, and _name.

void SerialPort::flushBoth ( )
throw (IOException
)

References _fd, and _name.

Referenced by nidas::dynld::isff::GOESXmtr::open(), and openPort().

void SerialPort::flushInput ( )
throw (IOException
)

Do a tcflush() system call on the device.

According to the tcflush man page, it "discards data received but not read".

References _fd, and _name.

void SerialPort::flushOutput ( )
throw (IOException
)

Do a tcflush() system call on the device.

According to the tcflush man page, it "discards data written to the object referred to by fd but not transmitted".

References _fd, and _name.

bool SerialPort::getBlocking ( )
throw (IOException
)

References _blocking, _fd, and _name.

bool SerialPort::getCarrierDetect ( )
throw (IOException
)

References getModemStatus().

int nidas::util::SerialPort::getFd ( ) const
inline
int SerialPort::getModemStatus ( )
throw (IOException
)

Get the current state of the modem bits.

Do "man tty_ioctl" from Linux for more information. These macros are useful for checking/setting the value of of individual bits:

TIOCM_LE DSR (data set ready/line enable)
TIOCM_DTR DTR (data terminal ready)
TIOCM_RTS RTS (request to send)
TIOCM_ST Secondary TXD (transmit)
TIOCM_SR Secondary RXD (receive)
TIOCM_CTS CTS (clear to send)
TIOCM_CAR DCD (data carrier detect)
TIOCM_CD see TIOCM_CAR
TIOCM_RNG RNG (ring)
TIOCM_RI see TIOCM_RNG
TIOCM_DSR DSR (data set ready)

References _fd, and _name.

Referenced by getCarrierDetect(), openPort(), ModemLineSetter::run(), ModemLineMonitor::run(), nidas::dynld::isff::SE_GOESXmtr::tosleep(), and nidas::dynld::isff::SE_GOESXmtr::wakeup().

const std::string& nidas::util::SerialPort::getName ( ) const
inline
const Termios& nidas::util::SerialPort::getTermios ( ) const
inline

Readonly reference to Termios.

References _termios.

Referenced by nidas::dynld::isff::SE_GOESXmtr::getXmtrClockDelay().

string SerialPort::modemFlagsToString ( int  modem)
static
int SerialPort::open ( int  mode = O_RDONLY)
throw (IOException
)
virtual

open the serial port.

The current Termios settings are also applied to the port.

References ILOG.

Referenced by nidas::dynld::isff::GOESXmtr::open(), openPort(), Garmin::run(), GPS_SetClock::run(), and TeeTTy::run().

SerialPort& nidas::util::SerialPort::operator= ( const SerialPort )
private

No assignment.

int SerialPort::read ( char *  buf,
int  len 
)
throw (IOException
)
virtual

References len.

Referenced by readchar(), TeeTTy::run(), Receiver::run(), and Csat3Sim::run().

char SerialPort::readchar ( )
throw (IOException
)
virtual

Do a buffered read and return character read.

If '\0' is read, then do a check of timeoutOrEOF() to see if the basic read returned 0.

References _savealloc, _savebuf, _savelen, _savep, and read().

int SerialPort::readLine ( char *  buf,
int  len 
)
throw (IOException
)
virtual

Do a readUntil with a newline terminator.

References len.

Referenced by GPS_SetClock::run().

int SerialPort::readUntil ( char *  buf,
int  len,
char  term 
)
throw (IOException
)
virtual

Read bytes until either the term character is read, or len-1 number of characters have been read.

buf will be null terminated.

References len.

Referenced by Garmin::readMessage().

void SerialPort::setBlocking ( bool  val)
throw (IOException
)

Referenced by openPort().

void SerialPort::setModemBits ( int  val)
throw (IOException
)

Set the indicated modem bits.

Referenced by nidas::dynld::isff::SE_GOESXmtr::wakeup().

void SerialPort::setModemStatus ( int  val)
throw (IOException
)

Set the current state of the modem bits.

Referenced by ModemLineSetter::run().

void nidas::util::SerialPort::setName ( const std::string &  val)
inline

Set device name of the SerialPort.

References _name.

Referenced by openPort(), and Garmin::run().

Termios& nidas::util::SerialPort::termios ( )
inline

Writable reference to the SerialPort's Termios.

If the SerialPort is open, the user should call applyTermios() for any modifications to take effect.

References _termios.

Referenced by openPort(), Garmin::run(), GPS_SetClock::run(), TeeTTy::run(), and nidas::dynld::isff::SE_GOESXmtr::SE_GOESXmtr().

int nidas::util::SerialPort::timeoutOrEOF ( ) const
inline

References _state, and TIMEOUT_OR_EOF.

int SerialPort::write ( const void *  buf,
int  len 
)
throw (IOException
)
virtual

Member Data Documentation

bool nidas::util::SerialPort::_blocking
private

Referenced by getBlocking().

int nidas::util::SerialPort::_fd
private
std::string nidas::util::SerialPort::_name
private
int nidas::util::SerialPort::_savealloc
private

Referenced by readchar().

char* nidas::util::SerialPort::_savebuf
private

Referenced by readchar(), and ~SerialPort().

int nidas::util::SerialPort::_savelen
private

Referenced by readchar().

char* nidas::util::SerialPort::_savep
private

Referenced by readchar().

enum nidas::util::SerialPort::state nidas::util::SerialPort::_state
private

Referenced by timeoutOrEOF().

Termios nidas::util::SerialPort::_termios
private

Referenced by applyTermios(), getTermios(), and termios().


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