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.cpp 31KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  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. #include "qextserialport.h"
  32. #include "qextserialport_p.h"
  33. #include <stdio.h>
  34. #include <QtCore/QDebug>
  35. #include <QtCore/QReadLocker>
  36. #include <QtCore/QWriteLocker>
  37. /*!
  38. \class PortSettings
  39. \brief The PortSettings class contain port settings
  40. Structure to contain port settings.
  41. \code
  42. BaudRateType BaudRate;
  43. DataBitsType DataBits;
  44. ParityType Parity;
  45. StopBitsType StopBits;
  46. FlowType FlowControl;
  47. long Timeout_Millisec;
  48. \endcode
  49. */
  50. QextSerialPortPrivate::QextSerialPortPrivate(QextSerialPort *q)
  51. :lock(QReadWriteLock::Recursive), q_ptr(q)
  52. {
  53. lastErr = E_NO_ERROR;
  54. Settings.BaudRate = BAUD9600;
  55. Settings.Parity = PAR_NONE;
  56. Settings.FlowControl = FLOW_OFF;
  57. Settings.DataBits = DATA_8;
  58. Settings.StopBits = STOP_1;
  59. Settings.Timeout_Millisec = 10;
  60. settingsDirtyFlags = DFE_ALL;
  61. platformSpecificInit();
  62. }
  63. QextSerialPortPrivate::~QextSerialPortPrivate()
  64. {
  65. platformSpecificDestruct();
  66. }
  67. void QextSerialPortPrivate::setBaudRate(BaudRateType baudRate, bool update)
  68. {
  69. switch (baudRate) {
  70. #ifdef Q_OS_WIN
  71. //Windows Special
  72. case BAUD14400:
  73. case BAUD56000:
  74. case BAUD128000:
  75. case BAUD256000:
  76. QESP_PORTABILITY_WARNING()<<"QextSerialPort Portability Warning: POSIX does not support baudRate:"<<baudRate;
  77. #elif defined(Q_OS_UNIX)
  78. //Unix Special
  79. case BAUD50:
  80. case BAUD75:
  81. case BAUD134:
  82. case BAUD150:
  83. case BAUD200:
  84. case BAUD1800:
  85. #ifdef B76800
  86. case BAUD76800:
  87. #endif
  88. #if defined(B230400) && defined(B4000000)
  89. case BAUD230400:
  90. case BAUD460800:
  91. case BAUD500000:
  92. case BAUD576000:
  93. case BAUD921600:
  94. case BAUD1000000:
  95. case BAUD1152000:
  96. case BAUD1500000:
  97. case BAUD2000000:
  98. case BAUD2500000:
  99. case BAUD3000000:
  100. case BAUD3500000:
  101. case BAUD4000000:
  102. #endif
  103. QESP_PORTABILITY_WARNING()<<"QextSerialPort Portability Warning: Windows does not support baudRate:"<<baudRate;
  104. #endif
  105. case BAUD110:
  106. case BAUD300:
  107. case BAUD600:
  108. case BAUD1200:
  109. case BAUD2400:
  110. case BAUD4800:
  111. case BAUD9600:
  112. case BAUD19200:
  113. case BAUD38400:
  114. case BAUD57600:
  115. case BAUD115200:
  116. Settings.BaudRate=baudRate;
  117. settingsDirtyFlags |= DFE_BaudRate;
  118. if (update && q_func()->isOpen())
  119. updatePortSettings();
  120. break;
  121. default:
  122. QESP_WARNING()<<"QextSerialPort does not support baudRate:"<<baudRate;
  123. }
  124. }
  125. void QextSerialPortPrivate::setParity(ParityType parity, bool update)
  126. {
  127. switch (parity) {
  128. case PAR_SPACE:
  129. if (Settings.DataBits==DATA_8) {
  130. #ifdef Q_OS_WIN
  131. QESP_PORTABILITY_WARNING("QextSerialPort Portability Warning: Space parity with 8 data bits is not supported by POSIX systems.");
  132. #else
  133. QESP_WARNING("Space parity with 8 data bits is not supported by POSIX systems.");
  134. #endif
  135. }
  136. break;
  137. #ifdef Q_OS_WIN
  138. /*mark parity - WINDOWS ONLY*/
  139. case PAR_MARK:
  140. QESP_PORTABILITY_WARNING("QextSerialPort Portability Warning: Mark parity is not supported by POSIX systems");
  141. break;
  142. #endif
  143. case PAR_NONE:
  144. case PAR_EVEN:
  145. case PAR_ODD:
  146. break;
  147. default:
  148. QESP_WARNING()<<"QextSerialPort does not support Parity:" << parity;
  149. }
  150. Settings.Parity=parity;
  151. settingsDirtyFlags |= DFE_Parity;
  152. if (update && q_func()->isOpen())
  153. updatePortSettings();
  154. }
  155. void QextSerialPortPrivate::setDataBits(DataBitsType dataBits, bool update)
  156. {
  157. switch(dataBits) {
  158. case DATA_5:
  159. if (Settings.StopBits==STOP_2) {
  160. QESP_WARNING("QextSerialPort: 5 Data bits cannot be used with 2 stop bits.");
  161. }
  162. else {
  163. Settings.DataBits=dataBits;
  164. settingsDirtyFlags |= DFE_DataBits;
  165. }
  166. break;
  167. case DATA_6:
  168. #ifdef Q_OS_WIN
  169. if (Settings.StopBits==STOP_1_5) {
  170. QESP_WARNING("QextSerialPort: 6 Data bits cannot be used with 1.5 stop bits.");
  171. }
  172. else
  173. #endif
  174. {
  175. Settings.DataBits=dataBits;
  176. settingsDirtyFlags |= DFE_DataBits;
  177. }
  178. break;
  179. case DATA_7:
  180. #ifdef Q_OS_WIN
  181. if (Settings.StopBits==STOP_1_5) {
  182. QESP_WARNING("QextSerialPort: 7 Data bits cannot be used with 1.5 stop bits.");
  183. }
  184. else
  185. #endif
  186. {
  187. Settings.DataBits=dataBits;
  188. settingsDirtyFlags |= DFE_DataBits;
  189. }
  190. break;
  191. case DATA_8:
  192. #ifdef Q_OS_WIN
  193. if (Settings.StopBits==STOP_1_5) {
  194. QESP_WARNING("QextSerialPort: 8 Data bits cannot be used with 1.5 stop bits.");
  195. }
  196. else
  197. #endif
  198. {
  199. Settings.DataBits=dataBits;
  200. settingsDirtyFlags |= DFE_DataBits;
  201. }
  202. break;
  203. default:
  204. QESP_WARNING()<<"QextSerialPort does not support Data bits:"<<dataBits;
  205. }
  206. if (update && q_func()->isOpen())
  207. updatePortSettings();
  208. }
  209. void QextSerialPortPrivate::setStopBits(StopBitsType stopBits, bool update)
  210. {
  211. switch (stopBits) {
  212. /*one stop bit*/
  213. case STOP_1:
  214. Settings.StopBits = stopBits;
  215. settingsDirtyFlags |= DFE_StopBits;
  216. break;
  217. #ifdef Q_OS_WIN
  218. /*1.5 stop bits*/
  219. case STOP_1_5:
  220. QESP_PORTABILITY_WARNING("QextSerialPort Portability Warning: 1.5 stop bit operation is not supported by POSIX.");
  221. if (Settings.DataBits!=DATA_5) {
  222. QESP_WARNING("QextSerialPort: 1.5 stop bits can only be used with 5 data bits");
  223. }
  224. else {
  225. Settings.StopBits = stopBits;
  226. settingsDirtyFlags |= DFE_StopBits;
  227. }
  228. break;
  229. #endif
  230. /*two stop bits*/
  231. case STOP_2:
  232. if (Settings.DataBits==DATA_5) {
  233. QESP_WARNING("QextSerialPort: 2 stop bits cannot be used with 5 data bits");
  234. }
  235. else {
  236. Settings.StopBits = stopBits;
  237. settingsDirtyFlags |= DFE_StopBits;
  238. }
  239. break;
  240. default:
  241. QESP_WARNING()<<"QextSerialPort does not support stop bits: "<<stopBits;
  242. }
  243. if (update && q_func()->isOpen())
  244. updatePortSettings();
  245. }
  246. void QextSerialPortPrivate::setFlowControl(FlowType flow, bool update)
  247. {
  248. Settings.FlowControl=flow;
  249. settingsDirtyFlags |= DFE_Flow;
  250. if (update && q_func()->isOpen())
  251. updatePortSettings();
  252. }
  253. void QextSerialPortPrivate::setTimeout(long millisec, bool update)
  254. {
  255. Settings.Timeout_Millisec = millisec;
  256. settingsDirtyFlags |= DFE_TimeOut;
  257. if (update && q_func()->isOpen())
  258. updatePortSettings();
  259. }
  260. void QextSerialPortPrivate::setPortSettings(const PortSettings &settings, bool update)
  261. {
  262. setBaudRate(settings.BaudRate, false);
  263. setDataBits(settings.DataBits, false);
  264. setStopBits(settings.StopBits, false);
  265. setParity(settings.Parity, false);
  266. setFlowControl(settings.FlowControl, false);
  267. setTimeout(settings.Timeout_Millisec, false);
  268. settingsDirtyFlags = DFE_ALL;
  269. if (update && q_func()->isOpen())
  270. updatePortSettings();
  271. }
  272. void QextSerialPortPrivate::_q_canRead()
  273. {
  274. qint64 maxSize = bytesAvailable_sys();
  275. if (maxSize > 0) {
  276. char * writePtr = readBuffer.reserve(size_t(maxSize));
  277. qint64 bytesRead = readData_sys(writePtr, maxSize);
  278. if (bytesRead < maxSize)
  279. readBuffer.chop(maxSize - bytesRead);
  280. Q_Q(QextSerialPort);
  281. Q_EMIT q->readyRead();
  282. }
  283. }
  284. /*! \class QextSerialPort
  285. \brief The QextSerialPort class encapsulates a serial port on both POSIX and Windows systems.
  286. \section1 Usage
  287. QextSerialPort offers both a polling and event driven API. Event driven
  288. is typically easier to use, since you never have to worry about checking
  289. for new data.
  290. \bold Example
  291. \code
  292. QextSerialPort* port = new QextSerialPort("COM1");
  293. connect(port, SIGNAL(readyRead()), myClass, SLOT(onDataAvailable()));
  294. port->open();
  295. void MyClass::onDataAvailable()
  296. {
  297. QByteArray data = port->readAll();
  298. processNewData(usbdata);
  299. }
  300. \endcode
  301. \section1 Compatibility
  302. The user will be notified of errors and possible portability conflicts at run-time
  303. by default.
  304. For example, if a application has used BAUD1800, when it is runing under unix, you
  305. will get following message.
  306. \code
  307. QextSerialPort Portability Warning: Windows does not support baudRate:1800
  308. \endcode
  309. This behavior can be turned off by defining macro QESP_NO_WARN (to turn off all warnings)
  310. or QESP_NO_PORTABILITY_WARN (to turn off portability warnings) in the project.
  311. \bold Author: Stefan Sander, Michal Policht, Brandon Fosdick, Liam Staskawicz, Debao Zhang
  312. */
  313. /*!
  314. \enum QextSerialPort::QueryMode
  315. This enum type specifies query mode used in a serial port:
  316. \value Polling
  317. asynchronously read and write
  318. \value EventDriven
  319. synchronously read and write
  320. */
  321. /*!
  322. \fn void QextSerialPort::dsrChanged(bool status)
  323. This signal is emitted whenever dsr line has changed its state. You may
  324. use this signal to check if device is connected.
  325. \a status true when DSR signal is on, false otherwise.
  326. */
  327. /*!
  328. \fn QueryMode QextSerialPort::queryMode() const
  329. Get query mode.
  330. */
  331. /*!
  332. Default constructor. Note that the name of the device used by a QextSerialPort is dependent on
  333. your OS. Possible naming conventions and their associated OS are:
  334. \code
  335. OS Constant Used By Naming Convention
  336. ------------- ------------- ------------------------
  337. Q_OS_WIN Windows COM1, COM2
  338. Q_OS_IRIX SGI/IRIX /dev/ttyf1, /dev/ttyf2
  339. Q_OS_HPUX HP-UX /dev/tty1p0, /dev/tty2p0
  340. Q_OS_SOLARIS SunOS/Slaris /dev/ttya, /dev/ttyb
  341. Q_OS_OSF Digital UNIX /dev/tty01, /dev/tty02
  342. Q_OS_FREEBSD FreeBSD /dev/ttyd0, /dev/ttyd1
  343. Q_OS_OPENBSD OpenBSD /dev/tty00, /dev/tty01
  344. Q_OS_LINUX Linux /dev/ttyS0, /dev/ttyS1
  345. <none> /dev/ttyS0, /dev/ttyS1
  346. \endcode
  347. This constructor assigns the device name to the name of the first port on the specified system.
  348. See the other constructors if you need to open a different port. Default \a mode is EventDriven.
  349. As a subclass of QObject, \a parent can be specified.
  350. */
  351. QextSerialPort::QextSerialPort(QextSerialPort::QueryMode mode, QObject *parent)
  352. : QIODevice(parent), d_ptr(new QextSerialPortPrivate(this))
  353. {
  354. #ifdef Q_OS_WIN
  355. setPortName(QLatin1String("COM1"));
  356. #elif defined(Q_OS_IRIX)
  357. setPortName(QLatin1String("/dev/ttyf1"));
  358. #elif defined(Q_OS_HPUX)
  359. setPortName(QLatin1String("/dev/tty1p0"));
  360. #elif defined(Q_OS_SOLARIS)
  361. setPortName(QLatin1String("/dev/ttya"));
  362. #elif defined(Q_OS_OSF) //formally DIGITAL UNIX
  363. setPortName(QLatin1String("/dev/tty01"));
  364. #elif defined(Q_OS_FREEBSD)
  365. setPortName(QLatin1String("/dev/ttyd1"));
  366. #elif defined(Q_OS_OPENBSD)
  367. setPortName(QLatin1String("/dev/tty00"));
  368. #else
  369. setPortName(QLatin1String("/dev/ttyS0"));
  370. #endif
  371. setQueryMode(mode);
  372. }
  373. /*!
  374. Constructs a serial port attached to the port specified by name.
  375. \a name is the name of the device, which is windowsystem-specific,
  376. e.g."COM1" or "/dev/ttyS0". \a mode
  377. */
  378. QextSerialPort::QextSerialPort(const QString & name, QextSerialPort::QueryMode mode, QObject *parent)
  379. : QIODevice(parent), d_ptr(new QextSerialPortPrivate(this))
  380. {
  381. setQueryMode(mode);
  382. setPortName(name);
  383. }
  384. /*!
  385. Constructs a port with default name and specified \a settings.
  386. */
  387. QextSerialPort::QextSerialPort(const PortSettings& settings, QextSerialPort::QueryMode mode, QObject *parent)
  388. : QIODevice(parent), d_ptr(new QextSerialPortPrivate(this))
  389. {
  390. Q_D(QextSerialPort);
  391. setQueryMode(mode);
  392. d->setPortSettings(settings);
  393. }
  394. /*!
  395. Constructs a port with specified \a name , \a mode and \a settings.
  396. */
  397. QextSerialPort::QextSerialPort(const QString & name, const PortSettings& settings, QextSerialPort::QueryMode mode, QObject *parent)
  398. : QIODevice(parent), d_ptr(new QextSerialPortPrivate(this))
  399. {
  400. Q_D(QextSerialPort);
  401. setPortName(name);
  402. setQueryMode(mode);
  403. d->setPortSettings(settings);
  404. }
  405. /*!
  406. Opens a serial port and sets its OpenMode to \a mode.
  407. Note that this function does not specify which device to open.
  408. Returns true if successful; otherwise returns false.This function has no effect
  409. if the port associated with the class is already open. The port is also
  410. configured to the current settings, as stored in the Settings structure.
  411. */
  412. bool QextSerialPort::open(OpenMode mode)
  413. {
  414. Q_D(QextSerialPort);
  415. QWriteLocker locker(&d->lock);
  416. if (mode != QIODevice::NotOpen && !isOpen())
  417. d->open_sys(mode);
  418. return isOpen();
  419. }
  420. /*! \reimp
  421. Closes a serial port. This function has no effect if the serial port associated with the class
  422. is not currently open.
  423. */
  424. void QextSerialPort::close()
  425. {
  426. Q_D(QextSerialPort);
  427. QWriteLocker locker(&d->lock);
  428. if (isOpen()) {
  429. // Be a good QIODevice and call QIODevice::close() before really close()
  430. // so the aboutToClose() signal is emitted at the proper time
  431. QIODevice::close(); // mark ourselves as closed
  432. d->close_sys();
  433. d->readBuffer.clear();
  434. }
  435. }
  436. /*!
  437. Flushes all pending I/O to the serial port. This function has no effect if the serial port
  438. associated with the class is not currently open.
  439. */
  440. void QextSerialPort::flush()
  441. {
  442. Q_D(QextSerialPort);
  443. QWriteLocker locker(&d->lock);
  444. if (isOpen())
  445. d->flush_sys();
  446. }
  447. /*! \reimp
  448. Returns the number of bytes waiting in the port's receive queue. This function will return 0 if
  449. the port is not currently open, or -1 on error.
  450. */
  451. qint64 QextSerialPort::bytesAvailable() const
  452. {
  453. QWriteLocker locker(&d_func()->lock);
  454. if (isOpen()) {
  455. qint64 bytes = d_func()->bytesAvailable_sys();
  456. if (bytes != -1) {
  457. return bytes + d_func()->readBuffer.size()
  458. + QIODevice::bytesAvailable();
  459. } else {
  460. return -1;
  461. }
  462. }
  463. return 0;
  464. }
  465. /*! \reimp
  466. */
  467. bool QextSerialPort::canReadLine() const
  468. {
  469. QReadLocker locker(&d_func()->lock);
  470. return QIODevice::canReadLine() || d_func()->readBuffer.canReadLine();
  471. }
  472. /*!
  473. * Set desired serial communication handling style. You may choose from polling
  474. * or event driven approach. This function does nothing when port is open; to
  475. * apply changes port must be reopened.
  476. *
  477. * In event driven approach read() and write() functions are acting
  478. * asynchronously. They return immediately and the operation is performed in
  479. * the background, so they doesn't freeze the calling thread.
  480. * To determine when operation is finished, QextSerialPort runs separate thread
  481. * and monitors serial port events. Whenever the event occurs, adequate signal
  482. * is emitted.
  483. *
  484. * When polling is set, read() and write() are acting synchronously. Signals are
  485. * not working in this mode and some functions may not be available. The advantage
  486. * of polling is that it generates less overhead due to lack of signals emissions
  487. * and it doesn't start separate thread to monitor events.
  488. *
  489. * Generally event driven approach is more capable and friendly, although some
  490. * applications may need as low overhead as possible and then polling comes.
  491. *
  492. * \a mode query mode.
  493. */
  494. void QextSerialPort::setQueryMode(QueryMode mode)
  495. {
  496. Q_D(QextSerialPort);
  497. QWriteLocker locker(&d->lock);
  498. if (mode != d->_queryMode) {
  499. d->_queryMode = mode;
  500. }
  501. }
  502. /*!
  503. Sets the \a name of the device associated with the object, e.g. "COM1", or "/dev/ttyS0".
  504. */
  505. void QextSerialPort::setPortName(const QString & name)
  506. {
  507. Q_D(QextSerialPort);
  508. QWriteLocker locker(&d->lock);
  509. d->port = name;
  510. }
  511. /*!
  512. Returns the name set by setPortName().
  513. */
  514. QString QextSerialPort::portName() const
  515. {
  516. QReadLocker locker(&d_func()->lock);
  517. return d_func()->port;
  518. }
  519. QextSerialPort::QueryMode QextSerialPort::queryMode() const
  520. {
  521. QReadLocker locker(&d_func()->lock);
  522. return d_func()->_queryMode;
  523. }
  524. /*!
  525. Reads all available data from the device, and returns it as a QByteArray.
  526. This function has no way of reporting errors; returning an empty QByteArray()
  527. can mean either that no data was currently available for reading, or that an error occurred.
  528. */
  529. QByteArray QextSerialPort::readAll()
  530. {
  531. int avail = this->bytesAvailable();
  532. return (avail > 0) ? this->read(avail) : QByteArray();
  533. }
  534. /*!
  535. Returns the baud rate of the serial port. For a list of possible return values see
  536. the definition of the enum BaudRateType.
  537. */
  538. BaudRateType QextSerialPort::baudRate() const
  539. {
  540. QReadLocker locker(&d_func()->lock);
  541. return d_func()->Settings.BaudRate;
  542. }
  543. /*!
  544. Returns the number of data bits used by the port. For a list of possible values returned by
  545. this function, see the definition of the enum DataBitsType.
  546. */
  547. DataBitsType QextSerialPort::dataBits() const
  548. {
  549. QReadLocker locker(&d_func()->lock);
  550. return d_func()->Settings.DataBits;
  551. }
  552. /*!
  553. Returns the type of parity used by the port. For a list of possible values returned by
  554. this function, see the definition of the enum ParityType.
  555. */
  556. ParityType QextSerialPort::parity() const
  557. {
  558. QReadLocker locker(&d_func()->lock);
  559. return d_func()->Settings.Parity;
  560. }
  561. /*!
  562. Returns the number of stop bits used by the port. For a list of possible return values, see
  563. the definition of the enum StopBitsType.
  564. */
  565. StopBitsType QextSerialPort::stopBits() const
  566. {
  567. QReadLocker locker(&d_func()->lock);
  568. return d_func()->Settings.StopBits;
  569. }
  570. /*!
  571. Returns the type of flow control used by the port. For a list of possible values returned
  572. by this function, see the definition of the enum FlowType.
  573. */
  574. FlowType QextSerialPort::flowControl() const
  575. {
  576. QReadLocker locker(&d_func()->lock);
  577. return d_func()->Settings.FlowControl;
  578. }
  579. /*!
  580. \reimp
  581. Returns true if device is sequential, otherwise returns false. Serial port is sequential device
  582. so this function always returns true. Check QIODevice::isSequential() documentation for more
  583. information.
  584. */
  585. bool QextSerialPort::isSequential() const
  586. {
  587. return true;
  588. }
  589. /*!
  590. Return the error number, or 0 if no error occurred.
  591. */
  592. ulong QextSerialPort::lastError() const
  593. {
  594. QReadLocker locker(&d_func()->lock);
  595. return d_func()->lastErr;
  596. }
  597. /*!
  598. Returns the line status as stored by the port function. This function will retrieve the states
  599. of the following lines: DCD, CTS, DSR, and RI. On POSIX systems, the following additional lines
  600. can be monitored: DTR, RTS, Secondary TXD, and Secondary RXD. The value returned is an unsigned
  601. long with specific bits indicating which lines are high. The following constants should be used
  602. to examine the states of individual lines:
  603. \code
  604. Mask Line
  605. ------ ----
  606. LS_CTS CTS
  607. LS_DSR DSR
  608. LS_DCD DCD
  609. LS_RI RI
  610. LS_RTS RTS (POSIX only)
  611. LS_DTR DTR (POSIX only)
  612. LS_ST Secondary TXD (POSIX only)
  613. LS_SR Secondary RXD (POSIX only)
  614. \endcode
  615. This function will return 0 if the port associated with the class is not currently open.
  616. */
  617. unsigned long QextSerialPort::lineStatus()
  618. {
  619. Q_D(QextSerialPort);
  620. QWriteLocker locker(&d->lock);
  621. if (isOpen())
  622. return d->lineStatus_sys();
  623. return 0;
  624. }
  625. /*!
  626. Returns a human-readable description of the last device error that occurred.
  627. */
  628. QString QextSerialPort::errorString()
  629. {
  630. Q_D(QextSerialPort);
  631. QReadLocker locker(&d->lock);
  632. switch(d->lastErr) {
  633. case E_NO_ERROR:
  634. return tr("No Error has occurred");
  635. case E_INVALID_FD:
  636. return tr("Invalid file descriptor (port was not opened correctly)");
  637. case E_NO_MEMORY:
  638. return tr("Unable to allocate memory tables (POSIX)");
  639. case E_CAUGHT_NON_BLOCKED_SIGNAL:
  640. return tr("Caught a non-blocked signal (POSIX)");
  641. case E_PORT_TIMEOUT:
  642. return tr("Operation timed out (POSIX)");
  643. case E_INVALID_DEVICE:
  644. return tr("The file opened by the port is not a valid device");
  645. case E_BREAK_CONDITION:
  646. return tr("The port detected a break condition");
  647. case E_FRAMING_ERROR:
  648. return tr("The port detected a framing error (usually caused by incorrect baud rate settings)");
  649. case E_IO_ERROR:
  650. return tr("There was an I/O error while communicating with the port");
  651. case E_BUFFER_OVERRUN:
  652. return tr("Character buffer overrun");
  653. case E_RECEIVE_OVERFLOW:
  654. return tr("Receive buffer overflow");
  655. case E_RECEIVE_PARITY_ERROR:
  656. return tr("The port detected a parity error in the received data");
  657. case E_TRANSMIT_OVERFLOW:
  658. return tr("Transmit buffer overflow");
  659. case E_READ_FAILED:
  660. return tr("General read operation failure");
  661. case E_WRITE_FAILED:
  662. return tr("General write operation failure");
  663. case E_FILE_NOT_FOUND:
  664. return tr("The %1 file doesn't exists").arg(this->portName());
  665. case E_PERMISSION_DENIED:
  666. return tr("Permission denied");
  667. case E_AGAIN:
  668. return tr("Device is already locked");
  669. default:
  670. return tr("Unknown error: %1").arg(d->lastErr);
  671. }
  672. }
  673. /*!
  674. Destructs the QextSerialPort object.
  675. */
  676. QextSerialPort::~QextSerialPort()
  677. {
  678. if (isOpen()) {
  679. close();
  680. }
  681. delete d_ptr;
  682. }
  683. /*!
  684. Sets the flow control used by the port to \a flow. Possible values of flow are:
  685. \code
  686. FLOW_OFF No flow control
  687. FLOW_HARDWARE Hardware (RTS/CTS) flow control
  688. FLOW_XONXOFF Software (XON/XOFF) flow control
  689. \endcode
  690. */
  691. void QextSerialPort::setFlowControl(FlowType flow)
  692. {
  693. Q_D(QextSerialPort);
  694. QWriteLocker locker(&d->lock);
  695. if (d->Settings.FlowControl != flow)
  696. d->setFlowControl(flow, true);
  697. }
  698. /*!
  699. Sets the parity associated with the serial port to \a parity. The possible values of parity are:
  700. \code
  701. PAR_SPACE Space Parity
  702. PAR_MARK Mark Parity
  703. PAR_NONE No Parity
  704. PAR_EVEN Even Parity
  705. PAR_ODD Odd Parity
  706. \endcode
  707. */
  708. void QextSerialPort::setParity(ParityType parity)
  709. {
  710. Q_D(QextSerialPort);
  711. QWriteLocker locker(&d->lock);
  712. if (d->Settings.Parity != parity)
  713. d->setParity(parity, true);
  714. }
  715. /*!
  716. Sets the number of data bits used by the serial port to \a dataBits. Possible values of dataBits are:
  717. \code
  718. DATA_5 5 data bits
  719. DATA_6 6 data bits
  720. DATA_7 7 data bits
  721. DATA_8 8 data bits
  722. \endcode
  723. \bold note:
  724. This function is subject to the following restrictions:
  725. \list
  726. \o 5 data bits cannot be used with 2 stop bits.
  727. \o 1.5 stop bits can only be used with 5 data bits.
  728. \o 8 data bits cannot be used with space parity on POSIX systems.
  729. \endlist
  730. */
  731. void QextSerialPort::setDataBits(DataBitsType dataBits)
  732. {
  733. Q_D(QextSerialPort);
  734. QWriteLocker locker(&d->lock);
  735. if (d->Settings.DataBits != dataBits)
  736. d->setDataBits(dataBits, true);
  737. }
  738. /*!
  739. Sets the number of stop bits used by the serial port to \a stopBits. Possible values of stopBits are:
  740. \code
  741. STOP_1 1 stop bit
  742. STOP_1_5 1.5 stop bits
  743. STOP_2 2 stop bits
  744. \endcode
  745. \bold note:
  746. This function is subject to the following restrictions:
  747. \list
  748. \o 2 stop bits cannot be used with 5 data bits.
  749. \o 1.5 stop bits cannot be used with 6 or more data bits.
  750. \o POSIX does not support 1.5 stop bits.
  751. \endlist
  752. */
  753. void QextSerialPort::setStopBits(StopBitsType stopBits)
  754. {
  755. Q_D(QextSerialPort);
  756. QWriteLocker locker(&d->lock);
  757. if (d->Settings.StopBits != stopBits)
  758. d->setStopBits(stopBits, true);
  759. }
  760. /*!
  761. Sets the baud rate of the serial port to \a baudRate. Note that not all rates are applicable on
  762. all platforms. The following table shows translations of the various baud rate
  763. constants on Windows(including NT/2000) and POSIX platforms. Speeds marked with an *
  764. are speeds that are usable on both Windows and POSIX.
  765. \code
  766. RATE Windows Speed POSIX Speed
  767. ----------- ------------- -----------
  768. BAUD50 X 50
  769. BAUD75 X 75
  770. *BAUD110 110 110
  771. BAUD134 X 134.5
  772. BAUD150 X 150
  773. BAUD200 X 200
  774. *BAUD300 300 300
  775. *BAUD600 600 600
  776. *BAUD1200 1200 1200
  777. BAUD1800 X 1800
  778. *BAUD2400 2400 2400
  779. *BAUD4800 4800 4800
  780. *BAUD9600 9600 9600
  781. BAUD14400 14400 X
  782. *BAUD19200 19200 19200
  783. *BAUD38400 38400 38400
  784. BAUD56000 56000 X
  785. *BAUD57600 57600 57600
  786. BAUD76800 X 76800
  787. *BAUD115200 115200 115200
  788. BAUD128000 128000 X
  789. BAUD230400 X 230400
  790. BAUD256000 256000 X
  791. BAUD460800 X 460800
  792. BAUD500000 X 500000
  793. BAUD576000 X 576000
  794. BAUD921600 X 921600
  795. BAUD1000000 X 1000000
  796. BAUD1152000 X 1152000
  797. BAUD1500000 X 1500000
  798. BAUD2000000 X 2000000
  799. BAUD2500000 X 2500000
  800. BAUD3000000 X 3000000
  801. BAUD3500000 X 3500000
  802. BAUD4000000 X 4000000
  803. \endcode
  804. */
  805. void QextSerialPort::setBaudRate(BaudRateType baudRate)
  806. {
  807. Q_D(QextSerialPort);
  808. QWriteLocker locker(&d->lock);
  809. if (d->Settings.BaudRate != baudRate)
  810. d->setBaudRate(baudRate, true);
  811. }
  812. /*!
  813. For Unix:
  814. Sets the read and write timeouts for the port to \a millisec milliseconds.
  815. Note that this is a per-character timeout, i.e. the port will wait this long for each
  816. individual character, not for the whole read operation. This timeout also applies to the
  817. bytesWaiting() function.
  818. \bold note:
  819. POSIX does not support millisecond-level control for I/O timeout values. Any
  820. timeout set using this function will be set to the next lowest tenth of a second for
  821. the purposes of detecting read or write timeouts. For example a timeout of 550 milliseconds
  822. will be seen by the class as a timeout of 500 milliseconds for the purposes of reading and
  823. writing the port. However millisecond-level control is allowed by the select() system call,
  824. so for example a 550-millisecond timeout will be seen as 550 milliseconds on POSIX systems for
  825. the purpose of detecting available bytes in the read buffer.
  826. For Windows:
  827. Sets the read and write timeouts for the port to \a millisec milliseconds.
  828. Setting 0 indicates that timeouts are not used for read nor write operations;
  829. however read() and write() functions will still block. Set -1 to provide
  830. non-blocking behaviour (read() and write() will return immediately).
  831. \bold note: this function does nothing in event driven mode.
  832. */
  833. void QextSerialPort::setTimeout(long millisec)
  834. {
  835. Q_D(QextSerialPort);
  836. QWriteLocker locker(&d->lock);
  837. if (d->Settings.Timeout_Millisec != millisec)
  838. d->setTimeout(millisec, true);
  839. }
  840. /*!
  841. Sets DTR line to the requested state (\a set default to high). This function will have no effect if
  842. the port associated with the class is not currently open.
  843. */
  844. void QextSerialPort::setDtr(bool set)
  845. {
  846. Q_D(QextSerialPort);
  847. QWriteLocker locker(&d->lock);
  848. if (isOpen())
  849. d->setDtr_sys(set);
  850. }
  851. /*!
  852. Sets RTS line to the requested state \a set (high by default).
  853. This function will have no effect if
  854. the port associated with the class is not currently open.
  855. */
  856. void QextSerialPort::setRts(bool set)
  857. {
  858. Q_D(QextSerialPort);
  859. QWriteLocker locker(&d->lock);
  860. if (isOpen())
  861. d->setRts_sys(set);
  862. }
  863. /*! \reimp
  864. Reads a block of data from the serial port. This function will read at most maxlen bytes from
  865. the serial port and place them in the buffer pointed to by data. Return value is the number of
  866. bytes actually read, or -1 on error.
  867. \warning before calling this function ensure that serial port associated with this class
  868. is currently open (use isOpen() function to check if port is open).
  869. */
  870. qint64 QextSerialPort::readData(char *data, qint64 maxSize)
  871. {
  872. Q_D(QextSerialPort);
  873. QWriteLocker locker(&d->lock);
  874. qint64 bytesFromBuffer = 0;
  875. if (!d->readBuffer.isEmpty()) {
  876. bytesFromBuffer = d->readBuffer.read(data, maxSize);
  877. if (bytesFromBuffer == maxSize)
  878. return bytesFromBuffer;
  879. }
  880. qint64 bytesFromDevice = d->readData_sys(data+bytesFromBuffer, maxSize-bytesFromBuffer);
  881. if (bytesFromDevice < 0) {
  882. return -1;
  883. }
  884. return bytesFromBuffer + bytesFromDevice;
  885. }
  886. /*! \reimp
  887. Writes a block of data to the serial port. This function will write len bytes
  888. from the buffer pointed to by data to the serial port. Return value is the number
  889. of bytes actually written, or -1 on error.
  890. \warning before calling this function ensure that serial port associated with this class
  891. is currently open (use isOpen() function to check if port is open).
  892. */
  893. qint64 QextSerialPort::writeData(const char *data, qint64 maxSize)
  894. {
  895. Q_D(QextSerialPort);
  896. QWriteLocker locker(&d->lock);
  897. return d->writeData_sys(data, maxSize);
  898. }
  899. #include "moc_qextserialport.cpp"