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_p.h 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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_P_H_
  32. #define _QEXTSERIALPORT_P_H_
  33. //
  34. // W A R N I N G
  35. // -------------
  36. //
  37. // This file is not part of the QESP API. It exists for the convenience
  38. // of other QESP classes. This header file may change from version to
  39. // version without notice, or even be removed.
  40. //
  41. // We mean it.
  42. //
  43. #include "qextserialport.h"
  44. #include <QtCore/QReadWriteLock>
  45. #ifdef Q_OS_UNIX
  46. # include <termios.h>
  47. #elif (defined Q_OS_WIN)
  48. # include <QtCore/qt_windows.h>
  49. #endif
  50. #include <stdlib.h>
  51. // This is QextSerialPort's read buffer, needed by posix system.
  52. // ref: QRingBuffer & QIODevicePrivateLinearBuffer
  53. class QextReadBuffer
  54. {
  55. public:
  56. inline QextReadBuffer(size_t growth=4096)
  57. : len(0), first(0), buf(0), capacity(0), basicBlockSize(growth) {
  58. }
  59. ~QextReadBuffer() {
  60. delete [] buf;
  61. }
  62. inline void clear() {
  63. first = buf;
  64. len = 0;
  65. }
  66. inline int size() const {
  67. return len;
  68. }
  69. inline bool isEmpty() const {
  70. return len == 0;
  71. }
  72. inline int read(char* target, int size) {
  73. int r = qMin(size, len);
  74. if (r == 1) {
  75. *target = *first;
  76. --len;
  77. ++first;
  78. } else {
  79. memcpy(target, first, r);
  80. len -= r;
  81. first += r;
  82. }
  83. return r;
  84. }
  85. inline char* reserve(size_t size) {
  86. if ((first - buf) + len + size > capacity) {
  87. size_t newCapacity = qMax(capacity, basicBlockSize);
  88. while (newCapacity < size)
  89. newCapacity *= 2;
  90. if (newCapacity > capacity) {
  91. // allocate more space
  92. char* newBuf = new char[newCapacity];
  93. memmove(newBuf, first, len);
  94. delete [] buf;
  95. buf = newBuf;
  96. capacity = newCapacity;
  97. } else {
  98. // shift any existing data to make space
  99. memmove(buf, first, len);
  100. }
  101. first = buf;
  102. }
  103. char* writePtr = first + len;
  104. len += (int)size;
  105. return writePtr;
  106. }
  107. inline void chop(int size) {
  108. if (size >= len) {
  109. clear();
  110. } else {
  111. len -= size;
  112. }
  113. }
  114. inline void squeeze() {
  115. if (first != buf) {
  116. memmove(buf, first, len);
  117. first = buf;
  118. }
  119. size_t newCapacity = basicBlockSize;
  120. while (newCapacity < size_t(len))
  121. newCapacity *= 2;
  122. if (newCapacity < capacity) {
  123. char * tmp = static_cast<char*>(realloc(buf, newCapacity));
  124. if (tmp) {
  125. buf = tmp;
  126. capacity = newCapacity;
  127. }
  128. }
  129. }
  130. inline QByteArray readAll() {
  131. char* f = first;
  132. int l = len;
  133. clear();
  134. return QByteArray(f, l);
  135. }
  136. inline int readLine(char* target, int size) {
  137. int r = qMin(size, len);
  138. char* eol = static_cast<char*>(memchr(first, '\n', r));
  139. if (eol)
  140. r = 1+(eol-first);
  141. memcpy(target, first, r);
  142. len -= r;
  143. first += r;
  144. return int(r);
  145. }
  146. inline bool canReadLine() const {
  147. return memchr(first, '\n', len);
  148. }
  149. private:
  150. int len;
  151. char* first;
  152. char* buf;
  153. size_t capacity;
  154. size_t basicBlockSize;
  155. };
  156. class QextWinEventNotifier;
  157. class QWinEventNotifier;
  158. class QReadWriteLock;
  159. class QSocketNotifier;
  160. class QextSerialPortPrivate
  161. {
  162. Q_DECLARE_PUBLIC(QextSerialPort)
  163. public:
  164. QextSerialPortPrivate(QextSerialPort * q);
  165. ~QextSerialPortPrivate();
  166. enum DirtyFlagEnum
  167. {
  168. DFE_BaudRate = 0x0001,
  169. DFE_Parity = 0x0002,
  170. DFE_StopBits = 0x0004,
  171. DFE_DataBits = 0x0008,
  172. DFE_Flow = 0x0010,
  173. DFE_TimeOut = 0x0100,
  174. DFE_ALL = 0x0fff,
  175. DFE_Settings_Mask = 0x00ff //without TimeOut
  176. };
  177. mutable QReadWriteLock lock;
  178. QString port;
  179. PortSettings Settings;
  180. QextReadBuffer readBuffer;
  181. int settingsDirtyFlags;
  182. ulong lastErr;
  183. QextSerialPort::QueryMode _queryMode;
  184. // platform specific members
  185. #ifdef Q_OS_UNIX
  186. int fd;
  187. QSocketNotifier *readNotifier;
  188. struct termios Posix_CommConfig;
  189. struct termios old_termios;
  190. #elif (defined Q_OS_WIN)
  191. HANDLE Win_Handle;
  192. OVERLAPPED overlap;
  193. COMMCONFIG Win_CommConfig;
  194. COMMTIMEOUTS Win_CommTimeouts;
  195. # ifndef QESP_NO_QT4_PRIVATE
  196. QWinEventNotifier *winEventNotifier;
  197. # else
  198. QextWinEventNotifier *winEventNotifier;
  199. # endif
  200. DWORD eventMask;
  201. QList<OVERLAPPED*> pendingWrites;
  202. QReadWriteLock* bytesToWriteLock;
  203. qint64 _bytesToWrite;
  204. #endif
  205. /*fill PortSettings*/
  206. void setBaudRate(BaudRateType baudRate, bool update=true);
  207. void setDataBits(DataBitsType dataBits, bool update=true);
  208. void setParity(ParityType parity, bool update=true);
  209. void setStopBits(StopBitsType stopbits, bool update=true);
  210. void setFlowControl(FlowType flow, bool update=true);
  211. void setTimeout(long millisec, bool update=true);
  212. void setPortSettings(const PortSettings& settings, bool update=true);
  213. void platformSpecificDestruct();
  214. void platformSpecificInit();
  215. void translateError(ulong error);
  216. void updatePortSettings();
  217. qint64 readData_sys(char * data, qint64 maxSize);
  218. qint64 writeData_sys(const char * data, qint64 maxSize);
  219. void setDtr_sys(bool set=true);
  220. void setRts_sys(bool set=true);
  221. bool open_sys(QIODevice::OpenMode mode);
  222. bool close_sys();
  223. bool flush_sys();
  224. ulong lineStatus_sys();
  225. qint64 bytesAvailable_sys() const;
  226. #ifdef Q_OS_WIN
  227. void _q_onWinEvent(HANDLE h);
  228. #endif
  229. void _q_canRead();
  230. QextSerialPort * q_ptr;
  231. };
  232. #endif //_QEXTSERIALPORT_P_H_