Typica is a free program for professional coffee roasters. https://typica.us
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

qextserialport.h 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /****************************************************************************
  2. ** Copyright (c) 2000-2003 Wayne Roth
  3. ** Copyright (c) 2004-2007 Stefan Sander
  4. ** Copyright (c) 2007 Michal Policht
  5. ** Copyright (c) 2008 Brandon Fosdick
  6. ** Copyright (c) 2009-2010 Liam Staskawicz
  7. ** Copyright (c) 2011 Debao Zhang
  8. ** All right reserved.
  9. ** Web: http://code.google.com/p/qextserialport/
  10. **
  11. ** Permission is hereby granted, free of charge, to any person obtaining
  12. ** a copy of this software and associated documentation files (the
  13. ** "Software"), to deal in the Software without restriction, including
  14. ** without limitation the rights to use, copy, modify, merge, publish,
  15. ** distribute, sublicense, and/or sell copies of the Software, and to
  16. ** permit persons to whom the Software is furnished to do so, subject to
  17. ** the following conditions:
  18. **
  19. ** The above copyright notice and this permission notice shall be
  20. ** included in all copies or substantial portions of the Software.
  21. **
  22. ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. ** NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. ** LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. ** OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. ** WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. **
  30. ****************************************************************************/
  31. #ifndef _QEXTSERIALPORT_H_
  32. #define _QEXTSERIALPORT_H_
  33. #include <QtCore/QIODevice>
  34. #include "qextserialport_global.h"
  35. #ifdef Q_OS_UNIX
  36. #include <termios.h>
  37. #endif
  38. /*line status constants*/
  39. // ### QESP2.0 move to enum
  40. #define LS_CTS 0x01
  41. #define LS_DSR 0x02
  42. #define LS_DCD 0x04
  43. #define LS_RI 0x08
  44. #define LS_RTS 0x10
  45. #define LS_DTR 0x20
  46. #define LS_ST 0x40
  47. #define LS_SR 0x80
  48. /*error constants*/
  49. // ### QESP2.0 move to enum
  50. #define E_NO_ERROR 0
  51. #define E_INVALID_FD 1
  52. #define E_NO_MEMORY 2
  53. #define E_CAUGHT_NON_BLOCKED_SIGNAL 3
  54. #define E_PORT_TIMEOUT 4
  55. #define E_INVALID_DEVICE 5
  56. #define E_BREAK_CONDITION 6
  57. #define E_FRAMING_ERROR 7
  58. #define E_IO_ERROR 8
  59. #define E_BUFFER_OVERRUN 9
  60. #define E_RECEIVE_OVERFLOW 10
  61. #define E_RECEIVE_PARITY_ERROR 11
  62. #define E_TRANSMIT_OVERFLOW 12
  63. #define E_READ_FAILED 13
  64. #define E_WRITE_FAILED 14
  65. #define E_FILE_NOT_FOUND 15
  66. #define E_PERMISSION_DENIED 16
  67. #define E_AGAIN 17
  68. enum BaudRateType
  69. {
  70. #if defined(Q_OS_UNIX) || defined(qdoc)
  71. BAUD50 = 50, //POSIX ONLY
  72. BAUD75 = 75, //POSIX ONLY
  73. BAUD134 = 134, //POSIX ONLY
  74. BAUD150 = 150, //POSIX ONLY
  75. BAUD200 = 200, //POSIX ONLY
  76. BAUD1800 = 1800, //POSIX ONLY
  77. # if defined(B76800) || defined(qdoc)
  78. BAUD76800 = 76800, //POSIX ONLY
  79. # endif
  80. # if (defined(B230400) && defined(B4000000)) || defined(qdoc)
  81. BAUD230400 = 230400, //POSIX ONLY
  82. BAUD460800 = 460800, //POSIX ONLY
  83. BAUD500000 = 500000, //POSIX ONLY
  84. BAUD576000 = 576000, //POSIX ONLY
  85. BAUD921600 = 921600, //POSIX ONLY
  86. BAUD1000000 = 1000000, //POSIX ONLY
  87. BAUD1152000 = 1152000, //POSIX ONLY
  88. BAUD1500000 = 1500000, //POSIX ONLY
  89. BAUD2000000 = 2000000, //POSIX ONLY
  90. BAUD2500000 = 2500000, //POSIX ONLY
  91. BAUD3000000 = 3000000, //POSIX ONLY
  92. BAUD3500000 = 3500000, //POSIX ONLY
  93. BAUD4000000 = 4000000, //POSIX ONLY
  94. # endif
  95. #endif //Q_OS_UNIX
  96. #if defined(Q_OS_WIN) || defined(qdoc)
  97. BAUD14400 = 14400, //WINDOWS ONLY
  98. BAUD56000 = 56000, //WINDOWS ONLY
  99. BAUD128000 = 128000, //WINDOWS ONLY
  100. BAUD256000 = 256000, //WINDOWS ONLY
  101. #endif //Q_OS_WIN
  102. BAUD110 = 110,
  103. BAUD300 = 300,
  104. BAUD600 = 600,
  105. BAUD1200 = 1200,
  106. BAUD2400 = 2400,
  107. BAUD4800 = 4800,
  108. BAUD9600 = 9600,
  109. BAUD19200 = 19200,
  110. BAUD38400 = 38400,
  111. BAUD57600 = 57600,
  112. BAUD115200 = 115200
  113. };
  114. enum DataBitsType
  115. {
  116. DATA_5 = 5,
  117. DATA_6 = 6,
  118. DATA_7 = 7,
  119. DATA_8 = 8
  120. };
  121. enum ParityType
  122. {
  123. PAR_NONE,
  124. PAR_ODD,
  125. PAR_EVEN,
  126. #if defined(Q_OS_WIN) || defined(qdoc)
  127. PAR_MARK, //WINDOWS ONLY
  128. #endif
  129. PAR_SPACE
  130. };
  131. enum StopBitsType
  132. {
  133. STOP_1,
  134. #if defined(Q_OS_WIN) || defined(qdoc)
  135. STOP_1_5, //WINDOWS ONLY
  136. #endif
  137. STOP_2
  138. };
  139. enum FlowType
  140. {
  141. FLOW_OFF,
  142. FLOW_HARDWARE,
  143. FLOW_XONXOFF
  144. };
  145. /**
  146. * structure to contain port settings
  147. */
  148. struct PortSettings
  149. {
  150. BaudRateType BaudRate;
  151. DataBitsType DataBits;
  152. ParityType Parity;
  153. StopBitsType StopBits;
  154. FlowType FlowControl;
  155. long Timeout_Millisec;
  156. };
  157. class QextSerialPortPrivate;
  158. class QEXTSERIALPORT_EXPORT QextSerialPort: public QIODevice
  159. {
  160. Q_OBJECT
  161. Q_DECLARE_PRIVATE(QextSerialPort)
  162. Q_ENUMS(QueryMode)
  163. Q_PROPERTY(QString portName READ portName WRITE setPortName)
  164. Q_PROPERTY(QueryMode queryMode READ queryMode WRITE setQueryMode)
  165. public:
  166. enum QueryMode {
  167. Polling,
  168. EventDriven
  169. };
  170. explicit QextSerialPort(QueryMode mode = EventDriven, QObject* parent = 0);
  171. explicit QextSerialPort(const QString & name, QueryMode mode = EventDriven, QObject * parent = 0);
  172. explicit QextSerialPort(const PortSettings & s, QueryMode mode = EventDriven, QObject * parent = 0);
  173. QextSerialPort(const QString & name, const PortSettings& s, QueryMode mode = EventDriven, QObject *parent=0);
  174. ~QextSerialPort();
  175. QString portName() const;
  176. QueryMode queryMode() const;
  177. BaudRateType baudRate() const;
  178. DataBitsType dataBits() const;
  179. ParityType parity() const;
  180. StopBitsType stopBits() const;
  181. FlowType flowControl() const;
  182. bool open(OpenMode mode);
  183. bool isSequential() const;
  184. void close();
  185. void flush();
  186. qint64 bytesAvailable() const;
  187. bool canReadLine() const;
  188. QByteArray readAll();
  189. ulong lastError() const;
  190. ulong lineStatus();
  191. QString errorString();
  192. public Q_SLOTS:
  193. void setPortName(const QString & name);
  194. void setQueryMode(QueryMode mode);
  195. void setBaudRate(BaudRateType);
  196. void setDataBits(DataBitsType);
  197. void setParity(ParityType);
  198. void setStopBits(StopBitsType);
  199. void setFlowControl(FlowType);
  200. void setTimeout(long);
  201. void setDtr(bool set=true);
  202. void setRts(bool set=true);
  203. Q_SIGNALS:
  204. void dsrChanged(bool status);
  205. protected:
  206. qint64 readData(char * data, qint64 maxSize);
  207. qint64 writeData(const char * data, qint64 maxSize);
  208. private:
  209. Q_DISABLE_COPY(QextSerialPort)
  210. #ifdef Q_OS_WIN
  211. Q_PRIVATE_SLOT(d_func(), void _q_onWinEvent(HANDLE))
  212. #endif
  213. Q_PRIVATE_SLOT(d_func(), void _q_canRead())
  214. QextSerialPortPrivate * const d_ptr;
  215. };
  216. #endif