From de253b6e81a8409087132ac5c5fe1d2973543d13 Mon Sep 17 00:00:00 2001 From: Swapnil Patel Date: Mon, 27 Jun 2022 22:38:18 -0400 Subject: [PATCH] catch boost exceptions in Serial.h --- include/create/serial.h | 2 ++ src/serial.cpp | 64 ++++++++++++++++++++++++++++++----------- 2 files changed, 50 insertions(+), 16 deletions(-) diff --git a/include/create/serial.h b/include/create/serial.h index 05ac648..ccfdaea 100644 --- a/include/create/serial.h +++ b/include/create/serial.h @@ -71,6 +71,8 @@ namespace create { // Start and stop reading data from Create bool startReading(); void stopReading(); + bool openPort(const std::string& portName, const int& baud); + bool closePort(); protected: std::shared_ptr data; diff --git a/src/serial.cpp b/src/serial.cpp index 97c6085..6617e50 100644 --- a/src/serial.cpp +++ b/src/serial.cpp @@ -40,26 +40,26 @@ namespace create { bool Serial::connect(const std::string& portName, const int& baud, std::function cb) { using namespace boost::asio; - port.open(portName); - port.set_option(serial_port::baud_rate(baud)); - port.set_option(serial_port::character_size(8)); - port.set_option(serial_port::parity(serial_port::parity::none)); - port.set_option(serial_port::stop_bits(serial_port::stop_bits::one)); - port.set_option(serial_port::flow_control(serial_port::flow_control::none)); + if (!openPort(portName, baud)) { + return false; + } signals.async_wait(std::bind(&Serial::signalHandler, this, std::placeholders::_1, std::placeholders::_2)); usleep(1000000); - if (port.is_open()) { - callback = cb; - bool startReadSuccess = startReading(); - if (!startReadSuccess) { - port.close(); - } - return startReadSuccess; + if (!port.is_open()) { + return false; } - return false; + + callback = cb; + bool startReadSuccess = startReading(); + if (!startReadSuccess) { + closePort(); + return false; + } + + return true; } void Serial::disconnect() { @@ -76,6 +76,33 @@ namespace create { } } + bool Serial::openPort(const std::string& portName, const int& baud) { + using namespace boost::asio; + try { + port.open(portName); + port.set_option(serial_port::baud_rate(baud)); + port.set_option(serial_port::character_size(8)); + port.set_option(serial_port::parity(serial_port::parity::none)); + port.set_option(serial_port::stop_bits(serial_port::stop_bits::one)); + port.set_option(serial_port::flow_control(serial_port::flow_control::none)); + } catch (const boost::system::system_error& /*e*/) { + CERR("[create::Serial] ", "failed to open port: " << portName); + return false; + } + return true; + } + + bool Serial::closePort() { + using namespace boost::asio; + try { + port.close(); + } catch (const boost::system::system_error& /*e*/) { + CERR("[create::Serial] ", "failed to close port"); + return false; + } + return true; + } + bool Serial::startReading() { if (!connected()) return false; @@ -186,8 +213,13 @@ namespace create { CERR("[create::Serial] ", "send failed, not connected."); return false; } - // TODO: catch boost exceptions - boost::asio::write(port, boost::asio::buffer(bytes, numBytes)); + + try { + boost::asio::write(port, boost::asio::buffer(bytes, numBytes)); + } catch (const boost::system::system_error & e) { + CERR("[create::Serial] ", "failed to write bytes to port"); + return false; + } return true; }