📄 serialstreambuf.h
字号:
/** Set the character size to be used during serial communication. It returns the character size on success and CHAR_SIZE_INVALID on failure. */ const CharSizeEnum SetCharSize(const CharSizeEnum char_size) ; /** Return the character size currently being used for serial communication. */ const CharSizeEnum CharSize() const ; /** Set the number of stop bits used during serial communication. The only valid values are 1 and 2. @param stop_bits The number of stop bits. (1 or 2). @return The number of stop bits or -1 on failure. */ short SetNumOfStopBits(short stop_bits) ; /** Get the number of stop bits being used during serial communication. @return The number of stop bits. */ short NumOfStopBits() const ; /** Set the parity for serial communication. @param parity The parity value. */ const ParityEnum SetParity(const ParityEnum parity) ; /** Get the current parity setting for the serial port. @return The parity setting for the serial port. */ const ParityEnum Parity() const ; /** Use the specified flow control. */ const FlowControlEnum SetFlowControl(const FlowControlEnum flow_c) ; /** Return the current flow control setting. */ const FlowControlEnum FlowControl() const ; /** Set character buffer size. */ const short SetVMin( short vtime ) ; /** Get current size of character buffer. */ const short VMin() const; /** Set character buffer timing in 10th of a second. */ const short SetVTime( short vtime ) ; /** Get current timing of character buffer in 10th of a second. */ const short VTime() const; //@} /** @name Operators */ //@{ //@} /* ------------------------------------------------------------ * Friends * ------------------------------------------------------------ */ protected: /* ------------------------------------------------------------ * Protected Data Members * ------------------------------------------------------------ */ /** Character used to signal that I/O can start while using software flow control with the serial port. */ static const char CTRL_Q = 0x11 ; /** Character used to signal that I/O should stop while using software flow control with the serial port. */ static const char CTRL_S = 0x13 ; /* ------------------------------------------------------------ * Protected Methods * ------------------------------------------------------------ */ /** Performs an operation that is defined separately for each class derived from streambuf. The default behavior is to do nothing if gptr() is non-null and gptr()!=egptr(). Also, setbuf(0, 0) usually means unbuffered I/O and setbuf(p, n) means use p[0]...p[n-1] to hold the buffered characters. In general, this method implements the subclass's notion of getting memory for the buffered characters. In the case of SerialStreamBuf, we want to keep using unbuffered I/O. Hence, using this method has no effect at present. */ virtual std::streambuf* setbuf( char_type*, std::streamsize ) ; /** Reads upto n characters from the serial port and returns them through the character array located at s. @return The number of characters actually read from the serial port. */ virtual std::streamsize xsgetn( char_type* s, std::streamsize n ) ; /** Check, wether input is available on the port. If you call \c SerialStream::in_avail, this method will be called to check for available input. \code while( serial_port.rdbuf()->in_avail() > 0 ) { serial_port.get(ch); ... } \endcode */ virtual std::streamsize showmanyc(); /** Reads and returns the next character from the associated serial port if one otherwise returns traits::eof(). This method is used for buffered I/O while uflow() is called for unbuffered I/O. @return The next character from the serial port. */ virtual int_type underflow() ; /** Reads and returns the next character from the associated serial port if one otherwise returns traits::eof(). This method is used for unbuffered I/O while underflow() is called for unbuffered I/O. @return The next character from the serial port. */ virtual int_type uflow() ; /** This function is called when a putback of a character fails. This must be implemented for unbuffered I/O as all streambuf subclasses are required to provide putback of at lease on character. */ virtual int_type pbackfail(int_type c = traits_type::eof()) ; /** Writes upto n characters from the character sequence at s to the serial port associated with the buffer. @return The number of characters that were successfully written to the serial port. */ virtual std::streamsize xsputn( const char_type* s, std::streamsize n ) ; /** Writes the specified character to the associated serial port. @return The character c. */ virtual int_type overflow(int_type c) ; private: /* ------------------------------------------------------------ * Private Data Members * ------------------------------------------------------------ */ /** We use unbuffered I/O for the serial port. However, we need to provide the putback of atleast one character. This character contains the putback character. */ char mPutbackChar ; /** True if a putback value is available in mPutbackChar. */ bool mPutbackAvailable ; /** The file descriptor associated with the serial port. */ int mFileDescriptor ; /* ------------------------------------------------------------ * Private Methods * ------------------------------------------------------------ */ /** This routine is called by open() in order to initialize some parameters of the serial port and setting its parameters to default values. @return -1 on failure and some other value on success. */ int InitializeSerialPort() ; } ; // class SerialStreamBuf inline SerialStreamBuf::SerialStreamBuf() : mPutbackChar(0), mPutbackAvailable(false), mFileDescriptor(-1) { setbuf(0, 0) ; return ; } inline SerialStreamBuf::~SerialStreamBuf() { if( this->is_open() ) { this->close() ; } return ; } inline bool SerialStreamBuf::is_open() const { return (-1 != mFileDescriptor) ; } inline std::streambuf* SerialStreamBuf::setbuf(char_type *, std::streamsize) { return std::streambuf::setbuf(0, 0) ; } inline SerialStreamBuf* SerialStreamBuf::close() { // // Return a null pointer if the serial port is not currently open. // if( this->is_open() == false ) { return 0 ; } // // Otherwise, close the serial port and set the file descriptor // to an invalid value. // if( -1 == ::close(mFileDescriptor) ) { // // If the close failed then return a null pointer. // return 0 ; } else { // // Set the file descriptor to an invalid value, -1. // mFileDescriptor = -1 ; // // On success, return "this" as required by the C++ standard. // return this ; } } inline std::streambuf::int_type SerialStreamBuf::uflow() { int_type next_ch = underflow() ; mPutbackAvailable = false ; return next_ch ; } } ; // namespace LibSerial} // extern "C++"#endif // #ifndef _SerialStreamBuf_h_
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -