YDLIDAR X2 SDK  V1.4.1
win_serial.h
1 #if defined(_WIN32)
2 
3 #ifndef SERIAL_IMPL_WINDOWS_H
4 #define SERIAL_IMPL_WINDOWS_H
5 
6 #include "serial.h"
7 #include "time.h"
8 #include <tchar.h>
9 
10 #ifndef UNICODE
11 #define UNICODE
12 #define UNICODE_WAS_UNDEFINED
13 #endif
14 #include "windows.h"
15 
16 #ifndef UNICODE_WAS_UNDEFINED
17 #undef UNICODE
18 #endif
19 
20 namespace serial {
21 
22 using std::string;
23 using std::wstring;
24 using std::invalid_argument;
25 
26 
28  public:
29  explicit SerialImpl(const string &port,
30  unsigned long baudrate,
31  bytesize_t bytesize,
32  parity_t parity,
33  stopbits_t stopbits,
34  flowcontrol_t flowcontrol);
35 
36  virtual ~SerialImpl();
37 
38  bool open();
39 
40  void close();
41 
42  bool isOpen() const;
43 
44  size_t available();
45 
46  bool waitReadable(uint32_t timeout);
47 
48  void waitByteTimes(size_t count);
49 
50  int waitfordata(size_t data_count, uint32_t timeout, size_t *returned_size);
51 
52  size_t read(uint8_t *buf, size_t size = 1);
53 
54  size_t write(const uint8_t *data, size_t length);
55 
56  void flush();
57 
58  void flushInput();
59 
60  void flushOutput();
61 
62  void sendBreak(int duration);
63 
64  bool setBreak(bool level);
65 
66  bool setRTS(bool level);
67 
68  bool setDTR(bool level);
69 
70  bool waitForChange();
71 
72  bool getCTS();
73 
74  bool getDSR();
75 
76  bool getRI();
77 
78  bool getCD();
79 
80  uint32_t getByteTime();
81 
82  void setPort(const string &port);
83 
84  string getPort() const;
85 
86  void setTimeout(Timeout &timeout);
87 
88  Timeout getTimeout() const;
89 
90  bool setBaudrate(unsigned long baudrate);
91 
92  unsigned long getBaudrate() const;
93 
94  bool setBytesize(bytesize_t bytesize);
95 
96  bytesize_t getBytesize() const;
97 
98  bool setParity(parity_t parity);
99 
100  parity_t getParity() const;
101 
102  bool setStopbits(stopbits_t stopbits);
103 
104  stopbits_t getStopbits() const;
105 
106  bool setFlowcontrol(flowcontrol_t flowcontrol);
107 
108  flowcontrol_t getFlowcontrol() const;
109 
110 
111  bool setDcb(DCB *dcb);
112 
113 
114  bool getDcb(DCB *dcb);
115 
116  int readLock();
117 
118  int readUnlock();
119 
120  int writeLock();
121 
122  int writeUnlock();
123 
124  protected:
125  bool reconfigurePort();
126 
127  public:
128  enum {
129  DEFAULT_RX_BUFFER_SIZE = 2048,
130  DEFAULT_TX_BUFFER_SIZE = 128,
131  };
132 
133 
134  private:
135  wstring port_; // Path to the file descriptor
136  HANDLE fd_;
137  OVERLAPPED _wait_o;
138 
139  OVERLAPPED communicationOverlapped;
140  OVERLAPPED readCompletionOverlapped;
141  OVERLAPPED writeCompletionOverlapped;
142  DWORD originalEventMask;
143  DWORD triggeredEventMask;
144 
145  COMMTIMEOUTS currentCommTimeouts;
146  COMMTIMEOUTS restoredCommTimeouts;
147 
148  bool is_open_;
149 
150  Timeout timeout_; // Timeout for read operations
151  unsigned long baudrate_; // Baudrate
152  uint32_t byte_time_ns_; // Nanoseconds to transmit/receive a single byte
153 
154  parity_t parity_; // Parity
155  bytesize_t bytesize_; // Size of the bytes
156  stopbits_t stopbits_; // Stop Bits
157  flowcontrol_t flowcontrol_; // Flow Control
158 
159  // Mutex used to lock the read functions
160  HANDLE read_mutex;
161  // Mutex used to lock the write functions
162  HANDLE write_mutex;
163 };
164 
165 }
166 
167 #endif // SERIAL_IMPL_WINDOWS_H
168 
169 #endif // if defined(_WIN32)
parity_t
Definition: serial.h:26
bytesize_t
Definition: serial.h:16
Definition: serial.h:11
size_t write(const uint8_t *data, size_t length)
Definition: unix_serial.cpp:957
stopbits_t
Definition: serial.h:37
Definition: unix_serial.h:27
flowcontrol_t
Definition: serial.h:46