From 90c3176f3e79e925662875c206e9e9ab5b3cf70b Mon Sep 17 00:00:00 2001 From: Dwatkins Date: Fri, 18 Aug 2017 09:21:57 -0500 Subject: [PATCH] updated cmakelists and added SendString function --- src/serial/CMakeLists.txt | 7 +++++++ src/serial/include/serial/SerialPort.h | 8 ++++---- src/serial/package.xml | 1 + src/serial/src/serial/SerialPort.cpp | 24 ++++++++++++------------ 4 files changed, 24 insertions(+), 16 deletions(-) diff --git a/src/serial/CMakeLists.txt b/src/serial/CMakeLists.txt index a63c086..80f47d1 100644 --- a/src/serial/CMakeLists.txt +++ b/src/serial/CMakeLists.txt @@ -1,6 +1,13 @@ cmake_minimum_required(VERSION 2.8.3) project(serial) +find_package(catkin REQUIRED COMPONENTS) + +catkin_package( + INCLUDE_DIRS include + LIBRARIES serial +) + ## Check C++11 / C++0x include(CheckCXXCompilerFlag) CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11) diff --git a/src/serial/include/serial/SerialPort.h b/src/serial/include/serial/SerialPort.h index 5b85241..663f5aa 100644 --- a/src/serial/include/serial/SerialPort.h +++ b/src/serial/include/serial/SerialPort.h @@ -5,14 +5,13 @@ #include #include #include +#include #include #include #include #include -const int blockTimeMS = 10; - class SerialPort { private: boost::asio::io_service io; @@ -20,13 +19,14 @@ class SerialPort { public: SerialPort(); - int connect (); - int connect (char * device, int baud); + int connect (std::string device, int baud); void disconnect(void); int sendArray(unsigned char *buffer, int len); + int sendString(std::string msg); int getArray (unsigned char *buffer, int len); }; + #endif /* SERIALPORT_H_ */ diff --git a/src/serial/package.xml b/src/serial/package.xml index 9a6c0b7..00d0703 100644 --- a/src/serial/package.xml +++ b/src/serial/package.xml @@ -9,6 +9,7 @@ William Emfinger Pranav Srinivas Kumar + Dexter Watkins diff --git a/src/serial/src/serial/SerialPort.cpp b/src/serial/src/serial/SerialPort.cpp index d24cc23..12db12f 100644 --- a/src/serial/src/serial/SerialPort.cpp +++ b/src/serial/src/serial/SerialPort.cpp @@ -3,35 +3,34 @@ #include "serial/SerialPort.h" + SerialPort::SerialPort() { port = new boost::asio::serial_port(io); } -int SerialPort::connect() { - port->open("/dev/ttyO5"); - port->set_option(boost::asio::serial_port_base::baud_rate(9600)); - return 1; -} -int SerialPort::connect(char *device, int baud) { - port->open(device); +int SerialPort::connect(std::string device, int baud) { + port->open((char *) device.c_str()); port->set_option(boost::asio::serial_port_base::baud_rate(baud)); return 1; } -void SerialPort::disconnect(void) -{ +void SerialPort::disconnect(void){ port->close(); } int SerialPort::sendArray(unsigned char *buffer, int len) { int n = boost::asio::write( *port, - boost::asio::buffer(buffer,len)); + boost::asio::buffer(buffer,len)); return n; } -int SerialPort::getArray (unsigned char *buffer, int len) -{ +int SerialPort::sendString(std::string msg){ + std::string tmp = msg + "\n"; + SerialPort::sendArray((unsigned char *) tmp.c_str(), tmp.length()); +} + +int SerialPort::getArray (unsigned char *buffer, int len){ char rcvChar; int i = 0; while ( i < len && boost::asio::read( *port, boost::asio::buffer(&rcvChar,1) ) == 1 ) @@ -39,3 +38,4 @@ int SerialPort::getArray (unsigned char *buffer, int len) return i; } +