00001 /* +---------------------------------------------------------------------------+ 00002 | The Mobile Robot Programming Toolkit (MRPT) C++ library | 00003 | | 00004 | http://www.mrpt.org/ | 00005 | | 00006 | Copyright (C) 2005-2011 University of Malaga | 00007 | | 00008 | This software was written by the Machine Perception and Intelligent | 00009 | Robotics Lab, University of Malaga (Spain). | 00010 | Contact: Jose-Luis Blanco <jlblanco@ctima.uma.es> | 00011 | | 00012 | This file is part of the MRPT project. | 00013 | | 00014 | MRPT is free software: you can redistribute it and/or modify | 00015 | it under the terms of the GNU General Public License as published by | 00016 | the Free Software Foundation, either version 3 of the License, or | 00017 | (at your option) any later version. | 00018 | | 00019 | MRPT is distributed in the hope that it will be useful, | 00020 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 00021 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 00022 | GNU General Public License for more details. | 00023 | | 00024 | You should have received a copy of the GNU General Public License | 00025 | along with MRPT. If not, see <http://www.gnu.org/licenses/>. | 00026 | | 00027 +---------------------------------------------------------------------------+ */ 00028 00029 #ifndef CSERIALPORT_H 00030 #define CSERIALPORT_H 00031 00032 #include <mrpt/config.h> 00033 #include <mrpt/utils/CStream.h> 00034 #include <mrpt/utils/CTicTac.h> 00035 #include <mrpt/utils/stl_extensions.h> 00036 #include <queue> 00037 00038 #include <mrpt/hwdrivers/link_pragmas.h> 00039 00040 namespace mrpt 00041 { 00042 namespace hwdrivers 00043 { 00044 using namespace mrpt::utils; 00045 /** A communications serial port built as an implementation of a utils::CStream. 00046 * On communication errors (eg. the given port number does not exist, timeouts,...), most of the methods will 00047 * raise an exception of the class "std::exception" 00048 * 00049 * The serial port to open is passed in the constructor in the form of a string description, 00050 * which is platform dependent. 00051 * 00052 * In windows they are numbered "COM1"-"COM4" and "\\.\COMXXX" for numbers above. 00053 * It is recomended to always use the prefix "\\.\" despite the actual port number. 00054 * 00055 * In Linux the name must refer to the device, for example: "ttyUSB0","ttyS0". If the name string does not 00056 * start with "/" (an absolute path), the constructor will assume the prefix "/dev/". 00057 * 00058 * History: 00059 * - 1/DEC/2005: (JLBC) First version 00060 * - 20/DEC/2006: (JLBC) Integration into the MRPT framework 00061 * - 12/DEC/2007: (JLBC) Added support for Linux. 00062 * 00063 * \todo Add the internal buffer to the Windows implementation also 00064 * \ingroup mrpt_hwdrivers_grp 00065 */ 00066 class HWDRIVERS_IMPEXP CSerialPort : public CStream 00067 { 00068 friend class PosixSignalDispatcherImpl; 00069 public: 00070 /** Constructor 00071 * \param portName The serial port to open. See comments at the begining of this page. 00072 * \param openNow Whether to try to open the port now. If not selected, the port should be open later with "open()". 00073 * 00074 */ 00075 CSerialPort( const std::string &portName, bool openNow = true ); 00076 00077 /** Default constructor: it does not open any port - later you must call "setSerialPortName" and then "open" 00078 */ 00079 CSerialPort(); 00080 00081 /** Destructor 00082 */ 00083 virtual ~CSerialPort(); 00084 00085 /** Sets the serial port to open (it is an error to try to change this while open yet). 00086 * \sa open, close 00087 */ 00088 void setSerialPortName( const std::string &COM_name ) 00089 { 00090 if (isOpen()) THROW_EXCEPTION("Cannot change serial port while open"); 00091 m_serialName = COM_name; 00092 } 00093 00094 /** Open the port. If is already open results in no action. 00095 * \exception std::exception On communication errors 00096 */ 00097 void open(); 00098 00099 /** Open the given serial port. If it is already open and the name does not match, an exception is raised. 00100 * \exception std::exception On communication errors or a different serial port already open. 00101 */ 00102 void open(const std::string &COM_name) 00103 { 00104 if (isOpen() && m_serialName!=COM_name) THROW_EXCEPTION("Cannot change serial port while open"); 00105 if (!isOpen()) 00106 { 00107 setSerialPortName(COM_name); 00108 open(); 00109 } 00110 } 00111 00112 00113 /** Close the port. If is already closed, results in no action. 00114 */ 00115 void close(); 00116 00117 /** Returns if port has been correctly open. 00118 */ 00119 bool isOpen(); 00120 00121 /** Purge tx and rx buffers. 00122 * \exception std::exception On communication errors 00123 */ 00124 void purgeBuffers(); 00125 00126 /** Changes the configuration of the port. 00127 * \param parity 0:No parity, 1:Odd, 2:Even (WINDOWS ONLY: 3:Mark, 4:Space) 00128 * \param baudRate The desired baud rate Accepted values: 50 - 230400 00129 * \param bits Bits per word (typ. 8) Accepted values: 5,6,7,8. 00130 * \param nStopBits Stop bits (typ. 1) Accepted values: 1,2 00131 * \param enableFlowControl Whether to enable the hardware flow control (RTS/CTS) (default=no) 00132 * \exception std::exception On communication errors 00133 */ 00134 void setConfig( 00135 int baudRate, 00136 int parity = 0, 00137 int bits = 8, 00138 int nStopBits = 1, 00139 bool enableFlowControl=false); 00140 00141 /** Changes the timeouts of the port, in milliseconds. 00142 * \exception std::exception On communication errors 00143 */ 00144 void setTimeouts( 00145 int ReadIntervalTimeout, 00146 int ReadTotalTimeoutMultiplier, 00147 int ReadTotalTimeoutConstant, 00148 int WriteTotalTimeoutMultiplier, 00149 int WriteTotalTimeoutConstant ); 00150 00151 00152 /** Implements the virtual method responsible for reading from the stream - Unlike CStream::ReadBuffer, this method will not raise an exception on zero bytes read, as long as there is not any fatal error in the communications. 00153 * \exception std::exception On communication errors 00154 */ 00155 size_t Read(void *Buffer, size_t Count); 00156 00157 /** Reads one text line from the serial port in POSIX "canonical mode". 00158 * This method reads from the serial port until one of the characters in \a eol are found. 00159 * \param eol_chars A line reception is finished when one of these characters is found. Default: LF (10), CR (13). 00160 * \param total_timeout_ms If >0, the maximum number of milliseconds to wait. 00161 * \param out_timeout If provided, will hold true on return if a timeout ocurred, false on a valid read. 00162 * \return The read string, without the final 00163 * \exception std::exception On communication errors 00164 */ 00165 std::string ReadString(const int total_timeout_ms=-1, bool *out_timeout =NULL, const char *eol_chars = "\r\n"); 00166 00167 /** Implements the virtual method responsible for writing to the stream. 00168 * Write attempts to write up to Count bytes to Buffer, and returns the number of bytes actually written. 00169 * \exception std::exception On communication errors 00170 */ 00171 size_t Write(const void *Buffer, size_t Count); 00172 00173 00174 /** Introduces a pure virtual method for moving to a specified position in the streamed resource. 00175 * he Origin parameter indicates how to interpret the Offset parameter. Origin should be one of the following values: 00176 * - sFromBeginning (Default) Offset is from the beginning of the resource. Seek moves to the position Offset. Offset must be >= 0. 00177 * - sFromCurrent Offset is from the current position in the resource. Seek moves to Position + Offset. 00178 * - sFromEnd Offset is from the end of the resource. Offset must be <= 0 to indicate a number of bytes before the end of the file. 00179 * \return Seek returns the new value of the Position property. 00180 */ 00181 uint64_t Seek(long Offset, CStream::TSeekOrigin Origin = sFromBeginning) 00182 { 00183 MRPT_START 00184 MRPT_UNUSED_PARAM(Origin); 00185 MRPT_UNUSED_PARAM(Offset); 00186 THROW_EXCEPTION("Method not applicable to serial communications port CStream!"); 00187 MRPT_END 00188 } 00189 00190 /** Returns the total amount of bytes in the stream. 00191 */ 00192 uint64_t getTotalBytesCount() 00193 { 00194 MRPT_START 00195 THROW_EXCEPTION("Method not applicable to serial communications port CStream!"); 00196 MRPT_END 00197 } 00198 00199 /** Method for getting the current cursor position, where 0 is the first byte and TotalBytesCount-1 the last one. 00200 */ 00201 uint64_t getPosition() 00202 { 00203 MRPT_START 00204 THROW_EXCEPTION("Method not applicable to serial communications port CStream!"); 00205 MRPT_END 00206 } 00207 00208 protected: 00209 00210 /** The complete name of the serial port device (i.e. "\\.\COM10","/dev/ttyS2",...) 00211 */ 00212 std::string m_serialName; 00213 int m_baudRate; 00214 int m_totalTimeout_ms,m_interBytesTimeout_ms; 00215 00216 CTicTac m_timer; //!< Used only in \a ReadString 00217 00218 #ifdef MRPT_OS_WINDOWS 00219 // WINDOWS 00220 void *hCOM; 00221 #else 00222 // LINUX 00223 /** The file handle (-1: Not open) 00224 */ 00225 int hCOM; 00226 // size_t ReadUnbuffered(void *Buffer, size_t Count); // JL: Remove?? 00227 #endif 00228 00229 }; // end of class 00230 00231 } // end of namespace 00232 } // end of namespace 00233 00234 #endif
| Page generated by Doxygen 1.7.5 for MRPT 0.9.5 SVN: at Thu Oct 13 21:25:36 UTC 2011 |