1. /*
  2. * File: BufferedAsyncSerial.h
  3. * Author: Terraneo Federico
  4. * Distributed under the Boost Software License, Version 1.0.
  5. * Created on January 6, 2011, 3:31 PM
  6. */
  7. #include "AsyncSerial.h"
  8. #ifndef BUFFEREDASYNCSERIAL_H
  9. #define BUFFEREDASYNCSERIAL_H
  10. class BufferedAsyncSerial: public AsyncSerial
  11. {
  12. public:
  13. BufferedAsyncSerial();
  14. /**
  15. * Opens a serial device.
  16. * \param devname serial device name, example "/dev/ttyS0" or "COM1"
  17. * \param baud_rate serial baud rate
  18. * \param opt_parity serial parity, default none
  19. * \param opt_csize serial character size, default 8bit
  20. * \param opt_flow serial flow control, default none
  21. * \param opt_stop serial stop bits, default 1
  22. * \throws boost::system::system_error if cannot open the
  23. * serial device
  24. */
  25. BufferedAsyncSerial(const std::string& devname, unsigned int baud_rate,
  26. boost::asio::serial_port_base::parity opt_parity=
  27. boost::asio::serial_port_base::parity(
  28. boost::asio::serial_port_base::parity::none),
  29. boost::asio::serial_port_base::character_size opt_csize=
  30. boost::asio::serial_port_base::character_size(8),
  31. boost::asio::serial_port_base::flow_control opt_flow=
  32. boost::asio::serial_port_base::flow_control(
  33. boost::asio::serial_port_base::flow_control::none),
  34. boost::asio::serial_port_base::stop_bits opt_stop=
  35. boost::asio::serial_port_base::stop_bits(
  36. boost::asio::serial_port_base::stop_bits::one));
  37. /**
  38. * Read some data asynchronously. Returns immediately.
  39. * \param data array of char to be read through the serial device
  40. * \param size array size
  41. * \return numbr of character actually read 0<=return<=size
  42. */
  43. size_t read(char *data, size_t size);
  44. /**
  45. * Read all available data asynchronously. Returns immediately.
  46. * \return the receive buffer. It iempty if no data is available
  47. */
  48. std::vector<char> read();
  49. /**
  50. * Read a string asynchronously. Returns immediately.
  51. * Can only be used if the user is sure that the serial device will not
  52. * send binary data. For binary data read, use read()
  53. * The returned string is empty if no data has arrived
  54. * \return a string with the received data.
  55. */
  56. std::string readString();
  57. /**
  58. * Read a line asynchronously. Returns immediately.
  59. * Can only be used if the user is sure that the serial device will not
  60. * send binary data. For binary data read, use read()
  61. * The returned string is empty if the line delimiter has not yet arrived.
  62. * \param delimiter line delimiter, default='\n'
  63. * \return a string with the received data. The delimiter is removed from
  64. * the string.
  65. */
  66. std::string readStringUntil(const std::string delim="\n");
  67. virtual ~BufferedAsyncSerial();
  68. private:
  69. /**
  70. * Read callback, stores data in the buffer
  71. */
  72. void readCallback(const char *data, size_t len);
  73. /**
  74. * Finds a substring in a vector of char. Used to look for the delimiter.
  75. * \param v vector where to find the string
  76. * \param s string to find
  77. * \return the beginning of the place in the vector where the first
  78. * occurrence of the string is, or v.end() if the string was not found
  79. */
  80. static std::vector<char>::iterator findStringInVector(std::vector<char>& v,
  81. const std::string& s);
  82. std::vector<char> readQueue;
  83. boost::mutex readQueueMutex;
  84. };
  85. #endif //BUFFEREDASYNCSERIAL_H