serial.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  1. /*!
  2. * \file serial/serial.h
  3. * \author William Woodall <wjwwood@gmail.com>
  4. * \author John Harrison <ash.gti@gmail.com>
  5. * \version 0.1
  6. *
  7. * \section LICENSE
  8. *
  9. * The MIT License
  10. *
  11. * Copyright (c) 2012 William Woodall
  12. *
  13. * Permission is hereby granted, free of charge, to any person obtaining a
  14. * copy of this software and associated documentation files (the "Software"),
  15. * to deal in the Software without restriction, including without limitation
  16. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  17. * and/or sell copies of the Software, and to permit persons to whom the
  18. * Software is furnished to do so, subject to the following conditions:
  19. *
  20. * The above copyright notice and this permission notice shall be included in
  21. * all copies or substantial portions of the Software.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  24. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  26. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  27. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  28. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  29. * DEALINGS IN THE SOFTWARE.
  30. *
  31. * \section DESCRIPTION
  32. *
  33. * This provides a cross platform interface for interacting with Serial Ports.
  34. */
  35. #ifndef SERIAL_H
  36. #define SERIAL_H
  37. #include <limits>
  38. #include <vector>
  39. #include <string>
  40. #include <cstring>
  41. #include <sstream>
  42. #include <exception>
  43. #include <stdexcept>
  44. #define THROW(exceptionClass, message) throw exceptionClass(__FILE__, \
  45. __LINE__, (message) )
  46. namespace serial {
  47. /*!
  48. * Enumeration defines the possible bytesizes for the serial port.
  49. */
  50. typedef enum {
  51. fivebits = 5,
  52. sixbits = 6,
  53. sevenbits = 7,
  54. eightbits = 8
  55. } bytesize_t;
  56. /*!
  57. * Enumeration defines the possible parity types for the serial port.
  58. */
  59. typedef enum {
  60. parity_none = 0,
  61. parity_odd = 1,
  62. parity_even = 2,
  63. parity_mark = 3,
  64. parity_space = 4
  65. } parity_t;
  66. /*!
  67. * Enumeration defines the possible stopbit types for the serial port.
  68. */
  69. typedef enum {
  70. stopbits_one = 1,
  71. stopbits_two = 2,
  72. stopbits_one_point_five
  73. } stopbits_t;
  74. /*!
  75. * Enumeration defines the possible flowcontrol types for the serial port.
  76. */
  77. typedef enum {
  78. flowcontrol_none = 0,
  79. flowcontrol_software,
  80. flowcontrol_hardware
  81. } flowcontrol_t;
  82. /*!
  83. * Structure for setting the timeout of the serial port, times are
  84. * in milliseconds.
  85. *
  86. * In order to disable the interbyte timeout, set it to Timeout::max().
  87. */
  88. struct Timeout {
  89. #ifdef max
  90. # undef max
  91. #endif
  92. static uint32_t max() {return std::numeric_limits<uint32_t>::max();}
  93. /*!
  94. * Convenience function to generate Timeout structs using a
  95. * single absolute timeout.
  96. *
  97. * \param timeout A long that defines the time in milliseconds until a
  98. * timeout occurs after a call to read or write is made.
  99. *
  100. * \return Timeout struct that represents this simple timeout provided.
  101. */
  102. static Timeout simpleTimeout(uint32_t timeout) {
  103. return Timeout(max(), timeout, 0, timeout, 0);
  104. }
  105. /*! Number of milliseconds between bytes received to timeout on. */
  106. uint32_t inter_byte_timeout;
  107. /*! A constant number of milliseconds to wait after calling read. */
  108. uint32_t read_timeout_constant;
  109. /*! A multiplier against the number of requested bytes to wait after
  110. * calling read.
  111. */
  112. uint32_t read_timeout_multiplier;
  113. /*! A constant number of milliseconds to wait after calling write. */
  114. uint32_t write_timeout_constant;
  115. /*! A multiplier against the number of requested bytes to wait after
  116. * calling write.
  117. */
  118. uint32_t write_timeout_multiplier;
  119. explicit Timeout (uint32_t inter_byte_timeout_=0,
  120. uint32_t read_timeout_constant_=0,
  121. uint32_t read_timeout_multiplier_=0,
  122. uint32_t write_timeout_constant_=0,
  123. uint32_t write_timeout_multiplier_=0)
  124. : inter_byte_timeout(inter_byte_timeout_),
  125. read_timeout_constant(read_timeout_constant_),
  126. read_timeout_multiplier(read_timeout_multiplier_),
  127. write_timeout_constant(write_timeout_constant_),
  128. write_timeout_multiplier(write_timeout_multiplier_)
  129. {}
  130. };
  131. /*!
  132. * Class that provides a portable serial port interface.
  133. */
  134. class Serial {
  135. public:
  136. /*!
  137. * Creates a Serial object and opens the port if a port is specified,
  138. * otherwise it remains closed until serial::Serial::open is called.
  139. *
  140. * \param port A std::string containing the address of the serial port,
  141. * which would be something like 'COM1' on Windows and '/dev/ttyS0'
  142. * on Linux.
  143. *
  144. * \param baudrate An unsigned 32-bit integer that represents the baudrate
  145. *
  146. * \param timeout A serial::Timeout struct that defines the timeout
  147. * conditions for the serial port. \see serial::Timeout
  148. *
  149. * \param bytesize Size of each byte in the serial transmission of data,
  150. * default is eightbits, possible values are: fivebits, sixbits, sevenbits,
  151. * eightbits
  152. *
  153. * \param parity Method of parity, default is parity_none, possible values
  154. * are: parity_none, parity_odd, parity_even
  155. *
  156. * \param stopbits Number of stop bits used, default is stopbits_one,
  157. * possible values are: stopbits_one, stopbits_one_point_five, stopbits_two
  158. *
  159. * \param flowcontrol Type of flowcontrol used, default is
  160. * flowcontrol_none, possible values are: flowcontrol_none,
  161. * flowcontrol_software, flowcontrol_hardware
  162. *
  163. * \throw serial::PortNotOpenedException
  164. * \throw serial::IOException
  165. * \throw std::invalid_argument
  166. */
  167. Serial (const std::string &port = "",
  168. uint32_t baudrate = 9600,
  169. Timeout timeout = Timeout(),
  170. bytesize_t bytesize = eightbits,
  171. parity_t parity = parity_none,
  172. stopbits_t stopbits = stopbits_one,
  173. flowcontrol_t flowcontrol = flowcontrol_none);
  174. /*! Destructor */
  175. virtual ~Serial ();
  176. /*!
  177. * Opens the serial port as long as the port is set and the port isn't
  178. * already open.
  179. *
  180. * If the port is provided to the constructor then an explicit call to open
  181. * is not needed.
  182. *
  183. * \see Serial::Serial
  184. *
  185. * \throw std::invalid_argument
  186. * \throw serial::SerialException
  187. * \throw serial::IOException
  188. */
  189. void
  190. open ();
  191. /*! Gets the open status of the serial port.
  192. *
  193. * \return Returns true if the port is open, false otherwise.
  194. */
  195. bool
  196. isOpen () const;
  197. /*! Closes the serial port. */
  198. void
  199. close ();
  200. /*! Return the number of characters in the buffer. */
  201. size_t
  202. available ();
  203. /*! Block until there is serial data to read or read_timeout_constant
  204. * number of milliseconds have elapsed. The return value is true when
  205. * the function exits with the port in a readable state, false otherwise
  206. * (due to timeout or select interruption). */
  207. bool
  208. waitReadable ();
  209. /*! Block for a period of time corresponding to the transmission time of
  210. * count characters at present serial settings. This may be used in con-
  211. * junction with waitReadable to read larger blocks of data from the
  212. * port. */
  213. void
  214. waitByteTimes (size_t count);
  215. /*! Read a given amount of bytes from the serial port into a given buffer.
  216. *
  217. * The read function will return in one of three cases:
  218. * * The number of requested bytes was read.
  219. * * In this case the number of bytes requested will match the size_t
  220. * returned by read.
  221. * * A timeout occurred, in this case the number of bytes read will not
  222. * match the amount requested, but no exception will be thrown. One of
  223. * two possible timeouts occurred:
  224. * * The inter byte timeout expired, this means that number of
  225. * milliseconds elapsed between receiving bytes from the serial port
  226. * exceeded the inter byte timeout.
  227. * * The total timeout expired, which is calculated by multiplying the
  228. * read timeout multiplier by the number of requested bytes and then
  229. * added to the read timeout constant. If that total number of
  230. * milliseconds elapses after the initial call to read a timeout will
  231. * occur.
  232. * * An exception occurred, in this case an actual exception will be thrown.
  233. *
  234. * \param buffer An uint8_t array of at least the requested size.
  235. * \param size A size_t defining how many bytes to be read.
  236. *
  237. * \return A size_t representing the number of bytes read as a result of the
  238. * call to read.
  239. *
  240. * \throw serial::PortNotOpenedException
  241. * \throw serial::SerialException
  242. */
  243. size_t
  244. read (uint8_t *buffer, size_t size);
  245. /*! Read a given amount of bytes from the serial port into a give buffer.
  246. *
  247. * \param buffer A reference to a std::vector of uint8_t.
  248. * \param size A size_t defining how many bytes to be read.
  249. *
  250. * \return A size_t representing the number of bytes read as a result of the
  251. * call to read.
  252. *
  253. * \throw serial::PortNotOpenedException
  254. * \throw serial::SerialException
  255. */
  256. size_t
  257. read (std::vector<uint8_t> &buffer, size_t size = 1);
  258. /*! Read a given amount of bytes from the serial port into a give buffer.
  259. *
  260. * \param buffer A reference to a std::string.
  261. * \param size A size_t defining how many bytes to be read.
  262. *
  263. * \return A size_t representing the number of bytes read as a result of the
  264. * call to read.
  265. *
  266. * \throw serial::PortNotOpenedException
  267. * \throw serial::SerialException
  268. */
  269. size_t
  270. read (std::string &buffer, size_t size = 1);
  271. /*! Read a given amount of bytes from the serial port and return a string
  272. * containing the data.
  273. *
  274. * \param size A size_t defining how many bytes to be read.
  275. *
  276. * \return A std::string containing the data read from the port.
  277. *
  278. * \throw serial::PortNotOpenedException
  279. * \throw serial::SerialException
  280. */
  281. std::string
  282. read (size_t size = 1);
  283. /*! Reads in a line or until a given delimiter has been processed.
  284. *
  285. * Reads from the serial port until a single line has been read.
  286. *
  287. * \param buffer A std::string reference used to store the data.
  288. * \param size A maximum length of a line, defaults to 65536 (2^16)
  289. * \param eol A string to match against for the EOL.
  290. *
  291. * \return A size_t representing the number of bytes read.
  292. *
  293. * \throw serial::PortNotOpenedException
  294. * \throw serial::SerialException
  295. */
  296. size_t
  297. readline (std::string &buffer, size_t size = 65536, std::string eol = "\n");
  298. /*! Reads in a line or until a given delimiter has been processed.
  299. *
  300. * Reads from the serial port until a single line has been read.
  301. *
  302. * \param size A maximum length of a line, defaults to 65536 (2^16)
  303. * \param eol A string to match against for the EOL.
  304. *
  305. * \return A std::string containing the line.
  306. *
  307. * \throw serial::PortNotOpenedException
  308. * \throw serial::SerialException
  309. */
  310. std::string
  311. readline (size_t size = 65536, std::string eol = "\n");
  312. /*! Reads in multiple lines until the serial port times out.
  313. *
  314. * This requires a timeout > 0 before it can be run. It will read until a
  315. * timeout occurs and return a list of strings.
  316. *
  317. * \param size A maximum length of combined lines, defaults to 65536 (2^16)
  318. *
  319. * \param eol A string to match against for the EOL.
  320. *
  321. * \return A vector<string> containing the lines.
  322. *
  323. * \throw serial::PortNotOpenedException
  324. * \throw serial::SerialException
  325. */
  326. std::vector<std::string>
  327. readlines (size_t size = 65536, std::string eol = "\n");
  328. /*! Write a string to the serial port.
  329. *
  330. * \param data A const reference containing the data to be written
  331. * to the serial port.
  332. *
  333. * \param size A size_t that indicates how many bytes should be written from
  334. * the given data buffer.
  335. *
  336. * \return A size_t representing the number of bytes actually written to
  337. * the serial port.
  338. *
  339. * \throw serial::PortNotOpenedException
  340. * \throw serial::SerialException
  341. * \throw serial::IOException
  342. */
  343. size_t
  344. write (const uint8_t *data, size_t size);
  345. /*! Write a string to the serial port.
  346. *
  347. * \param data A const reference containing the data to be written
  348. * to the serial port.
  349. *
  350. * \return A size_t representing the number of bytes actually written to
  351. * the serial port.
  352. *
  353. * \throw serial::PortNotOpenedException
  354. * \throw serial::SerialException
  355. * \throw serial::IOException
  356. */
  357. size_t
  358. write (const std::vector<uint8_t> &data);
  359. /*! Write a string to the serial port.
  360. *
  361. * \param data A const reference containing the data to be written
  362. * to the serial port.
  363. *
  364. * \return A size_t representing the number of bytes actually written to
  365. * the serial port.
  366. *
  367. * \throw serial::PortNotOpenedException
  368. * \throw serial::SerialException
  369. * \throw serial::IOException
  370. */
  371. size_t
  372. write (const std::string &data);
  373. /*! Sets the serial port identifier.
  374. *
  375. * \param port A const std::string reference containing the address of the
  376. * serial port, which would be something like 'COM1' on Windows and
  377. * '/dev/ttyS0' on Linux.
  378. *
  379. * \throw std::invalid_argument
  380. */
  381. void
  382. setPort (const std::string &port);
  383. /*! Gets the serial port identifier.
  384. *
  385. * \see Serial::setPort
  386. *
  387. * \throw std::invalid_argument
  388. */
  389. std::string
  390. getPort () const;
  391. /*! Sets the timeout for reads and writes using the Timeout struct.
  392. *
  393. * There are two timeout conditions described here:
  394. * * The inter byte timeout:
  395. * * The inter_byte_timeout component of serial::Timeout defines the
  396. * maximum amount of time, in milliseconds, between receiving bytes on
  397. * the serial port that can pass before a timeout occurs. Setting this
  398. * to zero will prevent inter byte timeouts from occurring.
  399. * * Total time timeout:
  400. * * The constant and multiplier component of this timeout condition,
  401. * for both read and write, are defined in serial::Timeout. This
  402. * timeout occurs if the total time since the read or write call was
  403. * made exceeds the specified time in milliseconds.
  404. * * The limit is defined by multiplying the multiplier component by the
  405. * number of requested bytes and adding that product to the constant
  406. * component. In this way if you want a read call, for example, to
  407. * timeout after exactly one second regardless of the number of bytes
  408. * you asked for then set the read_timeout_constant component of
  409. * serial::Timeout to 1000 and the read_timeout_multiplier to zero.
  410. * This timeout condition can be used in conjunction with the inter
  411. * byte timeout condition with out any problems, timeout will simply
  412. * occur when one of the two timeout conditions is met. This allows
  413. * users to have maximum control over the trade-off between
  414. * responsiveness and efficiency.
  415. *
  416. * Read and write functions will return in one of three cases. When the
  417. * reading or writing is complete, when a timeout occurs, or when an
  418. * exception occurs.
  419. *
  420. * \param timeout A serial::Timeout struct containing the inter byte
  421. * timeout, and the read and write timeout constants and multipliers.
  422. *
  423. * \see serial::Timeout
  424. */
  425. void
  426. setTimeout (Timeout &timeout);
  427. /*! Sets the timeout for reads and writes. */
  428. void
  429. setTimeout (uint32_t inter_byte_timeout, uint32_t read_timeout_constant,
  430. uint32_t read_timeout_multiplier, uint32_t write_timeout_constant,
  431. uint32_t write_timeout_multiplier)
  432. {
  433. Timeout timeout(inter_byte_timeout, read_timeout_constant,
  434. read_timeout_multiplier, write_timeout_constant,
  435. write_timeout_multiplier);
  436. return setTimeout(timeout);
  437. }
  438. /*! Gets the timeout for reads in seconds.
  439. *
  440. * \return A Timeout struct containing the inter_byte_timeout, and read
  441. * and write timeout constants and multipliers.
  442. *
  443. * \see Serial::setTimeout
  444. */
  445. Timeout
  446. getTimeout () const;
  447. /*! Sets the baudrate for the serial port.
  448. *
  449. * Possible baudrates depends on the system but some safe baudrates include:
  450. * 110, 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 38400, 56000,
  451. * 57600, 115200
  452. * Some other baudrates that are supported by some comports:
  453. * 128000, 153600, 230400, 256000, 460800, 921600
  454. *
  455. * \param baudrate An integer that sets the baud rate for the serial port.
  456. *
  457. * \throw std::invalid_argument
  458. */
  459. void
  460. setBaudrate (uint32_t baudrate);
  461. /*! Gets the baudrate for the serial port.
  462. *
  463. * \return An integer that sets the baud rate for the serial port.
  464. *
  465. * \see Serial::setBaudrate
  466. *
  467. * \throw std::invalid_argument
  468. */
  469. uint32_t
  470. getBaudrate () const;
  471. /*! Sets the bytesize for the serial port.
  472. *
  473. * \param bytesize Size of each byte in the serial transmission of data,
  474. * default is eightbits, possible values are: fivebits, sixbits, sevenbits,
  475. * eightbits
  476. *
  477. * \throw std::invalid_argument
  478. */
  479. void
  480. setBytesize (bytesize_t bytesize);
  481. /*! Gets the bytesize for the serial port.
  482. *
  483. * \see Serial::setBytesize
  484. *
  485. * \throw std::invalid_argument
  486. */
  487. bytesize_t
  488. getBytesize () const;
  489. /*! Sets the parity for the serial port.
  490. *
  491. * \param parity Method of parity, default is parity_none, possible values
  492. * are: parity_none, parity_odd, parity_even
  493. *
  494. * \throw std::invalid_argument
  495. */
  496. void
  497. setParity (parity_t parity);
  498. /*! Gets the parity for the serial port.
  499. *
  500. * \see Serial::setParity
  501. *
  502. * \throw std::invalid_argument
  503. */
  504. parity_t
  505. getParity () const;
  506. /*! Sets the stopbits for the serial port.
  507. *
  508. * \param stopbits Number of stop bits used, default is stopbits_one,
  509. * possible values are: stopbits_one, stopbits_one_point_five, stopbits_two
  510. *
  511. * \throw std::invalid_argument
  512. */
  513. void
  514. setStopbits (stopbits_t stopbits);
  515. /*! Gets the stopbits for the serial port.
  516. *
  517. * \see Serial::setStopbits
  518. *
  519. * \throw std::invalid_argument
  520. */
  521. stopbits_t
  522. getStopbits () const;
  523. /*! Sets the flow control for the serial port.
  524. *
  525. * \param flowcontrol Type of flowcontrol used, default is flowcontrol_none,
  526. * possible values are: flowcontrol_none, flowcontrol_software,
  527. * flowcontrol_hardware
  528. *
  529. * \throw std::invalid_argument
  530. */
  531. void
  532. setFlowcontrol (flowcontrol_t flowcontrol);
  533. /*! Gets the flow control for the serial port.
  534. *
  535. * \see Serial::setFlowcontrol
  536. *
  537. * \throw std::invalid_argument
  538. */
  539. flowcontrol_t
  540. getFlowcontrol () const;
  541. /*! Flush the input and output buffers */
  542. void
  543. flush ();
  544. /*! Flush only the input buffer */
  545. void
  546. flushInput ();
  547. /*! Flush only the output buffer */
  548. void
  549. flushOutput ();
  550. /*! Sends the RS-232 break signal. See tcsendbreak(3). */
  551. void
  552. sendBreak (int duration);
  553. /*! Set the break condition to a given level. Defaults to true. */
  554. void
  555. setBreak (bool level = true);
  556. /*! Set the RTS handshaking line to the given level. Defaults to true. */
  557. void
  558. setRTS (bool level = true);
  559. /*! Set the DTR handshaking line to the given level. Defaults to true. */
  560. void
  561. setDTR (bool level = true);
  562. /*!
  563. * Blocks until CTS, DSR, RI, CD changes or something interrupts it.
  564. *
  565. * Can throw an exception if an error occurs while waiting.
  566. * You can check the status of CTS, DSR, RI, and CD once this returns.
  567. * Uses TIOCMIWAIT via ioctl if available (mostly only on Linux) with a
  568. * resolution of less than +-1ms and as good as +-0.2ms. Otherwise a
  569. * polling method is used which can give +-2ms.
  570. *
  571. * \return Returns true if one of the lines changed, false if something else
  572. * occurred.
  573. *
  574. * \throw SerialException
  575. */
  576. bool
  577. waitForChange ();
  578. /*! Returns the current status of the CTS line. */
  579. bool
  580. getCTS ();
  581. /*! Returns the current status of the DSR line. */
  582. bool
  583. getDSR ();
  584. /*! Returns the current status of the RI line. */
  585. bool
  586. getRI ();
  587. /*! Returns the current status of the CD line. */
  588. bool
  589. getCD ();
  590. private:
  591. // Disable copy constructors
  592. Serial(const Serial&);
  593. Serial& operator=(const Serial&);
  594. // Pimpl idiom, d_pointer
  595. class SerialImpl;
  596. SerialImpl *pimpl_;
  597. // Scoped Lock Classes
  598. class ScopedReadLock;
  599. class ScopedWriteLock;
  600. // Read common function
  601. size_t
  602. read_ (uint8_t *buffer, size_t size);
  603. // Write common function
  604. size_t
  605. write_ (const uint8_t *data, size_t length);
  606. };
  607. class SerialException : public std::exception
  608. {
  609. // Disable copy constructors
  610. SerialException& operator=(const SerialException&);
  611. std::string e_what_;
  612. public:
  613. SerialException (const char *description) {
  614. std::stringstream ss;
  615. ss << "SerialException " << description << " failed.";
  616. e_what_ = ss.str();
  617. }
  618. SerialException (const SerialException& other) : e_what_(other.e_what_) {}
  619. virtual ~SerialException() throw() {}
  620. virtual const char* what () const throw () {
  621. return e_what_.c_str();
  622. }
  623. };
  624. class IOException : public std::exception
  625. {
  626. // Disable copy constructors
  627. IOException& operator=(const IOException&);
  628. std::string file_;
  629. int line_;
  630. std::string e_what_;
  631. int errno_;
  632. public:
  633. explicit IOException (std::string file, int line, int errnum)
  634. : file_(file), line_(line), errno_(errnum) {
  635. std::stringstream ss;
  636. #if defined(_WIN32) && !defined(__MINGW32__)
  637. char error_str [1024];
  638. strerror_s(error_str, 1024, errnum);
  639. #else
  640. char * error_str = strerror(errnum);
  641. #endif
  642. ss << "IO Exception (" << errno_ << "): " << error_str;
  643. ss << ", file " << file_ << ", line " << line_ << ".";
  644. e_what_ = ss.str();
  645. }
  646. explicit IOException (std::string file, int line, const char * description)
  647. : file_(file), line_(line), errno_(0) {
  648. std::stringstream ss;
  649. ss << "IO Exception: " << description;
  650. ss << ", file " << file_ << ", line " << line_ << ".";
  651. e_what_ = ss.str();
  652. }
  653. virtual ~IOException() throw() {}
  654. IOException (const IOException& other) : line_(other.line_), e_what_(other.e_what_), errno_(other.errno_) {}
  655. int getErrorNumber () { return errno_; }
  656. virtual const char* what () const throw () {
  657. return e_what_.c_str();
  658. }
  659. };
  660. class PortNotOpenedException : public std::exception
  661. {
  662. // Disable copy constructors
  663. const PortNotOpenedException& operator=(PortNotOpenedException);
  664. std::string e_what_;
  665. public:
  666. PortNotOpenedException (const char * description) {
  667. std::stringstream ss;
  668. ss << "PortNotOpenedException " << description << " failed.";
  669. e_what_ = ss.str();
  670. }
  671. PortNotOpenedException (const PortNotOpenedException& other) : e_what_(other.e_what_) {}
  672. virtual ~PortNotOpenedException() throw() {}
  673. virtual const char* what () const throw () {
  674. return e_what_.c_str();
  675. }
  676. };
  677. /*!
  678. * Structure that describes a serial device.
  679. */
  680. struct PortInfo {
  681. /*! Address of the serial port (this can be passed to the constructor of Serial). */
  682. std::string port;
  683. /*! Human readable description of serial device if available. */
  684. std::string description;
  685. /*! Hardware ID (e.g. VID:PID of USB serial devices) or "n/a" if not available. */
  686. std::string hardware_id;
  687. };
  688. /* Lists the serial ports available on the system
  689. *
  690. * Returns a vector of available serial ports, each represented
  691. * by a serial::PortInfo data structure:
  692. *
  693. * \return vector of serial::PortInfo.
  694. */
  695. std::vector<PortInfo>
  696. list_ports();
  697. } // namespace serial
  698. #endif