serialport.cc

Go to the documentation of this file.
00001 
00003 
00004 //
00005 // Copyright (c) 2000-2004 California Institute of Technology
00006 // Copyright (c) 2004-2007 University of Southern California
00007 // Rob Peters <rjpeters at usc dot edu>
00008 //
00009 // created: Wed Mar 29 13:46:11 2000
00010 // commit: $Id: serialport.cc 10065 2007-04-12 05:54:56Z rjpeters $
00011 // $HeadURL: file:///lab/rjpeters/svnrepo/code/trunk/groovx/src/rutz/serialport.cc $
00012 //
00013 // --------------------------------------------------------------------
00014 //
00015 // This file is part of GroovX.
00016 //   [http://ilab.usc.edu/rjpeters/groovx/]
00017 //
00018 // GroovX is free software; you can redistribute it and/or modify it
00019 // under the terms of the GNU General Public License as published by
00020 // the Free Software Foundation; either version 2 of the License, or
00021 // (at your option) any later version.
00022 //
00023 // GroovX is distributed in the hope that it will be useful, but
00024 // WITHOUT ANY WARRANTY; without even the implied warranty of
00025 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00026 // General Public License for more details.
00027 //
00028 // You should have received a copy of the GNU General Public License
00029 // along with GroovX; if not, write to the Free Software Foundation,
00030 // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
00031 //
00033 
00034 #ifndef GROOVX_RUTZ_SERIALPORT_CC_UTC20050626084020_DEFINED
00035 #define GROOVX_RUTZ_SERIALPORT_CC_UTC20050626084020_DEFINED
00036 
00037 #include "rutz/serialport.h"
00038 
00039 #ifdef HAVE_EXT_STDIO_FILEBUF_H
00040 #include <ext/stdio_filebuf.h>
00041 #else
00042 #include "rutz/stdiobuf.h"
00043 #endif
00044 
00045 #include <cstdio>
00046 #include <fcntl.h>
00047 #include <istream>
00048 #include <termios.h>
00049 #include <unistd.h>
00050 
00051 #include "rutz/trace.h"
00052 #include "rutz/debug.h"
00053 GVX_DBG_REGISTER
00054 
00055 rutz::serial_port::serial_port(const char* serial_device) :
00056   m_filedes(::open(serial_device, O_RDONLY|O_NOCTTY|O_NONBLOCK)),
00057   m_filebuf(0),
00058   m_stream(0),
00059   m_exit_status(0)
00060 {
00061 GVX_TRACE("rutz::serial_port::serial_port");
00062   if (m_filedes != -1)
00063     {
00064       struct termios ti;
00065 
00066       if ( tcgetattr( filedes(), &ti ) == -1 )
00067         { close(); return; }
00068 
00069       // input modes:
00070       //   ignore BREAK condition on input,
00071       //   ignore framing errors and parity errors
00072       ti.c_iflag = IGNBRK | IGNPAR;
00073 
00074       // output modes:
00075       //   none
00076       ti.c_oflag = 0x0;
00077 
00078       // control modes:
00079       //    character size 8
00080       //    ignore modem control lines
00081       //    baud rate 9600
00082       //    enable receiver
00083       ti.c_cflag = CS8 | CLOCAL | B9600 | CREAD;
00084 
00085       // local modes:
00086       //    none
00087       ti.c_lflag = 0;
00088 
00089       // control characters
00090       ti.c_cc[VTIME] = 0;         // intercharacter timer
00091       ti.c_cc[VMIN]  = 1;         // minimum number of characters to be read
00092 
00093       if ( tcsetattr( filedes(), TCSANOW, &ti) == -1 )
00094         { close(); return; }
00095 
00096 #ifdef HAVE_EXT_STDIO_FILEBUF_H
00097       typedef __gnu_cxx::stdio_filebuf<char> filebuf_t;
00098       m_filebuf = new filebuf_t(fdopen(m_filedes, "r"), std::ios::in);
00099 #else
00100       m_filebuf = new rutz::stdiobuf(m_filedes, std::ios::in, true);
00101 #endif
00102       m_stream = new std::iostream(m_filebuf);
00103     }
00104 }
00105 
00106 int rutz::serial_port::get()
00107 {
00108   if ( is_closed() ) return -1;
00109 
00110   m_stream->clear();
00111   m_stream->sync();
00112   return m_stream->get();
00113 }
00114 
00115 int rutz::serial_port::close()
00116 {
00117 GVX_TRACE("rutz::serial_port::close");
00118   if ( !is_closed() )
00119     {
00120       delete m_stream;
00121       m_stream = 0;
00122 
00123       delete m_filebuf;
00124       m_filebuf = 0;
00125 
00126       m_exit_status = ::close(m_filedes);
00127     }
00128   return m_exit_status;
00129 }
00130 
00131 static const char __attribute__((used)) vcid_groovx_rutz_serialport_cc_utc20050626084020[] = "$Id: serialport.cc 10065 2007-04-12 05:54:56Z rjpeters $ $HeadURL: file:
00132 #endif // !GROOVX_RUTZ_SERIALPORT_CC_UTC20050626084020_DEFINED

The software described here is Copyright (c) 1998-2005, Rob Peters.
This page was generated Wed Dec 3 06:49:40 2008 by Doxygen version 1.5.5.