NAP
serialport.h
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4 
5 #pragma once
6 
7 // External Includes
8 #include <nap/device.h>
9 #include <string.h>
10 #include <nap/numeric.h>
11 #include <nap/signalslot.h>
12 
13 // Forward declares
14 namespace serial
15 {
16  class Serial;
17 }
18 
19 namespace nap
20 {
24  enum class ESerialByteSize : int
25  {
26  Five = 5,
27  Six = 6,
28  Seven = 7,
29  Eight = 8
30  };
31 
35  enum class ESerialStopBits : int
36  {
37  One = 1,
38  Two = 2,
39  OnePointFive = 3
40  };
41 
45  enum class ESerialFlowControl : int
46  {
47  None = 0,
48  Software = 1,
49  Hardware = 2
50  };
51 
55  enum class ESerialParity : int
56  {
57  None = 0,
58  Odd = 1,
59  Even = 2,
60  Mark = 3,
61  Space = 4
62  };
63 
64 
72  class NAPAPI SerialPort : public Device
73  {
74  RTTI_ENABLE(Device)
75  public:
76 
80  struct Error
81  {
85  enum class EType : int
86  {
87  NoError = 0,
88  IOError = 1,
89  SerialError = 2,
90  PortError = 3
91  };
92 
96  bool failed() { return mType != EType::NoError; }
97 
101  const std::string& getMessage() { return mMessage; }
102 
106  EType getType() { return mType; }
107 
111  void clear();
112 
113  EType mType = EType::NoError;
114  std::string mMessage = "";
115  };
116 
120  SerialPort();
121 
122  // Stops the device
123  virtual ~SerialPort();
124 
129  virtual bool init(utility::ErrorState& errorState) override;
130 
136  virtual bool start(utility::ErrorState& errorState) override;
137 
141  virtual void stop() override;
142 
148  bool isOpen() const;
149 
161  uint32 read(std::vector<uint8>& buffer, uint32 count, SerialPort::Error& error);
162 
173  uint32 read(std::vector<uint8>& buffer, SerialPort::Error& error);
174 
185  uint32 read(uint8* buffer, uint32 count, SerialPort::Error& error);
186 
197  uint32 read(std:: string& buffer, uint32 count, SerialPort::Error& error);
198 
205  std::string read(uint32 count, SerialPort::Error& error);
206 
215  uint32 readLine(std::string& buffer, uint32 length, const std::string& eol, SerialPort::Error& error);
216 
224  std::string readLine(uint32 length, const std::string& eol, SerialPort::Error& error);
225 
234  std::vector<std::string> readLines(uint32 length, const std::string& eol, SerialPort::Error& error);
235 
243  uint32 write(const uint8* data, uint32 count, SerialPort::Error& error);
244 
251  uint32 write(const std::vector<uint8>& data, SerialPort::Error& error);
252 
259  uint32 write(const std::string& data, SerialPort::Error& error);
260 
264  uint32 available();
265 
271  bool waitReadable();
272 
273  /* Block for a period of time corresponding to the transmission time of the
274  * number of characters based on the present serial settings. This may be used in con-
275  * junction with waitReadable to read larger blocks of data from the port.
276  * @param count number of cycles to wait
277  */
278  void waitByteTimes(uint32 count);
279 
283  void flush();
284 
288  void flushInput();
289 
293  void flushOutput();
294 
298  void sendBreak(uint32 duration);
299 
304  bool waitForChange();
305 
310  void setBreak(bool level = true);
311 
316  void setRTS(bool level = true);
317 
322  void setDTR(bool level = true);
323 
327  bool getCTS();
328 
332  bool getDSR();
333 
337  bool getRI();
338 
342  bool getCD();
343 
348 
349  std::string mPortName = "COM1";
350  int mBaudRate = 9600;
355  int mReadTimeout = 0;
356  int mWriteTimeout = 0;
357  int mInterByteTimeout = 0;
358  bool mAllowFailure = false;
359  private:
360  std::unique_ptr<serial::Serial> mSerialPort;
361 
362  };
363 }
nap::ESerialByteSize::Seven
@ Seven
nap::ESerialStopBits::Two
@ Two
nap::ESerialParity
ESerialParity
Definition: serialport.h:55
nap::ESerialStopBits::OnePointFive
@ OnePointFive
nap::SerialPort::destructed
Signal< SerialPort * > destructed
Definition: serialport.h:347
nap::SerialPort::Error::EType
EType
Definition: serialport.h:85
nap::ESerialStopBits::One
@ One
nap::uint8
uint8_t uint8
Definition: numeric.h:16
nap::ESerialStopBits
ESerialStopBits
Definition: serialport.h:35
nap::ESerialFlowControl::Software
@ Software
nap::utility::ErrorState
Definition: errorstate.h:19
nap::SerialPort::Error::getMessage
const std::string & getMessage()
Definition: serialport.h:101
nap::SerialPort::Error::getType
EType getType()
Definition: serialport.h:106
nap::ESerialParity::Mark
@ Mark
nap::uint32
uint32_t uint32
Definition: numeric.h:20
nap::ESerialParity::Odd
@ Odd
nap::ESerialFlowControl::Hardware
@ Hardware
nap::ESerialParity::None
@ None
nap::Signal
Definition: signalslot.h:28
nap::ESerialByteSize::Five
@ Five
nap::ESerialFlowControl
ESerialFlowControl
Definition: serialport.h:45
nap::ESerialByteSize::Six
@ Six
nap::ESerialParity::Even
@ Even
nap
Definition: templateapp.h:17
nap::SerialPort::Error
Definition: serialport.h:80
nap::ESerialByteSize::Eight
@ Eight
nap::ESerialFlowControl::None
@ None
nap::SerialPort
Definition: serialport.h:72
nap::ESerialParity::Space
@ Space
nap::SerialPort::Error::failed
bool failed()
Definition: serialport.h:96
nap::Device
Definition: device.h:20
nap::ESerialByteSize
ESerialByteSize
Definition: serialport.h:24