Program Listing for File serial_w32.hpp
↰ Return to documentation for file (include/ecl/devices/serial_w32.hpp
)
/*****************************************************************************
** Ifdefs
*****************************************************************************/
#ifndef ECL_THREADS_SERIAL_W32_HPP_
#define ECL_THREADS_SERIAL_W32_HPP_
/*****************************************************************************
** Platform Check
*****************************************************************************/
#include <ecl/config.hpp>
#ifdef ECL_IS_WIN32
/*****************************************************************************
** Includes
*****************************************************************************/
#include <windows.h>
#include <string>
#include <ecl/exceptions/standard_exception.hpp>
#include <ecl/utilities/parameter.hpp>
#include <ecl/threads/thread.hpp>
#include "serial_parameters.hpp"
#include "traits.hpp"
#include "macros.hpp"
/*****************************************************************************
** Namespaces
*****************************************************************************/
namespace ecl {
/*****************************************************************************
** Interface [Serial]
*****************************************************************************/
class ecl_devices_PUBLIC Serial {
public:
/*********************
** C&D
**********************/
Serial() : is_open(false), is_run(false), file_descriptor(INVALID_HANDLE_VALUE), error_handler(NoError) {};
Serial(const std::string& port_name, const BaudRate &baud_rate = BaudRate_115200, const DataBits &data_bits = DataBits_8,
const StopBits &stop_bits = StopBits_1, const Parity &parity = NoParity );
virtual ~Serial();
/*********************
** Open/Close
**********************/
void open(const std::string& port_name, const BaudRate &baud_rate = BaudRate_115200, const DataBits &data_bits = DataBits_8,
const StopBits &stop_bits = StopBits_1, const Parity &parity = NoParity );
void close();
bool open() const { return is_open; }
/*********************
** Writing
**********************/
long write(const char &c);
long write(const char *s, unsigned long n);
void flush() {}
/*********************
** Reading Modes
**********************/
void block(const long &timeout = 500);
void unblock();
/*********************
** Reading
**********************/
long remaining();
long read(char &c);
long read(char *s, const unsigned long &n);
/*********************
** Serial Specific
**********************/
void clear() {
PurgeComm( file_descriptor, PURGE_RXCLEAR );
PurgeComm( file_descriptor, PURGE_TXCLEAR );
}
void clearInputBuffer() { PurgeComm( file_descriptor, PURGE_RXCLEAR ); }
void clearOutputBuffer() { PurgeComm( file_descriptor, PURGE_TXCLEAR ); }
const Error& error() const { return error_handler; }
private:
/*********************
** Variables
**********************/
HANDLE file_descriptor;
OVERLAPPED m_osRead, m_osWrite; // Offsets
std::string port;
bool is_open;
ecl::Error error_handler;
Thread event_receiver;
bool is_run;
private:
friend void event_proc(void* arg);
};
/*****************************************************************************
** Traits [Serial]
*****************************************************************************/
template <>
class is_sink<Serial> : public True {};
template <>
class is_source<Serial> : public True {};
template <>
class is_sourcesink<Serial> : public True {};
} // namespace ecl
#endif /* ECL_IS_WIN32 */
#endif /* ECL_THREADS_SERIAL_W32_HPP_ */