Skip to content

Commit 0eeab97

Browse files
author
William Emfinger
committed
updated serial library from lib-dynamixel.
1 parent 338b1d6 commit 0eeab97

File tree

4 files changed

+114
-0
lines changed

4 files changed

+114
-0
lines changed

src/serial/CMakeLists.txt

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
cmake_minimum_required(VERSION 2.8.3)
2+
project(serial)
3+
4+
## Check C++11 / C++0x
5+
include(CheckCXXCompilerFlag)
6+
CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
7+
CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
8+
if(COMPILER_SUPPORTS_CXX11)
9+
set(CMAKE_CXX_FLAGS "-std=c++11")
10+
elseif(COMPILER_SUPPORTS_CXX0X)
11+
set(CMAKE_CXX_FLAGS "-std=c++0x")
12+
else()
13+
message(FATAL_ERROR "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.")
14+
endif()
15+
16+
include_directories(include)
17+
add_library(serial
18+
src/dynamixel/SerialPort.cpp)
19+
20+
21+
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#ifndef SERIALPORT_H_
2+
#define SERIALPORT_H_
3+
4+
#include <stdio.h>
5+
#include <termios.h>
6+
#include <fcntl.h>
7+
#include <unistd.h>
8+
9+
#include <boost/asio/serial_port.hpp>
10+
#include <boost/asio.hpp>
11+
#include <boost/asio/deadline_timer.hpp>
12+
#include <boost/bind.hpp>
13+
14+
const int blockTimeMS = 10;
15+
16+
class SerialPort {
17+
private:
18+
boost::asio::io_service io;
19+
boost::asio::serial_port *port;
20+
public:
21+
SerialPort();
22+
23+
int connect ();
24+
int connect (char * device, int baud);
25+
void disconnect(void);
26+
27+
int sendArray(unsigned char *buffer, int len);
28+
int getArray (unsigned char *buffer, int len);
29+
};
30+
31+
32+
#endif /* SERIALPORT_H_ */

src/serial/package.xml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0"?>
2+
<package>
3+
<name>serial</name>
4+
<version>1.0.0</version>
5+
<description>Package for interfacing with serial devices.</description>
6+
7+
<!-- One maintainer tag required, multiple allowed, one person per tag -->
8+
<!-- Example: -->
9+
<!-- <maintainer email="[email protected]">Jane Doe</maintainer> -->
10+
<maintainer email="[email protected]">William Emfinger</maintainer>
11+
<maintainer email="[email protected]">Pranav Srinivas Kumar</maintainer>
12+
13+
<!-- One license tag required, multiple allowed, one license per tag -->
14+
<!-- Commonly used license strings: -->
15+
<!-- BSD, MIT, Boost Software License, GPLv2, GPLv3, LGPLv2.1, LGPLv3 -->
16+
<license>MIT</license>
17+
<buildtool_depend>catkin</buildtool_depend>
18+
<export>
19+
</export>
20+
</package>

src/serial/src/serial/SerialPort.cpp

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include <string.h>
2+
#include <sys/ioctl.h>
3+
4+
#include "serial/SerialPort.h"
5+
6+
SerialPort::SerialPort() {
7+
port = new boost::asio::serial_port(io);
8+
}
9+
10+
int SerialPort::connect() {
11+
port->open("/dev/ttyO5");
12+
port->set_option(boost::asio::serial_port_base::baud_rate(9600));
13+
return 1;
14+
}
15+
16+
int SerialPort::connect(char *device, int baud) {
17+
port->open(device);
18+
port->set_option(boost::asio::serial_port_base::baud_rate(baud));
19+
return 1;
20+
}
21+
22+
void SerialPort::disconnect(void)
23+
{
24+
port->close();
25+
}
26+
27+
int SerialPort::sendArray(unsigned char *buffer, int len) {
28+
int n = boost::asio::write( *port,
29+
boost::asio::buffer(buffer,len));
30+
return n;
31+
}
32+
33+
int SerialPort::getArray (unsigned char *buffer, int len)
34+
{
35+
char rcvChar;
36+
int i = 0;
37+
while ( i < len && boost::asio::read( *port, boost::asio::buffer(&rcvChar,1) ) == 1 )
38+
buffer[i++] = rcvChar;
39+
return i;
40+
}
41+

0 commit comments

Comments
 (0)