TOBI SignalServer  0.1
/home/breidi/Dropbox/signalserver/include/hardware/serial_port_base.h
Go to the documentation of this file.
00001 /*
00002     This file is part of the TOBI SignalServer.
00003 
00004     Commercial Usage
00005     Licensees holding valid Graz University of Technology Commercial
00006     licenses may use this file in accordance with the Graz University
00007     of Technology Commercial License Agreement provided with the
00008     Software or, alternatively, in accordance with the terms contained in
00009     a written agreement between you and Graz University of Technology.
00010 
00011     --------------------------------------------------
00012 
00013     GNU General Public License Usage
00014     Alternatively, this file may be used under the terms of the GNU
00015     General Public License version 3.0 as published by the Free Software
00016     Foundation and appearing in the file gpl.txt included in the
00017     packaging of this file.  Please review the following information to
00018     ensure the GNU General Public License version 3.0 requirements will be
00019     met: http://www.gnu.org/copyleft/gpl.html.
00020 
00021     In case of GNU General Public License Usage ,the TOBI SignalServer
00022     is distributed in the hope that it will be useful,
00023     but WITHOUT ANY WARRANTY; without even the implied warranty of
00024     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00025     GNU General Public License for more details.
00026 
00027     You should have received a copy of the GNU General Public License
00028     along with the TOBI SignalServer. If not, see <http://www.gnu.org/licenses/>.
00029 
00030     Copyright 2010 Graz University of Technology
00031     Contact: SignalServer@tobi-project.org
00032 */
00033 
00039 #ifndef SERIALPORTBASE_H
00040 #define SERIALPORTBASE_H
00041 
00042 #include <boost/asio.hpp>
00043 #include <boost/bind.hpp>
00044 #include <boost/cstdint.hpp>
00045 
00046 #include <string>
00047 #include <vector>
00048 #include <map>
00049 #include <functional>
00050 
00051 //#include "hw_thread.h"
00052 
00053 namespace tobiss
00054 {
00055 
00056 //-----------------------------------------------------------------------------
00057 
00066 class SerialPortBase
00067 {
00068 
00069 
00070   protected:
00074     SerialPortBase(boost::asio::io_service& io);
00078     virtual ~SerialPortBase();
00079 
00083     void setPortName(const std::string& name);
00084     void open();
00085     void close();
00086     void setBaudRate(const unsigned int rate);
00087     void setFlowControl(const std::string& type);
00088     void setParity(const std::string& type);
00089     void setStopBits(const std::string& bits);
00090     void setCharacterSize(const unsigned int size);
00091 
00095     template<typename T> void sync_read(std::vector<T>& values);
00096 
00100     template<typename T> void sync_read(std::vector<T>& values, unsigned int bytes_to_receive);
00101 
00105     template<typename T> void async_read(std::vector<T>& values);
00106 
00110     template<typename T> void async_read(std::vector<T>& values, unsigned int bytes_to_receive);
00111 
00115     template<typename T> void sync_write(std::vector<T>& values);
00119     template<typename T> void async_write(std::vector<T>& values);
00120 
00121     std::string getSerialPortName()
00122     {
00123       return(port_name_);
00124     }
00125 
00126     bool isDataAvailable()
00127     {
00128       return(data_available_);
00129     }
00130 
00131     bool wasDataWritten()
00132     {
00133       return(data_written_);
00134     }
00135 
00136   private:
00137     void handleAsyncRead(const boost::system::error_code& error,
00138                     std::size_t bytes_transferred );
00139     void handleAsyncWrite(const boost::system::error_code& error,
00140                     std::size_t bytes_transferred );
00141 
00142   private:
00143 
00144     class AsioSerialPortTypeNames
00145     {
00146       public:
00147         ~AsioSerialPortTypeNames()    {  }
00148         static AsioSerialPortTypeNames& getInstance();
00149 
00150         std::string getFlowControlName(unsigned int id);
00151         std::string getStopBitName(unsigned int id);
00152         std::string getParityName(unsigned int id);
00153 
00154         unsigned int getFlowControlID(std::string str);
00155         unsigned int getStopBitID(std::string);
00156         unsigned int getParityID(std::string);
00157 
00158       private:
00159         AsioSerialPortTypeNames(const AsioSerialPortTypeNames& cc);
00160         AsioSerialPortTypeNames();
00161 
00162         static std::map<unsigned int, std::string> flow_control_values_;
00163         static std::map<unsigned int, std::string> stop_bit_values_;
00164         static std::map<unsigned int, std::string> parity_values_;
00165 
00166         struct MapValue: public std::binary_function<
00167             std::pair<unsigned int, std::string>, std::string, bool >
00168         {
00169           bool operator () ( const std::pair<unsigned int, std::string> p, std::string str ) const
00170           {
00171             return(p.second == str);
00172           }
00173         };
00174     };
00175 
00176 
00177     boost::asio::serial_port    serial_port_;
00178     std::string                 port_name_;
00179 
00180     bool                        data_available_;
00181     bool                        data_written_;
00182 
00183     unsigned int baud_rate_;
00184     unsigned int flow_control_type_;
00185     unsigned int parity_;
00186     unsigned int stop_bits_;
00187     unsigned int character_size_;
00188 
00189     AsioSerialPortTypeNames& asio_types_;
00190 };
00191 
00192 //-----------------------------------------------------------------------------
00193 //-----------------------------------------------------------------------------
00194 
00195 // template code:
00196 
00197 template<typename T> void SerialPortBase::sync_read(std::vector<T>& values)
00198 {
00199   boost::system::error_code ec;
00200   boost::asio::read(serial_port_, boost::asio::buffer(values),
00201                     boost::asio::transfer_all(), ec);
00202 
00203   if(ec)
00204     throw(std::runtime_error("SerialPortBase::sync_read() -- \
00205                              Error reading serial port"));
00206 }
00207 
00208 //-----------------------------------------------------------------------------
00209 
00210 template<typename T> void SerialPortBase::sync_read(std::vector<T>& values, unsigned int bytes_to_receive)
00211 {
00212   boost::system::error_code ec;
00213 
00214   boost::asio::read(serial_port_, boost::asio::buffer(values),
00215                     boost::asio::transfer_at_least(bytes_to_receive), ec);
00216 
00217   if(ec)
00218     throw(std::runtime_error("SerialPortBase::sync_read() -- \
00219                              Error reading serial port -- bytes: " + bytes_to_receive));
00220 }
00221 
00222 //-----------------------------------------------------------------------------
00223 
00224 template<typename T> void SerialPortBase::async_read(std::vector<T>& values)
00225 {
00226   if(!data_available_)
00227     throw(std::logic_error("SerialPortBase::async_read() -- Still waiting for new data ...") );
00228 
00229   data_available_ = false;
00230   boost::asio::async_read(serial_port_,
00231                           boost::asio::buffer(values),
00232                           boost::bind(&SerialPortBase::handleAsyncRead,
00233                                       this,
00234                                       boost::asio::placeholders::error,
00235                                       boost::asio::placeholders::bytes_transferred)
00236                           );
00237 }
00238 
00239 //-----------------------------------------------------------------------------
00240 
00241 template<typename T> void SerialPortBase::async_read(std::vector<T>& values, unsigned int bytes_to_receive)
00242 {
00243   if(!data_available_)
00244     throw(std::logic_error("SerialPortBase::async_read() -- Still waiting for new data ...") );
00245 
00246   data_available_ = false;
00247   boost::asio::async_read(serial_port_,
00248                           boost::asio::buffer(values),
00249                           boost::asio::transfer_at_least(bytes_to_receive),
00250                           boost::bind(&SerialPortBase::handleAsyncRead,
00251                                       this,
00252                                       boost::asio::placeholders::error,
00253                                       boost::asio::placeholders::bytes_transferred)
00254                           );
00255 }
00256 
00257 //-----------------------------------------------------------------------------
00258 
00259 template<typename T> void SerialPortBase::sync_write(std::vector<T>& values)
00260 {
00261   boost::asio::write(serial_port_, boost::asio::buffer(values));
00262 }
00263 
00264 //-----------------------------------------------------------------------------
00265 
00266 template<typename T> void SerialPortBase::async_write(std::vector<T>& values)
00267 {
00268   if(!data_written_)
00269     throw(std::logic_error("SerialPortBase::async_read() -- Still writing data ...") );
00270 
00271   data_written_ = false;
00272   boost::asio::async_write(serial_port_,
00273                           boost::asio::buffer(values),
00274                           boost::bind(&SerialPortBase::handleAsyncWrite,
00275                                       this,
00276                                       boost::asio::placeholders::error,
00277                                       boost::asio::placeholders::bytes_transferred)
00278                           );
00279 }
00280 
00281 //-----------------------------------------------------------------------------
00282 //-----------------------------------------------------------------------------
00283 
00284 }  //tobiss
00285 
00286 #endif // SERIALPORTBASE_H
 All Data Structures Files Functions Variables