NptSerialPort.h

00001 /*****************************************************************
00002 |
00003 |   Neptune - Serial Ports
00004 |
00005 |   (c) 2001-2007 Gilles Boccon-Gibod
00006 |   Author: Gilles Boccon-Gibod (bok@bok.net)
00007 |
00008  ****************************************************************/
00009 
00010 #ifndef _NPT_SERIAL_PORT_H_
00011 #define _NPT_SERIAL_PORT_H_
00012 
00013 /*----------------------------------------------------------------------
00014 |   includes
00015 +---------------------------------------------------------------------*/
00016 #include "NptTypes.h"
00017 #include "NptStreams.h"
00018 
00019 /*----------------------------------------------------------------------
00020 |   constants
00021 +---------------------------------------------------------------------*/
00022 const int NPT_ERROR_NO_SUCH_SERIAL_PORT      = NPT_ERROR_BASE_SERIAL_PORT - 0;
00023 const int NPT_ERROR_SERIAL_PORT_NOT_OPEN     = NPT_ERROR_BASE_SERIAL_PORT - 1;
00024 const int NPT_ERROR_SERIAL_PORT_ALREADY_OPEN = NPT_ERROR_BASE_SERIAL_PORT - 2;
00025 const int NPT_ERROR_SERIAL_PORT_BUSY         = NPT_ERROR_BASE_SERIAL_PORT - 3;
00026 
00027 typedef enum {
00028     NPT_SERIAL_PORT_PARITY_NONE,
00029     NPT_SERIAL_PORT_PARITY_EVEN,
00030     NPT_SERIAL_PORT_PARITY_ODD,
00031     NPT_SERIAL_PORT_PARITY_MARK
00032 } NPT_SerialPortParity;
00033 
00034 typedef enum {
00035     NPT_SERIAL_PORT_STOP_BITS_1,
00036     NPT_SERIAL_PORT_STOP_BITS_1_5,
00037     NPT_SERIAL_PORT_STOP_BITS_2
00038 } NPT_SerialPortStopBits;
00039 
00040 typedef enum {
00041     NPT_SERIAL_PORT_FLOW_CONTROL_NONE,
00042     NPT_SERIAL_PORT_FLOW_CONTROL_HARDWARE,
00043     NPT_SERIAL_PORT_FLOW_CONTROL_XON_XOFF
00044 } NPT_SerialPortFlowControl;
00045 
00046 /*----------------------------------------------------------------------
00047 |   NPT_SerialPortInterface
00048 +---------------------------------------------------------------------*/
00049 class NPT_SerialPortInterface
00050 {
00051 public:
00052     // constructors and destructor
00053     virtual ~NPT_SerialPortInterface() {}
00054 
00055     // methods
00056     virtual NPT_Result Open(unsigned int              speed, 
00057                             NPT_SerialPortStopBits    stop_bits,
00058                             NPT_SerialPortFlowControl flow_control,
00059                             NPT_SerialPortParity      parity) = 0;
00060     virtual NPT_Result Close() = 0;
00061     virtual NPT_Result GetInputStream(NPT_InputStreamReference& stream) = 0;
00062     virtual NPT_Result GetOutputStream(NPT_OutputStreamReference& stream) = 0;
00063 };
00064 
00065 /*----------------------------------------------------------------------
00066 |   NPT_SerialPort
00067 +---------------------------------------------------------------------*/
00068 class NPT_SerialPort : public NPT_SerialPortInterface
00069 {
00070 public:
00071     // constructors and destructor
00072     NPT_SerialPort(const char* name);
00073    ~NPT_SerialPort() { delete m_Delegate; }
00074 
00075     // NPT_SerialPortInterface methods
00076     NPT_Result Open(unsigned int              speed, 
00077                     NPT_SerialPortStopBits    stop_bits = NPT_SERIAL_PORT_STOP_BITS_1,
00078                     NPT_SerialPortFlowControl flow_control = NPT_SERIAL_PORT_FLOW_CONTROL_NONE,
00079                     NPT_SerialPortParity      parity = NPT_SERIAL_PORT_PARITY_NONE) {
00080         return m_Delegate->Open(speed, stop_bits, flow_control, parity);
00081     }
00082     NPT_Result Close() {
00083         return m_Delegate->Close();
00084     }
00085     NPT_Result GetInputStream(NPT_InputStreamReference& stream) {
00086         return m_Delegate->GetInputStream(stream);
00087     }
00088     NPT_Result GetOutputStream(NPT_OutputStreamReference& stream) {
00089         return m_Delegate->GetOutputStream(stream);
00090     }
00091 
00092 protected:
00093     // members
00094     NPT_SerialPortInterface* m_Delegate;
00095 };
00096 
00097 #endif // _NPT_SERIAL_PORT_H_