serialport.h

Go to the documentation of this file.
00001 /**
00002 \file    serialport.h
00003 \brief   serial port communications 'c' function library.
00004 \author  Glenn D. MacGougan (GDM)
00005 \date    2008-11-26
00006 \since   2008-11-26
00007 
00008 \b "LICENSE INFORMATION" \n
00009 Copyright (c) 2008, refer to 'author' doxygen tags \n
00010 All rights reserved. \n
00011 
00012 Redistribution and use in source and binary forms, with or without
00013 modification, are permitted provided the following conditions are met: \n
00014 
00015 - Redistributions of source code must retain the above copyright
00016   notice, this list of conditions and the following disclaimer. \n
00017 - Redistributions in binary form must reproduce the above copyright
00018   notice, this list of conditions and the following disclaimer in the
00019   documentation and/or other materials provided with the distribution. \n
00020 - The name(s) of the contributor(s) may not be used to endorse or promote 
00021   products derived from this software without specific prior written 
00022   permission. \n
00023 
00024 THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND ANY EXPRESS 
00025 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
00026 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00027 DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
00028 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00029 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
00030 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
00031 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
00032 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 
00033 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
00034 SUCH DAMAGE.
00035 */
00036 
00037 #ifndef _C_SERIALPORT_H_
00038 #define _C_SERIALPORT_H_
00039 
00040 #ifdef __cplusplus
00041 extern "C" {
00042 #endif
00043 
00044 #include "basictypes.h"
00045 
00046 #define SERIALPORT_ERROR_MSG( msg )  { const char *themsg = msg; if( themsg != NULL ){ printf( "\n%s,\n%s, %d,\n%s\n", __FILE__, __FUNCTION__, __LINE__, themsg ); }else{ printf( "\n%s,\n%s, %d,\nUnknown Error\n", __FILE__, __FUNCTION__, __LINE__ ); } }
00047 
00048 #ifdef WIN32
00049 #include <windows.h>
00050 #endif
00051 
00052 
00053 /// \brief  baud rate enumeration
00054 typedef enum 
00055 {
00056   SERIALPORT_BAUD_110    = 110,
00057   SERIALPORT_BAUD_300    = 300,
00058   SERIALPORT_BAUD_600    = 600,
00059   SERIALPORT_BAUD_1200   = 1200,
00060   SERIALPORT_BAUD_2400   = 2400,
00061   SERIALPORT_BAUD_4800   = 4800,
00062   SERIALPORT_BAUD_9600   = 9600,
00063   SERIALPORT_BAUD_19200  = 19200,
00064   SERIALPORT_BAUD_38400  = 38400,
00065   SERIALPORT_BAUD_57600  = 57600,
00066   SERIALPORT_BAUD_115200 = 115200,
00067   SERIALPORT_BAUD_INVALID
00068 } SERIALPORT_enumBaudRate;
00069 
00070 /// \brief serial port parity enumeration.
00071 /// refer  http://msdn.microsoft.com/en-us/library/aa363214(VS.85).aspx
00072 typedef enum 
00073 {
00074   SERIALPORT_PARITY_NONE  = 0,
00075   SERIALPORT_PARITY_ODD   = 1,
00076   SERIALPORT_PARITY_EVEN  = 2,
00077   SERIALPORT_PARITY_MARK  = 3,
00078   SERIALPORT_PARITY_SPACE = 4,
00079   SERIALPORT_PARITY_INVALID
00080 } SERIALPORT_enumParity;
00081 
00082 
00083 /// \brief serial port data bits enumeration.
00084 typedef enum 
00085 {
00086   SERIALPORT_DATABITS_4 = 4,
00087   SERIALPORT_DATABITS_5 = 5,
00088   SERIALPORT_DATABITS_6 = 6,
00089   SERIALPORT_DATABITS_7 = 7,
00090   SERIALPORT_DATABITS_8 = 8,
00091   SERIALPORT_DATABITS_INVALID  
00092 } SERIALPORT_enumDataBits;
00093 
00094       
00095 /// \brief serial port stop bits enumeration
00096 /// refer  http://msdn.microsoft.com/en-us/library/aa363214(VS.85).aspx
00097 typedef enum 
00098 {
00099   SERIALPORT_STOPBITS_1       = 0,
00100   SERIALPORT_STOPBITS_1POINT5 = 1,
00101   SERIALPORT_STOPBITS_2       = 2,
00102   SERIALPORT_STOPBITS_INVALID
00103 } SERIALPORT_enumStopBits;
00104 
00105 
00106 /// \brief serial port handshake enumeration
00107 typedef enum 
00108 {
00109   SERIALPORT_FLOWCONTROL_NONE = 0,
00110   SERIALPORT_FLOWCONTROL_HARDWARE = 1,
00111   SERIALPORT_FLOWCONTROL_XONXOFF = 2,
00112   SERIALPORT_FLOWCONTROL_INVALID
00113 } SERIALPORT_enumFlowControl;
00114 
00115 
00116 /*
00117 // WIN32 serial port control settings struct
00118 // http://msdn.microsoft.com/en-us/library/aa363214(VS.85).aspx
00119 typedef struct _DCB {
00120   DWORD DCBlength;
00121   DWORD BaudRate;
00122   DWORD fBinary  :1;
00123   DWORD fParity  :1;
00124   DWORD fOutxCtsFlow  :1;
00125   DWORD fOutxDsrFlow  :1;
00126   DWORD fDtrControl  :2;
00127   DWORD fDsrSensitivity  :1;
00128   DWORD fTXContinueOnXoff  :1;
00129   DWORD fOutX  :1;
00130   DWORD fInX  :1;
00131   DWORD fErrorChar  :1;
00132   DWORD fNull  :1;
00133   DWORD fRtsControl  :2;
00134   DWORD fAbortOnError  :1;
00135   DWORD fDummy2  :17;
00136   WORD wReserved;
00137   WORD XonLim;
00138   WORD XoffLim;
00139   BYTE ByteSize;
00140   BYTE Parity;
00141   BYTE StopBits;
00142   char XonChar;
00143   char XoffChar;
00144   char ErrorChar;
00145   char EofChar;
00146   char EvtChar;
00147   WORD wReserved1;
00148 } DCB;
00149 */
00150 
00151 
00152 /// \brief  The serial port behaviour during read operations.
00153 typedef enum 
00154 {
00155   SERIALPORT_BEHAVIOUR_WAIT_UNTIL_BYTES_DETECTED = 0, //!< Wait until the timeout has elapsed or until bytes are present.
00156   SERIALPORT_BEHAVIOUR_RETURN_IMMEDIATELY = 1,        //!< Do not wait until the timeout has elapsed. Return immediately.
00157   SERIALPORT_BEHAVIOUR_INVALID
00158 } SERIALPORT_enumBehaviour;
00159 
00160 /**
00161 \brief The serial port data container. i.e. The serial port 'object'.
00162 
00163 An example method for the use of this object follows:
00164 
00165 \code
00166 BOOL TestSerialPort()
00167 {
00168   BOOL result;
00169   unsigned i;
00170   unsigned numBytesRead = 0;
00171   unsigned char buffer[8192]
00172   SERIALPORT_structObject port;
00173   result = SERIALPORT_Initialize( &port );
00174 
00175   port.port = 1; // COM1
00176   port.baudrate = SERIALPORT_BAUD_9600;
00177   port.parity = SERIALPORT_PARITY_NONE;
00178   port.databits = SERIALPORT_DATABITS_8;
00179   port.stopbits = SERIALPORT_STOPBITS_1;
00180   port.flowcontrol = SERIALPORT_FLOWCONTROL_NONE;
00181   port.timeout = 0;
00182   port.behaviour = SERIALPORT_BEHAVIOUR_RETURN_IMMEDIATELY;
00183 
00184   result = SERIALPORT_Open( &port );
00185   if( !result )
00186     return FALSE;
00187 
00188   for( i = 0; i < 100; i++ )
00189   {
00190     result = SERIALPORT_Read( &port, buffer, 8192, numBytesRead );
00191     if( !result )
00192       return FALSE; 
00193     if( numBytesRead > 0 )
00194       printf( "%s\n", buffer );
00195     Sleep(1);
00196   }
00197 
00198   sprintf( buffer, "Hello SERIALPORT\n" );
00199 
00200   for( i = 0; i < 100; i++ )
00201   {
00202     result = SERIALPORT_Write( &port, buffer, strlen( buffer ) );
00203     if( !result )
00204       return FALSE; 
00205     Sleep(1);
00206   }
00207 
00208   result = SERIALPORT_Close( &port );
00209 
00210   return TRUE;
00211 }
00212 \endcode
00213 */
00214 typedef struct
00215 {
00216   SERIALPORT_enumBaudRate  baudrate;       //!< The serial port baud rate [bits per second].
00217   SERIALPORT_enumParity    parity;         //!< The serial port parity.
00218   SERIALPORT_enumDataBits  databits;       //!< The serial port data bits.
00219   SERIALPORT_enumStopBits  stopbits;       //!< The serial port stop bits.
00220   SERIALPORT_enumFlowControl  flowcontrol; //!< The serial port flow control setting.
00221 
00222   BOOL isPortOpen;      //!< A boolean to indicate if the serial port is open.
00223 
00224   unsigned short port;  //!< The serial port number. A value greater than 0.
00225   unsigned int timeout; //!< The serial port timeout value [ms]. Not used if behaviour == SERIALPORT_BEHAVIOUR_RETURN_IMMEDIATELY.
00226 
00227   HANDLE handle; //!< A windows handle for the serial port.
00228 
00229   SERIALPORT_enumBehaviour behaviour;  //!< The serial port behaviour during read operationss.
00230 
00231   // Do not alter the members of the control settings. This is done internally only.
00232   DCB control_setting; //!< The control settings for a windows serial communications device. Do not alter the members of the control settings. This is done internally only.
00233 
00234 } SERIALPORT_structObject;
00235 
00236 
00237 /**
00238 \brief  Initialize a serial port 'object'. This does not open the serial port. 
00239         Call this function after the declaration of the struct.
00240 \return TRUE if successful, FALSE otherwise.
00241 */
00242 BOOL SERIALPORT_Initialize( SERIALPORT_structObject* serialport );
00243 
00244 /**
00245 \brief
00246 
00247 \return TRUE if successful, FALSE otherwise.
00248 */
00249 BOOL SERIALPORT_Open( SERIALPORT_structObject* serialport );
00250 
00251 /**
00252 \brief
00253 
00254 \return TRUE if successful, FALSE otherwise.
00255 */
00256 BOOL SERIALPORT_Close( SERIALPORT_structObject* serialport );
00257 
00258 /**
00259 \brief
00260 
00261 \return TRUE if successful, FALSE otherwise.
00262 */
00263 /* BOOL SERIALPORT_IsDataAvailableToRead( SERIALPORT_structObject* serialport, BOOL* isAvailable ); // disabled for now */
00264 
00265 /**
00266 \brief
00267 
00268 \return TRUE if successful, FALSE otherwise.
00269 */
00270 BOOL SERIALPORT_Read( 
00271   SERIALPORT_structObject* serialport,       //!< The serial port 'object'.
00272   unsigned char* buffer,                     //!< A valid buffer in which to place data.
00273   const unsigned int bytesAvailableInBuffer, //!< The maximum number of bytes avaiable in the buffer [bytes].
00274   unsigned int* bytesRead                    //!< The number of bytes read and stored in the buffer [bytes].
00275   );
00276 
00277 /**
00278 \brief
00279 
00280 \return TRUE if successful, FALSE otherwise.
00281 */
00282 BOOL SERIALPORT_Write( 
00283   SERIALPORT_structObject* serialport,     //!< The serial port 'object'.
00284   const unsigned char* buffer,             //!< A valid buffer containing the data to write.
00285   const unsigned int numberOfBytesToWrite  //!< The number of bytes to write [bytes].
00286   );
00287 
00288 /**
00289 \brief
00290 
00291 \return TRUE if successful, FALSE otherwise.
00292 */
00293 BOOL SERIALPORT_WaitOnRingIndicatorEvent(
00294   SERIALPORT_structObject* serialport,     //!< The serial port 'object'.
00295   unsigned short timeout_ms,               //!< Wait for up to this timeout value [ms].
00296   BOOL *ring_detected                      //!< A boolean to indicate if a ring signal was detected.
00297   );
00298 
00299 
00300 
00301 
00302 #ifdef __cplusplus
00303 }
00304 #endif
00305 
00306 
00307 #endif // _C_SERIALPORT_H_
SourceForge.net Logo