win_qextserialport.h 4.2 KB
Newer Older
pixhawk's avatar
pixhawk committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
#ifndef _WIN_QEXTSERIALPORT_H_
#define _WIN_QEXTSERIALPORT_H_

#include "qextserialbase.h"
#include <windows.h>
#include <QThread>


/*if all warning messages are turned off, flag portability warnings to be turned off as well*/
#ifdef _TTY_NOWARN_
#define _TTY_NOWARN_PORT_
#endif

class QReadWriteLock;
class Win_QextSerialThread;


/*!
\author Stefan Sander
\author Michal Policht

A cross-platform serial port class.
This class encapsulates the Windows portion of QextSerialPort.  The user will be notified of
errors and possible portability conflicts at run-time by default - this behavior can be turned
off by defining _TTY_NOWARN_ (to turn off all warnings) or _TTY_NOWARN_PORT_ (to turn off
portability warnings) in the project.  Note that defining _TTY_NOWARN_ also defines
_TTY_NOWARN_PORT_.

\note
On Windows NT/2000/XP this class uses Win32 serial port functions by default.  The user may
select POSIX behavior under NT, 2000, or XP ONLY by defining _TTY_POSIX_ in the project. I can
make no guarantees as to the quality of POSIX support under NT/2000 however.

\todo remove copy constructor and assign operator.
*/
class Win_QextSerialPort: public QextSerialBase 
{
	Q_OBJECT
	friend class Win_QextSerialThread;
	
	private:
		/*!
		 * This method is a part of constructor.
		 */
		void init();
		
	protected:
	    HANDLE Win_Handle;
		HANDLE threadStartEvent;
		HANDLE threadTerminateEvent;
		OVERLAPPED overlap;
	    OVERLAPPED overlapWrite;
		COMMCONFIG Win_CommConfig;
		COMMTIMEOUTS Win_CommTimeouts;
		QReadWriteLock * bytesToWriteLock;	///< @todo maybe move to QextSerialBase.
		qint64 _bytesToWrite;		///< @todo maybe move to QextSerialBase (and implement in POSIX).
		Win_QextSerialThread * overlapThread; ///< @todo maybe move to QextSerialBase (and implement in POSIX).
		 	
		void monitorCommEvent();
		void terminateCommWait();
	    virtual qint64 readData(char *data, qint64 maxSize);
	    virtual qint64 writeData(const char *data, qint64 maxSize);

	public:
	    Win_QextSerialPort(QextSerialBase::QueryMode mode);
	    Win_QextSerialPort(Win_QextSerialPort const& s);
	    Win_QextSerialPort(const QString & name, QextSerialBase::QueryMode mode);
	    Win_QextSerialPort(const PortSettings& settings, QextSerialBase::QueryMode mode);
	    Win_QextSerialPort(const QString & name, const PortSettings& settings, QextSerialBase::QueryMode mode);
	    Win_QextSerialPort& operator=(const Win_QextSerialPort& s);
	    virtual ~Win_QextSerialPort();
	    virtual bool open(OpenMode mode);
	    virtual void close();
	    virtual void flush();
	    virtual qint64 size() const;
	    virtual void ungetChar(char c);
	    virtual void setFlowControl(FlowType);
	    virtual void setParity(ParityType);
	    virtual void setDataBits(DataBitsType);
	    virtual void setStopBits(StopBitsType);
	    virtual void setBaudRate(BaudRateType);
	    virtual void setDtr(bool set=true);
	    virtual void setRts(bool set=true);
	    virtual ulong lineStatus(void);
	    virtual qint64 bytesAvailable() const;
	    virtual void translateError(ulong);
	    virtual void setTimeout(long);
	    
	    /*!
	     * Return number of bytes waiting in the buffer. Currently this shows number 
	     * of bytes queued within write() and before the TX_EMPTY event occured. TX_EMPTY
	     * event is created whenever last character in the system buffer was sent.
	     * 
	     * \return number of bytes queued within write(), before the first TX_EMPTY 
	     * event occur.
	     * 
	     * \warning this function may not give you expected results since TX_EMPTY may occur 
	     * while writing data to the buffer. Eventually some TX_EMPTY events may not be
	     * catched.
	     * 
	     * \note this function always returns 0 in polling mode.
	     * 
	     * \see flush().
	     */
		virtual qint64 bytesToWrite() const;
		
		virtual bool waitForReadyRead(int msecs);	///< @todo implement.
};

/*!
 * This thread monitors communication events.
 */
class Win_QextSerialThread: public QThread
{
	Win_QextSerialPort * qesp;
	bool terminate;

	public:
		/*!
		 * Constructor.
		 * 
		 * \param qesp valid serial port object.
		 */
		Win_QextSerialThread(Win_QextSerialPort * qesp);
		
		/*!
		 * Stop the thread.
		 */
		void stop();
	
	protected:
		//overriden
		virtual void run();
	
};

#endif