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_unix.cpp 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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 <fcntl.h>
  34. #include <stdio.h>
  35. #include <errno.h>
  36. #include <unistd.h>
  37. #include <sys/time.h>
  38. #include <sys/ioctl.h>
  39. #include <sys/select.h>
  40. #include <QtCore/QMutexLocker>
  41. #include <QtCore/QDebug>
  42. #include <QtCore/QSocketNotifier>
  43. void QextSerialPortPrivate::platformSpecificInit()
  44. {
  45. fd = 0;
  46. readNotifier = 0;
  47. }
  48. /*!
  49. Standard destructor.
  50. */
  51. void QextSerialPortPrivate::platformSpecificDestruct()
  52. {
  53. }
  54. bool QextSerialPortPrivate::open_sys(QIODevice::OpenMode mode)
  55. {
  56. Q_Q(QextSerialPort);
  57. //note: linux 2.6.21 seems to ignore O_NDELAY flag
  58. if ((fd = ::open(port.toAscii() ,O_RDWR | O_NOCTTY | O_NDELAY)) != -1) {
  59. /*In the Private class, We can not call QIODevice::open()*/
  60. q->setOpenMode(mode); // Flag the port as opened
  61. ::tcgetattr(fd, &old_termios); // Save the old termios
  62. Posix_CommConfig = old_termios; // Make a working copy
  63. ::cfmakeraw(&Posix_CommConfig); // Enable raw access
  64. /*set up other port settings*/
  65. Posix_CommConfig.c_cflag |= CREAD|CLOCAL;
  66. Posix_CommConfig.c_lflag &= (~(ICANON|ECHO|ECHOE|ECHOK|ECHONL|ISIG));
  67. Posix_CommConfig.c_iflag &= (~(INPCK|IGNPAR|PARMRK|ISTRIP|ICRNL|IXANY));
  68. Posix_CommConfig.c_oflag &= (~OPOST);
  69. Posix_CommConfig.c_cc[VMIN] = 0;
  70. #ifdef _POSIX_VDISABLE // Is a disable character available on this system?
  71. // Some systems allow for per-device disable-characters, so get the
  72. // proper value for the configured device
  73. const long vdisable = ::fpathconf(fd, _PC_VDISABLE);
  74. Posix_CommConfig.c_cc[VINTR] = vdisable;
  75. Posix_CommConfig.c_cc[VQUIT] = vdisable;
  76. Posix_CommConfig.c_cc[VSTART] = vdisable;
  77. Posix_CommConfig.c_cc[VSTOP] = vdisable;
  78. Posix_CommConfig.c_cc[VSUSP] = vdisable;
  79. #endif //_POSIX_VDISABLE
  80. settingsDirtyFlags = DFE_ALL;
  81. updatePortSettings();
  82. if (_queryMode == QextSerialPort::EventDriven) {
  83. readNotifier = new QSocketNotifier(fd, QSocketNotifier::Read, q);
  84. q->connect(readNotifier, SIGNAL(activated(int)), q, SLOT(_q_canRead()));
  85. }
  86. return true;
  87. } else {
  88. translateError(errno);
  89. return false;
  90. }
  91. }
  92. bool QextSerialPortPrivate::close_sys()
  93. {
  94. // Force a flush and then restore the original termios
  95. flush_sys();
  96. // Using both TCSAFLUSH and TCSANOW here discards any pending input
  97. ::tcsetattr(fd, TCSAFLUSH | TCSANOW, &old_termios); // Restore termios
  98. ::close(fd);
  99. if(readNotifier) {
  100. delete readNotifier;
  101. readNotifier = 0;
  102. }
  103. return true;
  104. }
  105. bool QextSerialPortPrivate::flush_sys()
  106. {
  107. ::tcdrain(fd);
  108. return true;
  109. }
  110. qint64 QextSerialPortPrivate::bytesAvailable_sys() const
  111. {
  112. int bytesQueued;
  113. if (::ioctl(fd, FIONREAD, &bytesQueued) == -1) {
  114. return (qint64)-1;
  115. }
  116. return bytesQueued;
  117. }
  118. /*!
  119. Translates a system-specific error code to a QextSerialPort error code. Used internally.
  120. */
  121. void QextSerialPortPrivate::translateError(ulong error)
  122. {
  123. switch (error) {
  124. case EBADF:
  125. case ENOTTY:
  126. lastErr = E_INVALID_FD;
  127. break;
  128. case EINTR:
  129. lastErr = E_CAUGHT_NON_BLOCKED_SIGNAL;
  130. break;
  131. case ENOMEM:
  132. lastErr = E_NO_MEMORY;
  133. break;
  134. case EACCES:
  135. lastErr = E_PERMISSION_DENIED;
  136. break;
  137. case EAGAIN:
  138. lastErr = E_AGAIN;
  139. break;
  140. }
  141. }
  142. void QextSerialPortPrivate::setDtr_sys(bool set)
  143. {
  144. int status;
  145. ::ioctl(fd, TIOCMGET, &status);
  146. if (set)
  147. status |= TIOCM_DTR;
  148. else
  149. status &= ~TIOCM_DTR;
  150. ::ioctl(fd, TIOCMSET, &status);
  151. }
  152. void QextSerialPortPrivate::setRts_sys(bool set)
  153. {
  154. int status;
  155. ::ioctl(fd, TIOCMGET, &status);
  156. if (set)
  157. status |= TIOCM_RTS;
  158. else
  159. status &= ~TIOCM_RTS;
  160. ::ioctl(fd, TIOCMSET, &status);
  161. }
  162. unsigned long QextSerialPortPrivate::lineStatus_sys()
  163. {
  164. unsigned long Status=0, Temp=0;
  165. ::ioctl(fd, TIOCMGET, &Temp);
  166. if (Temp & TIOCM_CTS) Status |= LS_CTS;
  167. if (Temp & TIOCM_DSR) Status |= LS_DSR;
  168. if (Temp & TIOCM_RI ) Status |= LS_RI;
  169. if (Temp & TIOCM_CD ) Status |= LS_DCD;
  170. if (Temp & TIOCM_DTR) Status |= LS_DTR;
  171. if (Temp & TIOCM_RTS) Status |= LS_RTS;
  172. if (Temp & TIOCM_ST ) Status |= LS_ST;
  173. if (Temp & TIOCM_SR ) Status |= LS_SR;
  174. return Status;
  175. }
  176. /*!
  177. Reads a block of data from the serial port. This function will read at most maxSize bytes from
  178. the serial port and place them in the buffer pointed to by data. Return value is the number of
  179. bytes actually read, or -1 on error.
  180. \warning before calling this function ensure that serial port associated with this class
  181. is currently open (use isOpen() function to check if port is open).
  182. */
  183. qint64 QextSerialPortPrivate::readData_sys(char * data, qint64 maxSize)
  184. {
  185. int retVal = ::read(fd, data, maxSize);
  186. if (retVal == -1)
  187. lastErr = E_READ_FAILED;
  188. return retVal;
  189. }
  190. /*!
  191. Writes a block of data to the serial port. This function will write maxSize bytes
  192. from the buffer pointed to by data to the serial port. Return value is the number
  193. of bytes actually written, or -1 on error.
  194. \warning before calling this function ensure that serial port associated with this class
  195. is currently open (use isOpen() function to check if port is open).
  196. */
  197. qint64 QextSerialPortPrivate::writeData_sys(const char * data, qint64 maxSize)
  198. {
  199. int retVal = ::write(fd, data, maxSize);
  200. if (retVal == -1)
  201. lastErr = E_WRITE_FAILED;
  202. return (qint64)retVal;
  203. }
  204. static void setBaudRate2Termios(termios *config, int baudRate)
  205. {
  206. #ifdef CBAUD
  207. config->c_cflag &= (~CBAUD);
  208. config->c_cflag |= baudRate;
  209. #else
  210. ::cfsetispeed(config, baudRate);
  211. ::cfsetospeed(config, baudRate);
  212. #endif
  213. }
  214. /*
  215. All the platform settings was performed in this function.
  216. */
  217. void QextSerialPortPrivate::updatePortSettings()
  218. {
  219. if (!q_func()->isOpen() || !settingsDirtyFlags)
  220. return;
  221. if (settingsDirtyFlags & DFE_BaudRate) {
  222. switch (Settings.BaudRate) {
  223. case BAUD50:
  224. setBaudRate2Termios(&Posix_CommConfig, B50);
  225. break;
  226. case BAUD75:
  227. setBaudRate2Termios(&Posix_CommConfig, B75);
  228. break;
  229. case BAUD110:
  230. setBaudRate2Termios(&Posix_CommConfig, B110);
  231. break;
  232. case BAUD134:
  233. setBaudRate2Termios(&Posix_CommConfig, B134);
  234. break;
  235. case BAUD150:
  236. setBaudRate2Termios(&Posix_CommConfig, B150);
  237. break;
  238. case BAUD200:
  239. setBaudRate2Termios(&Posix_CommConfig, B200);
  240. break;
  241. case BAUD300:
  242. setBaudRate2Termios(&Posix_CommConfig, B300);
  243. break;
  244. case BAUD600:
  245. setBaudRate2Termios(&Posix_CommConfig, B600);
  246. break;
  247. case BAUD1200:
  248. setBaudRate2Termios(&Posix_CommConfig, B1200);
  249. break;
  250. case BAUD1800:
  251. setBaudRate2Termios(&Posix_CommConfig, B1800);
  252. break;
  253. case BAUD2400:
  254. setBaudRate2Termios(&Posix_CommConfig, B2400);
  255. break;
  256. case BAUD4800:
  257. setBaudRate2Termios(&Posix_CommConfig, B4800);
  258. break;
  259. case BAUD9600:
  260. setBaudRate2Termios(&Posix_CommConfig, B9600);
  261. break;
  262. case BAUD19200:
  263. setBaudRate2Termios(&Posix_CommConfig, B19200);
  264. break;
  265. case BAUD38400:
  266. setBaudRate2Termios(&Posix_CommConfig, B38400);
  267. break;
  268. case BAUD57600:
  269. setBaudRate2Termios(&Posix_CommConfig, B57600);
  270. break;
  271. #ifdef B76800
  272. case BAUD76800:
  273. setBaudRate2Termios(&Posix_CommConfig, B76800);
  274. break;
  275. #endif
  276. case BAUD115200:
  277. setBaudRate2Termios(&Posix_CommConfig, B115200);
  278. break;
  279. #if defined(B230400) && defined(B4000000)
  280. case BAUD230400:
  281. setBaudRate2Termios(&Posix_CommConfig, B230400);
  282. break;
  283. case BAUD460800:
  284. setBaudRate2Termios(&Posix_CommConfig, B460800);
  285. break;
  286. case BAUD500000:
  287. setBaudRate2Termios(&Posix_CommConfig, B500000);
  288. break;
  289. case BAUD576000:
  290. setBaudRate2Termios(&Posix_CommConfig, B576000);
  291. break;
  292. case BAUD921600:
  293. setBaudRate2Termios(&Posix_CommConfig, B921600);
  294. break;
  295. case BAUD1000000:
  296. setBaudRate2Termios(&Posix_CommConfig, B1000000);
  297. break;
  298. case BAUD1152000:
  299. setBaudRate2Termios(&Posix_CommConfig, B1152000);
  300. break;
  301. case BAUD1500000:
  302. setBaudRate2Termios(&Posix_CommConfig, B1500000);
  303. break;
  304. case BAUD2000000:
  305. setBaudRate2Termios(&Posix_CommConfig, B2000000);
  306. break;
  307. case BAUD2500000:
  308. setBaudRate2Termios(&Posix_CommConfig, B2500000);
  309. break;
  310. case BAUD3000000:
  311. setBaudRate2Termios(&Posix_CommConfig, B3000000);
  312. break;
  313. case BAUD3500000:
  314. setBaudRate2Termios(&Posix_CommConfig, B3500000);
  315. break;
  316. case BAUD4000000:
  317. setBaudRate2Termios(&Posix_CommConfig, B4000000);
  318. break;
  319. #endif
  320. }
  321. }
  322. if (settingsDirtyFlags & DFE_Parity) {
  323. switch (Settings.Parity) {
  324. case PAR_SPACE:
  325. /*space parity not directly supported - add an extra data bit to simulate it*/
  326. settingsDirtyFlags |= DFE_DataBits;
  327. break;
  328. case PAR_NONE:
  329. Posix_CommConfig.c_cflag &= (~PARENB);
  330. break;
  331. case PAR_EVEN:
  332. Posix_CommConfig.c_cflag &= (~PARODD);
  333. Posix_CommConfig.c_cflag |= PARENB;
  334. break;
  335. case PAR_ODD:
  336. Posix_CommConfig.c_cflag |= (PARENB|PARODD);
  337. break;
  338. }
  339. }
  340. /*must after Parity settings*/
  341. if (settingsDirtyFlags & DFE_DataBits) {
  342. if (Settings.Parity != PAR_SPACE) {
  343. Posix_CommConfig.c_cflag &= (~CSIZE);
  344. switch(Settings.DataBits) {
  345. case DATA_5:
  346. Posix_CommConfig.c_cflag |= CS5;
  347. break;
  348. case DATA_6:
  349. Posix_CommConfig.c_cflag |= CS6;
  350. break;
  351. case DATA_7:
  352. Posix_CommConfig.c_cflag |= CS7;
  353. break;
  354. case DATA_8:
  355. Posix_CommConfig.c_cflag |= CS8;
  356. break;
  357. }
  358. } else {
  359. /*space parity not directly supported - add an extra data bit to simulate it*/
  360. Posix_CommConfig.c_cflag &= ~(PARENB|CSIZE);
  361. switch(Settings.DataBits) {
  362. case DATA_5:
  363. Posix_CommConfig.c_cflag |= CS6;
  364. break;
  365. case DATA_6:
  366. Posix_CommConfig.c_cflag |= CS7;
  367. break;
  368. case DATA_7:
  369. Posix_CommConfig.c_cflag |= CS8;
  370. break;
  371. case DATA_8:
  372. /*this will never happen, put here to Suppress an warning*/
  373. break;
  374. }
  375. }
  376. }
  377. if (settingsDirtyFlags & DFE_StopBits) {
  378. switch (Settings.StopBits) {
  379. case STOP_1:
  380. Posix_CommConfig.c_cflag &= (~CSTOPB);
  381. break;
  382. case STOP_2:
  383. Posix_CommConfig.c_cflag |= CSTOPB;
  384. break;
  385. }
  386. }
  387. if (settingsDirtyFlags & DFE_Flow) {
  388. switch(Settings.FlowControl) {
  389. case FLOW_OFF:
  390. Posix_CommConfig.c_cflag &= (~CRTSCTS);
  391. Posix_CommConfig.c_iflag &= (~(IXON|IXOFF|IXANY));
  392. break;
  393. case FLOW_XONXOFF:
  394. /*software (XON/XOFF) flow control*/
  395. Posix_CommConfig.c_cflag &= (~CRTSCTS);
  396. Posix_CommConfig.c_iflag |= (IXON|IXOFF|IXANY);
  397. break;
  398. case FLOW_HARDWARE:
  399. Posix_CommConfig.c_cflag |= CRTSCTS;
  400. Posix_CommConfig.c_iflag &= (~(IXON|IXOFF|IXANY));
  401. break;
  402. }
  403. }
  404. /*if any thing in Posix_CommConfig changed, flush*/
  405. if (settingsDirtyFlags & DFE_Settings_Mask)
  406. ::tcsetattr(fd, TCSAFLUSH, &Posix_CommConfig);
  407. if (settingsDirtyFlags & DFE_TimeOut) {
  408. int millisec = Settings.Timeout_Millisec;
  409. if (millisec == -1) {
  410. ::fcntl(fd, F_SETFL, O_NDELAY);
  411. }
  412. else {
  413. //O_SYNC should enable blocking ::write()
  414. //however this seems not working on Linux 2.6.21 (works on OpenBSD 4.2)
  415. ::fcntl(fd, F_SETFL, O_SYNC);
  416. }
  417. ::tcgetattr(fd, & Posix_CommConfig);
  418. Posix_CommConfig.c_cc[VTIME] = millisec/100;
  419. ::tcsetattr(fd, TCSAFLUSH, & Posix_CommConfig);
  420. }
  421. settingsDirtyFlags = 0;
  422. }