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