serial.h 23 KB

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