diff --git a/include/Frame.hpp b/include/Frame.hpp index 4630396..f3cb27b 100644 --- a/include/Frame.hpp +++ b/include/Frame.hpp @@ -11,7 +11,7 @@ namespace XBEE { virtual std::vector SerializeFrame() const = 0; protected: - const uint8_t start = 0x7E; + uint8_t start = 0x7E; uint16_t length; uint8_t frame_type; uint8_t checksum; @@ -20,4 +20,4 @@ namespace XBEE { virtual void SetChecksum() = 0; }; } -#endif \ No newline at end of file +#endif diff --git a/include/SerialXbee.hpp b/include/SerialXbee.hpp index b710c61..2b14436 100644 --- a/include/SerialXbee.hpp +++ b/include/SerialXbee.hpp @@ -46,7 +46,7 @@ namespace XBEE { public: std::function ReadHandler; std::function WriteHandler; - // Testing a way to call the SerialXbee class from the library itself + // Testing a way to call the SerialXbee class from the library itself SerialXbee(); ~SerialXbee(); // TODO: Add a blocking (synchronous) read function @@ -54,8 +54,8 @@ namespace XBEE { // TODO: Add a blocking (synchronous) write function void AsyncWriteFrame(Frame *a_frame); // TODO: Add support for port options, data bit size, parity etc... - void Connect(); - void Connect2(std::string device_path = kDefaultPath, uint32_t baud_rate = 57600); + int Connect(); + int Connect2(std::string device_path = kDefaultPath, uint32_t baud_rate = 57600); void Stop(); }; } diff --git a/src/SerialXbee.cpp b/src/SerialXbee.cpp index b304249..b842bc1 100644 --- a/src/SerialXbee.cpp +++ b/src/SerialXbee.cpp @@ -29,22 +29,25 @@ namespace XBEE { io.reset(); runner.join(); } - void SerialXbee::Connect(){ + int SerialXbee::Connect(){ XBEE::SerialXbee xbee_suicide; - xbee_suicide.Connect2(); + if(xbee_suicide.Connect2() != EXIT_SUCCESS) { + return EXIT_FAILURE; + } usleep(1000000); tcflush(port.lowest_layer().native_handle(), TCIFLUSH); xbee_suicide.Stop(); SerialXbee(); - Connect2(); + return Connect2(); } - void SerialXbee::Connect2(std::string device_path, uint32_t baud_rate) { + int SerialXbee::Connect2(std::string device_path, uint32_t baud_rate) { boost::system::error_code connect_error; port.open(device_path, connect_error); + if (connect_error) { // TODO: Throw a "Port was unable to connect exception, append the connect_error, and port name" std::cerr << "Unable to open Serial Port" << std::endl; - exit(EXIT_FAILURE); + return EXIT_FAILURE; } port.set_option(boost::asio::serial_port_base::baud_rate(baud_rate)); @@ -62,6 +65,8 @@ namespace XBEE { // Let the io_service run in background while main thread continues runner = boost::thread(boost::bind(&boost::asio::io_service::run, &io)); + + return EXIT_SUCCESS; } // TODO: Clean up and optimize this messy code when time permits @@ -81,7 +86,7 @@ namespace XBEE { std::cerr << error.message() << std::endl; // throw an error, by repeating system error code } else { - + /*if (num_bytes != 2) { std::cout << "[ERROR] NOT ENOUGH BYTES" << std::endl; }*/ @@ -89,11 +94,11 @@ namespace XBEE { // Clears buffer (Should only contain 0x7E delimiter, which is added automatically when new Frame object is created) buffer.consume(num_bytes); num_bytes = 0; - + // Synchronously read the next 2 bytes of Frame (Frame Length) while (buffer.size() < 3) read(port, buffer, transfer_exactly(1), read_error); - + if(read_error){ std::cerr << read_error.message() << std::endl; } @@ -110,7 +115,7 @@ namespace XBEE { if(read_error){ std::cerr << read_error.message() << std::endl; } - + // Construct Frame object switch(FrameType(frame_type)) { case FrameType::RECEIVE_PACKET: