Skip to content

Commit

Permalink
updated serial library from lib-dynamixel.
Browse files Browse the repository at this point in the history
  • Loading branch information
William Emfinger committed Apr 17, 2016
1 parent 338b1d6 commit 0eeab97
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/serial/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
cmake_minimum_required(VERSION 2.8.3)
project(serial)

## Check C++11 / C++0x
include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
if(COMPILER_SUPPORTS_CXX11)
set(CMAKE_CXX_FLAGS "-std=c++11")
elseif(COMPILER_SUPPORTS_CXX0X)
set(CMAKE_CXX_FLAGS "-std=c++0x")
else()
message(FATAL_ERROR "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.")
endif()

include_directories(include)
add_library(serial
src/dynamixel/SerialPort.cpp)



32 changes: 32 additions & 0 deletions src/serial/include/serial/SerialPort.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#ifndef SERIALPORT_H_
#define SERIALPORT_H_

#include <stdio.h>
#include <termios.h>
#include <fcntl.h>
#include <unistd.h>

#include <boost/asio/serial_port.hpp>
#include <boost/asio.hpp>
#include <boost/asio/deadline_timer.hpp>
#include <boost/bind.hpp>

const int blockTimeMS = 10;

class SerialPort {
private:
boost::asio::io_service io;
boost::asio::serial_port *port;
public:
SerialPort();

int connect ();
int connect (char * device, int baud);
void disconnect(void);

int sendArray(unsigned char *buffer, int len);
int getArray (unsigned char *buffer, int len);
};


#endif /* SERIALPORT_H_ */
20 changes: 20 additions & 0 deletions src/serial/package.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0"?>
<package>
<name>serial</name>
<version>1.0.0</version>
<description>Package for interfacing with serial devices.</description>

<!-- One maintainer tag required, multiple allowed, one person per tag -->
<!-- Example: -->
<!-- <maintainer email="[email protected]">Jane Doe</maintainer> -->
<maintainer email="[email protected]">William Emfinger</maintainer>
<maintainer email="[email protected]">Pranav Srinivas Kumar</maintainer>

<!-- One license tag required, multiple allowed, one license per tag -->
<!-- Commonly used license strings: -->
<!-- BSD, MIT, Boost Software License, GPLv2, GPLv3, LGPLv2.1, LGPLv3 -->
<license>MIT</license>
<buildtool_depend>catkin</buildtool_depend>
<export>
</export>
</package>
41 changes: 41 additions & 0 deletions src/serial/src/serial/SerialPort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <string.h>
#include <sys/ioctl.h>

#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);
port->set_option(boost::asio::serial_port_base::baud_rate(baud));
return 1;
}

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));
return n;
}

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 )
buffer[i++] = rcvChar;
return i;
}

0 comments on commit 0eeab97

Please sign in to comment.