📄 serial.h
字号:
// Copyright (C) 1999-2005 Open Source Telecom Corporation.//// This program is free software; you can redistribute it and/or modify// it under the terms of the GNU General Public License as published by// the Free Software Foundation; either version 2 of the License, or// (at your option) any later version.// // This program is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the// GNU General Public License for more details.// // You should have received a copy of the GNU General Public License// along with this program; if not, write to the Free Software// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.// // As a special exception, you may use this file as part of a free software// library without restriction. Specifically, if other files instantiate// templates or use macros or inline functions from this file, or you compile// this file and link it with other files to produce an executable, this// file does not by itself cause the resulting executable to be covered by// the GNU General Public License. This exception does not however // invalidate any other reasons why the executable file might be covered by// the GNU General Public License. //// This exception applies only to the code released under the name GNU// Common C++. If you copy code from other releases into a copy of GNU// Common C++, as the General Public License permits, the exception does// not apply to the code that you add in this way. To avoid misleading// anyone as to the status of such modified files, you must delete// this exception notice from them.//// If you write modifications of your own for GNU Common C++, it is your choice// whether to permit this exception to apply to your modifications.// If you do not wish that, delete this exception notice.///** * @file serial.h * @short Serial I/O services. **/#ifndef CCXX_SERIAL_H_#define CCXX_SERIAL_H_#ifndef CCXX_MISSING_H_#include <cc++/missing.h>#endif#ifndef CCXX_THREAD_H_#include <cc++/thread.h>#endif#ifndef CCXX_EXCEPTION_H_#include <cc++/exception.h>#endif#ifndef WIN32typedef int HANDLE;#define INVALID_HANDLE_VALUE (-1)#endif#ifdef CCXX_NAMESPACESnamespace ost {#endif/** * The Serial class is used as the base for all serial I/O services * under APE. A serial is a system serial port that is used either * for line or packet based data input. Serial ports may also be * "streamable" in a derived form. * * Common C++ serial I/O classes are used to manage serial devices and * implement serial device protocols. From the point of view of Common C++, * serial devices are supported by the underlying Posix specified "termios" * call interface. * * The serial I/O base class is used to hold a descriptor to a serial device * and to provide an exception handling interface for all serial I/O classes. * The base class is also used to specify serial I/O properties such as * communication speed, flow control, data size, and parity. The "Serial" * base class is not itself directly used in application development, * however. * * Common C++ Serial I/O is itself divided into two conceptual modes; frame * oriented and line oriented I/O. Both frame and line oriented I/O makes * use of the ability of the underlying tty driver to buffer data and return * "ready" status from when select either a specified number of bytes or * newline record has been reached by manipulating termios c_cc fields * appropriately. This provides some advantage in that a given thread * servicing a serial port can block and wait rather than have to continually * poll or read each and every byte as soon as it appears at the serial port. * * @author David Sugar <dyfet@ostel.com> * @short base class for all serial I/O services. */class __EXPORT Serial{public: enum Error { errSuccess = 0, errOpenNoTty, errOpenFailed, errSpeedInvalid, errFlowInvalid, errParityInvalid, errCharsizeInvalid, errStopbitsInvalid, errOptionInvalid, errResourceFailure, errOutput, errInput, errTimeout, errExtended }; typedef enum Error Error; enum Flow { flowNone, flowSoft, flowHard, flowBoth }; typedef enum Flow Flow; enum Parity { parityNone, parityOdd, parityEven }; typedef enum Parity Parity; enum Pending { pendingInput, pendingOutput, pendingError }; typedef enum Pending Pending;private: Error errid; char *errstr; struct { bool thrown: 1; bool linebuf: 1; } flags; void * original; void * current; /** * Used to properly initialize serial object. */ void initSerial(void);protected: HANDLE dev; int bufsize; /** * Opens the serial device. * * @param fname Pathname of device to open */ void open(const char *fname); /** * Closes the serial device. * */ void close(void); /** * Reads from serial device. * * @param Data Point to character buffer to receive data. Buffers MUST * be at least Length + 1 bytes in size. * @param Length Number of bytes to read. */ virtual int aRead(char * Data, const int Length); /** * Writes to serial device. * * @param Data Point to character buffer containing data to write. Buffers MUST * @param Length Number of bytes to write. */ virtual int aWrite(const char * Data, const int Length); /** * This service is used to throw all serial errors which usually * occur during the serial constructor. * * @param error defined serial error id. * @param errstr string or message to optionally pass. */ Error error(Error error, char *errstr = NULL); /** * This service is used to thow application defined serial * errors where the application specific error code is a string. * * @param err string or message to pass. */ inline void error(char *err) {error(errExtended, err);}; /** * This method is used to turn the error handler on or off for * "throwing" execptions by manipulating the thrown flag. * * @param enable true to enable handler. */ inline void setError(bool enable) {flags.thrown = !enable;}; /** * Set packet read mode and "size" of packet read buffer. * This sets VMIN to x. VTIM is normally set to "0" so that * "isPending()" can wait for an entire packet rather than just * the first byte. * * @return actual buffer size set. * @param size of packet read request. * @param btimer optional inter-byte data packet timeout. */ int setPacketInput(int size, unsigned char btimer = 0); /** * Set "line buffering" read mode and specifies the newline * character to be used in seperating line records. isPending * can then be used to wait for an entire line of input. * * @param newline newline character. * @param nl1 EOL2 control character. * @return size of conical input buffer. */ int setLineInput(char newline = 13, char nl1 = 0); /** * Restore serial device to the original settings at time of open. */ void restore(void); /** * Used to flush the input waiting queue. */ void flushInput(void); /** * Used to flush any pending output data. */ void flushOutput(void); /** * Used to wait until all output has been sent. */ void waitOutput(void); /** * Used as the default destructor for ending serial I/O * services. It will restore the port to it's original state. */ void endSerial(void); /** * Used to initialize a newly opened serial file handle. You * should set serial properties and DTR manually before first * use. */ void initConfig(void); /** * This allows later ttystream class to open and close a serial * device. */ Serial() {initSerial();}; /** * A serial object may be constructed from a named file on the * file system. This named device must be "isatty()". * * @param name of file. */ Serial(const char *name);public: /** * The serial base class may be "thrown" as a result on an error, * and the "catcher" may then choose to destory the object. By * assuring the socket base class is a virtual destructor, we * can assure the full object is properly terminated. */ virtual ~Serial(); /** * Serial ports may also be duplecated by the assignment * operator. */ Serial &operator=(const Serial &from); /** * Set serial port speed for both input and output. * * @return 0 on success. * @param speed to select. 0 signifies modem "hang up". */ Error setSpeed(unsigned long speed); /** * Set character size. * * @return 0 on success. * @param bits character size to use (usually 7 or 8). */ Error setCharBits(int bits); /** * Set parity mode. * * @return 0 on success. * @param parity mode. */ Error setParity(Parity parity); /** * Set number of stop bits. * * @return 0 on success. * @param bits stop bits. */ Error setStopBits(int bits); /** * Set flow control. * * @return 0 on success. * @param flow control mode. */ Error setFlowControl(Flow flow); /** * Set the DTR mode off momentarily. * * @param millisec number of milliseconds. */ void toggleDTR(timeout_t millisec); /** * Send the "break" signal. */ void sendBreak(void); /** * Often used by a "catch" to fetch the last error of a thrown * serial. * * @return error numbr of last Error. */ inline Error getErrorNumber(void) {return errid;}; /** * Often used by a "catch" to fetch the user set error string * of a thrown serial. * * @return string for error message. */ inline char *getErrorString(void) {return errstr;}; /** * Get the "buffer" size for buffered operations. This can * be used when setting packet or line read modes to determine * how many bytes to wait for in a given read call. * * @return number of bytes used for buffering. */ inline int getBufferSize(void) {return bufsize;}; /** * Get the status of pending operations. This can be used to * examine if input or output is waiting, or if an error has * occured on the serial device. * * @return true if ready, false if timeout. * @param pend ready check to perform. * @param timeout in milliseconds. */ virtual bool isPending(Pending pend, timeout_t timeout = TIMEOUT_INF);};/** * TTY streams are used to represent serial connections that are fully * "streamable" objects using C++ stream classes and friends. * * The first application relevant serial I/O class is the TTYStream class. * TTYStream offers a linearly buffered "streaming" I/O session with the * serial device. Furthermore, traditional C++ "stream" operators (<< and * >>) may be used with the serial device. A more "true" to ANSI C++ library * format "ttystream" is also available, and this supports an "open" method * in which one can pass initial serial device parameters immediately * following the device name in a single string, as in * "/dev/tty3a:9600,7,e,1", as an example. * * The TTYSession aggragates a TTYStream and a Common C++ Thread which is * assumed to be the execution context that will be used to perform actual * I/O operations. This class is very anagolous to TCPSession. * * * @author David Sugar <dyfet@ostel.com> * @short streamable tty serial I/O class. */class __EXPORT TTYStream : protected std::streambuf, public Serial, public std::iostream{private: int doallocate(); friend TTYStream& crlf(TTYStream&); friend TTYStream& lfcr(TTYStream&);protected: char *gbuf, *pbuf; timeout_t timeout; /** * This constructor is used to derive "ttystream", a more * C++ style version of the TTYStream class. */ TTYStream();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -