GCodeSender.hpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. ///|/ Copyright (c) Prusa Research 2016 - 2021 Vojtěch Bubník @bubnikv, Vojtěch Král @vojtechkral
  2. ///|/ Copyright (c) Slic3r 2014 - 2016 Alessandro Ranellucci @alranel
  3. ///|/
  4. ///|/ PrusaSlicer is released under the terms of the AGPLv3 or higher
  5. ///|/
  6. #ifndef slic3r_GCodeSender_hpp_
  7. #define slic3r_GCodeSender_hpp_
  8. #include "libslic3r.h"
  9. #include <queue>
  10. #include <string>
  11. #include <vector>
  12. #include <boost/asio.hpp>
  13. #include <boost/bind/bind.hpp>
  14. #include <boost/thread.hpp>
  15. namespace Slic3r {
  16. namespace asio = boost::asio;
  17. class GCodeSender : private boost::noncopyable {
  18. public:
  19. GCodeSender();
  20. ~GCodeSender();
  21. bool connect(std::string devname, unsigned int baud_rate);
  22. void send(const std::vector<std::string> &lines, bool priority = false);
  23. void send(const std::string &s, bool priority = false);
  24. void disconnect();
  25. bool error_status() const;
  26. bool is_connected() const;
  27. bool wait_connected(unsigned int timeout = 3) const;
  28. size_t queue_size() const;
  29. void pause_queue();
  30. void resume_queue();
  31. void purge_queue(bool priority = false);
  32. std::vector<std::string> purge_log();
  33. std::string getT() const;
  34. std::string getB() const;
  35. void set_DTR(bool on);
  36. void reset();
  37. private:
  38. asio::io_service io;
  39. asio::serial_port serial;
  40. boost::thread background_thread;
  41. boost::asio::streambuf read_buffer, write_buffer;
  42. bool open; // whether the serial socket is connected
  43. bool connected; // whether the printer is online
  44. bool error;
  45. mutable boost::mutex error_mutex;
  46. // this mutex guards queue, priqueue, can_send, queue_paused, sent, last_sent
  47. mutable boost::mutex queue_mutex;
  48. std::queue<std::string> queue;
  49. std::list<std::string> priqueue;
  50. bool can_send;
  51. bool queue_paused;
  52. size_t sent;
  53. std::deque<std::string> last_sent;
  54. // this mutex guards log, T, B
  55. mutable boost::mutex log_mutex;
  56. std::queue<std::string> log;
  57. std::string T, B;
  58. void set_baud_rate(unsigned int baud_rate);
  59. void set_error_status(bool e);
  60. void do_send();
  61. void on_write(const boost::system::error_code& error, size_t bytes_transferred);
  62. void do_close();
  63. void do_read();
  64. void on_read(const boost::system::error_code& error, size_t bytes_transferred);
  65. void send();
  66. };
  67. } // namespace Slic3r
  68. #endif /* slic3r_GCodeSender_hpp_ */