omw  0.2.1-beta
serialPort.h
1 /*
2 author Oliver Blaser
3 date 12.12.2023
4 copyright MIT - Copyright (c) 2023 Oliver Blaser
5 */
6 
7 #ifndef IG_OMW_IO_SERIALPORT_H
8 #define IG_OMW_IO_SERIALPORT_H
9 
10 #include <cstddef>
11 #include <cstdint>
12 #include <string>
13 #include <vector>
14 
15 #include "../../omw/defs.h"
16 #include "../../omw/string.h"
17 #include "../../omw/vector.h"
18 
19 namespace omw
20 {
21  namespace io
22  {
27  class SerialPort
28  {
29  public:
30  using baud_type = uint32_t;
31 
32 #if defined(OMW_PLAT_WIN)
33  static void initDcb(void* DCB_customDcb, baud_type baud/*, nDataBits, parity, nStopBits*/);
34 #elif defined(OMW_PLAT_UNIX)
35 #endif
36 
37  public:
38  SerialPort();
39  virtual ~SerialPort() {}
40 
41  int open(const std::string& port, baud_type baud/*, nDataBits, parity, nStopBits*/);
42 #if defined(OMW_PLAT_WIN)
43  int open(const std::string& port, void* DCB_customDcb, const void* COMMTIMEOUTS_customTmo = nullptr);
44 #elif defined(OMW_PLAT_UNIX)
45 #endif
46  int close();
47 
48  int read(uint8_t* buffer, size_t bufferSize, size_t* nBytesRead);
49  int read(char* buffer, size_t bufferSize, size_t* nBytesRead) { return read(reinterpret_cast<uint8_t*>(buffer), bufferSize, nBytesRead); }
50  int readByte(uint8_t* byte, size_t* nBytesRead) { return read(byte, 1, nBytesRead); }
51  int write(const uint8_t* data, size_t count, size_t* nBytesWritten = nullptr);
52  int write(const char* data, size_t count, size_t* nBytesWritten = nullptr) { return write(reinterpret_cast<const uint8_t*>(data), count, nBytesWritten); }
53 
54  bool isOpen() const { return m_isOpen; }
55  bool good() const { return m_good; }
56 
57  private:
58  //std::string m_port;
59  //baud_type m_baud;
60  // m_nDataBits
61  // m_parity;
62  // m_nStopBits;
63 
64  bool m_isOpen;
65  bool m_good;
66 
67  private: // platform specific
68 #if defined(OMW_PLAT_WIN)
69  void* m_handle;
70 #elif defined(OMW_PLAT_UNIX)
71  int m_fd;
72 #endif
73  };
74 
76  }
77 
78  namespace preview
79  {
83  omw::vector<omw::string> getSerialPortList(bool onlyCOMx = true);
84  void sortSerialPortList(std::vector<omw::string>& ports);
85  void sortSerialPortList(std::vector<std::string>& ports);
87  }
88 }
89 
90 #endif // IG_OMW_IO_SERIALPORT_H
omw::vector
Definition: vector.h:48
std::string
C++ standard string. See std::basic_string.
Definition: linkToStd.dox:19
omw::io::SerialPort
Definition: serialPort.h:28
omw
Main namespace.