21 bool Serial::connect(
const std::string& portName,
const int& baud, boost::function<
void()> cb) {
22 using namespace boost::asio;
24 port.set_option(serial_port::baud_rate(baud));
25 port.set_option(serial_port::flow_control(serial_port::flow_control::none));
32 if (!startReadSuccess) {
35 return startReadSuccess;
58 CERR(
"[create::Serial] ",
"data pointer not initialized.");
73 boost::asio::async_read(
port,
77 ioThread = boost::thread(boost::bind(&boost::asio::io_service::run, &
io));
85 if (!
dataReadyCond.timed_wait(lock, boost::get_system_time() + boost::posix_time::milliseconds(500))) {
86 if (attempts >= maxAttempts) {
87 CERR(
"[create::Serial] ",
"failed to receive data from Create. Check if robot is powered!");
134 void Serial::onData(
const boost::system::error_code& e,
const std::size_t& size) {
136 CERR(
"[create::Serial] ",
"serial error - " << e.message());
146 boost::asio::async_read(
port,
153 CERR(
"[create::Serial] ",
"send failed, not connected.");
157 boost::asio::write(
port, boost::asio::buffer(bytes, numBytes));
162 uint8_t oc = (uint8_t) code;
boost::mutex dataReadyMut
uint64_t getTotalPackets() const
boost::asio::serial_port port
virtual void processByte(uint8_t byteRead)=0
bool sendOpcode(const Opcode &code)
boost::shared_ptr< Data > data
Serial(boost::shared_ptr< Data > data)
#define CERR(prefix, msg)
bool connect(const std::string &port, const int &baud=115200, boost::function< void()> cb=0)
bool send(const uint8_t *bytes, const uint32_t numBytes)
virtual bool startSensorStream()=0
boost::function< void()> callback
boost::condition_variable dataReadyCond
void onData(const boost::system::error_code &e, const std::size_t &size)
boost::asio::io_service io
uint64_t getNumCorruptPackets() const