nidas v1.2.3
|
#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. | |
Termios & | termios () |
Writable reference to the SerialPort's Termios. | |
const Termios & | getTermios () 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 | |
SerialPort & | operator= (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 |
|
private |
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 ) |
For serial port that is already open (stdin for example).
References getBlocking().
|
virtual |
void SerialPort::applyTermios | ( | ) |
Apply the Termios settings to an opened serial port.
References _fd, _name, _termios, and nidas::util::Termios::apply().
void SerialPort::clearModemBits | ( | int | val | ) |
void SerialPort::close | ( | ) |
close the file descriptor.
References _fd, close(), getName(), and ILOG.
Referenced by close(), createPty(), Garmin::run(), GPS_SetClock::run(), and ~SerialPort().
|
static |
Create a symbolic link to ptsname(fd).
fd
must be a pty, such as returned by createPty().
References fd.
Referenced by createPtyLink().
|
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().
|
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:
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. |
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.
void SerialPort::drain | ( | ) |
void SerialPort::flushInput | ( | ) |
void SerialPort::flushOutput | ( | ) |
bool SerialPort::getBlocking | ( | ) |
References _blocking, _fd, and _name.
Referenced by SerialPort().
bool SerialPort::getCarrierDetect | ( | ) |
References getModemStatus().
|
inline |
References _fd.
Referenced by GPS_SetClock::run(), and Csat3Sim::run().
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:
Referenced by getCarrierDetect().
|
inline |
Get device name of the SerialPort.
References _name.
Referenced by close(), open(), Garmin::readMessage(), GPS_SetClock::run(), and Csat3Sim::run().
|
inline |
|
static |
|
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().
|
private |
No assignment.
|
virtual |
References _fd, _name, _state, len, OK, read(), and TIMEOUT_OR_EOF.
Referenced by read(), readchar(), readUntil(), and Csat3Sim::run().
|
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().
|
virtual |
Do a readUntil with a newline terminator.
References len, and readUntil().
Referenced by GPS_SetClock::run().
|
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().
void SerialPort::setBlocking | ( | bool | val | ) |
void SerialPort::setModemStatus | ( | int | val | ) |
|
inline |
|
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().
|
inline |
References _state, and TIMEOUT_OR_EOF.
|
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:
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.
|
virtual |
References _blocking, _fd, _name, len, and write().
Referenced by Garmin::disableAllMessages(), Garmin::disablePPS(), Garmin::enableAllMessages(), Garmin::enablePPS(), Garmin::readInit(), Csat3Sim::run(), Csat3Sim::sendMessage(), Garmin::setBaudRateOption(), write(), SensorSimulator::writeMessage(), and SensorSimulator::writeMessage().
|
private |
Referenced by getBlocking(), open(), setBlocking(), and write().
|
private |
Referenced by applyTermios(), clearModemBits(), close(), drain(), flushBoth(), flushInput(), flushOutput(), getBlocking(), getFd(), getModemStatus(), open(), read(), setBlocking(), setModemBits(), setModemStatus(), and write().
|
private |
Referenced by applyTermios(), clearModemBits(), drain(), flushBoth(), flushInput(), flushOutput(), getBlocking(), getModemStatus(), getName(), open(), read(), setBlocking(), setModemBits(), setModemStatus(), setName(), and write().
|
private |
Referenced by readchar(), and readUntil().
|
private |
Referenced by readchar(), readUntil(), and ~SerialPort().
|
private |
Referenced by readchar(), and readUntil().
|
private |
Referenced by readchar(), and readUntil().
|
private |
Referenced by read(), and timeoutOrEOF().
|
private |
Referenced by applyTermios(), getTermios(), open(), and termios().