00001 #include "segwayrmp/segwayrmp.h" 00002 #include "segwayrmp/impl/rmp_serial.h" 00003 00004 using namespace segwayrmp; 00005 00007 // SerialRMPIO 00008 00009 SerialRMPIO::SerialRMPIO() : configured(false), baudrate(460800), port("") { 00010 this->connected = false; 00011 } 00012 00013 SerialRMPIO::~SerialRMPIO() { 00014 this->disconnect(); 00015 } 00016 00017 void SerialRMPIO::configure(std::string port, int baudrate) { 00018 this->port = port; 00019 this->baudrate = baudrate; 00020 this->configured = true; 00021 } 00022 00023 void SerialRMPIO::connect() { 00024 if(!this->configured) { 00025 RMP_THROW_MSG(ConnectionFailedException, "The serial port must be " 00026 "configured before connecting."); 00027 } 00028 try { 00029 // Configure and open the serial port 00030 this->serial_port.setPort(this->port); 00031 this->serial_port.setBaudrate(this->baudrate); 00032 serial::Timeout timeout = serial::Timeout::simpleTimeout(1000); 00033 this->serial_port.setTimeout(timeout); 00034 this->serial_port.open(); 00035 } catch(std::exception &e) { 00036 RMP_THROW_MSG(ConnectionFailedException, e.what()); 00037 } 00038 this->connected = true; 00039 } 00040 00041 void SerialRMPIO::disconnect() { 00042 if(this->connected) { 00043 if(this->serial_port.isOpen()) 00044 this->serial_port.close(); 00045 this->connected = false; 00046 } 00047 } 00048 00049 int SerialRMPIO::read(unsigned char* buffer, int size) { 00050 return this->serial_port.read(buffer, size); 00051 } 00052 00053 int SerialRMPIO::write(unsigned char* buffer, int size) { 00054 return this->serial_port.write(buffer, size); 00055 }