📄 serial.h
字号:
/** * Used to allocate the buffer space needed for iostream * operations. This is based on MAX_INPUT. */ void allocate(void); /** * Used to terminate the buffer space and clean up the tty * connection. This function is called by the destructor. */ void endStream(void); /** * This streambuf method is used to load the input buffer * through the established tty serial port. * * @return char from get buffer, EOF also possible. */ int underflow(void); /** * This streambuf method is used for doing unbuffered reads * through the establish tty serial port when in interactive mode. * Also this method will handle proper use of buffers if not in * interative mode. * * @return char from tty serial port, EOF also possible. */ int uflow(void); /** * This streambuf method is used to write the output * buffer through the established tty port. * * @param ch char to push through. * @return char pushed through. */ int overflow(int ch);public: /** * Create and open a tty serial port. * * @param filename char name of device to open. * @param to default timeout. */ TTYStream(const char *filename, timeout_t to = 0); /** * End the tty stream and cleanup. */ virtual ~TTYStream(); /** * Set the timeout control. * * @param to timeout to use. */ inline void setTimeout(timeout_t to) {timeout = to;}; /** * Set tty mode to buffered or "interactive". When interactive, * all streamed I/O is directly sent to the serial port * immediately. * * @param flag bool set to true to make interactive. */ void interactive(bool flag); /** * Flushes the stream input and out buffers, writes * pending output. * * @return 0 on success. */ int sync(void); /** * 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. If read buffer contains data * then input is ready and if write buffer contains data it is * first flushed then checked. * * @return true if ready, false if timeout. * @param pend ready check to perform. * @param timeout in milliseconds. */ bool isPending(Pending pend, timeout_t timeout = TIMEOUT_INF);}; /** * A more natural C++ "ttystream" class for use by non-threaded * applications. This class behaves a lot more like fstream and * similar classes. * * @author David Sugar <dyfet@ostel.com> * @short C++ "fstream" style ttystream class. */class __EXPORT ttystream : public TTYStream{public: /** * Construct an unopened "ttystream" object. */ ttystream(); /** * Construct and "open" a tty stream object. A filename in * the form "device:options[,options]" may be used to pass * device options as part of the open. * * @param name of file and serial options. */ ttystream(const char *name); /** * Open method for a tty stream. * * @param name filename to open. */ void open(const char *name); /** * Close method for a tty stream. */ void close(void); /** * Test to see if stream is opened. */ inline bool operator!() {return (dev < 0);};};/** * * 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 This class is very anagolous to TCPSession. */class __EXPORT TTYSession : public Thread, public TTYStream{public: /** * Create TTY stream that will be managed by it's own thread. * * @param name of tty device to open. * @param pri execution priority. * @param stack allocation needed on some platforms. */ TTYSession(const char *name, int pri = 0, int stack = 0); virtual ~TTYSession();};#ifndef WIN32// Not support this right now.......//class __EXPORT SerialPort;class __EXPORT SerialService;/** * The serial port is an internal class which is attached to and then * serviced by a specified SerialService thread. Derived versions of * this class offer specific functionality such as serial integration * protocols. * * The TTYPort and TTYService classes are used to form thread-pool serviced * serial I/O protocol sets. These can be used when one has a large number * of serial devices to manage, and a single (or limited number of) thread(s) * can then be used to service the tty port objects present. Each tty port * supports a timer control and several virtual methods that the service * thread can call when events occur. This model provides for "callback" * event management, whereby the service thread performs a "callback" into * the port object when events occur. Specific events supported include the * expiration of a TTYPort timer, pending input data waiting to be read, and * "sighup" connection breaks. * * * @author David Sugar <dyfet@ostel.com> * @short base class for thread pool serviced serial I/O. */class __EXPORT SerialPort: public Serial, public TimerPort{private: SerialPort *next, *prev; SerialService *service;#ifdef USE_POLL struct pollfd *ufd;#endif bool detect_pending; bool detect_output; bool detect_disconnect; friend class SerialService;protected: /** * Construct a tty serial port for a named serial device. * * @param svc pool thread object. * @param name of tty port. */ SerialPort(SerialService *svc, const char *name); /** * Disconnect the Serial Port from the service pool thread * and shutdown the port. */ virtual ~SerialPort(); /** * Used to indicate if the service thread should monitor pending * data for us. */ void setDetectPending( bool ); /** * Get the current state of the DetectPending flag. */ inline bool getDetectPending( void ) const { return detect_pending; } /** * Used to indicate if output ready monitoring should be performed * by the service thread. */ void setDetectOutput( bool ); /** * Get the current state of the DetectOutput flag. */ inline bool getDetectOutput( void ) const { return detect_output; } /** * Called by the service thread when the objects timer * has expired. */ virtual void expired(void); /** * Called by the service thread when input data is pending * for this tty port. Effected by setPacketInput and by * setLineInput. */ virtual void pending(void); /** * Called by the service thread when an exception has occured * such as a hangup. */ virtual void disconnect(void); /** * Transmit "send" data to the serial port. This is not public * since it's meant to support internal protocols rather than * direct public access to the device. * * @return number of bytes send. * @param buf address of buffer to send. * @param len of bytes to send. */ inline int output(void *buf, int len) {return aWrite((char *)buf, len);}; /** * Perform when output is available for sending data. */ virtual void output(void); /** * Receive "input" for pending data from the serial port. This * is not a public member since it's meant to support internal * protocols rather than direct external access to the device. * * @return number of bytes received. * @param buf address of buffer to input. * @param len of input buffer used. */ inline int input(void *buf, int len) {return aRead((char *)buf, len);};public: /** * Derived setTimer to notify the service thread pool of changes * in expected timeout. This allows SerialService to * reschedule all timers. * * @param timeout in milliseconds. */ void setTimer(timeout_t timeout = 0); /** * Derived incTimer to notify the service thread pool of a * change in expected timeout. This allows SerialService to * reschedule all timers. */ void incTimer(timeout_t timeout);};/** * The SerialService is a thead service object that is meant to service * attached serial ports. Multiple pool objects may be created and * multiple serial ports may be attached to the same thread of * of execution. This allows one to balance threads and the serial ports * they service. * * The TTYPort and TTYService classes are used to form thread-pool serviced * serial I/O protocol sets. These can be used when one has a large number * of serial devices to manage, and a single (or limited number of) thread(s) * can then be used to service the tty port objects present. Each tty port * supports a timer control and several virtual methods that the service * thread can call when events occur. This model provides for "callback" * event management, whereby the service thread performs a "callback" into * the port object when events occur. Specific events supported include the * expiration of a TTYPort timer, pending input data waiting to be read, and * "sighup" connection breaks. * * * @author David Sugar <dyfet@ostel.com> * @short Thread pool service for serial ports. */class __EXPORT SerialService : public Thread, private Mutex{private: fd_set connect; int iosync[2]; int hiwater; int count; SerialPort *first, *last; /** * Attach a new serial port to this service thread. * * @param port of SerialPort derived object to attach. */ void attach(SerialPort *port); /** * Detach a serial port from this service thread. * * @param port of SerialPort derived object to remove. */ void detach(SerialPort *port); /** * The service thread itself. */ void run(void); friend class SerialPort;protected: /** * A virtual handler for processing user defined update * requests (1-254) which have been posted through Update. * * @param flag of update request. */ virtual void onUpdate(unsigned char flag); /** * A virtual handler for event loop calls. This can be * used to extend event loop processing. */ virtual void onEvent(void); /** * A virtual handler for adding support for additional * callback events into SerialPort. * * @param port serial port currently being evaluated. */ virtual void onCallback(SerialPort *port);public: /** * Notify service thread that a port has been added or * removed, or a timer changed, so that a new schedule * can be computed for expiring attached ports. This * can also be used to pass requests to the OnUpdate() * event handler. * * @param flag event for OnUpdate, termination, or reschedule. */ void update(unsigned char flag = 0xff); /** * Create a service thread for attaching serial ports. The * thread begins execution with the first attached port. * * @param pri of this thread to run under. * @param stack stack size. * @param id stack ID. */ SerialService(int pri = 0, size_t stack = 0, const char *id = NULL); /** * Terminate the service thread and update attached objects. */ virtual ~SerialService(); /** * Get current reference count. This can be used when selecting * the lead used service handler from a pool. * * @return count of active ports. */ inline int getCount(void) {return count;};};#endif#ifdef COMMON_STD_EXCEPTIONclass __EXPORT SerException : public IOException{public: SerException(const String &str) : IOException(str) {};};#endif#ifdef CCXX_NAMESPACES}#endif#endif/** EMACS ** * Local variables: * mode: c++ * c-basic-offset: 8 * End: */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -