Typica is a free program for professional coffee roasters. https://typica.us
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

qextserialport.cpp 31KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005
  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. #if defined(Q_OS_WIN) || defined(Q_OS_MAC)
  117. default:
  118. #endif
  119. settings.BaudRate = baudRate;
  120. settingsDirtyFlags |= DFE_BaudRate;
  121. if (update && q_func()->isOpen())
  122. updatePortSettings();
  123. break;
  124. #if !(defined(Q_OS_WIN) || defined(Q_OS_MAC))
  125. default:
  126. QESP_WARNING()<<"QextSerialPort does not support baudRate:"<<baudRate;
  127. #endif
  128. }
  129. }
  130. void QextSerialPortPrivate::setParity(ParityType parity, bool update)
  131. {
  132. switch (parity) {
  133. case PAR_SPACE:
  134. if (settings.DataBits == DATA_8) {
  135. #ifdef Q_OS_WIN
  136. QESP_PORTABILITY_WARNING("QextSerialPort Portability Warning: Space parity with 8 data bits is not supported by POSIX systems.");
  137. #else
  138. QESP_WARNING("Space parity with 8 data bits is not supported by POSIX systems.");
  139. #endif
  140. }
  141. break;
  142. #ifdef Q_OS_WIN
  143. /*mark parity - WINDOWS ONLY*/
  144. case PAR_MARK:
  145. QESP_PORTABILITY_WARNING("QextSerialPort Portability Warning: Mark parity is not supported by POSIX systems");
  146. break;
  147. #endif
  148. case PAR_NONE:
  149. case PAR_EVEN:
  150. case PAR_ODD:
  151. break;
  152. default:
  153. QESP_WARNING()<<"QextSerialPort does not support Parity:" << parity;
  154. }
  155. settings.Parity = parity;
  156. settingsDirtyFlags |= DFE_Parity;
  157. if (update && q_func()->isOpen())
  158. updatePortSettings();
  159. }
  160. void QextSerialPortPrivate::setDataBits(DataBitsType dataBits, bool update)
  161. {
  162. switch(dataBits) {
  163. case DATA_5:
  164. if (settings.StopBits == STOP_2) {
  165. QESP_WARNING("QextSerialPort: 5 Data bits cannot be used with 2 stop bits.");
  166. } else {
  167. settings.DataBits = dataBits;
  168. settingsDirtyFlags |= DFE_DataBits;
  169. }
  170. break;
  171. case DATA_6:
  172. #ifdef Q_OS_WIN
  173. if (settings.StopBits == STOP_1_5) {
  174. QESP_WARNING("QextSerialPort: 6 Data bits cannot be used with 1.5 stop bits.");
  175. }
  176. else
  177. #endif
  178. {
  179. settings.DataBits = dataBits;
  180. settingsDirtyFlags |= DFE_DataBits;
  181. }
  182. break;
  183. case DATA_7:
  184. #ifdef Q_OS_WIN
  185. if (settings.StopBits == STOP_1_5) {
  186. QESP_WARNING("QextSerialPort: 7 Data bits cannot be used with 1.5 stop bits.");
  187. }
  188. else
  189. #endif
  190. {
  191. settings.DataBits = dataBits;
  192. settingsDirtyFlags |= DFE_DataBits;
  193. }
  194. break;
  195. case DATA_8:
  196. #ifdef Q_OS_WIN
  197. if (settings.StopBits == STOP_1_5) {
  198. QESP_WARNING("QextSerialPort: 8 Data bits cannot be used with 1.5 stop bits.");
  199. }
  200. else
  201. #endif
  202. {
  203. settings.DataBits = dataBits;
  204. settingsDirtyFlags |= DFE_DataBits;
  205. }
  206. break;
  207. default:
  208. QESP_WARNING()<<"QextSerialPort does not support Data bits:"<<dataBits;
  209. }
  210. if (update && q_func()->isOpen())
  211. updatePortSettings();
  212. }
  213. void QextSerialPortPrivate::setStopBits(StopBitsType stopBits, bool update)
  214. {
  215. switch (stopBits) {
  216. /*one stop bit*/
  217. case STOP_1:
  218. settings.StopBits = stopBits;
  219. settingsDirtyFlags |= DFE_StopBits;
  220. break;
  221. #ifdef Q_OS_WIN
  222. /*1.5 stop bits*/
  223. case STOP_1_5:
  224. QESP_PORTABILITY_WARNING("QextSerialPort Portability Warning: 1.5 stop bit operation is not supported by POSIX.");
  225. if (settings.DataBits != DATA_5) {
  226. QESP_WARNING("QextSerialPort: 1.5 stop bits can only be used with 5 data bits");
  227. } else {
  228. settings.StopBits = stopBits;
  229. settingsDirtyFlags |= DFE_StopBits;
  230. }
  231. break;
  232. #endif
  233. /*two stop bits*/
  234. case STOP_2:
  235. if (settings.DataBits == DATA_5) {
  236. QESP_WARNING("QextSerialPort: 2 stop bits cannot be used with 5 data bits");
  237. } else {
  238. settings.StopBits = stopBits;
  239. settingsDirtyFlags |= DFE_StopBits;
  240. }
  241. break;
  242. default:
  243. QESP_WARNING()<<"QextSerialPort does not support stop bits: "<<stopBits;
  244. }
  245. if (update && q_func()->isOpen())
  246. updatePortSettings();
  247. }
  248. void QextSerialPortPrivate::setFlowControl(FlowType flow, bool update)
  249. {
  250. settings.FlowControl = flow;
  251. settingsDirtyFlags |= DFE_Flow;
  252. if (update && q_func()->isOpen())
  253. updatePortSettings();
  254. }
  255. void QextSerialPortPrivate::setTimeout(long millisec, bool update)
  256. {
  257. settings.Timeout_Millisec = millisec;
  258. settingsDirtyFlags |= DFE_TimeOut;
  259. if (update && q_func()->isOpen())
  260. updatePortSettings();
  261. }
  262. void QextSerialPortPrivate::setPortSettings(const PortSettings &settings, bool update)
  263. {
  264. setBaudRate(settings.BaudRate, false);
  265. setDataBits(settings.DataBits, false);
  266. setStopBits(settings.StopBits, false);
  267. setParity(settings.Parity, false);
  268. setFlowControl(settings.FlowControl, false);
  269. setTimeout(settings.Timeout_Millisec, false);
  270. settingsDirtyFlags = DFE_ALL;
  271. if (update && q_func()->isOpen())
  272. updatePortSettings();
  273. }
  274. void QextSerialPortPrivate::_q_canRead()
  275. {
  276. qint64 maxSize = bytesAvailable_sys();
  277. if (maxSize > 0) {
  278. char *writePtr = readBuffer.reserve(size_t(maxSize));
  279. qint64 bytesRead = readData_sys(writePtr, maxSize);
  280. if (bytesRead < maxSize)
  281. readBuffer.chop(maxSize - bytesRead);
  282. Q_Q(QextSerialPort);
  283. Q_EMIT q->readyRead();
  284. }
  285. }
  286. /*! \class QextSerialPort
  287. \brief The QextSerialPort class encapsulates a serial port on both POSIX and Windows systems.
  288. \section1 Usage
  289. QextSerialPort offers both a polling and event driven API. Event driven
  290. is typically easier to use, since you never have to worry about checking
  291. for new data.
  292. \bold Example
  293. \code
  294. QextSerialPort *port = new QextSerialPort("COM1");
  295. connect(port, SIGNAL(readyRead()), myClass, SLOT(onDataAvailable()));
  296. port->open();
  297. void MyClass::onDataAvailable()
  298. {
  299. QByteArray data = port->readAll();
  300. processNewData(usbdata);
  301. }
  302. \endcode
  303. \section1 Compatibility
  304. The user will be notified of errors and possible portability conflicts at run-time
  305. by default.
  306. For example, if a application has used BAUD1800, when it is runing under unix, you
  307. will get following message.
  308. \code
  309. QextSerialPort Portability Warning: Windows does not support baudRate:1800
  310. \endcode
  311. This behavior can be turned off by defining macro QESP_NO_WARN (to turn off all warnings)
  312. or QESP_NO_PORTABILITY_WARN (to turn off portability warnings) in the project.
  313. \bold Author: Stefan Sander, Michal Policht, Brandon Fosdick, Liam Staskawicz, Debao Zhang
  314. */
  315. /*!
  316. \enum QextSerialPort::QueryMode
  317. This enum type specifies query mode used in a serial port:
  318. \value Polling
  319. asynchronously read and write
  320. \value EventDriven
  321. synchronously read and write
  322. */
  323. /*!
  324. \fn void QextSerialPort::dsrChanged(bool status)
  325. This signal is emitted whenever dsr line has changed its state. You may
  326. use this signal to check if device is connected.
  327. \a status true when DSR signal is on, false otherwise.
  328. */
  329. /*!
  330. \fn QueryMode QextSerialPort::queryMode() const
  331. Get query mode.
  332. */
  333. /*!
  334. Default constructor. Note that the name of the device used by a QextSerialPort is dependent on
  335. your OS. Possible naming conventions and their associated OS are:
  336. \code
  337. OS Constant Used By Naming Convention
  338. ------------- ------------- ------------------------
  339. Q_OS_WIN Windows COM1, COM2
  340. Q_OS_IRIX SGI/IRIX /dev/ttyf1, /dev/ttyf2
  341. Q_OS_HPUX HP-UX /dev/tty1p0, /dev/tty2p0
  342. Q_OS_SOLARIS SunOS/Slaris /dev/ttya, /dev/ttyb
  343. Q_OS_OSF Digital UNIX /dev/tty01, /dev/tty02
  344. Q_OS_FREEBSD FreeBSD /dev/ttyd0, /dev/ttyd1
  345. Q_OS_OPENBSD OpenBSD /dev/tty00, /dev/tty01
  346. Q_OS_LINUX Linux /dev/ttyS0, /dev/ttyS1
  347. <none> /dev/ttyS0, /dev/ttyS1
  348. \endcode
  349. This constructor assigns the device name to the name of the first port on the specified system.
  350. See the other constructors if you need to open a different port. Default \a mode is EventDriven.
  351. As a subclass of QObject, \a parent can be specified.
  352. */
  353. QextSerialPort::QextSerialPort(QextSerialPort::QueryMode mode, QObject *parent)
  354. : QIODevice(parent), d_ptr(new QextSerialPortPrivate(this))
  355. {
  356. #ifdef Q_OS_WIN
  357. setPortName(QLatin1String("COM1"));
  358. #elif defined(Q_OS_IRIX)
  359. setPortName(QLatin1String("/dev/ttyf1"));
  360. #elif defined(Q_OS_HPUX)
  361. setPortName(QLatin1String("/dev/tty1p0"));
  362. #elif defined(Q_OS_SOLARIS)
  363. setPortName(QLatin1String("/dev/ttya"));
  364. #elif defined(Q_OS_OSF) //formally DIGITAL UNIX
  365. setPortName(QLatin1String("/dev/tty01"));
  366. #elif defined(Q_OS_FREEBSD)
  367. setPortName(QLatin1String("/dev/ttyd1"));
  368. #elif defined(Q_OS_OPENBSD)
  369. setPortName(QLatin1String("/dev/tty00"));
  370. #else
  371. setPortName(QLatin1String("/dev/ttyS0"));
  372. #endif
  373. setQueryMode(mode);
  374. }
  375. /*!
  376. Constructs a serial port attached to the port specified by name.
  377. \a name is the name of the device, which is windowsystem-specific,
  378. e.g."COM1" or "/dev/ttyS0". \a mode
  379. */
  380. QextSerialPort::QextSerialPort(const QString &name, QextSerialPort::QueryMode mode, QObject *parent)
  381. : QIODevice(parent), d_ptr(new QextSerialPortPrivate(this))
  382. {
  383. setQueryMode(mode);
  384. setPortName(name);
  385. }
  386. /*!
  387. Constructs a port with default name and specified \a settings.
  388. */
  389. QextSerialPort::QextSerialPort(const PortSettings &settings, QextSerialPort::QueryMode mode, QObject *parent)
  390. : QIODevice(parent), d_ptr(new QextSerialPortPrivate(this))
  391. {
  392. Q_D(QextSerialPort);
  393. setQueryMode(mode);
  394. d->setPortSettings(settings);
  395. }
  396. /*!
  397. Constructs a port with specified \a name , \a mode and \a settings.
  398. */
  399. QextSerialPort::QextSerialPort(const QString &name, const PortSettings &settings, QextSerialPort::QueryMode mode, QObject *parent)
  400. : QIODevice(parent), d_ptr(new QextSerialPortPrivate(this))
  401. {
  402. Q_D(QextSerialPort);
  403. setPortName(name);
  404. setQueryMode(mode);
  405. d->setPortSettings(settings);
  406. }
  407. /*!
  408. Opens a serial port and sets its OpenMode to \a mode.
  409. Note that this function does not specify which device to open.
  410. Returns true if successful; otherwise returns false.This function has no effect
  411. if the port associated with the class is already open. The port is also
  412. configured to the current settings, as stored in the settings structure.
  413. */
  414. bool QextSerialPort::open(OpenMode mode)
  415. {
  416. Q_D(QextSerialPort);
  417. QWriteLocker locker(&d->lock);
  418. if (mode != QIODevice::NotOpen && !isOpen())
  419. d->open_sys(mode);
  420. return isOpen();
  421. }
  422. /*! \reimp
  423. Closes a serial port. This function has no effect if the serial port associated with the class
  424. is not currently open.
  425. */
  426. void QextSerialPort::close()
  427. {
  428. Q_D(QextSerialPort);
  429. QWriteLocker locker(&d->lock);
  430. if (isOpen()) {
  431. // Be a good QIODevice and call QIODevice::close() before really close()
  432. // so the aboutToClose() signal is emitted at the proper time
  433. QIODevice::close(); // mark ourselves as closed
  434. d->close_sys();
  435. d->readBuffer.clear();
  436. }
  437. }
  438. /*!
  439. Flushes all pending I/O to the serial port. This function has no effect if the serial port
  440. associated with the class is not currently open.
  441. */
  442. void QextSerialPort::flush()
  443. {
  444. Q_D(QextSerialPort);
  445. QWriteLocker locker(&d->lock);
  446. if (isOpen())
  447. d->flush_sys();
  448. }
  449. /*! \reimp
  450. Returns the number of bytes waiting in the port's receive queue. This function will return 0 if
  451. the port is not currently open, or -1 on error.
  452. */
  453. qint64 QextSerialPort::bytesAvailable() const
  454. {
  455. QWriteLocker locker(&d_func()->lock);
  456. if (isOpen()) {
  457. qint64 bytes = d_func()->bytesAvailable_sys();
  458. if (bytes != -1) {
  459. return bytes + d_func()->readBuffer.size()
  460. + QIODevice::bytesAvailable();
  461. }
  462. return -1;
  463. }
  464. return 0;
  465. }
  466. /*! \reimp
  467. */
  468. bool QextSerialPort::canReadLine() const
  469. {
  470. QReadLocker locker(&d_func()->lock);
  471. return QIODevice::canReadLine() || d_func()->readBuffer.canReadLine();
  472. }
  473. /*!
  474. * Set desired serial communication handling style. You may choose from polling
  475. * or event driven approach. This function does nothing when port is open; to
  476. * apply changes port must be reopened.
  477. *
  478. * In event driven approach read() and write() functions are acting
  479. * asynchronously. They return immediately and the operation is performed in
  480. * the background, so they doesn't freeze the calling thread.
  481. * To determine when operation is finished, QextSerialPort runs separate thread
  482. * and monitors serial port events. Whenever the event occurs, adequate signal
  483. * is emitted.
  484. *
  485. * When polling is set, read() and write() are acting synchronously. Signals are
  486. * not working in this mode and some functions may not be available. The advantage
  487. * of polling is that it generates less overhead due to lack of signals emissions
  488. * and it doesn't start separate thread to monitor events.
  489. *
  490. * Generally event driven approach is more capable and friendly, although some
  491. * applications may need as low overhead as possible and then polling comes.
  492. *
  493. * \a mode query mode.
  494. */
  495. void QextSerialPort::setQueryMode(QueryMode mode)
  496. {
  497. Q_D(QextSerialPort);
  498. QWriteLocker locker(&d->lock);
  499. if (mode != d->queryMode)
  500. d->queryMode = mode;
  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. delete d_ptr;
  681. }
  682. /*!
  683. Sets the flow control used by the port to \a flow. Possible values of flow are:
  684. \code
  685. FLOW_OFF No flow control
  686. FLOW_HARDWARE Hardware (RTS/CTS) flow control
  687. FLOW_XONXOFF Software (XON/XOFF) flow control
  688. \endcode
  689. */
  690. void QextSerialPort::setFlowControl(FlowType flow)
  691. {
  692. Q_D(QextSerialPort);
  693. QWriteLocker locker(&d->lock);
  694. if (d->settings.FlowControl != flow)
  695. d->setFlowControl(flow, true);
  696. }
  697. /*!
  698. Sets the parity associated with the serial port to \a parity. The possible values of parity are:
  699. \code
  700. PAR_SPACE Space Parity
  701. PAR_MARK Mark Parity
  702. PAR_NONE No Parity
  703. PAR_EVEN Even Parity
  704. PAR_ODD Odd Parity
  705. \endcode
  706. */
  707. void QextSerialPort::setParity(ParityType parity)
  708. {
  709. Q_D(QextSerialPort);
  710. QWriteLocker locker(&d->lock);
  711. if (d->settings.Parity != parity)
  712. d->setParity(parity, true);
  713. }
  714. /*!
  715. Sets the number of data bits used by the serial port to \a dataBits. Possible values of dataBits are:
  716. \code
  717. DATA_5 5 data bits
  718. DATA_6 6 data bits
  719. DATA_7 7 data bits
  720. DATA_8 8 data bits
  721. \endcode
  722. \bold note:
  723. This function is subject to the following restrictions:
  724. \list
  725. \o 5 data bits cannot be used with 2 stop bits.
  726. \o 1.5 stop bits can only be used with 5 data bits.
  727. \o 8 data bits cannot be used with space parity on POSIX systems.
  728. \endlist
  729. */
  730. void QextSerialPort::setDataBits(DataBitsType dataBits)
  731. {
  732. Q_D(QextSerialPort);
  733. QWriteLocker locker(&d->lock);
  734. if (d->settings.DataBits != dataBits)
  735. d->setDataBits(dataBits, true);
  736. }
  737. /*!
  738. Sets the number of stop bits used by the serial port to \a stopBits. Possible values of stopBits are:
  739. \code
  740. STOP_1 1 stop bit
  741. STOP_1_5 1.5 stop bits
  742. STOP_2 2 stop bits
  743. \endcode
  744. \bold note:
  745. This function is subject to the following restrictions:
  746. \list
  747. \o 2 stop bits cannot be used with 5 data bits.
  748. \o 1.5 stop bits cannot be used with 6 or more data bits.
  749. \o POSIX does not support 1.5 stop bits.
  750. \endlist
  751. */
  752. void QextSerialPort::setStopBits(StopBitsType stopBits)
  753. {
  754. Q_D(QextSerialPort);
  755. QWriteLocker locker(&d->lock);
  756. if (d->settings.StopBits != stopBits)
  757. d->setStopBits(stopBits, true);
  758. }
  759. /*!
  760. Sets the baud rate of the serial port to \a baudRate. Note that not all rates are applicable on
  761. all platforms. The following table shows translations of the various baud rate
  762. constants on Windows(including NT/2000) and POSIX platforms. Speeds marked with an *
  763. are speeds that are usable on both Windows and POSIX.
  764. \code
  765. RATE Windows Speed POSIX Speed
  766. ----------- ------------- -----------
  767. BAUD50 X 50
  768. BAUD75 X 75
  769. *BAUD110 110 110
  770. BAUD134 X 134.5
  771. BAUD150 X 150
  772. BAUD200 X 200
  773. *BAUD300 300 300
  774. *BAUD600 600 600
  775. *BAUD1200 1200 1200
  776. BAUD1800 X 1800
  777. *BAUD2400 2400 2400
  778. *BAUD4800 4800 4800
  779. *BAUD9600 9600 9600
  780. BAUD14400 14400 X
  781. *BAUD19200 19200 19200
  782. *BAUD38400 38400 38400
  783. BAUD56000 56000 X
  784. *BAUD57600 57600 57600
  785. BAUD76800 X 76800
  786. *BAUD115200 115200 115200
  787. BAUD128000 128000 X
  788. BAUD230400 X 230400
  789. BAUD256000 256000 X
  790. BAUD460800 X 460800
  791. BAUD500000 X 500000
  792. BAUD576000 X 576000
  793. BAUD921600 X 921600
  794. BAUD1000000 X 1000000
  795. BAUD1152000 X 1152000
  796. BAUD1500000 X 1500000
  797. BAUD2000000 X 2000000
  798. BAUD2500000 X 2500000
  799. BAUD3000000 X 3000000
  800. BAUD3500000 X 3500000
  801. BAUD4000000 X 4000000
  802. \endcode
  803. */
  804. void QextSerialPort::setBaudRate(BaudRateType baudRate)
  805. {
  806. Q_D(QextSerialPort);
  807. QWriteLocker locker(&d->lock);
  808. if (d->settings.BaudRate != baudRate)
  809. d->setBaudRate(baudRate, true);
  810. }
  811. /*!
  812. For Unix:
  813. Sets the read and write timeouts for the port to \a millisec milliseconds.
  814. Note that this is a per-character timeout, i.e. the port will wait this long for each
  815. individual character, not for the whole read operation. This timeout also applies to the
  816. bytesWaiting() function.
  817. \bold note:
  818. POSIX does not support millisecond-level control for I/O timeout values. Any
  819. timeout set using this function will be set to the next lowest tenth of a second for
  820. the purposes of detecting read or write timeouts. For example a timeout of 550 milliseconds
  821. will be seen by the class as a timeout of 500 milliseconds for the purposes of reading and
  822. writing the port. However millisecond-level control is allowed by the select() system call,
  823. so for example a 550-millisecond timeout will be seen as 550 milliseconds on POSIX systems for
  824. the purpose of detecting available bytes in the read buffer.
  825. For Windows:
  826. Sets the read and write timeouts for the port to \a millisec milliseconds.
  827. Setting 0 indicates that timeouts are not used for read nor write operations;
  828. however read() and write() functions will still block. Set -1 to provide
  829. non-blocking behaviour (read() and write() will return immediately).
  830. \bold note: this function does nothing in event driven mode.
  831. */
  832. void QextSerialPort::setTimeout(long millisec)
  833. {
  834. Q_D(QextSerialPort);
  835. QWriteLocker locker(&d->lock);
  836. if (d->settings.Timeout_Millisec != millisec)
  837. d->setTimeout(millisec, true);
  838. }
  839. /*!
  840. Sets DTR line to the requested state (\a set default to high). This function will have no effect if
  841. the port associated with the class is not currently open.
  842. */
  843. void QextSerialPort::setDtr(bool set)
  844. {
  845. Q_D(QextSerialPort);
  846. QWriteLocker locker(&d->lock);
  847. if (isOpen())
  848. d->setDtr_sys(set);
  849. }
  850. /*!
  851. Sets RTS line to the requested state \a set (high by default).
  852. This function will have no effect if
  853. the port associated with the class is not currently open.
  854. */
  855. void QextSerialPort::setRts(bool set)
  856. {
  857. Q_D(QextSerialPort);
  858. QWriteLocker locker(&d->lock);
  859. if (isOpen())
  860. d->setRts_sys(set);
  861. }
  862. /*! \reimp
  863. Reads a block of data from the serial port. This function will read at most maxlen bytes from
  864. the serial port and place them in the buffer pointed to by data. Return value is the number of
  865. bytes actually read, or -1 on error.
  866. \warning before calling this function ensure that serial port associated with this class
  867. is currently open (use isOpen() function to check if port is open).
  868. */
  869. qint64 QextSerialPort::readData(char *data, qint64 maxSize)
  870. {
  871. Q_D(QextSerialPort);
  872. QWriteLocker locker(&d->lock);
  873. qint64 bytesFromBuffer = 0;
  874. if (!d->readBuffer.isEmpty()) {
  875. bytesFromBuffer = d->readBuffer.read(data, maxSize);
  876. if (bytesFromBuffer == maxSize)
  877. return bytesFromBuffer;
  878. }
  879. qint64 bytesFromDevice = d->readData_sys(data+bytesFromBuffer, maxSize-bytesFromBuffer);
  880. if (bytesFromDevice < 0)
  881. return -1;
  882. return bytesFromBuffer + bytesFromDevice;
  883. }
  884. /*! \reimp
  885. Writes a block of data to the serial port. This function will write len bytes
  886. from the buffer pointed to by data to the serial port. Return value is the number
  887. of bytes actually written, or -1 on error.
  888. \warning before calling this function ensure that serial port associated with this class
  889. is currently open (use isOpen() function to check if port is open).
  890. */
  891. qint64 QextSerialPort::writeData(const char *data, qint64 maxSize)
  892. {
  893. Q_D(QextSerialPort);
  894. QWriteLocker locker(&d->lock);
  895. return d->writeData_sys(data, maxSize);
  896. }
  897. #include "moc_qextserialport.cpp"