Flexiport 2.0.0

serialport.h

Go to the documentation of this file.
00001 /* Flexiport
00002  *
00003  * Header file for the serial port class.
00004  *
00005  * Copyright 2008-2011 Geoffrey Biggs geoffrey.biggs@aist.go.jp
00006  *     RT-Synthesis Research Group
00007  *     Intelligent Systems Research Institute,
00008  *     National Institute of Advanced Industrial Science and Technology (AIST),
00009  *     Japan
00010  *     All rights reserved.
00011  *
00012  * This file is part of Flexiport.
00013  *
00014  * Flexiport is free software; you can redistribute it and/or modify it
00015  * under the terms of the GNU Lesser General Public License as published
00016  * by the Free Software Foundation; either version 2.1 of the License,
00017  * or (at your option) any later version.
00018  *
00019  * Flexiport is distributed in the hope that it will be useful, but
00020  * WITHOUT ANY WARRANTY; without even the implied warranty of
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00022  * Lesser General Public License for more details.
00023  *
00024  * You should have received a copy of the GNU Lesser General Public
00025  * License along with Flexiport. If not, see
00026  * <http://www.gnu.org/licenses/>.
00027  */
00028 
00029 #ifndef __SERIALPORT_H
00030 #define __SERIALPORT_H
00031 
00032 #include <flexiport/port.h>
00033 
00034 #include <map>
00035 #include <string>
00036 #if defined (WIN32)
00037     #include <Windows.h>
00038 #endif
00039 
00044 namespace flexiport
00045 {
00046 
00074 class FLEXIPORT_EXPORT SerialPort : public Port
00075 {
00076     public:
00077         SerialPort (std::map<std::string, std::string> options);
00078         ~SerialPort ();
00079 
00081         void Open ();
00083         void Close ();
00085         ssize_t Read (void * const buffer, size_t count);
00087         ssize_t ReadFull (void * const buffer, size_t count);
00089         ssize_t BytesAvailable ();
00095         ssize_t BytesAvailableWait ();
00097         ssize_t Write (const void * const buffer, size_t count);
00099         void Flush ();
00101         void Drain ();
00103         std::string GetStatus () const;
00105         void SetTimeout (Timeout timeout);
00107         void SetCanRead (bool canRead);
00109         void SetCanWrite (bool canWrite);
00111         bool IsOpen () const                        { return _open; }
00112 
00114         void SetBaudRate (unsigned int baud);
00116         unsigned int GetBaudRate () const           { return _baud; }
00117 
00118     private:
00119 #if defined (WIN32)
00120         HANDLE _fd;             // Serial device handle
00121 #else
00122         int _fd;                // Serial device file descriptor
00123 #endif
00124 
00125         std::string _device;
00126         unsigned int _baud;
00127         unsigned int _dataBits;
00128         unsigned int _stopBits;
00129         typedef enum {PAR_NONE, PAR_EVEN, PAR_ODD} Parity;
00130         Parity _parity;
00131         bool _hwFlowCtrl;
00132         bool _open;
00133 
00134         void CheckPort (bool read);
00135         bool ProcessOption (const std::string &option, const std::string &value);
00136 
00137         bool IsDataAvailable ();
00138 #if !defined (WIN32)
00139         typedef enum {TIMED_OUT, DATA_AVAILABLE, CAN_WRITE} WaitStatus;
00140         WaitStatus WaitForDataOrTimeout ();
00141         WaitStatus WaitForWritableOrTimeout ();
00142 #endif
00143         void SetPortSettings ();
00144         void SetPortTimeout ();
00145 };
00146 
00147 } // namespace flexiport
00148 
00151 #endif // __SERIALPORT_H
00152 
 All Classes Namespaces Files Functions Variables Defines