UniSet  2.24.2
ComPort.h
1 /*
2  * Copyright (c) 2015 Pavel Vainerman.
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as
6  * published by the Free Software Foundation, version 2.1.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * Lesser General Lesser Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 // --------------------------------------------------------------------------
17 #ifndef COMPORT_H_
18 #define COMPORT_H_
19 // --------------------------------------------------------------------------
20 #include <termios.h>
21 #include <fcntl.h>
22 #include <sys/ioctl.h>
23 #include <string>
24 #include "PassiveTimer.h" // for use timeout_t
25 // --------------------------------------------------------------------------
26 namespace uniset
27 {
28 
29  class ComPort
30  {
31  public:
32  enum Speed
33  {
34  ComSpeed0 = B0,
35  ComSpeed50 = B50,
36  ComSpeed75 = B75,
37  ComSpeed110 = B110,
38  ComSpeed134 = B134,
39  ComSpeed150 = B150,
40  ComSpeed200 = B200,
41  ComSpeed300 = B300,
42  ComSpeed600 = B600,
43  ComSpeed1200 = B1200,
44  ComSpeed1800 = B1800,
45  ComSpeed2400 = B2400,
46  ComSpeed4800 = B4800,
47  ComSpeed9600 = B9600,
48  ComSpeed19200 = B19200,
49  ComSpeed38400 = B38400,
50  ComSpeed57600 = B57600,
51  ComSpeed115200 = B115200,
52  ComSpeed230400 = B230400,
53  ComSpeed460800 = B460800,
54  ComSpeed500000 = B500000,
55  ComSpeed576000 = B576000,
56  ComSpeed921600 = B921600,
57  ComSpeed1000000 = B1000000,
58  ComSpeed1152000 = B1152000,
59  ComSpeed1500000 = B1500000,
60  ComSpeed2000000 = B2000000,
61  ComSpeed2500000 = B2500000,
62  ComSpeed3000000 = B3000000,
63  ComSpeed3500000 = B3500000,
64  ComSpeed4000000 = B4000000
65  };
66  enum Parity
67  {
68  Odd,
69  Even,
70  Space,
71  Mark,
72  NoParity
73  };
74  enum CharacterSize
75  {
76  CSize5 = CS5,
77  CSize6 = CS6,
78  CSize7 = CS7,
79  CSize8 = CS8
80  };
81  enum StopBits
82  {
83  OneBit = 1,
84  OneAndHalfBits = 2,
85  TwoBits = 3
86  };
87 
88  ComPort( const std::string& comDevice, bool nocreate = false );
89  virtual ~ComPort();
90 
91  inline std::string getDevice()
92  {
93  return dev;
94  }
95 
96  void setSpeed( Speed s );
97  void setSpeed( const std::string& speed );
98  Speed getSpeed() const;
99 
100  static Speed getSpeed( const std::string& s );
101  static std::string getSpeed( Speed s );
102  static Parity getParity( const std::string& s );
103  static CharacterSize getCharacterSize(const std::string& s );
104 
105  void setParity(Parity);
106  void setParity(const std::string& s);
107  Parity getParity();
108 
109  void setCharacterSize(CharacterSize);
110  CharacterSize getCharacterSize();
111 
112  void setStopBits(StopBits sBit);
113  StopBits getStopBits();
114 
115  virtual void setTimeout( timeout_t msec );
116  timeout_t getTimeout() const;
117 
118  void setWaiting(bool waiting);
119 
120  virtual unsigned char receiveByte();
121  virtual void sendByte(unsigned char x);
122 
123  virtual size_t receiveBlock( unsigned char* msg, size_t len );
124  virtual ssize_t sendBlock( unsigned char* msg, size_t len );
125 
126  void setBlocking(bool blocking);
127 
128  virtual void cleanupChannel();
129  virtual void reopen();
130 
131  protected:
132  void openPort();
133 
134  static const size_t BufSize = 8192;
135  unsigned char buf[BufSize];
136  ssize_t curSym = { 0 };
137  ssize_t bufLength = { 0 };
138  int fd = { -1 };
139  timeout_t uTimeout = { 0 };
140  bool waiting = { false };
141  Speed speed = ComSpeed38400;
142  std::string dev = { "" };
143  Parity parity = NoParity;
144  CharacterSize charSize = CSize8;
145  StopBits stopBits = OneBit;
146 
147  virtual unsigned char m_receiveByte( bool wait );
148 
149  private:
150  struct termios oldTermios;
151  };
152  // -------------------------------------------------------------------------
153 } // end of uniset namespace
154 // --------------------------------------------------------------------------
155 #endif // COMPORT_H_
156 // --------------------------------------------------------------------------
Definition: ComPort.h:30
Definition: CommonEventLoop.h:15