qCNC 0.82-alpha
/home/trilog/Desktop/project/gui/Final/src/lib/qextserialport/qextserialport.h
Go to the documentation of this file.
00001 
00002 #ifndef _QEXTSERIALPORT_H_
00003 #define _QEXTSERIALPORT_H_
00004 
00005 #include "qextserialport_global.h"
00006 
00007 /*if all warning messages are turned off, flag portability warnings to be turned off as well*/
00008 #ifdef _TTY_NOWARN_
00009 #define _TTY_NOWARN_PORT_
00010 #endif
00011 
00012 /*macros for warning and debug messages*/
00013 #ifdef _TTY_NOWARN_PORT_
00014 #define TTY_PORTABILITY_WARNING(s)
00015 #else
00016 #define TTY_PORTABILITY_WARNING(s) qWarning(s)
00017 #endif /*_TTY_NOWARN_PORT_*/
00018 #ifdef _TTY_NOWARN_
00019 #define TTY_WARNING(s)
00020 #else
00021 #define TTY_WARNING(s) qWarning(s)
00022 #endif /*_TTY_NOWARN_*/
00023 
00024 
00025 /*line status constants*/
00026 #define LS_CTS  0x01
00027 #define LS_DSR  0x02
00028 #define LS_DCD  0x04
00029 #define LS_RI   0x08
00030 #define LS_RTS  0x10
00031 #define LS_DTR  0x20
00032 #define LS_ST   0x40
00033 #define LS_SR   0x80
00034 
00035 /*error constants*/
00036 #define E_NO_ERROR                   0
00037 #define E_INVALID_FD                 1
00038 #define E_NO_MEMORY                  2
00039 #define E_CAUGHT_NON_BLOCKED_SIGNAL  3
00040 #define E_PORT_TIMEOUT               4
00041 #define E_INVALID_DEVICE             5
00042 #define E_BREAK_CONDITION            6
00043 #define E_FRAMING_ERROR              7
00044 #define E_IO_ERROR                   8
00045 #define E_BUFFER_OVERRUN             9
00046 #define E_RECEIVE_OVERFLOW          10
00047 #define E_RECEIVE_PARITY_ERROR      11
00048 #define E_TRANSMIT_OVERFLOW         12
00049 #define E_READ_FAILED               13
00050 #define E_WRITE_FAILED              14
00051 #define E_FILE_NOT_FOUND            15
00052 
00053 enum BaudRateType
00054 {
00055     BAUD50,                //POSIX ONLY
00056     BAUD75,                //POSIX ONLY
00057     BAUD110,
00058     BAUD134,               //POSIX ONLY
00059     BAUD150,               //POSIX ONLY
00060     BAUD200,               //POSIX ONLY
00061     BAUD300,
00062     BAUD600,
00063     BAUD1200,
00064     BAUD1800,              //POSIX ONLY
00065     BAUD2400,
00066     BAUD4800,
00067     BAUD9600,
00068     BAUD14400,             //WINDOWS ONLY
00069     BAUD19200,
00070     BAUD38400,
00071     BAUD56000,             //WINDOWS ONLY
00072     BAUD57600,
00073     BAUD76800,             //POSIX ONLY
00074     BAUD115200,
00075     BAUD128000,            //WINDOWS ONLY
00076     BAUD256000             //WINDOWS ONLY
00077 };
00078 
00079 enum DataBitsType
00080 {
00081     DATA_5,
00082     DATA_6,
00083     DATA_7,
00084     DATA_8
00085 };
00086 
00087 enum ParityType
00088 {
00089     PAR_NONE,
00090     PAR_ODD,
00091     PAR_EVEN,
00092     PAR_MARK,               //WINDOWS ONLY
00093     PAR_SPACE
00094 };
00095 
00096 enum StopBitsType
00097 {
00098     STOP_1,
00099     STOP_1_5,               //WINDOWS ONLY
00100     STOP_2
00101 };
00102 
00103 enum FlowType
00104 {
00105     FLOW_OFF,
00106     FLOW_HARDWARE,
00107     FLOW_XONXOFF
00108 };
00109 
00113 struct PortSettings
00114 {
00115     BaudRateType BaudRate;
00116     DataBitsType DataBits;
00117     ParityType Parity;
00118     StopBitsType StopBits;
00119     FlowType FlowControl;
00120     long Timeout_Millisec;
00121 };
00122 
00123 #include <QIODevice>
00124 #include <QMutex>
00125 #ifdef Q_OS_UNIX
00126 #include <stdio.h>
00127 #include <termios.h>
00128 #include <errno.h>
00129 #include <unistd.h>
00130 #include <sys/time.h>
00131 #include <sys/ioctl.h>
00132 #include <sys/select.h>
00133 #include <QSocketNotifier>
00134 #elif (defined Q_OS_WIN)
00135 #include <windows.h>
00136 #include <QThread>
00137 #include <QReadWriteLock>
00138 #include <QtCore/private/qwineventnotifier_p.h>
00139 #endif
00140 
00182 class QEXTSERIALPORT_EXPORT QextSerialPort: public QIODevice
00183 {
00184     Q_OBJECT
00185     public:
00186         enum QueryMode {
00187             Polling,
00188             EventDriven
00189         };
00190 
00191         QextSerialPort(QueryMode mode = EventDriven);
00192         QextSerialPort(const QString & name, QueryMode mode = EventDriven);
00193         QextSerialPort(PortSettings const& s, QueryMode mode = EventDriven);
00194         QextSerialPort(const QString & name, PortSettings const& s, QueryMode mode = EventDriven);
00195         ~QextSerialPort();
00196 
00197         void setPortName(const QString & name);
00198         QString portName() const;
00199 
00204         inline QueryMode queryMode() const { return _queryMode; }
00205 
00228         void setQueryMode(QueryMode mode);
00229 
00230         void setBaudRate(BaudRateType);
00231         BaudRateType baudRate() const;
00232 
00233         void setDataBits(DataBitsType);
00234         DataBitsType dataBits() const;
00235 
00236         void setParity(ParityType);
00237         ParityType parity() const;
00238 
00239         void setStopBits(StopBitsType);
00240         StopBitsType stopBits() const;
00241 
00242         void setFlowControl(FlowType);
00243         FlowType flowControl() const;
00244 
00245         void setTimeout(long);
00246 
00247         bool open(OpenMode mode);
00248         bool isSequential() const;
00249         void close();
00250         void flush();
00251 
00252         qint64 size() const;
00253         qint64 bytesAvailable() const;
00254         QByteArray readAll();
00255 
00256         void ungetChar(char c);
00257 
00258         ulong lastError() const;
00259         void translateError(ulong error);
00260 
00261         void setDtr(bool set=true);
00262         void setRts(bool set=true);
00263         ulong lineStatus();
00264         QString errorString();
00265 
00266 #ifdef Q_OS_WIN
00267         virtual bool waitForReadyRead(int msecs);  
00268         virtual qint64 bytesToWrite() const;
00269         static QString fullPortNameWin(const QString & name);
00270 #endif
00271 
00272     protected:
00273         QMutex* mutex;
00274         QString port;
00275         PortSettings Settings;
00276         ulong lastErr;
00277         QueryMode _queryMode;
00278 
00279         // platform specific members
00280 #ifdef Q_OS_UNIX
00281         int fd;
00282         QSocketNotifier *readNotifier;
00283         struct termios Posix_CommConfig;
00284         struct termios old_termios;
00285         struct timeval Posix_Timeout;
00286         struct timeval Posix_Copy_Timeout;
00287 #elif (defined Q_OS_WIN)
00288         HANDLE Win_Handle;
00289         OVERLAPPED overlap;
00290         COMMCONFIG Win_CommConfig;
00291         COMMTIMEOUTS Win_CommTimeouts;
00292         QWinEventNotifier *winEventNotifier;
00293         DWORD eventMask;
00294         QList<OVERLAPPED*> pendingWrites;
00295         QReadWriteLock* bytesToWriteLock;
00296         qint64 _bytesToWrite;
00297 #endif
00298 
00299         void construct(); // common construction
00300         void platformSpecificDestruct();
00301         void platformSpecificInit();
00302         qint64 readData(char * data, qint64 maxSize);
00303         qint64 writeData(const char * data, qint64 maxSize);
00304 
00305 #ifdef Q_OS_WIN
00306     private slots:
00307         void onWinEvent(HANDLE h);
00308 #endif
00309 
00310     private:
00311         Q_DISABLE_COPY(QextSerialPort)
00312 
00313     signals:
00314 //        /**
00315 //         * This signal is emitted whenever port settings are updated.
00316 //         *    \param valid \p true if settings are valid, \p false otherwise.
00317 //         *
00318 //         *    @todo implement.
00319 //         */
00320 //        // void validSettings(bool valid);
00321 
00329         void dsrChanged(bool status);
00330 
00331 };
00332 
00333 #endif
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Defines