qextserialport.h
Aller à la documentation de ce fichier.
00001 /**************************************************************************** 00002 ** Copyright (c) 2000-2003 Wayne Roth 00003 ** Copyright (c) 2004-2007 Stefan Sander 00004 ** Copyright (c) 2007 Michal Policht 00005 ** Copyright (c) 2008 Brandon Fosdick 00006 ** Copyright (c) 2009-2010 Liam Staskawicz 00007 ** Copyright (c) 2011 Debao Zhang 00008 ** All right reserved. 00009 ** Web: http://code.google.com/p/qextserialport/ 00010 ** 00011 ** Permission is hereby granted, free of charge, to any person obtaining 00012 ** a copy of this software and associated documentation files (the 00013 ** "Software"), to deal in the Software without restriction, including 00014 ** without limitation the rights to use, copy, modify, merge, publish, 00015 ** distribute, sublicense, and/or sell copies of the Software, and to 00016 ** permit persons to whom the Software is furnished to do so, subject to 00017 ** the following conditions: 00018 ** 00019 ** The above copyright notice and this permission notice shall be 00020 ** included in all copies or substantial portions of the Software. 00021 ** 00022 ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 00023 ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00024 ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00025 ** NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 00026 ** LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 00027 ** OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 00028 ** WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00029 ** 00030 ****************************************************************************/ 00031 00032 #ifndef _QEXTSERIALPORT_H_ 00033 #define _QEXTSERIALPORT_H_ 00034 00035 #include <QtCore/QIODevice> 00036 #include "qextserialport_global.h" 00037 #ifdef Q_OS_UNIX 00038 #include <termios.h> 00039 #endif 00040 /*line status constants*/ 00041 // ### QESP2.0 move to enum 00042 #define LS_CTS 0x01 00043 #define LS_DSR 0x02 00044 #define LS_DCD 0x04 00045 #define LS_RI 0x08 00046 #define LS_RTS 0x10 00047 #define LS_DTR 0x20 00048 #define LS_ST 0x40 00049 #define LS_SR 0x80 00050 00051 /*error constants*/ 00052 // ### QESP2.0 move to enum 00053 #define E_NO_ERROR 0 00054 #define E_INVALID_FD 1 00055 #define E_NO_MEMORY 2 00056 #define E_CAUGHT_NON_BLOCKED_SIGNAL 3 00057 #define E_PORT_TIMEOUT 4 00058 #define E_INVALID_DEVICE 5 00059 #define E_BREAK_CONDITION 6 00060 #define E_FRAMING_ERROR 7 00061 #define E_IO_ERROR 8 00062 #define E_BUFFER_OVERRUN 9 00063 #define E_RECEIVE_OVERFLOW 10 00064 #define E_RECEIVE_PARITY_ERROR 11 00065 #define E_TRANSMIT_OVERFLOW 12 00066 #define E_READ_FAILED 13 00067 #define E_WRITE_FAILED 14 00068 #define E_FILE_NOT_FOUND 15 00069 #define E_PERMISSION_DENIED 16 00070 #define E_AGAIN 17 00071 00076 enum BaudRateType 00077 { 00078 #if defined(Q_OS_UNIX) || defined(qdoc) 00079 BAUD50 = 50, //POSIX ONLY 00080 BAUD75 = 75, //POSIX ONLY 00081 BAUD134 = 134, //POSIX ONLY 00082 BAUD150 = 150, //POSIX ONLY 00083 BAUD200 = 200, //POSIX ONLY 00084 BAUD1800 = 1800, //POSIX ONLY 00085 # if defined(B76800) || defined(qdoc) 00086 BAUD76800 = 76800, //POSIX ONLY 00087 # endif 00088 # if (defined(B230400) && defined(B4000000)) || defined(qdoc) 00089 BAUD230400 = 230400, //POSIX ONLY 00090 BAUD460800 = 460800, //POSIX ONLY 00091 BAUD500000 = 500000, //POSIX ONLY 00092 BAUD576000 = 576000, //POSIX ONLY 00093 BAUD921600 = 921600, //POSIX ONLY 00094 BAUD1000000 = 1000000, //POSIX ONLY 00095 BAUD1152000 = 1152000, //POSIX ONLY 00096 BAUD1500000 = 1500000, //POSIX ONLY 00097 BAUD2000000 = 2000000, //POSIX ONLY 00098 BAUD2500000 = 2500000, //POSIX ONLY 00099 BAUD3000000 = 3000000, //POSIX ONLY 00100 BAUD3500000 = 3500000, //POSIX ONLY 00101 BAUD4000000 = 4000000, //POSIX ONLY 00102 # endif 00103 #endif //Q_OS_UNIX 00104 #if defined(Q_OS_WIN) || defined(qdoc) 00105 BAUD14400 = 14400, 00106 decoupageTrame(); //WINDOWS ONLY 00107 BAUD56000 = 56000, //WINDOWS ONLY 00108 BAUD128000 = 128000, //WINDOWS ONLY 00109 BAUD256000 = 256000, //WINDOWS ONLY 00110 #endif //Q_OS_WIN 00111 BAUD110 = 110, 00112 BAUD300 = 300, 00113 BAUD600 = 600, 00114 BAUD1200 = 1200, 00115 BAUD2400 = 2400, 00116 BAUD4800 = 4800, 00117 BAUD9600 = 9600, 00118 BAUD19200 = 19200, 00119 BAUD38400 = 38400, 00120 BAUD57600 = 57600, 00121 BAUD115200 = 115200 00122 }; 00123 00128 enum DataBitsType 00129 { 00130 DATA_5 = 5, 00131 DATA_6 = 6, 00132 DATA_7 = 7, 00133 DATA_8 = 8 00134 }; 00135 00140 enum ParityType 00141 { 00142 PAR_NONE, 00143 PAR_ODD, 00144 PAR_EVEN, 00145 #if defined(Q_OS_WIN) || defined(qdoc) 00146 PAR_MARK, //WINDOWS ONLY 00147 #endif 00148 PAR_SPACE 00149 }; 00150 00155 enum StopBitsType 00156 { 00157 STOP_1, 00158 #if defined(Q_OS_WIN) || defined(qdoc) 00159 STOP_1_5, //WINDOWS ONLY 00160 #endif 00161 STOP_2 00162 }; 00163 00168 enum FlowType 00169 { 00170 FLOW_OFF, 00171 FLOW_HARDWARE, 00172 FLOW_XONXOFF 00173 }; 00174 00182 struct PortSettings 00183 { 00184 BaudRateType BaudRate; 00185 DataBitsType DataBits; 00186 ParityType Parity; 00187 StopBitsType StopBits; 00188 FlowType FlowControl; 00189 long Timeout_Millisec; 00190 }; 00191 00192 class QextSerialPortPrivate; 00197 class QEXTSERIALPORT_EXPORT QextSerialPort: public QIODevice 00198 { 00199 Q_OBJECT 00200 Q_DECLARE_PRIVATE(QextSerialPort) 00201 Q_ENUMS(QueryMode) 00202 Q_PROPERTY(QString portName READ portName WRITE setPortName) 00203 Q_PROPERTY(QueryMode queryMode READ queryMode WRITE setQueryMode) 00204 public: 00209 enum QueryMode { 00210 Polling, 00211 EventDriven 00212 }; 00213 00220 explicit QextSerialPort(QueryMode mode = EventDriven, QObject *parent = 0); 00228 explicit QextSerialPort(const QString &name, QueryMode mode = EventDriven, QObject *parent = 0); 00236 explicit QextSerialPort(const PortSettings &s, QueryMode mode = EventDriven, QObject *parent = 0); 00245 QextSerialPort(const QString &name, const PortSettings &s, QueryMode mode = EventDriven, QObject *parent=0); 00246 00251 ~QextSerialPort(); 00252 00258 QString portName() const; 00264 QueryMode queryMode() const; 00270 BaudRateType baudRate() const; 00276 DataBitsType dataBits() const; 00282 ParityType parity() const; 00288 StopBitsType stopBits() const; 00294 FlowType flowControl() const; 00295 00302 bool open(OpenMode mode); 00308 bool isSequential() const; 00313 void close(); 00318 void flush(); 00324 qint64 bytesAvailable() const; 00330 bool canReadLine() const; 00336 QByteArray readAll(); 00337 00343 ulong lastError() const; 00344 00350 ulong lineStatus(); 00356 QString errorString(); 00357 00358 public Q_SLOTS: 00364 void setPortName(const QString &name); 00370 void setQueryMode(QueryMode mode); 00376 void setBaudRate(BaudRateType); 00382 void setDataBits(DataBitsType); 00388 void setParity(ParityType); 00394 void setStopBits(StopBitsType); 00400 void setFlowControl(FlowType); 00406 void setTimeout(long); 00407 00413 void setDtr(bool set=true); 00419 void setRts(bool set=true); 00420 00421 Q_SIGNALS: 00427 void dsrChanged(bool status); 00428 00429 protected: 00437 qint64 readData(char *data, qint64 maxSize); 00445 qint64 writeData(const char *data, qint64 maxSize); 00446 00447 private: 00448 Q_DISABLE_COPY(QextSerialPort) 00449 00450 #ifdef Q_OS_WIN 00451 Q_PRIVATE_SLOT(d_func(), void _q_onWinEvent(HANDLE)) 00452 #endif 00453 Q_PRIVATE_SLOT(d_func(), void _q_canRead()) 00454 00455 QextSerialPortPrivate *const d_ptr; 00456 }; 00457 00458 #endif