Typica is a free program for professional coffee roasters. https://typica.us
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

qextserialport_p.h 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 < len + 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. inline void squeeze() {
  114. if (first != buf) {
  115. memmove(buf, first, len);
  116. first = buf;
  117. }
  118. size_t newCapacity = basicBlockSize;
  119. while (newCapacity < size_t(len))
  120. newCapacity *= 2;
  121. if (newCapacity < capacity) {
  122. char *tmp = static_cast<char *>(realloc(buf, newCapacity));
  123. if (tmp) {
  124. buf = tmp;
  125. capacity = newCapacity;
  126. }
  127. }
  128. }
  129. inline QByteArray readAll() {
  130. char *f = first;
  131. int l = len;
  132. clear();
  133. return QByteArray(f, l);
  134. }
  135. inline int readLine(char *target, int size) {
  136. int r = qMin(size, len);
  137. char *eol = static_cast<char *>(memchr(first, '\n', r));
  138. if (eol)
  139. r = 1+(eol-first);
  140. memcpy(target, first, r);
  141. len -= r;
  142. first += r;
  143. return int(r);
  144. }
  145. inline bool canReadLine() const {
  146. return memchr(first, '\n', len);
  147. }
  148. private:
  149. int len;
  150. char *first;
  151. char *buf;
  152. size_t capacity;
  153. size_t basicBlockSize;
  154. };
  155. class QWinEventNotifier;
  156. class QReadWriteLock;
  157. class QSocketNotifier;
  158. class QextSerialPortPrivate
  159. {
  160. Q_DECLARE_PUBLIC(QextSerialPort)
  161. public:
  162. QextSerialPortPrivate(QextSerialPort *q);
  163. ~QextSerialPortPrivate();
  164. enum DirtyFlagEnum
  165. {
  166. DFE_BaudRate = 0x0001,
  167. DFE_Parity = 0x0002,
  168. DFE_StopBits = 0x0004,
  169. DFE_DataBits = 0x0008,
  170. DFE_Flow = 0x0010,
  171. DFE_TimeOut = 0x0100,
  172. DFE_ALL = 0x0fff,
  173. DFE_Settings_Mask = 0x00ff //without TimeOut
  174. };
  175. mutable QReadWriteLock lock;
  176. QString port;
  177. PortSettings settings;
  178. QextReadBuffer readBuffer;
  179. int settingsDirtyFlags;
  180. ulong lastErr;
  181. QextSerialPort::QueryMode queryMode;
  182. // platform specific members
  183. #ifdef Q_OS_UNIX
  184. int fd;
  185. QSocketNotifier *readNotifier;
  186. struct termios currentTermios;
  187. struct termios oldTermios;
  188. #elif (defined Q_OS_WIN)
  189. HANDLE handle;
  190. OVERLAPPED overlap;
  191. COMMCONFIG commConfig;
  192. COMMTIMEOUTS commTimeouts;
  193. QWinEventNotifier *winEventNotifier;
  194. DWORD eventMask;
  195. QList<OVERLAPPED *> pendingWrites;
  196. QReadWriteLock *bytesToWriteLock;
  197. #endif
  198. /*fill PortSettings*/
  199. void setBaudRate(BaudRateType baudRate, bool update=true);
  200. void setDataBits(DataBitsType dataBits, bool update=true);
  201. void setParity(ParityType parity, bool update=true);
  202. void setStopBits(StopBitsType stopbits, bool update=true);
  203. void setFlowControl(FlowType flow, bool update=true);
  204. void setTimeout(long millisec, bool update=true);
  205. void setPortSettings(const PortSettings &settings, bool update=true);
  206. void platformSpecificDestruct();
  207. void platformSpecificInit();
  208. void translateError(ulong error);
  209. void updatePortSettings();
  210. qint64 readData_sys(char *data, qint64 maxSize);
  211. qint64 writeData_sys(const char *data, qint64 maxSize);
  212. void setDtr_sys(bool set=true);
  213. void setRts_sys(bool set=true);
  214. bool open_sys(QIODevice::OpenMode mode);
  215. bool close_sys();
  216. bool flush_sys();
  217. ulong lineStatus_sys();
  218. qint64 bytesAvailable_sys() const;
  219. #ifdef Q_OS_WIN
  220. void _q_onWinEvent(HANDLE h);
  221. #endif
  222. void _q_canRead();
  223. QextSerialPort *q_ptr;
  224. };
  225. #endif //_QEXTSERIALPORT_P_H_