19 if (install_signal_handler) {
41 bool Serial::connect(
const std::string& portName,
const int& baud, std::function<
void()> cb) {
42 using namespace boost::asio;
51 if (!
port.is_open()) {
57 if (!startReadSuccess) {
80 using namespace boost::asio;
83 port.set_option(serial_port::baud_rate(baud));
84 port.set_option(serial_port::character_size(8));
85 port.set_option(serial_port::parity(serial_port::parity::none));
86 port.set_option(serial_port::stop_bits(serial_port::stop_bits::one));
87 port.set_option(serial_port::flow_control(serial_port::flow_control::none));
88 }
catch (
const boost::system::system_error& ) {
89 CERR(
"[create::Serial] ",
"failed to open port: " << portName);
96 using namespace boost::asio;
99 }
catch (
const boost::system::system_error& ) {
100 CERR(
"[create::Serial] ",
"failed to close port");
110 CERR(
"[create::Serial] ",
"data pointer not initialized.");
125 boost::asio::async_read(
port,
129 std::placeholders::_1,
130 std::placeholders::_2));
133 static_cast<std::size_t(boost::asio::io_service::*)(
void)
>(
134 &boost::asio::io_service::run), &
io));
140 int maxAttempts = 10;
142 if (
dataReadyCond.wait_for(lock, std::chrono::milliseconds(500)) == std::cv_status::timeout) {
143 if (attempts >= maxAttempts) {
144 CERR(
"[create::Serial] ",
"failed to receive data from Create. Check if robot is powered!");
191 void Serial::onData(
const boost::system::error_code& e,
const std::size_t& size) {
193 CERR(
"[create::Serial] ",
"serial error - " << e.message());
203 boost::asio::async_read(
port,
207 std::placeholders::_1,
208 std::placeholders::_2));
213 CERR(
"[create::Serial] ",
"send failed, not connected.");
218 boost::asio::write(
port, boost::asio::buffer(bytes, numBytes));
219 }
catch (
const boost::system::system_error & e) {
220 CERR(
"[create::Serial] ",
"failed to write bytes to port");
227 uint8_t oc = (uint8_t) code;