Skip to content
Merged
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
2 changes: 2 additions & 0 deletions include/create/serial.h
Original file line number Diff line number Diff line change
Expand Up @@ -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> data;
Expand Down
1 change: 1 addition & 0 deletions include/create/serial_query.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ namespace create {

public:
SerialQuery(std::shared_ptr<Data> data, bool install_signal_handler = true);
virtual ~SerialQuery() = default;
};
} // namespace create

Expand Down
1 change: 1 addition & 0 deletions include/create/serial_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ namespace create {
std::shared_ptr<Data> data,
const uint8_t& header = create::util::STREAM_HEADER,
bool install_signal_handler = true);
virtual ~SerialStream() = default;

};
} // namespace create
Expand Down
6 changes: 3 additions & 3 deletions src/create.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,10 @@ namespace create {
prevTicksRight = totalTicksRight;

// Handle wrap around
if (fabs(ticksLeft) > 0.9 * util::V_3_MAX_ENCODER_TICKS) {
if (std::abs(ticksLeft) > 0.9 * util::V_3_MAX_ENCODER_TICKS) {
ticksLeft = (ticksLeft % util::V_3_MAX_ENCODER_TICKS) + 1;
}
if (fabs(ticksRight) > 0.9 * util::V_3_MAX_ENCODER_TICKS) {
if (std::abs(ticksRight) > 0.9 * util::V_3_MAX_ENCODER_TICKS) {
ticksRight = (ticksRight % util::V_3_MAX_ENCODER_TICKS) + 1;
}

Expand Down Expand Up @@ -373,7 +373,7 @@ namespace create {
min > 59)
return false;

uint8_t cmd[4] = { OC_DATE, day, hour, min };
uint8_t cmd[4] = { OC_DATE, static_cast<uint8_t>(day), hour, min };
return serial->send(cmd, 4);
}

Expand Down
64 changes: 48 additions & 16 deletions src/serial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,26 +40,26 @@ namespace create {

bool Serial::connect(const std::string& portName, const int& baud, std::function<void()> 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;
}

callback = cb;
bool startReadSuccess = startReading();
if (!startReadSuccess) {
closePort();
return false;
}
return false;

return true;
}

void Serial::disconnect() {
Expand All @@ -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;

Expand Down Expand Up @@ -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;
}

Expand Down