Skip to content
This repository was archived by the owner on Feb 28, 2019. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions include/Frame.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace XBEE {
virtual std::vector<uint8_t> SerializeFrame() const = 0;

protected:
const uint8_t start = 0x7E;
uint8_t start = 0x7E;
uint16_t length;
uint8_t frame_type;
uint8_t checksum;
Expand All @@ -20,4 +20,4 @@ namespace XBEE {
virtual void SetChecksum() = 0;
};
}
#endif
#endif
6 changes: 3 additions & 3 deletions include/SerialXbee.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,16 @@ namespace XBEE {
public:
std::function<void(Frame *)> ReadHandler;
std::function<void(Frame *)> 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
void AsyncReadFrame();
// 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();
};
}
Expand Down
23 changes: 14 additions & 9 deletions src/SerialXbee.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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
Expand All @@ -81,19 +86,19 @@ 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;
}*/

// 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;
}
Expand All @@ -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:
Expand Down