diff --git a/src/serial_port_wrapper.cpp b/src/serial_port_wrapper.cpp index cde8a36..277102a 100644 --- a/src/serial_port_wrapper.cpp +++ b/src/serial_port_wrapper.cpp @@ -24,6 +24,10 @@ port_.set_option(serial_port_base::parity(serial_port_base::parity::none)); port_.set_option(serial_port_base::stop_bits(serial_port_base::stop_bits::one)); + start_timer(); +} + +void SerialPortWrapper::start_timer() { timer_.expires_from_now(boost::posix_time::seconds(3)); timer_.async_wait([this](const boost::system::error_code &error) { if (!error) { @@ -47,23 +51,23 @@ reinterpret_cast(received_data.data()), static_cast(bytes_transferred) ); - std::cout << "received " << bytes_transferred << " bytes: "; - for (size_t i = 0; i < bytes_transferred; ++i) { - std::cout << std::hex - << std::uppercase - << std::setw(2) - << std::setfill('0') - << static_cast(received_data[i]) - << " "; - } - std::cout << std::endl; + // std::cout << "received " << bytes_transferred << " bytes: "; + // for (size_t i = 0; i < bytes_transferred; ++i) { + // std::cout << std::hex + // << std::uppercase + // << std::setw(2) + // << std::setfill('0') + // << static_cast(received_data[i]) + // << " "; + // } + // std::cout << std::endl; handle_data(received_data); - read_from_port(); + start_timer(); } else { std::cerr << "error: " << error.message() << std::endl; - read_from_port(); + start_timer(); } }); } diff --git a/src/serial_port_wrapper.cpp b/src/serial_port_wrapper.cpp index cde8a36..277102a 100644 --- a/src/serial_port_wrapper.cpp +++ b/src/serial_port_wrapper.cpp @@ -24,6 +24,10 @@ port_.set_option(serial_port_base::parity(serial_port_base::parity::none)); port_.set_option(serial_port_base::stop_bits(serial_port_base::stop_bits::one)); + start_timer(); +} + +void SerialPortWrapper::start_timer() { timer_.expires_from_now(boost::posix_time::seconds(3)); timer_.async_wait([this](const boost::system::error_code &error) { if (!error) { @@ -47,23 +51,23 @@ reinterpret_cast(received_data.data()), static_cast(bytes_transferred) ); - std::cout << "received " << bytes_transferred << " bytes: "; - for (size_t i = 0; i < bytes_transferred; ++i) { - std::cout << std::hex - << std::uppercase - << std::setw(2) - << std::setfill('0') - << static_cast(received_data[i]) - << " "; - } - std::cout << std::endl; + // std::cout << "received " << bytes_transferred << " bytes: "; + // for (size_t i = 0; i < bytes_transferred; ++i) { + // std::cout << std::hex + // << std::uppercase + // << std::setw(2) + // << std::setfill('0') + // << static_cast(received_data[i]) + // << " "; + // } + // std::cout << std::endl; handle_data(received_data); - read_from_port(); + start_timer(); } else { std::cerr << "error: " << error.message() << std::endl; - read_from_port(); + start_timer(); } }); } diff --git a/src/serial_port_wrapper.hpp b/src/serial_port_wrapper.hpp index b4c90f4..cadcd45 100644 --- a/src/serial_port_wrapper.hpp +++ b/src/serial_port_wrapper.hpp @@ -24,6 +24,8 @@ boost::asio::serial_port port_; boost::asio::deadline_timer timer_; boost::asio::streambuf buffer_; + + void start_timer(); }; #endif //SERIAL_PORT_WRAPPER_HPP