00001 /* +---------------------------------------------------------------------------+ 00002 | The Mobile Robot Programming Toolkit (MRPT) C++ library | 00003 | | 00004 | http://mrpt.sourceforge.net/ | 00005 | | 00006 | Copyright (C) 2005-2009 University of Malaga | 00007 | | 00008 | This software was written by the Perception and Robotics | 00009 | research group, 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 <queue> 00035 00036 #include <mrpt/hwdrivers/link_pragmas.h> 00037 00038 namespace mrpt 00039 { 00040 namespace hwdrivers 00041 { 00042 using namespace mrpt::utils; 00043 /** A communications serial port built as an implementation of a utils::CStream. 00044 * On communication errors (eg. the given port number does not exist, timeouts,...), most of the methods will 00045 * raise an exception of the class "std::exception" 00046 * 00047 * The serial port to open is passed in the constructor in the form of a string description, 00048 * which is platform dependent. 00049 * 00050 * In windows they are numbered "COM1"-"COM4" and "\\.\COMXXX" for numbers above. 00051 * It is recomended to always use the prefix "\\.\" despite the actual port number. 00052 * 00053 * In Linux the name must refer to the device, for example: "ttyUSB0","ttyS0". If the name string does not 00054 * start with "/" (an absolute path), the constructor will assume the prefix "/dev/". 00055 * 00056 * History: 00057 * - 1/DEC/2005: (JLBC) First version 00058 * - 20/DEC/2006: (JLBC) Integration into the MRPT framework 00059 * - 12/DEC/2007: (JLBC) Added support for Linux. 00060 * 00061 * \todo Add the internal buffer to the Windows implementation also 00062 */ 00063 class HWDLLIMPEXP CSerialPort : public CStream 00064 { 00065 friend class PosixSignalDispatcherImpl; 00066 public: 00067 /** Constructor 00068 * \param portName The serial port to open. See comments at the begining of this page. 00069 * \param openNow Whether to try to open the port now. If not selected, the port should be open later with "open()". 00070 * 00071 */ 00072 CSerialPort( const std::string &portName, bool openNow = true ); 00073 00074 /** Default constructor: it does not open any port - later you must call "setSerialPortName" and then "open" 00075 */ 00076 CSerialPort(); 00077 00078 /** Destructor 00079 */ 00080 virtual ~CSerialPort(); 00081 00082 /** Sets the serial port to open (it is an error to try to change this while open yet). 00083 * \sa open, close 00084 */ 00085 void setSerialPortName( const std::string &COM_name ) 00086 { 00087 if (isOpen()) THROW_EXCEPTION("Cannot change serial port while open"); 00088 m_serialName = COM_name; 00089 } 00090 00091 /** Open the port. If is already open results in no action. 00092 * \exception std::exception On communication errors 00093 */ 00094 void open(); 00095 00096 /** Open the given serial port. If it is already open and the name does not match, an exception is raised. 00097 * \exception std::exception On communication errors or a different serial port already open. 00098 */ 00099 void open(const std::string &COM_name) 00100 { 00101 if (isOpen() && m_serialName!=COM_name) THROW_EXCEPTION("Cannot change serial port while open"); 00102 if (!isOpen()) 00103 { 00104 setSerialPortName(COM_name); 00105 open(); 00106 } 00107 } 00108 00109 00110 /** Close the port. If is already closed, results in no action. 00111 */ 00112 void close(); 00113 00114 /** Returns if port has been correctly open. 00115 */ 00116 bool isOpen(); 00117 00118 /** Purge tx and rx buffers. 00119 * \exception std::exception On communication errors 00120 */ 00121 void purgeBuffers(); 00122 00123 /** Changes the configuration of the port. 00124 * \param parity 0:No parity, 1:Odd, 2:Even (WINDOWS ONLY: 3:Mark, 4:Space) 00125 * \param baudRate The desired baud rate Accepted values: 50 - 230400 00126 * \param bits Bits per word (typ. 8) Accepted values: 5,6,7,8. 00127 * \param nStopBits Stop bits (typ. 1) Accepted values: 1,2 00128 * \param enableFlowControl Whether to enable the hardware flow control (RTS/CTS) (default=no) 00129 * \exception std::exception On communication errors 00130 */ 00131 void setConfig( 00132 int baudRate, 00133 int parity = 0, 00134 int bits = 8, 00135 int nStopBits = 1, 00136 bool enableFlowControl=false); 00137 00138 /** Changes the timeouts of the port, in milliseconds. 00139 * \exception std::exception On communication errors 00140 */ 00141 void setTimeouts( 00142 int ReadIntervalTimeout, 00143 int ReadTotalTimeoutMultiplier, 00144 int ReadTotalTimeoutConstant, 00145 int WriteTotalTimeoutMultiplier, 00146 int WriteTotalTimeoutConstant ); 00147 00148 00149 /** 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. 00150 * \exception std::exception On communication errors 00151 */ 00152 size_t Read(void *Buffer, size_t Count); 00153 00154 /** Implements the virtual method responsible for writing to the stream. 00155 * Write attempts to write up to Count bytes to Buffer, and returns the number of bytes actually written. 00156 * \exception std::exception On communication errors 00157 */ 00158 size_t Write(const void *Buffer, size_t Count); 00159 00160 00161 /** Introduces a pure virtual method for moving to a specified position in the streamed resource. 00162 * he Origin parameter indicates how to interpret the Offset parameter. Origin should be one of the following values: 00163 * - sFromBeginning (Default) Offset is from the beginning of the resource. Seek moves to the position Offset. Offset must be >= 0. 00164 * - sFromCurrent Offset is from the current position in the resource. Seek moves to Position + Offset. 00165 * - 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. 00166 * \return Seek returns the new value of the Position property. 00167 */ 00168 size_t Seek(long Offset, CStream::TSeekOrigin Origin = sFromBeginning) 00169 { 00170 MRPT_TRY_START 00171 MRPT_UNUSED_PARAM(Origin); 00172 MRPT_UNUSED_PARAM(Offset); 00173 THROW_EXCEPTION("Method not applicable to serial communications port CStream!"); 00174 MRPT_TRY_END 00175 } 00176 00177 /** Returns the total amount of bytes in the stream. 00178 */ 00179 size_t getTotalBytesCount() 00180 { 00181 MRPT_TRY_START 00182 THROW_EXCEPTION("Method not applicable to serial communications port CStream!"); 00183 MRPT_TRY_END 00184 } 00185 00186 /** Method for getting the current cursor position, where 0 is the first byte and TotalBytesCount-1 the last one. 00187 */ 00188 size_t getPosition() 00189 { 00190 MRPT_TRY_START 00191 THROW_EXCEPTION("Method not applicable to serial communications port CStream!"); 00192 MRPT_TRY_END 00193 } 00194 00195 protected: 00196 00197 /** The complete name of the serial port device (i.e. "\\.\COM10","/dev/ttyS2",...) 00198 */ 00199 std::string m_serialName; 00200 int m_baudRate; 00201 int m_totalTimeout_ms,m_interBytesTimeout_ms; 00202 00203 #ifdef MRPT_OS_WINDOWS 00204 // WINDOWS 00205 void *hCOM; 00206 #else 00207 // LINUX 00208 /** The file handle (-1: Not open) 00209 */ 00210 int hCOM; 00211 #endif 00212 00213 }; // end of class 00214 00215 } // end of namespace 00216 } // end of namespace 00217 00218 #endif
Page generated by Doxygen 1.5.7.1 for MRPT 0.7.1 SVN: at Mon Aug 17 23:02:22 EDT 2009 |