|
From: <mor...@us...> - 2008-03-27 23:56:08
|
Revision: 51
http://personalrobots.svn.sourceforge.net/personalrobots/?rev=51&view=rev
Author: morgan_quigley
Date: 2008-03-27 16:56:14 -0700 (Thu, 27 Mar 2008)
Log Message:
-----------
copy my silly little serial port library from my personal svn
Added Paths:
-----------
pkg/trunk/serial_port/
pkg/trunk/serial_port/build.yaml
pkg/trunk/serial_port/include/
pkg/trunk/serial_port/include/serial_port/
pkg/trunk/serial_port/include/serial_port/lightweightserial.h
pkg/trunk/serial_port/lib/
pkg/trunk/serial_port/manifest.xml
pkg/trunk/serial_port/rosbuild
pkg/trunk/serial_port/src/
pkg/trunk/serial_port/src/libserial_port/
pkg/trunk/serial_port/src/libserial_port/Makefile
pkg/trunk/serial_port/src/libserial_port/lightweightserial.cpp
Added: pkg/trunk/serial_port/build.yaml
===================================================================
--- pkg/trunk/serial_port/build.yaml (rev 0)
+++ pkg/trunk/serial_port/build.yaml 2008-03-27 23:56:14 UTC (rev 51)
@@ -0,0 +1,3 @@
+cpp:
+ make:
+ - src/libserial_port
Added: pkg/trunk/serial_port/include/serial_port/lightweightserial.h
===================================================================
--- pkg/trunk/serial_port/include/serial_port/lightweightserial.h (rev 0)
+++ pkg/trunk/serial_port/include/serial_port/lightweightserial.h 2008-03-27 23:56:14 UTC (rev 51)
@@ -0,0 +1,62 @@
+///////////////////////////////////////////////////////////////////////////////
+// The serial_port package provides small, simple static libraries to access
+// serial devices
+//
+// Copyright (C) 2008, Morgan Quigley
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+// * Redistributions of source code must retain the above copyright notice,
+// this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above copyright
+// notice, this list of conditions and the following disclaimer in the
+// documentation and/or other materials provided with the distribution.
+// * Neither the name of Stanford University nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+// POSSIBILITY OF SUCH DAMAGE.
+
+#ifndef SERIALPORT_LIGHTWEIGHT_SERIAL_H
+#define SERIALPORT_LIGHTWEIGHT_SERIAL_H
+
+#include <stdint.h>
+
+class LightweightSerial
+{
+ public:
+ LightweightSerial(const char *port, int baud);
+ ~LightweightSerial();
+
+ bool read(uint8_t *b);
+ int read_block(uint8_t *block, uint32_t max_read_len);
+
+ bool write(const uint8_t b);
+ bool write_block(const uint8_t *block, uint32_t write_len);
+
+ inline bool is_ok() { return happy; }
+
+ // type-conversion wrappers so we can handle either signed or unsigned
+ inline bool read(char *c) { return read((uint8_t *)c); }
+ inline int read_block(char *block, uint32_t max_read_len) { return read_block(block, max_read_len); }
+ inline bool write(char c) { return write((uint8_t)c); }
+ inline bool write_block(const char *block, uint32_t write_len) { return write_block((uint8_t *)block, write_len); }
+
+ private:
+ int baud;
+ int fd;
+ bool happy;
+};
+
+#endif
+
Added: pkg/trunk/serial_port/manifest.xml
===================================================================
--- pkg/trunk/serial_port/manifest.xml (rev 0)
+++ pkg/trunk/serial_port/manifest.xml 2008-03-27 23:56:14 UTC (rev 51)
@@ -0,0 +1,9 @@
+<package>
+<description brief="A few simple serial port classes">
+These are used all over the place for talking to hardware. These implementations are certainly suboptimal, but they do seem to work.
+</description>
+<author>Morgan Quigley (email: mqu...@cs...)</author>
+<license>BSD</license>
+<url>http://stair.stanford.edu</url>
+</package>
+
Added: pkg/trunk/serial_port/rosbuild
===================================================================
--- pkg/trunk/serial_port/rosbuild (rev 0)
+++ pkg/trunk/serial_port/rosbuild 2008-03-27 23:56:14 UTC (rev 51)
@@ -0,0 +1,2 @@
+#!/usr/bin/env ruby
+exec("#{`#{ENV['ROS_ROOT']}/rospack find rostools`}/scripts/yamlbuild", 'build.yaml', *ARGV)
Property changes on: pkg/trunk/serial_port/rosbuild
___________________________________________________________________
Name: svn:executable
+ *
Added: pkg/trunk/serial_port/src/libserial_port/Makefile
===================================================================
--- pkg/trunk/serial_port/src/libserial_port/Makefile (rev 0)
+++ pkg/trunk/serial_port/src/libserial_port/Makefile 2008-03-27 23:56:14 UTC (rev 51)
@@ -0,0 +1,5 @@
+SRC = lightweightserial.cpp
+OUT = ../../lib/libserial_port.a
+PKG = serial_port
+CFLAGS = -I../../include
+include $(shell $(ROS_ROOT)/rospack find roscpp)/make_include/only_rules_lib.mk
Added: pkg/trunk/serial_port/src/libserial_port/lightweightserial.cpp
===================================================================
--- pkg/trunk/serial_port/src/libserial_port/lightweightserial.cpp (rev 0)
+++ pkg/trunk/serial_port/src/libserial_port/lightweightserial.cpp 2008-03-27 23:56:14 UTC (rev 51)
@@ -0,0 +1,124 @@
+///////////////////////////////////////////////////////////////////////////////
+// The serial_port package provides small, simple static libraries to access
+// serial devices
+//
+// Copyright (C) 2008, Morgan Quigley
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+// * Redistributions of source code must retain the above copyright notice,
+// this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above copyright
+// notice, this list of conditions and the following disclaimer in the
+// documentation and/or other materials provided with the distribution.
+// * Neither the name of Stanford University nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+// POSSIBILITY OF SUCH DAMAGE.
+
+#include "serial_port/lightweightserial.h"
+#include <stdio.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <termios.h>
+#include <string.h>
+
+LightweightSerial::LightweightSerial(const char *port, int baud) :
+ baud(baud), happy(false), fd(0)
+{
+ printf("about to try to open [%s]\n", port);
+ fd = open(port, O_RDWR | O_NOCTTY | O_NONBLOCK);
+ if (fd < 0)
+ {
+ printf(" ahhhhhhhhh couldn't open port [%s]\n", port);
+ return;
+ }
+ else
+ printf("opened [%s] successfully\n", port);
+ // put the port in nonblocking mode
+ struct termios oldtio, newtio;
+ if (tcgetattr(fd, &oldtio) < 0)
+ {
+ printf("ahhhhhhh couldn't run tcgetattr()\n");
+ return;
+ }
+ bzero(&newtio, sizeof(newtio));
+ newtio.c_iflag = IGNPAR | INPCK;
+ newtio.c_oflag = 0;
+ newtio.c_cflag = CS8 | CLOCAL | CREAD;
+ newtio.c_lflag = 0;
+ newtio.c_cc[VTIME] = 0;
+ newtio.c_cc[VMIN] = 0; // poll
+ cfsetspeed(&newtio, baud);
+ tcflush(fd, TCIOFLUSH);
+ if (tcsetattr(fd, TCSANOW, &newtio) < 0)
+ {
+ printf(" ahhhhhhhhhhh tcsetattr failed\n");
+ return;
+ }
+
+ // flush the buffer of the serial device
+ uint8_t b;
+ while (this->read(&b) > 0);
+ happy = true;
+}
+
+LightweightSerial::~LightweightSerial()
+{
+ if (fd > 0)
+ close(fd);
+ fd = 0; // prevent future reads...
+}
+
+bool LightweightSerial::read(uint8_t *b)
+{
+ if (!happy)
+ return false;
+ unsigned long nread;
+ nread = ::read(fd,b,1);
+ if (nread < 0)
+ {
+ printf("ahhhhhh read returned <0\n");
+ happy = false;
+ return false;
+ }
+ return (nread == 1);
+}
+
+int LightweightSerial::read_block(uint8_t *block, uint32_t max_read_len)
+{
+ if (!happy)
+ return false;
+ unsigned long nread = ::read(fd,block,(size_t)max_read_len);
+ return (nread < 0 ? 0 : nread);
+}
+
+bool LightweightSerial::write(const uint8_t b)
+{
+ if (!happy)
+ return false;
+ if (fd >= 0 && ::write(fd, &b, 1) < 0)
+ return false;
+ else
+ return true;
+}
+
+bool LightweightSerial::write_block(const uint8_t *block, uint32_t block_len)
+{
+ if (fd >= 0 && ::write(fd, block, block_len) < 0)
+ return false;
+ tcflush(fd, TCOFLUSH);
+ return true;
+}
+
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|