00001 /*!@file Devices/SerialAdvanced.H class for interfacing with a serial port */ 00002 00003 // //////////////////////////////////////////////////////////////////// // 00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the // 00005 // University of Southern California (USC) and the iLab at USC. // 00006 // See http://iLab.usc.edu for information about this project. // 00007 // //////////////////////////////////////////////////////////////////// // 00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00010 // in Visual Environments, and Applications'' by Christof Koch and // 00011 // Laurent Itti, California Institute of Technology, 2001 (patent // 00012 // pending; application number 09/912,225 filed July 23, 2001; see // 00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00014 // //////////////////////////////////////////////////////////////////// // 00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00016 // // 00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00018 // redistribute it and/or modify it under the terms of the GNU General // 00019 // Public License as published by the Free Software Foundation; either // 00020 // version 2 of the License, or (at your option) any later version. // 00021 // // 00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00025 // PURPOSE. See the GNU General Public License for more details. // 00026 // // 00027 // You should have received a copy of the GNU General Public License // 00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00030 // Boston, MA 02111-1307 USA. // 00031 // //////////////////////////////////////////////////////////////////// // 00032 // 00033 // Primary maintainer for this file: Nitin Dhavale <dhavale@usc.edu> 00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Devices/SerialAdvanced.H $ 00035 // $Id: SerialAdvanced.H 11656 2009-09-04 01:25:53Z rand $ 00036 // 00037 00038 #ifndef __SERIAL_H__ 00039 #define __SERIAL_H__ 00040 00041 #include "Component/ModelComponent.H" 00042 #include "Component/ModelParam.H" 00043 #include "Component/ModelOptionDef.H" 00044 00045 #include <fcntl.h> 00046 #include <stdio.h> 00047 #include <sys/stat.h> 00048 #include <sys/types.h> 00049 #include <termios.h> 00050 #include <unistd.h> 00051 00052 //! enum for different serial errors that could occur 00053 enum serialError 00054 { 00055 serialErrSuccess = 0, 00056 serialErrOpenNoTty, 00057 serialErrOpenFailed, 00058 serialErrSpeedInvalid, 00059 serialErrFlowInvalid, 00060 serialErrParityInvalid, 00061 serialErrCharsizeInvalid, 00062 serialErrStopbitsInvalid, 00063 serialErrOptionInvalid, 00064 serialErrResourceFailure, 00065 serialErrOutput, 00066 serialErrInput, 00067 serialErrTimeout, 00068 serialErrExtended, 00069 serialErrReadFailed, 00070 serialErrReadTimedOut, 00071 serialErrWriteFailed, 00072 serialErrFcntlFailed, 00073 serialErrTcGetAttrFailed, 00074 serialErrTcSetAttrFailed 00075 }; 00076 00077 //! Interface to a serial port 00078 /*! The port will be open at start() time; see ModelComponent.H for 00079 details. Because typically one may use several serial ports with 00080 different configs, this ModelComponent does not export any 00081 command-line option (there would be too many otherwise if many ports 00082 are used). Typically, thus, the port can be configured either via 00083 config files or using the various access functions provided in this 00084 class. */ 00085 class SerialAdvanced : public ModelComponent { 00086 public: 00087 //! Constructor 00088 SerialAdvanced(OptionManager& mgr, const std::string& descrName = "SerialAdvanced Port", 00089 const std::string& tagName = "SerialAdvancedPort"); 00090 00091 //! destructor 00092 ~SerialAdvanced(void); 00093 00094 //! Configure the port before it is started. 00095 /*! This will update our internal ModelParam values to the specified 00096 ones; thus this should be called before start(). In contrast, 00097 the setXXX functions below are for reconfiguration at runtime, 00098 once the port is already open and running. This function is 00099 provided as a shortcut to set a bunch of ModelParam values in 00100 one simple call. 00101 @param dev device name (e.g., "/dev/ttyS0") 00102 @param speed speed in bauds. See setSpeed() for valid values 00103 @param format a 3-char string for charbits, parity, and stop bits; for 00104 example "8N1"; parity should be "N", "E" or "O". 00105 @param flowSoft use software flow control if true 00106 @param flowHard use hardware flow control if true 00107 @param tout read timeout in 1/10000s or 0 for no timeout */ 00108 void configure(const char *dev = "/dev/ttyS0", const int speed = 9600, 00109 const char *format = "8N1", 00110 const bool flowSoft = false, const bool flowHard = true, 00111 const int = 0); 00112 00113 00114 //! Set the DTR mode off momentarily. 00115 /*! @param millisec number of milliseconds. */ 00116 void toggleDTR(const time_t millisec); 00117 00118 //! transmit continuous stream of zero-valued bits for specific duration. 00119 void sendBreak(void); 00120 00121 //! attempt to read up to nbytes from serial port into the buffer 00122 /*! @param buffer holds bytes after read 00123 @nbytes number of bytes to attempt to read 00124 @return 0 on a timeout, -1 on error, number of bytes actually read 00125 on a success */ 00126 int read(void* buffer, const int nbytes); 00127 00128 //! attempts to get the next character from the serial port 00129 /*! for comptability with dirk's code */ 00130 char read(void); 00131 00132 //! write bytes to the port 00133 /*! @param buffer begin writing from the location buffer. 00134 @param nbytes number of bytes to write 00135 @return -1 on failure, number of bytes written on success */ 00136 int write(const void* buffer, const int nbytes); 00137 00138 //! print a verbal message indicating the last error. 00139 void perror(void); 00140 00141 protected: 00142 //! open the port and get started 00143 void start2(); 00144 00145 //! close the port and get stopped 00146 void stop2(); 00147 00148 private: 00149 int dev; // descriptor associated with the device file 00150 serialError serialErrno; // error id of the last error 00151 termios savedState; // saved state to restore in the destructor 00152 00153 // set our errno 00154 serialError error(const serialError serialErrorNum); 00155 00156 // open the serial port. This is called from the constructor, so there is 00157 // no need to explicitly call this 00158 // @return -1 on error 00159 int openPort(); 00160 00161 //! Set serial port speed for both input and output. 00162 /*! This function is to change the speed while already started. 00163 @param speed to select. 0 signifies modem "hang up" and possible 00164 values are 0, 110, 300, 600, 1200, 2400, 4800, 9600, 00165 19200, 38400, 57600, 115200. 00166 @return 0 or serialErrSuccess on success. The perror() method 00167 can be used to print a longer description of the error. */ 00168 serialError setSpeed(const int speed); 00169 00170 //! Set the serial port flow control on Input as well as Ouput 00171 /*! This function is to change the flow control while already started. 00172 @param useHard use hardware flow control if true 00173 @param useSoft use software flow control if true 00174 @return 0 or serialErrSuccess on success */ 00175 serialError setFlowControl(const bool useHard, const bool useSoft); 00176 00177 //! Set character size. 00178 /*! This function is to change the char size while already started. 00179 @return 0 on success. 00180 @param bits character size to use; valid values are 5, 6, 7, 8. */ 00181 serialError setCharBits(const int bits); 00182 00183 //! Set parity mode. 00184 /*! This function is to change the parity while already started. 00185 @param useParity use some parity if true 00186 @param oddParity parity is odd if true, otherwise even 00187 @return 0 on success. */ 00188 serialError setParity(const bool useParity, const bool oddParity); 00189 00190 //! Set number of stop bits. 00191 /*! This function is to change the stop bits while started. 00192 @return 0 on success. 00193 @param bits stop bits (only values 1 and 2 are valid). */ 00194 serialError setStopBits(const int bits); 00195 00196 //! Set the access to blocking or not 00197 /*! If blocking, a write will block until all data has actually been 00198 sent through; otherwise it will return immediately while the data 00199 is being transmitted. */ 00200 serialError setBlocking(const bool blocking); 00201 00202 OModelParam<std::string> itsDevName; //! device name 00203 OModelParam<int> itsBaud; //!< Baudrate 00204 OModelParam<int> itsCharBits; //!< number of useful bits per char 00205 OModelParam<int> itsStopBits; //!< number of stop bits 00206 OModelParam<bool> itsUseParity; //!< use parity if true 00207 OModelParam<bool> itsParityOdd; //!< parity is odd if true, otherwise even 00208 OModelParam<bool> itsFlowHard; //!< use hardware flow control if true 00209 OModelParam<bool> itsFlowSoft; //!< use software flow control if true 00210 OModelParam<int> itsRdTout; //!< read timeout in 1/10000s 00211 OModelParam<bool> itsBlocking; //!< use blocking mode 00212 00213 OModelParam<std::string> itsDevSearchDescriptor; //!< A string to search for on all serial ports 00214 00215 }; 00216 00217 #endif 00218 00219 // ###################################################################### 00220 /* So things look consistent in everyone's emacs... */ 00221 /* Local Variables: */ 00222 /* indent-tabs-mode: nil */ 00223 /* End: */