📄 serialstreambuf.h
字号:
/* * Time-stamp: <04/05/05 16:21:47 pagey> * * $Id: SerialStreamBuf.h,v 1.7 2005/09/05 14:19:55 wedesoft Exp $ * * */#ifndef _SerialStreamBuf_h_#define _SerialStreamBuf_h_#ifndef _termios_h_INCLUDED_# include <termios.h># define _termios_h_INCLUDED_#endif#ifndef _unistd_h_INCLUDED_# include <unistd.h># define _unistd_h_INCLUDED_#endif#ifndef _std_iosfwd_INCLUDED_# include <iosfwd># define _std_iosfwd_INCLUDED_#endif#ifndef _std_streambuf_INCLUDED_# include <streambuf># define _std_streambuf_INCLUDED_#endif#ifndef _std_string_INCLUDED_# include <string># define _std_string_INCLUDED_#endifextern "C++" { namespace LibSerial { /** This is the streambuf subclass used by SerialStream. This * subclass takes care of opening the serial port file in the * required modes and providing the corresponding file * descriptor to SerialStream so that various parameters * associated with the serial port can be set. Several * features of this streambuf class resemble those of * std::filebuf, however this class it not made a subclass of * filebuf because we need access to the file descriptor * associated with the serial port and the standard filebuf * does not provide access to it. * * At present, this class uses unbuffered I/O and all calls to * setbuf() will be ignored. * * @author $Author: wedesoft $ <A HREF="pagey@gnudom.org">Manish P. Pagey</A> * @version $Id: SerialStreamBuf.h,v 1.7 2005/09/05 14:19:55 wedesoft Exp $ * */ class SerialStreamBuf : public std::streambuf { public: /** @name Typedefs */ //@{ //@} /** @name Enumerations */ //@{ /** The baud rates currently supported by the SUS-2 general terminal interface specification. Note that B0 is not supported because it is not really a baud rate (it causes the modem to hang up i.e. drop DTR). Use the close() method instead. */ enum BaudRateEnum { BAUD_50 = B50, //!< 50 baud. BAUD_75 = B75, //!< 75 baud. BAUD_110 = B110, //!< 110 baud. BAUD_134 = B134, //!< 134.5 baud. Yes 134.5. I did not mistype that. BAUD_150 = B150, //!< 150 baud. BAUD_200 = B200, //!< 200 baud. BAUD_300 = B300, //!< 300 baud. BAUD_600 = B600, //!< 600 baud. BAUD_1200 = B1200, //!< 1200 baud. BAUD_1800 = B1800, //!< 1800 baud. BAUD_2400 = B2400, //!< 2400 baud. BAUD_4800 = B4800, //!< 4800 baud. BAUD_9600 = B9600, //!< 9600 baud. BAUD_19200 = B19200, //!< 19200 baud. BAUD_38400 = B38400, //!< 38400 baud. BAUD_57600 = B57600, //!< 57600 baud. BAUD_115200 = B115200, //!< 115200 baud. BAUD_INVALID //!< Invalid baud rate. } ; /** The allowed values of character sizes that can be used during the serial communication. */ enum CharSizeEnum { CHAR_SIZE_5 = CS5, //!< 5 bit characters. CHAR_SIZE_6 = CS6, //!< 6 bit characters. CHAR_SIZE_7 = CS7, //!< 7 bit characters. CHAR_SIZE_8 = CS8, //!< 8 bit characters. CHAR_SIZE_INVALID //!< Invalid character size. } ; /** The allowed values of the parity associated with the serial port communications. */ enum ParityEnum { PARITY_EVEN, //!< Even parity. PARITY_ODD, //!< Odd parity. PARITY_NONE, //!< No parity i.e. parity checking disabled. PARITY_INVALID //!< Invalid parity value. } ; /** The values of the flow control settings for a serial port. */ enum FlowControlEnum { FLOW_CONTROL_HARD, //!< Hardware flow control. FLOW_CONTROL_SOFT, //!< Software flow control. FLOW_CONTROL_NONE, //!< No flow control. FLOW_CONTROL_INVALID //!< Invalid flow control setting. } ; //@} /* ------------------------------------------------------------ * Public Static Members * ------------------------------------------------------------ */ /** @name Public static members. */ //@{ /** The default value of the baud rate of the serial port. */ static const BaudRateEnum DEFAULT_BAUD ; /** The default value of the character size used during the serial communication. */ static const CharSizeEnum DEFAULT_CHAR_SIZE ; /** The default number of stop bits used. */ static const short DEFAULT_NO_OF_STOP_BITS ; /** The default parity setting. */ static const ParityEnum DEFAULT_PARITY ; /** The default flow control setting. */ static const FlowControlEnum DEFAULT_FLOW_CONTROL ; /** The default character buffer size. */ static const short DEFAULT_VMIN ; /** The default character buffer timing. */ static const short DEFAULT_VTIME ; //@} /** @name Exceptions */ //@{ //@} /** @name Constructors and Destructor */ //@{ /** The default constructor. */ SerialStreamBuf() ; /** The destructor. */ ~SerialStreamBuf() ; //@} /** @name Other Public Methods */ //@{ /** Returns true if a previos call to open() succeeded (returned a non-null value) and there has been no intervening call to close. */ bool is_open() const ; /** If is_open() != <tt>false</tt>, returns a null pointer. Otherwise, initializes the <tt>streambuf</tt> as required. It then opens a file, if possible, whose name is given as the string <tt>filename</tt> using the system call <tt>std::open(filename.data(), flags)</tt>. The value of parameter <tt>flags</tt> is obtained from the value of the parameter mode. At present, only <tt>ios_base::in</tt>, <tt>ios_base::out</tt>, and (<tt>ios_base::in|ios_base::out</tt>) make sense for a serial port and hence all other settings result in the call to fail. The value of <tt>flags</tt> is obtained as: <br> <tt>flags = u_flags | O_NOCTTY</tt> <br> where <tt>u_flags</tt> is obtained from the following table depending on the value of the parameter mode: <table align="center"> <tr> <td> <b><tt>in</tt></b> </td> <td> <b><tt>out</tt></b> </td> <td> <b><tt>u_flags</tt></b> </td> </tr> <tr> <td> + </td> <td> </td> <td> <tt>O_RDONLY</tt> </td> </tr> <tr> <td> </td> <td> + </td> <td> <tt>O_WRONLY</tt> </td> </tr> <tr> <td> + </td> <td> + </td> <td> <tt>O_RDWR</tt> </td> </tr> </table> @return If the <tt>open</tt>() system call succeeds the method returns <tt>this</tt>. If the call fails, then it returns a null pointer. */ SerialStreamBuf* open( const std::string filename, std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out ) ; /** If is_open() == false, returns a null pointer. If a put area exists, calls overflow(EOF) to flush characters. Finally it closes the file by calling <tt>std::close(mFileDescriptor)</tt> where mFileDescriptor is the value returned by the last call to open(). For the implementation of the corresponding function in class filebuf, if the last virtual member function called on <tt>*this</tt> (between underflow, overflow, <tt>seekoff</tt>, and <tt>seekpos</tt>) was overflow then it calls <tt>a_codecvt.unshift</tt> (possible several times) to determine a termination sequence, inserts those characters and calls overflow(EOF) again. However, <b>this is not implemented here yet</b>. <b>Postcondition</b>: is_open() == <tt>false<tt> @return <tt>this</tt> on success, a null pointer otherwise. */ SerialStreamBuf* close() ; /** Initialize the serial communication parameters to their default values. */ int SetParametersToDefault() ; /** If is_open() != true, return -1. Otherwise, set the baud rate of the associated serial port. Return the baud rate on success and BAUD_INVALID on failure. */ const BaudRateEnum SetBaudRate(const BaudRateEnum baud_rate) ; /** Return the current baud rate of the serial port. If the baud rate is not set to a valid value then it returns BAUD_INVALID. */ const BaudRateEnum BaudRate() const ;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -