serial_port_base.ipp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. //
  2. // serial_port_base.ipp
  3. // ~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2008 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. // Copyright (c) 2008 Rep Invariant Systems, Inc. (info@repinvariant.com)
  7. //
  8. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  9. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  10. //
  11. #ifndef BOOST_ASIO_SERIAL_PORT_BASE_IPP
  12. #define BOOST_ASIO_SERIAL_PORT_BASE_IPP
  13. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  14. # pragma once
  15. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  16. #include <boost/asio/detail/push_options.hpp>
  17. #include <boost/asio/detail/push_options.hpp>
  18. #include <boost/throw_exception.hpp>
  19. #include <boost/asio/detail/pop_options.hpp>
  20. namespace boost {
  21. namespace asio {
  22. inline serial_port_base::baud_rate::baud_rate(unsigned int rate)
  23. : value_(rate)
  24. {
  25. }
  26. inline unsigned int serial_port_base::baud_rate::value() const
  27. {
  28. return value_;
  29. }
  30. inline boost::system::error_code serial_port_base::baud_rate::store(
  31. BOOST_ASIO_OPTION_STORAGE& storage, boost::system::error_code& ec) const
  32. {
  33. #if defined(BOOST_WINDOWS) || defined(__CYGWIN__)
  34. storage.BaudRate = value_;
  35. #else
  36. speed_t baud;
  37. switch (value_)
  38. {
  39. // Do POSIX-specified rates first.
  40. case 0: baud = B0; break;
  41. case 50: baud = B50; break;
  42. case 75: baud = B75; break;
  43. case 110: baud = B110; break;
  44. case 134: baud = B134; break;
  45. case 150: baud = B150; break;
  46. case 200: baud = B200; break;
  47. case 300: baud = B300; break;
  48. case 600: baud = B600; break;
  49. case 1200: baud = B1200; break;
  50. case 1800: baud = B1800; break;
  51. case 2400: baud = B2400; break;
  52. case 4800: baud = B4800; break;
  53. case 9600: baud = B9600; break;
  54. case 19200: baud = B19200; break;
  55. case 38400: baud = B38400; break;
  56. // And now the extended ones conditionally.
  57. # ifdef B7200
  58. case 7200: baud = B7200; break;
  59. # endif
  60. # ifdef B14400
  61. case 14400: baud = B14400; break;
  62. # endif
  63. # ifdef B57600
  64. case 57600: baud = B57600; break;
  65. # endif
  66. # ifdef B115200
  67. case 115200: baud = B115200; break;
  68. # endif
  69. # ifdef B230400
  70. case 230400: baud = B230400; break;
  71. # endif
  72. # ifdef B460800
  73. case 460800: baud = B460800; break;
  74. # endif
  75. # ifdef B500000
  76. case 500000: baud = B500000; break;
  77. # endif
  78. # ifdef B576000
  79. case 576000: baud = B576000; break;
  80. # endif
  81. # ifdef B921600
  82. case 921600: baud = B921600; break;
  83. # endif
  84. # ifdef B1000000
  85. case 1000000: baud = B1000000; break;
  86. # endif
  87. # ifdef B1152000
  88. case 1152000: baud = B1152000; break;
  89. # endif
  90. # ifdef B2000000
  91. case 2000000: baud = B2000000; break;
  92. # endif
  93. # ifdef B3000000
  94. case 3000000: baud = B3000000; break;
  95. # endif
  96. # ifdef B3500000
  97. case 3500000: baud = B3500000; break;
  98. # endif
  99. # ifdef B4000000
  100. case 4000000: baud = B4000000; break;
  101. # endif
  102. default:
  103. baud = B0;
  104. ec = boost::asio::error::invalid_argument;
  105. return ec;
  106. }
  107. # if defined(_BSD_SOURCE)
  108. ::cfsetspeed(&storage, baud);
  109. # else
  110. ::cfsetispeed(&storage, baud);
  111. ::cfsetospeed(&storage, baud);
  112. # endif
  113. #endif
  114. ec = boost::system::error_code();
  115. return ec;
  116. }
  117. inline boost::system::error_code serial_port_base::baud_rate::load(
  118. const BOOST_ASIO_OPTION_STORAGE& storage, boost::system::error_code& ec)
  119. {
  120. #if defined(BOOST_WINDOWS) || defined(__CYGWIN__)
  121. value_ = storage.BaudRate;
  122. #else
  123. speed_t baud = ::cfgetospeed(&storage);
  124. switch (baud)
  125. {
  126. // First do those specified by POSIX.
  127. case B0: value_ = 0; break;
  128. case B50: value_ = 50; break;
  129. case B75: value_ = 75; break;
  130. case B110: value_ = 110; break;
  131. case B134: value_ = 134; break;
  132. case B150: value_ = 150; break;
  133. case B200: value_ = 200; break;
  134. case B300: value_ = 300; break;
  135. case B600: value_ = 600; break;
  136. case B1200: value_ = 1200; break;
  137. case B1800: value_ = 1800; break;
  138. case B2400: value_ = 2400; break;
  139. case B4800: value_ = 4800; break;
  140. case B9600: value_ = 9600; break;
  141. case B19200: value_ = 19200; break;
  142. case B38400: value_ = 38400; break;
  143. // Now conditionally handle a bunch of extended rates.
  144. # ifdef B7200
  145. case B7200: value_ = 7200; break;
  146. # endif
  147. # ifdef B14400
  148. case B14400: value_ = 14400; break;
  149. # endif
  150. # ifdef B57600
  151. case B57600: value_ = 57600; break;
  152. # endif
  153. # ifdef B115200
  154. case B115200: value_ = 115200; break;
  155. # endif
  156. # ifdef B230400
  157. case B230400: value_ = 230400; break;
  158. # endif
  159. # ifdef B460800
  160. case B460800: value_ = 460800; break;
  161. # endif
  162. # ifdef B500000
  163. case B500000: value_ = 500000; break;
  164. # endif
  165. # ifdef B576000
  166. case B576000: value_ = 576000; break;
  167. # endif
  168. # ifdef B921600
  169. case B921600: value_ = 921600; break;
  170. # endif
  171. # ifdef B1000000
  172. case B1000000: value_ = 1000000; break;
  173. # endif
  174. # ifdef B1152000
  175. case B1152000: value_ = 1152000; break;
  176. # endif
  177. # ifdef B2000000
  178. case B2000000: value_ = 2000000; break;
  179. # endif
  180. # ifdef B3000000
  181. case B3000000: value_ = 3000000; break;
  182. # endif
  183. # ifdef B3500000
  184. case B3500000: value_ = 3500000; break;
  185. # endif
  186. # ifdef B4000000
  187. case B4000000: value_ = 4000000; break;
  188. # endif
  189. default:
  190. value_ = 0;
  191. ec = boost::asio::error::invalid_argument;
  192. return ec;
  193. }
  194. #endif
  195. ec = boost::system::error_code();
  196. return ec;
  197. }
  198. inline serial_port_base::flow_control::flow_control(
  199. serial_port_base::flow_control::type t)
  200. : value_(t)
  201. {
  202. if (t != none && t != software && t != hardware)
  203. {
  204. std::out_of_range ex("invalid flow_control value");
  205. boost::throw_exception(ex);
  206. }
  207. }
  208. inline serial_port_base::flow_control::type
  209. serial_port_base::flow_control::value() const
  210. {
  211. return value_;
  212. }
  213. inline boost::system::error_code serial_port_base::flow_control::store(
  214. BOOST_ASIO_OPTION_STORAGE& storage, boost::system::error_code& ec) const
  215. {
  216. #if defined(BOOST_WINDOWS) || defined(__CYGWIN__)
  217. storage.fOutxCtsFlow = FALSE;
  218. storage.fOutxDsrFlow = FALSE;
  219. storage.fTXContinueOnXoff = TRUE;
  220. storage.fDtrControl = DTR_CONTROL_ENABLE;
  221. storage.fDsrSensitivity = FALSE;
  222. storage.fOutX = FALSE;
  223. storage.fInX = FALSE;
  224. storage.fRtsControl = RTS_CONTROL_ENABLE;
  225. switch (value_)
  226. {
  227. case none:
  228. break;
  229. case software:
  230. storage.fOutX = TRUE;
  231. storage.fInX = TRUE;
  232. break;
  233. case hardware:
  234. storage.fOutxCtsFlow = TRUE;
  235. storage.fRtsControl = RTS_CONTROL_HANDSHAKE;
  236. break;
  237. default:
  238. break;
  239. }
  240. #else
  241. switch (value_)
  242. {
  243. case none:
  244. storage.c_iflag &= ~(IXOFF | IXON);
  245. # if defined(_BSD_SOURCE)
  246. storage.c_cflag &= ~CRTSCTS;
  247. # endif
  248. break;
  249. case software:
  250. storage.c_iflag |= IXOFF | IXON;
  251. # if defined(_BSD_SOURCE)
  252. storage.c_cflag &= ~CRTSCTS;
  253. # endif
  254. break;
  255. case hardware:
  256. # if defined(_BSD_SOURCE)
  257. storage.c_iflag &= ~(IXOFF | IXON);
  258. storage.c_cflag |= CRTSCTS;
  259. break;
  260. # else
  261. ec = boost::asio::error::operation_not_supported;
  262. return ec;
  263. # endif
  264. default:
  265. break;
  266. }
  267. #endif
  268. ec = boost::system::error_code();
  269. return ec;
  270. }
  271. inline boost::system::error_code serial_port_base::flow_control::load(
  272. const BOOST_ASIO_OPTION_STORAGE& storage, boost::system::error_code& ec)
  273. {
  274. #if defined(BOOST_WINDOWS) || defined(__CYGWIN__)
  275. if (storage.fOutX && storage.fInX)
  276. {
  277. value_ = software;
  278. }
  279. else if (storage.fOutxCtsFlow && storage.fRtsControl == RTS_CONTROL_HANDSHAKE)
  280. {
  281. value_ = hardware;
  282. }
  283. else
  284. {
  285. value_ = none;
  286. }
  287. #else
  288. if (storage.c_iflag & (IXOFF | IXON))
  289. {
  290. value_ = software;
  291. }
  292. # if defined(_BSD_SOURCE)
  293. else if (storage.c_cflag & CRTSCTS)
  294. {
  295. value_ = hardware;
  296. }
  297. # endif
  298. else
  299. {
  300. value_ = none;
  301. }
  302. #endif
  303. ec = boost::system::error_code();
  304. return ec;
  305. }
  306. inline serial_port_base::parity::parity(serial_port_base::parity::type t)
  307. : value_(t)
  308. {
  309. if (t != none && t != odd && t != even)
  310. {
  311. std::out_of_range ex("invalid parity value");
  312. boost::throw_exception(ex);
  313. }
  314. }
  315. inline serial_port_base::parity::type serial_port_base::parity::value() const
  316. {
  317. return value_;
  318. }
  319. inline boost::system::error_code serial_port_base::parity::store(
  320. BOOST_ASIO_OPTION_STORAGE& storage, boost::system::error_code& ec) const
  321. {
  322. #if defined(BOOST_WINDOWS) || defined(__CYGWIN__)
  323. switch (value_)
  324. {
  325. case none:
  326. storage.fParity = FALSE;
  327. storage.Parity = NOPARITY;
  328. break;
  329. case odd:
  330. storage.fParity = TRUE;
  331. storage.Parity = ODDPARITY;
  332. break;
  333. case even:
  334. storage.fParity = TRUE;
  335. storage.Parity = EVENPARITY;
  336. break;
  337. default:
  338. break;
  339. }
  340. #else
  341. switch (value_)
  342. {
  343. case none:
  344. storage.c_iflag |= IGNPAR;
  345. storage.c_cflag &= ~(PARENB | PARODD);
  346. break;
  347. case even:
  348. storage.c_iflag &= ~(IGNPAR | PARMRK);
  349. storage.c_iflag |= INPCK;
  350. storage.c_cflag |= PARENB;
  351. storage.c_cflag &= ~PARODD;
  352. break;
  353. case odd:
  354. storage.c_iflag &= ~(IGNPAR | PARMRK);
  355. storage.c_iflag |= INPCK;
  356. storage.c_cflag |= (PARENB | PARODD);
  357. break;
  358. default:
  359. break;
  360. }
  361. #endif
  362. ec = boost::system::error_code();
  363. return ec;
  364. }
  365. inline boost::system::error_code serial_port_base::parity::load(
  366. const BOOST_ASIO_OPTION_STORAGE& storage, boost::system::error_code& ec)
  367. {
  368. #if defined(BOOST_WINDOWS) || defined(__CYGWIN__)
  369. if (storage.Parity == EVENPARITY)
  370. {
  371. value_ = even;
  372. }
  373. else if (storage.Parity == ODDPARITY)
  374. {
  375. value_ = odd;
  376. }
  377. else
  378. {
  379. value_ = none;
  380. }
  381. #else
  382. if (storage.c_cflag & PARENB)
  383. {
  384. if (storage.c_cflag & PARODD)
  385. {
  386. value_ = odd;
  387. }
  388. else
  389. {
  390. value_ = even;
  391. }
  392. }
  393. else
  394. {
  395. value_ = none;
  396. }
  397. #endif
  398. ec = boost::system::error_code();
  399. return ec;
  400. }
  401. inline serial_port_base::stop_bits::stop_bits(
  402. serial_port_base::stop_bits::type t)
  403. : value_(t)
  404. {
  405. if (t != one && t != onepointfive && t != two)
  406. {
  407. std::out_of_range ex("invalid stop_bits value");
  408. boost::throw_exception(ex);
  409. }
  410. }
  411. inline serial_port_base::stop_bits::type
  412. serial_port_base::stop_bits::value() const
  413. {
  414. return value_;
  415. }
  416. inline boost::system::error_code serial_port_base::stop_bits::store(
  417. BOOST_ASIO_OPTION_STORAGE& storage, boost::system::error_code& ec) const
  418. {
  419. #if defined(BOOST_WINDOWS) || defined(__CYGWIN__)
  420. switch (value_)
  421. {
  422. case one:
  423. storage.StopBits = ONESTOPBIT;
  424. break;
  425. case onepointfive:
  426. storage.StopBits = ONE5STOPBITS;
  427. break;
  428. case two:
  429. storage.StopBits = TWOSTOPBITS;
  430. break;
  431. default:
  432. break;
  433. }
  434. #else
  435. switch (value_)
  436. {
  437. case one:
  438. storage.c_cflag &= ~CSTOPB;
  439. break;
  440. case two:
  441. storage.c_cflag |= CSTOPB;
  442. break;
  443. default:
  444. ec = boost::asio::error::operation_not_supported;
  445. return ec;
  446. }
  447. #endif
  448. ec = boost::system::error_code();
  449. return ec;
  450. }
  451. inline boost::system::error_code serial_port_base::stop_bits::load(
  452. const BOOST_ASIO_OPTION_STORAGE& storage, boost::system::error_code& ec)
  453. {
  454. #if defined(BOOST_WINDOWS) || defined(__CYGWIN__)
  455. if (storage.StopBits == ONESTOPBIT)
  456. {
  457. value_ = one;
  458. }
  459. else if (storage.StopBits == ONE5STOPBITS)
  460. {
  461. value_ = onepointfive;
  462. }
  463. else if (storage.StopBits == TWOSTOPBITS)
  464. {
  465. value_ = two;
  466. }
  467. else
  468. {
  469. value_ = one;
  470. }
  471. #else
  472. value_ = (storage.c_cflag & CSTOPB) ? two : one;
  473. #endif
  474. ec = boost::system::error_code();
  475. return ec;
  476. }
  477. inline serial_port_base::character_size::character_size(unsigned int t)
  478. : value_(t)
  479. {
  480. if (t < 5 || t > 8)
  481. {
  482. std::out_of_range ex("invalid character_size value");
  483. boost::throw_exception(ex);
  484. }
  485. }
  486. inline unsigned int serial_port_base::character_size::value() const
  487. {
  488. return value_;
  489. }
  490. inline boost::system::error_code serial_port_base::character_size::store(
  491. BOOST_ASIO_OPTION_STORAGE& storage, boost::system::error_code& ec) const
  492. {
  493. #if defined(BOOST_WINDOWS) || defined(__CYGWIN__)
  494. storage.ByteSize = value_;
  495. #else
  496. storage.c_cflag &= ~CSIZE;
  497. switch (value_)
  498. {
  499. case 5: storage.c_cflag |= CS5; break;
  500. case 6: storage.c_cflag |= CS6; break;
  501. case 7: storage.c_cflag |= CS7; break;
  502. case 8: storage.c_cflag |= CS8; break;
  503. default: break;
  504. }
  505. #endif
  506. ec = boost::system::error_code();
  507. return ec;
  508. }
  509. inline boost::system::error_code serial_port_base::character_size::load(
  510. const BOOST_ASIO_OPTION_STORAGE& storage, boost::system::error_code& ec)
  511. {
  512. #if defined(BOOST_WINDOWS) || defined(__CYGWIN__)
  513. value_ = storage.ByteSize;
  514. #else
  515. if ((storage.c_cflag & CSIZE) == CS5) { value_ = 5; }
  516. else if ((storage.c_cflag & CSIZE) == CS6) { value_ = 6; }
  517. else if ((storage.c_cflag & CSIZE) == CS7) { value_ = 7; }
  518. else if ((storage.c_cflag & CSIZE) == CS8) { value_ = 8; }
  519. else
  520. {
  521. // Hmmm, use 8 for now.
  522. value_ = 8;
  523. }
  524. #endif
  525. ec = boost::system::error_code();
  526. return ec;
  527. }
  528. } // namespace asio
  529. } // namespace boost
  530. #include <boost/asio/detail/pop_options.hpp>
  531. #endif // BOOST_ASIO_SERIAL_PORT_BASE_IPP