00001 /** 00002 \file SerialPortClass.h 00003 \brief A class for handling serial port communications. \n 00004 The intention is for this class to handle WIN32 serial port 00005 communications and eventually also linux serial port 00006 communications. A preprocessor directive will be used 00007 to distinguish the build. 00008 00009 \author Glenn D. MacGougan (GDM) 00010 \date 2008-11-26 00011 \since 2008-11-26 00012 00013 \b "LICENSE INFORMATION" \n 00014 Copyright (c) 2008, refer to 'author' doxygen tags \n 00015 All rights reserved. \n 00016 00017 Redistribution and use in source and binary forms, with or without 00018 modification, are permitted provided the following conditions are met: \n 00019 00020 - Redistributions of source code must retain the above copyright 00021 notice, this list of conditions and the following disclaimer. \n 00022 - Redistributions in binary form must reproduce the above copyright 00023 notice, this list of conditions and the following disclaimer in the 00024 documentation and/or other materials provided with the distribution. \n 00025 - The name(s) of the contributor(s) may not be used to endorse or promote 00026 products derived from this software without specific prior written 00027 permission. \n 00028 00029 THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND ANY EXPRESS 00030 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00031 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00032 DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 00033 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00034 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00035 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00036 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00037 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 00038 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 00039 SUCH DAMAGE. 00040 */ 00041 00042 #ifndef _SERIALPORT_H_ 00043 #define _SERIALPORT_H_ 00044 00045 #include "serialport.h" // 'C' functions for serial communications 00046 00047 00048 class SerialPort 00049 { 00050 public: // Lifecycle 00051 00052 /// \brief SerialPort destructor 00053 virtual ~SerialPort(); 00054 00055 /// \brief The default constructor. 00056 SerialPort(); 00057 00058 /// \brief The disabled copy constructor. 00059 SerialPort( SerialPort& rhs ) {} ; 00060 00061 public: // Assignment 00062 00063 /// \brief operator= overload (GDM_TODO should be disabled) 00064 const SerialPort& operator=( const SerialPort& src ) { return *this; } 00065 00066 public: // Usage 00067 00068 /** 00069 \brief Open the serial port with the settings specified 00070 00071 \return true if succesful, false otherwise. 00072 */ 00073 bool Open( 00074 const unsigned short portnumber, //!< The COM port number [1-N]. 00075 const SERIALPORT_enumBaudRate baudrate, //!< The port baud rate [bps]. 00076 const SERIALPORT_enumParity parity = SERIALPORT_PARITY_NONE, //!< The port parity setting. 00077 const SERIALPORT_enumDataBits databits = SERIALPORT_DATABITS_8, //!< The port data bits setting. 00078 const SERIALPORT_enumStopBits stopbits = SERIALPORT_STOPBITS_1, //!< The port stop bits setting. 00079 const SERIALPORT_enumFlowControl flowcontrol = SERIALPORT_FLOWCONTROL_NONE, //!< The port flow control setting. 00080 const SERIALPORT_enumBehaviour behaviour = SERIALPORT_BEHAVIOUR_RETURN_IMMEDIATELY, //!< The port's read behaviour (settings for waiting on the read timeout). 00081 const unsigned timeout_in_ms = 0 //!< The read function's timeout [ms]. Not used if behaviour == SERIALPORT_BEHAVIOUR_RETURN_IMMEDIATELY. 00082 ); 00083 00084 /** 00085 \brief Read data from the serial port. 00086 00087 \pre Open must have been called. 00088 00089 \return true if succesful, false otherwise. 00090 */ 00091 bool Read( 00092 unsigned char* buffer, //!< A valid buffer (!NULL) in which to place data. 00093 const unsigned bufferSize, //!< The size of the buffer [bytes]. 00094 unsigned& nrBytesRead //!< The number of bytes read [bytes]. 00095 ); 00096 00097 /** 00098 \brief Write data to the serial port. 00099 00100 \pre Open must have been called. 00101 00102 \return true if succesful, false otherwise. 00103 */ 00104 bool Write( 00105 unsigned char* buffer, //!< A valid buffer (!NULL) with data to be written to the port. 00106 const unsigned nrBytesToWrite //!< The number of bytes in the buffer to write [bytes]. 00107 ); 00108 00109 00110 /** 00111 \brief Wait for the Ring Indicator pin to signal an event. 00112 00113 \pre Open must have been called. 00114 00115 \return true if succesful, false otherwise. 00116 */ 00117 bool WaitOnRingIndicator( unsigned short timeout_ms, bool& ring_detected ); 00118 00119 00120 /** 00121 \brief Close the serial port. 00122 00123 \pre Open must have been called. 00124 00125 \return true if succesful, false otherwise. 00126 */ 00127 bool Close(); 00128 00129 /// Get the current serial port number used. 00130 unsigned short GetPortNumber() { return m_serialport.port; } 00131 00132 protected: // Member Variables 00133 00134 /// \brief The serial port 'c' container. 00135 SERIALPORT_structObject m_serialport; 00136 00137 }; 00138 00139 00140 #endif // _SERIALPORT_H_