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;
44 port.set_option(serial_port::baud_rate(baud));
45 port.set_option(serial_port::character_size(8));
46 port.set_option(serial_port::parity(serial_port::parity::none));
47 port.set_option(serial_port::stop_bits(serial_port::stop_bits::one));
48 port.set_option(serial_port::flow_control(serial_port::flow_control::none));
57 if (!startReadSuccess) {
60 return startReadSuccess;
83 CERR(
"[create::Serial] ",
"data pointer not initialized.");
98 boost::asio::async_read(
port,
102 std::placeholders::_1,
103 std::placeholders::_2));
106 static_cast<std::size_t(boost::asio::io_service::*)(
void)
>(
107 &boost::asio::io_service::run), &
io));
113 int maxAttempts = 10;
115 if (
dataReadyCond.wait_for(lock, std::chrono::milliseconds(500)) == std::cv_status::timeout) {
116 if (attempts >= maxAttempts) {
117 CERR(
"[create::Serial] ",
"failed to receive data from Create. Check if robot is powered!");
164 void Serial::onData(
const boost::system::error_code& e,
const std::size_t& size) {
166 CERR(
"[create::Serial] ",
"serial error - " << e.message());
176 boost::asio::async_read(
port,
180 std::placeholders::_1,
181 std::placeholders::_2));
186 CERR(
"[create::Serial] ",
"send failed, not connected.");
190 boost::asio::write(
port, boost::asio::buffer(bytes, numBytes));
195 uint8_t oc = (uint8_t) code;
std::function< void()> callback
uint64_t getTotalPackets() const
boost::asio::serial_port port
virtual void processByte(uint8_t byteRead)=0
bool sendOpcode(const Opcode &code)
std::condition_variable dataReadyCond
#define CERR(prefix, msg)
std::shared_ptr< Data > data
boost::asio::signal_set signals
Serial(std::shared_ptr< Data > data, bool install_signal_handler)
bool send(const uint8_t *bytes, const uint32_t numBytes)
virtual bool startSensorStream()=0
void signalHandler(const boost::system::error_code &error, int signal_number)
bool connect(const std::string &port, const int &baud=115200, std::function< void()> cb=0)
void onData(const boost::system::error_code &e, const std::size_t &size)
boost::asio::io_service io
uint64_t getNumCorruptPackets() const