SerialPortClass.cpp

Go to the documentation of this file.
00001 /**
00002 \file    SerialPortClass.cpp
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 #include <stdio.h>
00043 #include "SerialPortClass.h"
00044 
00045 
00046 SerialPort::~SerialPort()
00047 {
00048   if( m_serialport.isPortOpen )
00049   {
00050     if( !SERIALPORT_Close( &m_serialport ) )
00051     {
00052       SERIALPORT_ERROR_MSG( "SERIALPORT_Close returned FALSE." );
00053     }
00054   }
00055 }
00056 
00057 
00058 SerialPort::SerialPort() 
00059 {     
00060   if( !SERIALPORT_Initialize( &m_serialport) )
00061   {
00062     SERIALPORT_ERROR_MSG( "SERIALPORT_Init returned FALSE." );    
00063   }
00064 }
00065 
00066 bool SerialPort::Close()
00067 {
00068   if( m_serialport.isPortOpen )
00069   {
00070     if( !SERIALPORT_Close( &m_serialport ) )
00071     {
00072       SERIALPORT_ERROR_MSG( "SERIALPORT_Close returned FALSE." );
00073       return false;
00074     }
00075   }
00076   return true;
00077 }
00078 
00079 bool SerialPort::Open(
00080   const unsigned short              portnumber,   //!< The COM port number [1-N].
00081   const SERIALPORT_enumBaudRate     baudrate,     //!< The port baud rate [bps].
00082   const SERIALPORT_enumParity       parity,       //!< The port parity setting.
00083   const SERIALPORT_enumDataBits     databits,     //!< The port data bits setting.
00084   const SERIALPORT_enumStopBits     stopbits,     //!< The port stop bits setting.
00085   const SERIALPORT_enumFlowControl  flowcontrol,  //!< The port flow control setting.
00086   const SERIALPORT_enumBehaviour    behaviour,    //!< The port's read behaviour (settings for waiting on the read timeout).
00087   const unsigned                    timeout_in_ms //!< The read function's timeout [ms]. Not used if behaviour == SERIALPORT_BEHAVIOUR_RETURN_IMMEDIATELY.
00088   )
00089 {
00090   m_serialport.port = portnumber;
00091   m_serialport.baudrate = baudrate;
00092   m_serialport.parity = parity;
00093   m_serialport.databits = databits;
00094   m_serialport.stopbits = stopbits;
00095   m_serialport.flowcontrol = flowcontrol;
00096   m_serialport.behaviour = behaviour;
00097   m_serialport.timeout = timeout_in_ms;
00098 
00099   if( !SERIALPORT_Open( &m_serialport ) )
00100   {
00101     SERIALPORT_ERROR_MSG( "SERIALPORT_Open returned FALSE." );    
00102     return false;
00103   } 
00104   return true;
00105 }
00106 
00107 bool SerialPort::Read(
00108   unsigned char* buffer,     //!< A valid buffer (!NULL) in which to place data. 
00109   const unsigned bufferSize, //!< The size of the buffer [bytes].
00110   unsigned& nrBytesRead      //!< The number of bytes read [bytes].
00111   )
00112 {
00113   if( !SERIALPORT_Read( &m_serialport, buffer, bufferSize, &nrBytesRead ) )
00114   {
00115     SERIALPORT_ERROR_MSG( "SERIALPORT_Read returned FALSE." );
00116     return false;
00117   } 
00118   return true;
00119 }
00120 
00121 bool SerialPort::Write(
00122   unsigned char* buffer,        //!< A valid buffer (!NULL) with data to be written to the port. 
00123   const unsigned nrBytesToWrite //!< The number of bytes in the buffer to write [bytes].    
00124   )
00125 {
00126   if( !SERIALPORT_Write( &m_serialport, buffer, nrBytesToWrite ) )
00127   {
00128     SERIALPORT_ERROR_MSG( "SERIALPORT_Write returned FALSE." );
00129     return false;
00130   } 
00131   return true;
00132 }
00133 
00134 bool SerialPort::WaitOnRingIndicator( unsigned short timeout_ms, bool& ring_detected )
00135 {
00136   BOOL rd = FALSE;
00137   if( !SERIALPORT_WaitOnRingIndicatorEvent( &m_serialport, timeout_ms, &rd ) )
00138   {
00139     SERIALPORT_ERROR_MSG( "SERIALPORT_WaitOnRingIndicatorEvent returned FALSE." );
00140     return false;
00141   } 
00142   if( rd )
00143     ring_detected = true;
00144   else
00145     ring_detected = false;
00146 
00147   return true;
00148 }
00149 
SourceForge.net Logo