00001
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
00026 #pragma once
00027 #pragma warning( disable : 4355 )
00028 #endif
00029
00030 #ifndef VET_ASYNCH_SERIAL_H_INCLUDED
00031 #define VET_ASYNCH_SERIAL_H_INCLUDED
00032 #include "excepts.h"
00033 #include <string>
00034 #include <deque>
00035 #include <boost/bind.hpp>
00036 #include <boost/asio.hpp>
00037 #include <boost/asio/serial_port.hpp>
00038 #include <boost/lexical_cast.hpp>
00039 #include <boost/date_time/posix_time/posix_time_types.hpp>
00040 #include <boost/signals2.hpp>
00041
00049 namespace cornelluniversity {
00050
00056 namespace vetserial {
00057
00058
00059 #if defined(unix) || defined(__unix__) || defined(__unix) || defined(__linux__)
00060 const static std::string DFLT_DEVICE = "/dev/ttyS0";
00061 #elif defined(__APPLE__)
00062
00063 const static std::string DFLT_DEVICE = "/dev/ttyUSB0";
00064 #else
00065 const static std::string DFLT_DEVICE = "COM1";
00066 #endif
00067
00091 class asynch_serial {
00092
00093 typedef boost::signals2::signal<void (const std::string &)> Reader;
00094 typedef Reader::slot_type ReaderSlotType;
00096 public:
00105 asynch_serial( boost::asio::io_service& io_service, unsigned int baud = 9600, const std::string& device = DFLT_DEVICE );
00106
00110 virtual ~asynch_serial( void ) { ; }
00111
00120 void open( unsigned int baud = 9600, const std::string& device = DFLT_DEVICE );
00121
00125 inline void close( void ) { this->io_service_.post( boost::bind(&asynch_serial::do_close, this, boost::system::error_code()) ); }
00126
00131 inline bool active() const { return( this->active_ ); }
00132
00137 inline void active( const bool on_off ) { this->active_ = on_off; }
00138
00143 inline void write( const char msg ) { this->io_service_.post( boost::bind(&asynch_serial::do_write, this, msg) ); }
00144
00150 inline boost::signals2::connection register_reader( const ReaderSlotType & slot ) { return(reader_.connect(slot)); }
00151
00152 private:
00159 void do_close( const boost::system::error_code& error );
00160
00166 void do_write( const char msg );
00167
00171 void write_start( void );
00172
00177 void write_complete( const boost::system::error_code& error );
00178
00182 void read_start( void );
00183
00189 void read_complete( const boost::system::error_code& error, size_t bytes_transferred );
00190
00191 private:
00192 static const int max_read_length = 512;
00194 bool active_;
00195 boost::asio::io_service& io_service_;
00196 boost::asio::serial_port serial_port_;
00197 char read_msg_[max_read_length];
00198 std::deque<char> write_msgs_;
00199 Reader reader_;
00200 };
00201 }
00202 }
00203
00204 #endif
00205
00206
00207
00208
00209
00210
00211