/qextserialport/posix_qextserialport.cpp

https://github.com/poes-weather/Sensor-Benchmark · C++ · 959 lines · 639 code · 76 blank · 244 comment · 80 complexity · 220f340374bb5dc5c02fdc35eecc3bbe MD5 · raw file

  1. #include <fcntl.h>
  2. #include <stdio.h>
  3. #include "qextserialport.h"
  4. #include <QMutexLocker>
  5. #include <QDebug>
  6. void QextSerialPort::platformSpecificInit()
  7. {
  8. fd = 0;
  9. readNotifier = 0;
  10. }
  11. /*!
  12. Standard destructor.
  13. */
  14. void QextSerialPort::platformSpecificDestruct()
  15. {}
  16. /*!
  17. Sets the baud rate of the serial port. Note that not all rates are applicable on
  18. all platforms. The following table shows translations of the various baud rate
  19. constants on Windows(including NT/2000) and POSIX platforms. Speeds marked with an *
  20. are speeds that are usable on both Windows and POSIX.
  21. \note
  22. BAUD76800 may not be supported on all POSIX systems. SGI/IRIX systems do not support
  23. BAUD1800.
  24. \verbatim
  25. RATE Windows Speed POSIX Speed
  26. ----------- ------------- -----------
  27. BAUD50 110 50
  28. BAUD75 110 75
  29. *BAUD110 110 110
  30. BAUD134 110 134.5
  31. BAUD150 110 150
  32. BAUD200 110 200
  33. *BAUD300 300 300
  34. *BAUD600 600 600
  35. *BAUD1200 1200 1200
  36. BAUD1800 1200 1800
  37. *BAUD2400 2400 2400
  38. *BAUD4800 4800 4800
  39. *BAUD9600 9600 9600
  40. BAUD14400 14400 9600
  41. *BAUD19200 19200 19200
  42. *BAUD38400 38400 38400
  43. BAUD56000 56000 38400
  44. *BAUD57600 57600 57600
  45. BAUD76800 57600 76800
  46. *BAUD115200 115200 115200
  47. BAUD128000 128000 115200
  48. BAUD256000 256000 115200
  49. \endverbatim
  50. */
  51. void QextSerialPort::setBaudRate(BaudRateType baudRate)
  52. {
  53. QMutexLocker lock(mutex);
  54. if (Settings.BaudRate!=baudRate) {
  55. switch (baudRate) {
  56. case BAUD14400:
  57. Settings.BaudRate=BAUD9600;
  58. break;
  59. case BAUD56000:
  60. Settings.BaudRate=BAUD38400;
  61. break;
  62. case BAUD76800:
  63. #ifndef B76800
  64. Settings.BaudRate=BAUD57600;
  65. #else
  66. Settings.BaudRate=baudRate;
  67. #endif
  68. break;
  69. case BAUD128000:
  70. case BAUD256000:
  71. Settings.BaudRate=BAUD115200;
  72. break;
  73. default:
  74. Settings.BaudRate=baudRate;
  75. break;
  76. }
  77. }
  78. if (isOpen()) {
  79. switch (baudRate) {
  80. /*50 baud*/
  81. case BAUD50:
  82. TTY_PORTABILITY_WARNING("QextSerialPort Portability Warning: Windows does not support 50 baud operation.");
  83. #ifdef CBAUD
  84. Posix_CommConfig.c_cflag&=(~CBAUD);
  85. Posix_CommConfig.c_cflag|=B50;
  86. #else
  87. cfsetispeed(&Posix_CommConfig, B50);
  88. cfsetospeed(&Posix_CommConfig, B50);
  89. #endif
  90. break;
  91. /*75 baud*/
  92. case BAUD75:
  93. TTY_PORTABILITY_WARNING("QextSerialPort Portability Warning: Windows does not support 75 baud operation.");
  94. #ifdef CBAUD
  95. Posix_CommConfig.c_cflag&=(~CBAUD);
  96. Posix_CommConfig.c_cflag|=B75;
  97. #else
  98. cfsetispeed(&Posix_CommConfig, B75);
  99. cfsetospeed(&Posix_CommConfig, B75);
  100. #endif
  101. break;
  102. /*110 baud*/
  103. case BAUD110:
  104. #ifdef CBAUD
  105. Posix_CommConfig.c_cflag&=(~CBAUD);
  106. Posix_CommConfig.c_cflag|=B110;
  107. #else
  108. cfsetispeed(&Posix_CommConfig, B110);
  109. cfsetospeed(&Posix_CommConfig, B110);
  110. #endif
  111. break;
  112. /*134.5 baud*/
  113. case BAUD134:
  114. TTY_PORTABILITY_WARNING("QextSerialPort Portability Warning: Windows does not support 134.5 baud operation.");
  115. #ifdef CBAUD
  116. Posix_CommConfig.c_cflag&=(~CBAUD);
  117. Posix_CommConfig.c_cflag|=B134;
  118. #else
  119. cfsetispeed(&Posix_CommConfig, B134);
  120. cfsetospeed(&Posix_CommConfig, B134);
  121. #endif
  122. break;
  123. /*150 baud*/
  124. case BAUD150:
  125. TTY_PORTABILITY_WARNING("QextSerialPort Portability Warning: Windows does not support 150 baud operation.");
  126. #ifdef CBAUD
  127. Posix_CommConfig.c_cflag&=(~CBAUD);
  128. Posix_CommConfig.c_cflag|=B150;
  129. #else
  130. cfsetispeed(&Posix_CommConfig, B150);
  131. cfsetospeed(&Posix_CommConfig, B150);
  132. #endif
  133. break;
  134. /*200 baud*/
  135. case BAUD200:
  136. TTY_PORTABILITY_WARNING("QextSerialPort Portability Warning: Windows does not support 200 baud operation.");
  137. #ifdef CBAUD
  138. Posix_CommConfig.c_cflag&=(~CBAUD);
  139. Posix_CommConfig.c_cflag|=B200;
  140. #else
  141. cfsetispeed(&Posix_CommConfig, B200);
  142. cfsetospeed(&Posix_CommConfig, B200);
  143. #endif
  144. break;
  145. /*300 baud*/
  146. case BAUD300:
  147. #ifdef CBAUD
  148. Posix_CommConfig.c_cflag&=(~CBAUD);
  149. Posix_CommConfig.c_cflag|=B300;
  150. #else
  151. cfsetispeed(&Posix_CommConfig, B300);
  152. cfsetospeed(&Posix_CommConfig, B300);
  153. #endif
  154. break;
  155. /*600 baud*/
  156. case BAUD600:
  157. #ifdef CBAUD
  158. Posix_CommConfig.c_cflag&=(~CBAUD);
  159. Posix_CommConfig.c_cflag|=B600;
  160. #else
  161. cfsetispeed(&Posix_CommConfig, B600);
  162. cfsetospeed(&Posix_CommConfig, B600);
  163. #endif
  164. break;
  165. /*1200 baud*/
  166. case BAUD1200:
  167. #ifdef CBAUD
  168. Posix_CommConfig.c_cflag&=(~CBAUD);
  169. Posix_CommConfig.c_cflag|=B1200;
  170. #else
  171. cfsetispeed(&Posix_CommConfig, B1200);
  172. cfsetospeed(&Posix_CommConfig, B1200);
  173. #endif
  174. break;
  175. /*1800 baud*/
  176. case BAUD1800:
  177. TTY_PORTABILITY_WARNING("QextSerialPort Portability Warning: Windows and IRIX do not support 1800 baud operation.");
  178. #ifdef CBAUD
  179. Posix_CommConfig.c_cflag&=(~CBAUD);
  180. Posix_CommConfig.c_cflag|=B1800;
  181. #else
  182. cfsetispeed(&Posix_CommConfig, B1800);
  183. cfsetospeed(&Posix_CommConfig, B1800);
  184. #endif
  185. break;
  186. /*2400 baud*/
  187. case BAUD2400:
  188. #ifdef CBAUD
  189. Posix_CommConfig.c_cflag&=(~CBAUD);
  190. Posix_CommConfig.c_cflag|=B2400;
  191. #else
  192. cfsetispeed(&Posix_CommConfig, B2400);
  193. cfsetospeed(&Posix_CommConfig, B2400);
  194. #endif
  195. break;
  196. /*4800 baud*/
  197. case BAUD4800:
  198. #ifdef CBAUD
  199. Posix_CommConfig.c_cflag&=(~CBAUD);
  200. Posix_CommConfig.c_cflag|=B4800;
  201. #else
  202. cfsetispeed(&Posix_CommConfig, B4800);
  203. cfsetospeed(&Posix_CommConfig, B4800);
  204. #endif
  205. break;
  206. /*9600 baud*/
  207. case BAUD9600:
  208. #ifdef CBAUD
  209. Posix_CommConfig.c_cflag&=(~CBAUD);
  210. Posix_CommConfig.c_cflag|=B9600;
  211. #else
  212. cfsetispeed(&Posix_CommConfig, B9600);
  213. cfsetospeed(&Posix_CommConfig, B9600);
  214. #endif
  215. break;
  216. /*14400 baud*/
  217. case BAUD14400:
  218. TTY_WARNING("QextSerialPort: POSIX does not support 14400 baud operation. Switching to 9600 baud.");
  219. #ifdef CBAUD
  220. Posix_CommConfig.c_cflag&=(~CBAUD);
  221. Posix_CommConfig.c_cflag|=B9600;
  222. #else
  223. cfsetispeed(&Posix_CommConfig, B9600);
  224. cfsetospeed(&Posix_CommConfig, B9600);
  225. #endif
  226. break;
  227. /*19200 baud*/
  228. case BAUD19200:
  229. #ifdef CBAUD
  230. Posix_CommConfig.c_cflag&=(~CBAUD);
  231. Posix_CommConfig.c_cflag|=B19200;
  232. #else
  233. cfsetispeed(&Posix_CommConfig, B19200);
  234. cfsetospeed(&Posix_CommConfig, B19200);
  235. #endif
  236. break;
  237. /*38400 baud*/
  238. case BAUD38400:
  239. #ifdef CBAUD
  240. Posix_CommConfig.c_cflag&=(~CBAUD);
  241. Posix_CommConfig.c_cflag|=B38400;
  242. #else
  243. cfsetispeed(&Posix_CommConfig, B38400);
  244. cfsetospeed(&Posix_CommConfig, B38400);
  245. #endif
  246. break;
  247. /*56000 baud*/
  248. case BAUD56000:
  249. TTY_WARNING("QextSerialPort: POSIX does not support 56000 baud operation. Switching to 38400 baud.");
  250. #ifdef CBAUD
  251. Posix_CommConfig.c_cflag&=(~CBAUD);
  252. Posix_CommConfig.c_cflag|=B38400;
  253. #else
  254. cfsetispeed(&Posix_CommConfig, B38400);
  255. cfsetospeed(&Posix_CommConfig, B38400);
  256. #endif
  257. break;
  258. /*57600 baud*/
  259. case BAUD57600:
  260. #ifdef CBAUD
  261. Posix_CommConfig.c_cflag&=(~CBAUD);
  262. Posix_CommConfig.c_cflag|=B57600;
  263. #else
  264. cfsetispeed(&Posix_CommConfig, B57600);
  265. cfsetospeed(&Posix_CommConfig, B57600);
  266. #endif
  267. break;
  268. /*76800 baud*/
  269. case BAUD76800:
  270. TTY_PORTABILITY_WARNING("QextSerialPort Portability Warning: Windows and some POSIX systems do not support 76800 baud operation.");
  271. #ifdef CBAUD
  272. Posix_CommConfig.c_cflag&=(~CBAUD);
  273. #ifdef B76800
  274. Posix_CommConfig.c_cflag|=B76800;
  275. #else
  276. TTY_WARNING("QextSerialPort: QextSerialPort was compiled without 76800 baud support. Switching to 57600 baud.");
  277. Posix_CommConfig.c_cflag|=B57600;
  278. #endif //B76800
  279. #else //CBAUD
  280. #ifdef B76800
  281. cfsetispeed(&Posix_CommConfig, B76800);
  282. cfsetospeed(&Posix_CommConfig, B76800);
  283. #else
  284. TTY_WARNING("QextSerialPort: QextSerialPort was compiled without 76800 baud support. Switching to 57600 baud.");
  285. cfsetispeed(&Posix_CommConfig, B57600);
  286. cfsetospeed(&Posix_CommConfig, B57600);
  287. #endif //B76800
  288. #endif //CBAUD
  289. break;
  290. /*115200 baud*/
  291. case BAUD115200:
  292. #ifdef CBAUD
  293. Posix_CommConfig.c_cflag&=(~CBAUD);
  294. Posix_CommConfig.c_cflag|=B115200;
  295. #else
  296. cfsetispeed(&Posix_CommConfig, B115200);
  297. cfsetospeed(&Posix_CommConfig, B115200);
  298. #endif
  299. break;
  300. /*128000 baud*/
  301. case BAUD128000:
  302. TTY_WARNING("QextSerialPort: POSIX does not support 128000 baud operation. Switching to 115200 baud.");
  303. #ifdef CBAUD
  304. Posix_CommConfig.c_cflag&=(~CBAUD);
  305. Posix_CommConfig.c_cflag|=B115200;
  306. #else
  307. cfsetispeed(&Posix_CommConfig, B115200);
  308. cfsetospeed(&Posix_CommConfig, B115200);
  309. #endif
  310. break;
  311. /*256000 baud*/
  312. case BAUD256000:
  313. TTY_WARNING("QextSerialPort: POSIX does not support 256000 baud operation. Switching to 115200 baud.");
  314. #ifdef CBAUD
  315. Posix_CommConfig.c_cflag&=(~CBAUD);
  316. Posix_CommConfig.c_cflag|=B115200;
  317. #else
  318. cfsetispeed(&Posix_CommConfig, B115200);
  319. cfsetospeed(&Posix_CommConfig, B115200);
  320. #endif
  321. break;
  322. }
  323. tcsetattr(fd, TCSAFLUSH, &Posix_CommConfig);
  324. }
  325. }
  326. /*!
  327. Sets the number of data bits used by the serial port. Possible values of dataBits are:
  328. \verbatim
  329. DATA_5 5 data bits
  330. DATA_6 6 data bits
  331. DATA_7 7 data bits
  332. DATA_8 8 data bits
  333. \endverbatim
  334. \note
  335. This function is subject to the following restrictions:
  336. \par
  337. 5 data bits cannot be used with 2 stop bits.
  338. \par
  339. 8 data bits cannot be used with space parity on POSIX systems.
  340. */
  341. void QextSerialPort::setDataBits(DataBitsType dataBits)
  342. {
  343. QMutexLocker lock(mutex);
  344. if (Settings.DataBits!=dataBits) {
  345. if ((Settings.StopBits==STOP_2 && dataBits==DATA_5) ||
  346. (Settings.StopBits==STOP_1_5 && dataBits!=DATA_5) ||
  347. (Settings.Parity==PAR_SPACE && dataBits==DATA_8)) {
  348. }
  349. else {
  350. Settings.DataBits=dataBits;
  351. }
  352. }
  353. if (isOpen()) {
  354. switch(dataBits) {
  355. /*5 data bits*/
  356. case DATA_5:
  357. if (Settings.StopBits==STOP_2) {
  358. TTY_WARNING("QextSerialPort: 5 Data bits cannot be used with 2 stop bits.");
  359. }
  360. else {
  361. Settings.DataBits=dataBits;
  362. Posix_CommConfig.c_cflag&=(~CSIZE);
  363. Posix_CommConfig.c_cflag|=CS5;
  364. tcsetattr(fd, TCSAFLUSH, &Posix_CommConfig);
  365. }
  366. break;
  367. /*6 data bits*/
  368. case DATA_6:
  369. if (Settings.StopBits==STOP_1_5) {
  370. TTY_WARNING("QextSerialPort: 6 Data bits cannot be used with 1.5 stop bits.");
  371. }
  372. else {
  373. Settings.DataBits=dataBits;
  374. Posix_CommConfig.c_cflag&=(~CSIZE);
  375. Posix_CommConfig.c_cflag|=CS6;
  376. tcsetattr(fd, TCSAFLUSH, &Posix_CommConfig);
  377. }
  378. break;
  379. /*7 data bits*/
  380. case DATA_7:
  381. if (Settings.StopBits==STOP_1_5) {
  382. TTY_WARNING("QextSerialPort: 7 Data bits cannot be used with 1.5 stop bits.");
  383. }
  384. else {
  385. Settings.DataBits=dataBits;
  386. Posix_CommConfig.c_cflag&=(~CSIZE);
  387. Posix_CommConfig.c_cflag|=CS7;
  388. tcsetattr(fd, TCSAFLUSH, &Posix_CommConfig);
  389. }
  390. break;
  391. /*8 data bits*/
  392. case DATA_8:
  393. if (Settings.StopBits==STOP_1_5) {
  394. TTY_WARNING("QextSerialPort: 8 Data bits cannot be used with 1.5 stop bits.");
  395. }
  396. else {
  397. Settings.DataBits=dataBits;
  398. Posix_CommConfig.c_cflag&=(~CSIZE);
  399. Posix_CommConfig.c_cflag|=CS8;
  400. tcsetattr(fd, TCSAFLUSH, &Posix_CommConfig);
  401. }
  402. break;
  403. }
  404. }
  405. }
  406. /*!
  407. Sets the parity associated with the serial port. The possible values of parity are:
  408. \verbatim
  409. PAR_SPACE Space Parity
  410. PAR_MARK Mark Parity
  411. PAR_NONE No Parity
  412. PAR_EVEN Even Parity
  413. PAR_ODD Odd Parity
  414. \endverbatim
  415. \note
  416. This function is subject to the following limitations:
  417. \par
  418. POSIX systems do not support mark parity.
  419. \par
  420. POSIX systems support space parity only if tricked into doing so, and only with
  421. fewer than 8 data bits. Use space parity very carefully with POSIX systems.
  422. */
  423. void QextSerialPort::setParity(ParityType parity)
  424. {
  425. QMutexLocker lock(mutex);
  426. if (Settings.Parity!=parity) {
  427. if (parity==PAR_MARK || (parity==PAR_SPACE && Settings.DataBits==DATA_8)) {
  428. }
  429. else {
  430. Settings.Parity=parity;
  431. }
  432. }
  433. if (isOpen()) {
  434. switch (parity) {
  435. /*space parity*/
  436. case PAR_SPACE:
  437. if (Settings.DataBits==DATA_8) {
  438. TTY_PORTABILITY_WARNING("QextSerialPort: Space parity is only supported in POSIX with 7 or fewer data bits");
  439. }
  440. else {
  441. /*space parity not directly supported - add an extra data bit to simulate it*/
  442. Posix_CommConfig.c_cflag&=~(PARENB|CSIZE);
  443. switch(Settings.DataBits) {
  444. case DATA_5:
  445. Settings.DataBits=DATA_6;
  446. Posix_CommConfig.c_cflag|=CS6;
  447. break;
  448. case DATA_6:
  449. Settings.DataBits=DATA_7;
  450. Posix_CommConfig.c_cflag|=CS7;
  451. break;
  452. case DATA_7:
  453. Settings.DataBits=DATA_8;
  454. Posix_CommConfig.c_cflag|=CS8;
  455. break;
  456. case DATA_8:
  457. break;
  458. }
  459. tcsetattr(fd, TCSAFLUSH, &Posix_CommConfig);
  460. }
  461. break;
  462. /*mark parity - WINDOWS ONLY*/
  463. case PAR_MARK:
  464. TTY_WARNING("QextSerialPort: Mark parity is not supported by POSIX.");
  465. break;
  466. /*no parity*/
  467. case PAR_NONE:
  468. Posix_CommConfig.c_cflag&=(~PARENB);
  469. tcsetattr(fd, TCSAFLUSH, &Posix_CommConfig);
  470. break;
  471. /*even parity*/
  472. case PAR_EVEN:
  473. Posix_CommConfig.c_cflag&=(~PARODD);
  474. Posix_CommConfig.c_cflag|=PARENB;
  475. tcsetattr(fd, TCSAFLUSH, &Posix_CommConfig);
  476. break;
  477. /*odd parity*/
  478. case PAR_ODD:
  479. Posix_CommConfig.c_cflag|=(PARENB|PARODD);
  480. tcsetattr(fd, TCSAFLUSH, &Posix_CommConfig);
  481. break;
  482. }
  483. }
  484. }
  485. /*!
  486. Sets the number of stop bits used by the serial port. Possible values of stopBits are:
  487. \verbatim
  488. STOP_1 1 stop bit
  489. STOP_1_5 1.5 stop bits
  490. STOP_2 2 stop bits
  491. \endverbatim
  492. \note
  493. This function is subject to the following restrictions:
  494. \par
  495. 2 stop bits cannot be used with 5 data bits.
  496. \par
  497. POSIX does not support 1.5 stop bits.
  498. */
  499. void QextSerialPort::setStopBits(StopBitsType stopBits)
  500. {
  501. QMutexLocker lock(mutex);
  502. if (Settings.StopBits!=stopBits) {
  503. if ((Settings.DataBits==DATA_5 && stopBits==STOP_2) || stopBits==STOP_1_5) {}
  504. else {
  505. Settings.StopBits=stopBits;
  506. }
  507. }
  508. if (isOpen()) {
  509. switch (stopBits) {
  510. /*one stop bit*/
  511. case STOP_1:
  512. Settings.StopBits=stopBits;
  513. Posix_CommConfig.c_cflag&=(~CSTOPB);
  514. tcsetattr(fd, TCSAFLUSH, &Posix_CommConfig);
  515. break;
  516. /*1.5 stop bits*/
  517. case STOP_1_5:
  518. TTY_WARNING("QextSerialPort: 1.5 stop bit operation is not supported by POSIX.");
  519. break;
  520. /*two stop bits*/
  521. case STOP_2:
  522. if (Settings.DataBits==DATA_5) {
  523. TTY_WARNING("QextSerialPort: 2 stop bits cannot be used with 5 data bits");
  524. }
  525. else {
  526. Settings.StopBits=stopBits;
  527. Posix_CommConfig.c_cflag|=CSTOPB;
  528. tcsetattr(fd, TCSAFLUSH, &Posix_CommConfig);
  529. }
  530. break;
  531. }
  532. }
  533. }
  534. /*!
  535. Sets the flow control used by the port. Possible values of flow are:
  536. \verbatim
  537. FLOW_OFF No flow control
  538. FLOW_HARDWARE Hardware (RTS/CTS) flow control
  539. FLOW_XONXOFF Software (XON/XOFF) flow control
  540. \endverbatim
  541. \note
  542. FLOW_HARDWARE may not be supported on all versions of UNIX. In cases where it is
  543. unsupported, FLOW_HARDWARE is the same as FLOW_OFF.
  544. */
  545. void QextSerialPort::setFlowControl(FlowType flow)
  546. {
  547. QMutexLocker lock(mutex);
  548. if (Settings.FlowControl!=flow) {
  549. Settings.FlowControl=flow;
  550. }
  551. if (isOpen()) {
  552. switch(flow) {
  553. /*no flow control*/
  554. case FLOW_OFF:
  555. Posix_CommConfig.c_cflag&=(~CRTSCTS);
  556. Posix_CommConfig.c_iflag&=(~(IXON|IXOFF|IXANY));
  557. tcsetattr(fd, TCSAFLUSH, &Posix_CommConfig);
  558. break;
  559. /*software (XON/XOFF) flow control*/
  560. case FLOW_XONXOFF:
  561. Posix_CommConfig.c_cflag&=(~CRTSCTS);
  562. Posix_CommConfig.c_iflag|=(IXON|IXOFF|IXANY);
  563. tcsetattr(fd, TCSAFLUSH, &Posix_CommConfig);
  564. break;
  565. case FLOW_HARDWARE:
  566. Posix_CommConfig.c_cflag|=CRTSCTS;
  567. Posix_CommConfig.c_iflag&=(~(IXON|IXOFF|IXANY));
  568. tcsetattr(fd, TCSAFLUSH, &Posix_CommConfig);
  569. break;
  570. }
  571. }
  572. }
  573. /*!
  574. Sets the read and write timeouts for the port to millisec milliseconds.
  575. Note that this is a per-character timeout, i.e. the port will wait this long for each
  576. individual character, not for the whole read operation. This timeout also applies to the
  577. bytesWaiting() function.
  578. \note
  579. POSIX does not support millisecond-level control for I/O timeout values. Any
  580. timeout set using this function will be set to the next lowest tenth of a second for
  581. the purposes of detecting read or write timeouts. For example a timeout of 550 milliseconds
  582. will be seen by the class as a timeout of 500 milliseconds for the purposes of reading and
  583. writing the port. However millisecond-level control is allowed by the select() system call,
  584. so for example a 550-millisecond timeout will be seen as 550 milliseconds on POSIX systems for
  585. the purpose of detecting available bytes in the read buffer.
  586. */
  587. void QextSerialPort::setTimeout(long millisec)
  588. {
  589. QMutexLocker lock(mutex);
  590. Settings.Timeout_Millisec = millisec;
  591. Posix_Copy_Timeout.tv_sec = millisec / 1000;
  592. Posix_Copy_Timeout.tv_usec = millisec % 1000;
  593. if (isOpen()) {
  594. if (millisec == -1)
  595. fcntl(fd, F_SETFL, O_NDELAY);
  596. else
  597. //O_SYNC should enable blocking ::write()
  598. //however this seems not working on Linux 2.6.21 (works on OpenBSD 4.2)
  599. fcntl(fd, F_SETFL, O_SYNC);
  600. tcgetattr(fd, & Posix_CommConfig);
  601. Posix_CommConfig.c_cc[VTIME] = millisec/100;
  602. tcsetattr(fd, TCSAFLUSH, & Posix_CommConfig);
  603. }
  604. }
  605. /*!
  606. Opens the serial port associated to this class.
  607. This function has no effect if the port associated with the class is already open.
  608. The port is also configured to the current settings, as stored in the Settings structure.
  609. */
  610. bool QextSerialPort::open(OpenMode mode)
  611. {
  612. QMutexLocker lock(mutex);
  613. if (mode == QIODevice::NotOpen)
  614. return isOpen();
  615. if (!isOpen()) {
  616. qDebug() << "trying to open file" << port.toAscii();
  617. //note: linux 2.6.21 seems to ignore O_NDELAY flag
  618. if ((fd = ::open(port.toAscii() ,O_RDWR | O_NOCTTY | O_NDELAY)) != -1) {
  619. qDebug("file opened succesfully");
  620. setOpenMode(mode); // Flag the port as opened
  621. tcgetattr(fd, &old_termios); // Save the old termios
  622. Posix_CommConfig = old_termios; // Make a working copy
  623. cfmakeraw(&Posix_CommConfig); // Enable raw access
  624. /*set up other port settings*/
  625. Posix_CommConfig.c_cflag|=CREAD|CLOCAL;
  626. Posix_CommConfig.c_lflag&=(~(ICANON|ECHO|ECHOE|ECHOK|ECHONL|ISIG));
  627. Posix_CommConfig.c_iflag&=(~(INPCK|IGNPAR|PARMRK|ISTRIP|ICRNL|IXANY));
  628. Posix_CommConfig.c_oflag&=(~OPOST);
  629. Posix_CommConfig.c_cc[VMIN]= 0;
  630. #ifdef _POSIX_VDISABLE // Is a disable character available on this system?
  631. // Some systems allow for per-device disable-characters, so get the
  632. // proper value for the configured device
  633. const long vdisable = fpathconf(fd, _PC_VDISABLE);
  634. Posix_CommConfig.c_cc[VINTR] = vdisable;
  635. Posix_CommConfig.c_cc[VQUIT] = vdisable;
  636. Posix_CommConfig.c_cc[VSTART] = vdisable;
  637. Posix_CommConfig.c_cc[VSTOP] = vdisable;
  638. Posix_CommConfig.c_cc[VSUSP] = vdisable;
  639. #endif //_POSIX_VDISABLE
  640. setBaudRate(Settings.BaudRate);
  641. setDataBits(Settings.DataBits);
  642. setParity(Settings.Parity);
  643. setStopBits(Settings.StopBits);
  644. setFlowControl(Settings.FlowControl);
  645. setTimeout(Settings.Timeout_Millisec);
  646. tcsetattr(fd, TCSAFLUSH, &Posix_CommConfig);
  647. if (queryMode() == QextSerialPort::EventDriven) {
  648. readNotifier = new QSocketNotifier(fd, QSocketNotifier::Read, this);
  649. connect(readNotifier, SIGNAL(activated(int)), this, SIGNAL(readyRead()));
  650. }
  651. } else {
  652. qDebug() << "could not open file:" << strerror(errno);
  653. lastErr = E_FILE_NOT_FOUND;
  654. }
  655. }
  656. return isOpen();
  657. }
  658. /*!
  659. Closes a serial port. This function has no effect if the serial port associated with the class
  660. is not currently open.
  661. */
  662. void QextSerialPort::close()
  663. {
  664. QMutexLocker lock(mutex);
  665. if( isOpen() )
  666. {
  667. // Force a flush and then restore the original termios
  668. flush();
  669. // Using both TCSAFLUSH and TCSANOW here discards any pending input
  670. tcsetattr(fd, TCSAFLUSH | TCSANOW, &old_termios); // Restore termios
  671. // Be a good QIODevice and call QIODevice::close() before POSIX close()
  672. // so the aboutToClose() signal is emitted at the proper time
  673. QIODevice::close(); // Flag the device as closed
  674. // QIODevice::close() doesn't actually close the port, so do that here
  675. ::close(fd);
  676. if(readNotifier) {
  677. delete readNotifier;
  678. readNotifier = 0;
  679. }
  680. }
  681. }
  682. /*!
  683. Flushes all pending I/O to the serial port. This function has no effect if the serial port
  684. associated with the class is not currently open.
  685. */
  686. void QextSerialPort::flush()
  687. {
  688. QMutexLocker lock(mutex);
  689. if (isOpen())
  690. tcflush(fd, TCIOFLUSH);
  691. }
  692. /*!
  693. This function will return the number of bytes waiting in the receive queue of the serial port.
  694. It is included primarily to provide a complete QIODevice interface, and will not record errors
  695. in the lastErr member (because it is const). This function is also not thread-safe - in
  696. multithreading situations, use QextSerialPort::bytesWaiting() instead.
  697. */
  698. qint64 QextSerialPort::size() const
  699. {
  700. int numBytes;
  701. if (ioctl(fd, FIONREAD, &numBytes)<0) {
  702. numBytes = 0;
  703. }
  704. return (qint64)numBytes;
  705. }
  706. /*!
  707. Returns the number of bytes waiting in the port's receive queue. This function will return 0 if
  708. the port is not currently open, or -1 on error.
  709. */
  710. qint64 QextSerialPort::bytesAvailable() const
  711. {
  712. QMutexLocker lock(mutex);
  713. if (isOpen()) {
  714. int bytesQueued;
  715. if (ioctl(fd, FIONREAD, &bytesQueued) == -1) {
  716. return (qint64)-1;
  717. }
  718. return bytesQueued + QIODevice::bytesAvailable();
  719. }
  720. return 0;
  721. }
  722. /*!
  723. This function is included to implement the full QIODevice interface, and currently has no
  724. purpose within this class. This function is meaningless on an unbuffered device and currently
  725. only prints a warning message to that effect.
  726. */
  727. void QextSerialPort::ungetChar(char)
  728. {
  729. /*meaningless on unbuffered sequential device - return error and print a warning*/
  730. TTY_WARNING("QextSerialPort: ungetChar() called on an unbuffered sequential device - operation is meaningless");
  731. }
  732. /*!
  733. Translates a system-specific error code to a QextSerialPort error code. Used internally.
  734. */
  735. void QextSerialPort::translateError(ulong error)
  736. {
  737. switch (error) {
  738. case EBADF:
  739. case ENOTTY:
  740. lastErr=E_INVALID_FD;
  741. break;
  742. case EINTR:
  743. lastErr=E_CAUGHT_NON_BLOCKED_SIGNAL;
  744. break;
  745. case ENOMEM:
  746. lastErr=E_NO_MEMORY;
  747. break;
  748. }
  749. }
  750. /*!
  751. Sets DTR line to the requested state (high by default). This function will have no effect if
  752. the port associated with the class is not currently open.
  753. */
  754. void QextSerialPort::setDtr(bool set)
  755. {
  756. QMutexLocker lock(mutex);
  757. if (isOpen()) {
  758. int status;
  759. ioctl(fd, TIOCMGET, &status);
  760. if (set) {
  761. status|=TIOCM_DTR;
  762. }
  763. else {
  764. status&=~TIOCM_DTR;
  765. }
  766. ioctl(fd, TIOCMSET, &status);
  767. }
  768. }
  769. /*!
  770. Sets RTS line to the requested state (high by default). This function will have no effect if
  771. the port associated with the class is not currently open.
  772. */
  773. void QextSerialPort::setRts(bool set)
  774. {
  775. QMutexLocker lock(mutex);
  776. if (isOpen()) {
  777. int status;
  778. ioctl(fd, TIOCMGET, &status);
  779. if (set) {
  780. status|=TIOCM_RTS;
  781. }
  782. else {
  783. status&=~TIOCM_RTS;
  784. }
  785. ioctl(fd, TIOCMSET, &status);
  786. }
  787. }
  788. /*!
  789. Returns the line status as stored by the port function. This function will retrieve the states
  790. of the following lines: DCD, CTS, DSR, and RI. On POSIX systems, the following additional lines
  791. can be monitored: DTR, RTS, Secondary TXD, and Secondary RXD. The value returned is an unsigned
  792. long with specific bits indicating which lines are high. The following constants should be used
  793. to examine the states of individual lines:
  794. \verbatim
  795. Mask Line
  796. ------ ----
  797. LS_CTS CTS
  798. LS_DSR DSR
  799. LS_DCD DCD
  800. LS_RI RI
  801. LS_RTS RTS (POSIX only)
  802. LS_DTR DTR (POSIX only)
  803. LS_ST Secondary TXD (POSIX only)
  804. LS_SR Secondary RXD (POSIX only)
  805. \endverbatim
  806. This function will return 0 if the port associated with the class is not currently open.
  807. */
  808. unsigned long QextSerialPort::lineStatus()
  809. {
  810. unsigned long Status=0, Temp=0;
  811. QMutexLocker lock(mutex);
  812. if (isOpen()) {
  813. ioctl(fd, TIOCMGET, &Temp);
  814. if (Temp&TIOCM_CTS) {
  815. Status|=LS_CTS;
  816. }
  817. if (Temp&TIOCM_DSR) {
  818. Status|=LS_DSR;
  819. }
  820. if (Temp&TIOCM_RI) {
  821. Status|=LS_RI;
  822. }
  823. if (Temp&TIOCM_CD) {
  824. Status|=LS_DCD;
  825. }
  826. if (Temp&TIOCM_DTR) {
  827. Status|=LS_DTR;
  828. }
  829. if (Temp&TIOCM_RTS) {
  830. Status|=LS_RTS;
  831. }
  832. if (Temp&TIOCM_ST) {
  833. Status|=LS_ST;
  834. }
  835. if (Temp&TIOCM_SR) {
  836. Status|=LS_SR;
  837. }
  838. }
  839. return Status;
  840. }
  841. /*!
  842. Reads a block of data from the serial port. This function will read at most maxSize bytes from
  843. the serial port and place them in the buffer pointed to by data. Return value is the number of
  844. bytes actually read, or -1 on error.
  845. \warning before calling this function ensure that serial port associated with this class
  846. is currently open (use isOpen() function to check if port is open).
  847. */
  848. qint64 QextSerialPort::readData(char * data, qint64 maxSize)
  849. {
  850. QMutexLocker lock(mutex);
  851. int retVal = ::read(fd, data, maxSize);
  852. if (retVal == -1)
  853. lastErr = E_READ_FAILED;
  854. return retVal;
  855. }
  856. /*!
  857. Writes a block of data to the serial port. This function will write maxSize bytes
  858. from the buffer pointed to by data to the serial port. Return value is the number
  859. of bytes actually written, or -1 on error.
  860. \warning before calling this function ensure that serial port associated with this class
  861. is currently open (use isOpen() function to check if port is open).
  862. */
  863. qint64 QextSerialPort::writeData(const char * data, qint64 maxSize)
  864. {
  865. QMutexLocker lock(mutex);
  866. int retVal = ::write(fd, data, maxSize);
  867. if (retVal == -1)
  868. lastErr = E_WRITE_FAILED;
  869. return (qint64)retVal;
  870. }