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