win.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*!
  2. * \file serial/impl/windows.h
  3. * \author William Woodall <wjwwood@gmail.com>
  4. * \author John Harrison <ash@greaterthaninfinity.com>
  5. * \version 0.1
  6. *
  7. * \section LICENSE
  8. *
  9. * The MIT License
  10. *
  11. * Copyright (c) 2012 William Woodall, John Harrison
  12. *
  13. * Permission is hereby granted, free of charge, to any person obtaining a
  14. * copy of this software and associated documentation files (the "Software"),
  15. * to deal in the Software without restriction, including without limitation
  16. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  17. * and/or sell copies of the Software, and to permit persons to whom the
  18. * Software is furnished to do so, subject to the following conditions:
  19. *
  20. * The above copyright notice and this permission notice shall be included in
  21. * all copies or substantial portions of the Software.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  24. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  26. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  27. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  28. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  29. * DEALINGS IN THE SOFTWARE.
  30. *
  31. * \section DESCRIPTION
  32. *
  33. * This provides a windows implementation of the Serial class interface.
  34. *
  35. */
  36. #if defined(_WIN32)
  37. #ifndef SERIAL_IMPL_WINDOWS_H
  38. #define SERIAL_IMPL_WINDOWS_H
  39. #include "serial/serial.h"
  40. #include "windows.h"
  41. namespace serial {
  42. using std::string;
  43. using std::wstring;
  44. using std::invalid_argument;
  45. using serial::SerialException;
  46. using serial::IOException;
  47. class serial::Serial::SerialImpl {
  48. public:
  49. SerialImpl (const string &port,
  50. unsigned long baudrate,
  51. bytesize_t bytesize,
  52. parity_t parity,
  53. stopbits_t stopbits,
  54. flowcontrol_t flowcontrol);
  55. virtual ~SerialImpl ();
  56. void
  57. open ();
  58. void
  59. close ();
  60. bool
  61. isOpen () const;
  62. size_t
  63. available ();
  64. bool
  65. waitReadable (uint32_t timeout);
  66. void
  67. waitByteTimes (size_t count);
  68. size_t
  69. read (uint8_t *buf, size_t size = 1);
  70. size_t
  71. write (const uint8_t *data, size_t length);
  72. void
  73. flush ();
  74. void
  75. flushInput ();
  76. void
  77. flushOutput ();
  78. void
  79. sendBreak (int duration);
  80. void
  81. setBreak (bool level);
  82. void
  83. setRTS (bool level);
  84. void
  85. setDTR (bool level);
  86. bool
  87. waitForChange ();
  88. bool
  89. getCTS ();
  90. bool
  91. getDSR ();
  92. bool
  93. getRI ();
  94. bool
  95. getCD ();
  96. void
  97. setPort (const string &port);
  98. string
  99. getPort () const;
  100. void
  101. setTimeout (Timeout &timeout);
  102. Timeout
  103. getTimeout () const;
  104. void
  105. setBaudrate (unsigned long baudrate);
  106. unsigned long
  107. getBaudrate () const;
  108. void
  109. setBytesize (bytesize_t bytesize);
  110. bytesize_t
  111. getBytesize () const;
  112. void
  113. setParity (parity_t parity);
  114. parity_t
  115. getParity () const;
  116. void
  117. setStopbits (stopbits_t stopbits);
  118. stopbits_t
  119. getStopbits () const;
  120. void
  121. setFlowcontrol (flowcontrol_t flowcontrol);
  122. flowcontrol_t
  123. getFlowcontrol () const;
  124. void
  125. readLock ();
  126. void
  127. readUnlock ();
  128. void
  129. writeLock ();
  130. void
  131. writeUnlock ();
  132. protected:
  133. void reconfigurePort ();
  134. private:
  135. wstring port_; // Path to the file descriptor
  136. HANDLE fd_;
  137. bool is_open_;
  138. Timeout timeout_; // Timeout for read operations
  139. unsigned long baudrate_; // Baudrate
  140. parity_t parity_; // Parity
  141. bytesize_t bytesize_; // Size of the bytes
  142. stopbits_t stopbits_; // Stop Bits
  143. flowcontrol_t flowcontrol_; // Flow Control
  144. // Mutex used to lock the read functions
  145. HANDLE read_mutex;
  146. // Mutex used to lock the write functions
  147. HANDLE write_mutex;
  148. };
  149. }
  150. #endif // SERIAL_IMPL_WINDOWS_H
  151. #endif // if defined(_WIN32)