Go to the documentation of this file.00001 
00008 
00009 
00010 
00011 
00012 #ifndef ECL_THREADS_SERIAL_W32_HPP_
00013 #define ECL_THREADS_SERIAL_W32_HPP_
00014 
00015 
00016 
00017 
00018 
00019 #include <ecl/config.hpp>
00020 #ifdef ECL_IS_WIN32
00021 
00022 
00023 
00024 
00025 
00026 #include <windows.h>
00027 #include <string>
00028 #include <ecl/exceptions/standard_exception.hpp>
00029 #include <ecl/utilities/parameter.hpp>
00030 #include <ecl/threads/thread.hpp>
00031 #include "serial_parameters.hpp"
00032 #include "traits.hpp"
00033 #include "macros.hpp"
00034 
00035 
00036 
00037 
00038 
00039 namespace ecl {
00040 
00041 
00042 
00043 
00115 class ecl_devices_PUBLIC Serial {
00116 public:
00117         
00118 
00119 
00127         Serial() : is_open(false), is_run(false), file_descriptor(INVALID_HANDLE_VALUE), error_handler(NoError) {};
00144         Serial(const std::string& port_name, const BaudRate &baud_rate = BaudRate_115200, const DataBits &data_bits = DataBits_8,
00145                         const StopBits &stop_bits = StopBits_1, const Parity &parity = NoParity ) ecl_throw_decl(StandardException);
00146 
00152         virtual ~Serial();
00153 
00154         
00155 
00156 
00173         void open(const std::string& port_name, const BaudRate &baud_rate = BaudRate_115200, const DataBits &data_bits = DataBits_8,
00174                         const StopBits &stop_bits = StopBits_1, const Parity &parity = NoParity ) ecl_throw_decl(StandardException);
00175 
00183         void close();
00189         bool open() const { return is_open; }
00190 
00191         
00192 
00193 
00202         long write(const char &c) ecl_assert_throw_decl(StandardException);
00203 
00212         long write(const char *s, unsigned long n) ecl_assert_throw_decl(StandardException);
00213 
00220         void flush() {}
00221 
00222         
00223 
00224 
00234         void block(const long &timeout = 500) ecl_assert_throw_decl(StandardException);
00241         void unblock();
00242 
00243         
00244 
00245 
00253         long remaining();
00264     long read(char &c) ecl_assert_throw_decl(StandardException);
00276     long read(char *s, const unsigned long &n) ecl_assert_throw_decl(StandardException);
00277 
00278 
00279     
00280 
00281 
00288     void clear() {
00289         PurgeComm( file_descriptor, PURGE_RXCLEAR );
00290         PurgeComm( file_descriptor, PURGE_TXCLEAR );
00291     }
00297     void clearInputBuffer() { PurgeComm( file_descriptor, PURGE_RXCLEAR ); }
00303     void clearOutputBuffer() { PurgeComm( file_descriptor, PURGE_TXCLEAR ); }
00304 
00311     const Error& error() const { return error_handler; }
00312 
00313 private:
00314         
00315 
00316 
00317         HANDLE file_descriptor;
00318         OVERLAPPED  m_osRead, m_osWrite; 
00319     std::string port;
00320     bool is_open;
00321     ecl::Error error_handler;
00322     Thread event_receiver;
00323     bool is_run;
00324 
00325 private:
00331     friend void event_proc(void* arg);
00332 
00333 };
00334 
00335 
00336 
00337 
00343 template <>
00344 class is_sink<Serial> : public True {};
00345 
00351 template <>
00352 class is_source<Serial> : public True {};
00353 
00359 template <>
00360 class is_sourcesink<Serial> : public True {};
00361 
00362 } 
00363 
00364 #endif 
00365 #endif