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

Static Public Member Functions

static std::string modemFlagsToString (int modem)
 
static int createPtyLink (const std::string &linkname)
 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.
 
static void createLinkToPty (const std::string &linkname, int fd)
 Create a symbolic link to ptsname(fd).
 
static int createPty (bool hup)
 Create a pseudo-terminal and return the file descriptor.
 
static bool waitForOpen (int fd, int timeout)
 Wait up to timeout seconds for pts to be opened.
 

Private Types

enum  state { OK , TIMEOUT_OR_EOF }
 

Private Member Functions

SerialPortoperator= (const SerialPort &)
 No assignment.
 

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

◆ state

Enumerator
OK 
TIMEOUT_OR_EOF 

Constructor & Destructor Documentation

◆ SerialPort() [1/4]

SerialPort::SerialPort ( )

◆ SerialPort() [2/4]

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() [3/4]

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() [4/4]

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

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

References getBlocking().

◆ ~SerialPort()

SerialPort::~SerialPort ( )
virtual

close the file descriptor if is is open.

References _savebuf, and close().

Member Function Documentation

◆ applyTermios()

void SerialPort::applyTermios ( )

Apply the Termios settings to an opened serial port.

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

◆ clearModemBits()

void SerialPort::clearModemBits ( int val)

Clear the indicated modem bits.

References _fd, and _name.

◆ close()

void SerialPort::close ( )

close the file descriptor.

References _fd, close(), getName(), and ILOG.

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

◆ createLinkToPty()

void SerialPort::createLinkToPty ( const std::string & linkname,
int fd )
static

Create a symbolic link to ptsname(fd).

fd must be a pty, such as returned by createPty().

References fd.

Referenced by createPtyLink().

◆ createPty()

int SerialPort::createPty ( bool hup)
static

Create a pseudo-terminal and return the file descriptor.

This is called by createPtyLink(), but it can also be called separately if the caller wants to pass hup as true to set the HUP condition on the slave side before creating the link.

References close(), DLOG, fd, and open().

Referenced by createPtyLink().

◆ createPtyLink()

int SerialPort::createPtyLink ( const std::string & linkname)
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.

It is a convenient wrapper for this code:

int fd = createPty(false);
createLinkToPty(linkname, fd);
static int createPty(bool hup)
Create a pseudo-terminal and return the file descriptor.
Definition SerialPort.cc:368
static void createLinkToPty(const std::string &linkname, int fd)
Create a symbolic link to ptsname(fd).
Definition SerialPort.cc:391
int fd
Definition twod.c:56
Parameters
linknameName 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 createLinkToPty(), createPty(), and fd.

◆ drain()

void SerialPort::drain ( )

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.

◆ flushBoth()

void SerialPort::flushBoth ( )

References _fd, and _name.

◆ flushInput()

void SerialPort::flushInput ( )

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.

◆ flushOutput()

void SerialPort::flushOutput ( )

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.

◆ getBlocking()

bool SerialPort::getBlocking ( )

References _blocking, _fd, and _name.

Referenced by SerialPort().

◆ getCarrierDetect()

bool SerialPort::getCarrierDetect ( )

References getModemStatus().

◆ getFd()

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

References _fd.

Referenced by GPS_SetClock::run(), and Csat3Sim::run().

◆ getModemStatus()

int SerialPort::getModemStatus ( )

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().

◆ getName()

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

Get device name of the SerialPort.

References _name.

Referenced by close(), open(), Garmin::readMessage(), GPS_SetClock::run(), and Csat3Sim::run().

◆ getTermios()

const Termios & nidas::util::SerialPort::getTermios ( ) const
inline

Readonly reference to Termios.

References _termios.

◆ modemFlagsToString()

string SerialPort::modemFlagsToString ( int modem)
static

◆ open()

int SerialPort::open ( int mode = O_RDONLY)
virtual

open the serial port.

The current Termios settings are also applied to the port.

References _blocking, _fd, _name, _termios, nidas::util::Termios::apply(), getName(), ILOG, open(), and setBlocking().

Referenced by createPty(), open(), Garmin::run(), and GPS_SetClock::run().

◆ operator=()

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

No assignment.

◆ read()

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

References _fd, _name, _state, len, OK, read(), and TIMEOUT_OR_EOF.

Referenced by read(), readchar(), readUntil(), and Csat3Sim::run().

◆ readchar()

char SerialPort::readchar ( )
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().

Referenced by SensorSimulator::readPrompts().

◆ readLine()

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

Do a readUntil with a newline terminator.

References len, and readUntil().

Referenced by GPS_SetClock::run().

◆ readUntil()

int SerialPort::readUntil ( char * buf,
int len,
char term )
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 _savealloc, _savebuf, _savelen, _savep, len, and read().

Referenced by readLine(), and Garmin::readMessage().

◆ setBlocking()

void SerialPort::setBlocking ( bool val)

References _blocking, _fd, and _name.

Referenced by open().

◆ setModemBits()

void SerialPort::setModemBits ( int val)

Set the indicated modem bits.

References _fd, and _name.

◆ setModemStatus()

void SerialPort::setModemStatus ( int val)

Set the current state of the modem bits.

References _fd, and _name.

◆ setName()

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

Set device name of the SerialPort.

References _name.

Referenced by Garmin::run().

◆ termios()

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 Garmin::run(), and GPS_SetClock::run().

◆ timeoutOrEOF()

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

References _state, and TIMEOUT_OR_EOF.

◆ waitForOpen()

bool SerialPort::waitForOpen ( int fd,
int timeout )
static

Wait up to timeout seconds for pts to be opened.

Wait up to the timeout for the HUP to be cleared. If the HUP condition was set when the pty was created, then the cleared HUP means the ptsname(fd) device has been opened. Wait forever if timeout is negative. The poll() does not block on the fd, so it is called at 1-second intervals using sleep(), until the timeout expires or the poll returns with the POLLHUP bit cleared.

So here is a typical usage:

int fd = createPty(true);
bool opened = waitForOpen(fd, -1);
static bool waitForOpen(int fd, int timeout)
Wait up to timeout seconds for pts to be opened.
Definition SerialPort.cc:438

The existence of the link is a synchronization point, indicating the pty has been created and HUP set, so it's safe to wait for HUP to be cleared to know that it has been opened.

References fd.

◆ write()

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

Member Data Documentation

◆ _blocking

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

Referenced by getBlocking(), open(), setBlocking(), and write().

◆ _fd

int nidas::util::SerialPort::_fd
private

◆ _name

std::string nidas::util::SerialPort::_name
private

◆ _savealloc

int nidas::util::SerialPort::_savealloc
private

Referenced by readchar(), and readUntil().

◆ _savebuf

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

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

◆ _savelen

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

Referenced by readchar(), and readUntil().

◆ _savep

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

Referenced by readchar(), and readUntil().

◆ _state

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

Referenced by read(), and timeoutOrEOF().

◆ _termios

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

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