00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00036
00037
00038
00039
00040
00041
00042
00043
00044 #ifndef ASYNCSERIAL_H
00045 #define ASYNCSERIAL_H
00046
00047 #include <vector>
00048 #include <boost/asio.hpp>
00049 #include <boost/bind.hpp>
00050 #include <boost/thread.hpp>
00051 #include <boost/utility.hpp>
00052 #include <boost/function.hpp>
00053 #include <boost/shared_array.hpp>
00054
00059 class AsyncSerialImpl;
00060
00066 class AsyncSerial:private
00067 boost::noncopyable {
00068 public:
00069 AsyncSerial ( );
00070
00082 AsyncSerial ( const std::string & devname, unsigned int baud_rate,
00083 boost::asio::serial_port_base::parity opt_parity =
00084 boost::asio::serial_port_base::parity ( boost::asio::
00085 serial_port_base::parity::none ),
00086 boost::asio::serial_port_base::character_size opt_csize =
00087 boost::asio::serial_port_base::character_size ( 8 ),
00088 boost::asio::serial_port_base::flow_control opt_flow =
00089 boost::asio::serial_port_base::flow_control ( boost::
00090 asio::serial_port_base::flow_control::
00091 none ),
00092 boost::asio::serial_port_base::stop_bits opt_stop =
00093 boost::asio::serial_port_base::stop_bits ( boost::asio::
00094 serial_port_base::stop_bits::
00095 one ) );
00096
00108 void
00109
00110
00111 open ( const std::string & devname, unsigned int baud_rate,
00112 boost::asio::serial_port_base::parity opt_parity =
00113 boost::asio::serial_port_base::parity ( boost::asio::serial_port_base::
00114 parity::none ),
00115 boost::asio::serial_port_base::character_size opt_csize =
00116 boost::asio::serial_port_base::character_size ( 8 ),
00117 boost::asio::serial_port_base::flow_control opt_flow =
00118 boost::asio::serial_port_base::flow_control ( boost::asio::
00119 serial_port_base::flow_control::
00120 none ),
00121 boost::asio::serial_port_base::stop_bits opt_stop =
00122 boost::asio::serial_port_base::stop_bits ( boost::asio::
00123 serial_port_base::stop_bits::
00124 one ) );
00125
00129 bool
00130 isOpen ( ) const;
00131
00135 bool
00136 errorStatus ( ) const;
00137
00142 void
00143 close ( );
00144
00150 void
00151 write ( const char *data, size_t size );
00152
00157 void
00158 write ( const std::vector < char >&data );
00159
00166 void
00167 writeString ( const std::string & s );
00168
00169 virtual ~
00170 AsyncSerial ( ) = 0;
00171
00175 static const int
00176 readBufferSize = 512;
00177 private:
00178
00183 void
00184 doRead ( );
00185
00190 void
00191 readEnd ( const boost::system::error_code & error, size_t bytes_transferred );
00192
00198 void
00199 doWrite ( );
00200
00206 void
00207 writeEnd ( const boost::system::error_code & error );
00208
00212 void
00213 doClose ( );
00214
00215 boost::shared_ptr <
00216 AsyncSerialImpl >
00217 pimpl;
00218
00219 protected:
00220
00225 void
00226 setErrorStatus ( bool e );
00227
00231 void
00232
00233 setReadCallback ( const boost::function < void ( const char *, size_t ) >
00234 &callback );
00235
00241 void
00242 clearReadCallback ( );
00243
00244 };
00245
00252 class CallbackAsyncSerial:
00253 public AsyncSerial
00254 {
00255 public:
00256 CallbackAsyncSerial ( );
00257
00269 CallbackAsyncSerial ( const std::string & devname, unsigned int baud_rate,
00270 boost::asio::serial_port_base::parity opt_parity =
00271 boost::asio::serial_port_base::parity ( boost::
00272 asio::serial_port_base::parity::
00273 none ),
00274 boost::asio::serial_port_base::character_size opt_csize =
00275 boost::asio::serial_port_base::character_size ( 8 ),
00276 boost::asio::serial_port_base::flow_control opt_flow =
00277 boost::asio::serial_port_base::flow_control ( boost::
00278 asio::serial_port_base::flow_control::
00279 none ),
00280 boost::asio::serial_port_base::stop_bits opt_stop =
00281 boost::asio::serial_port_base::stop_bits ( boost::
00282 asio::serial_port_base::stop_bits::
00283 one ) );
00284
00291 void
00292 setCallback ( const boost::function < void ( const char *, size_t ) > &callback );
00293
00298 void
00299 clearCallback ( );
00300
00301 virtual ~ CallbackAsyncSerial ( );
00302 };
00303
00304 #endif //ASYNCSERIAL_H