Typica is a free program for professional coffee roasters. https://typica.us
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

qextserialport_unix.cpp 14KB

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