YDLIDAR SDK  V1.3.2
unix_serial.h
1 #if !defined(_WIN32)
2 
3 #ifndef SERIAL_IMPL_UNIX_H
4 #define SERIAL_IMPL_UNIX_H
5 
6 #include <pthread.h>
7 #include <assert.h>
8 #include <termios.h>
9 #include "serial.h"
10 
11 namespace serial {
12 
13  using std::size_t;
14  using std::string;
15 
16 
18  public:
19  explicit MillisecondTimer(const uint32_t millis);
20  int64_t remaining();
21 
22  private:
23  static timespec timespec_now();
24  timespec expiry;
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 
57  void flush ();
58 
59  void flushInput ();
60 
61  void flushOutput ();
62 
63  void sendBreak (int duration);
64 
65  bool setBreak (bool level);
66 
67  bool setRTS (bool level);
68 
69  bool setDTR (bool level);
70 
71  bool waitForChange ();
72 
73  bool getCTS ();
74 
75  bool getDSR ();
76 
77  bool getRI ();
78 
79  bool getCD ();
80 
81  uint32_t getByteTime();
82 
83  void setPort (const string &port);
84 
85  string getPort () const;
86 
87  void setTimeout (Timeout &timeout);
88 
89  Timeout getTimeout () const;
90 
91  bool setBaudrate (unsigned long baudrate);
92 
93  bool setStandardBaudRate(speed_t baudrate);
94 
95  bool setCustomBaudRate(unsigned long baudrate);
96 
97  unsigned long getBaudrate () const;
98 
99  bool setBytesize (bytesize_t bytesize);
100 
101  bytesize_t getBytesize () const;
102 
103  bool setParity (parity_t parity);
104 
105  parity_t getParity () const;
106 
107  bool setStopbits (stopbits_t stopbits);
108 
109  stopbits_t getStopbits () const;
110 
111  bool setFlowcontrol (flowcontrol_t flowcontrol);
112 
113  flowcontrol_t getFlowcontrol () const;
114 
115  bool setTermios(const termios *tio);
116 
117  bool getTermios(termios *tio);
118 
119  int readLock ();
120 
121  int readUnlock ();
122 
123  int writeLock ();
124 
125  int writeUnlock ();
126 
127 
128  private:
129  string port_; // Path to the file descriptor
130  int fd_; // The current file descriptor
131 
132  bool is_open_;
133  bool xonxoff_;
134  bool rtscts_;
135 
136  Timeout timeout_; // Timeout for read operations
137  unsigned long baudrate_; // Baudrate
138  uint32_t byte_time_ns_; // Nanoseconds to transmit/receive a single byte
139 
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 
145  // Mutex used to lock the read functions
146  pthread_mutex_t read_mutex;
147  // Mutex used to lock the write functions
148  pthread_mutex_t write_mutex;
149  };
150 
151 }
152 
153 #endif // SERIAL_IMPL_UNIX_H
154 
155 #endif // !defined(_WIN32)
Definition: unix_serial.h:17
parity_t
Definition: serial.h:26
bytesize_t
Definition: serial.h:16
Definition: serial.h:11
Definition: serial.h:58
stopbits_t
Definition: serial.h:37
Definition: unix_serial.h:27
flowcontrol_t
Definition: serial.h:46