UniSet  2.8.0
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 
103  void setParity(Parity);
104  void setCharacterSize(CharacterSize);
105  void setStopBits(StopBits sBit);
106 
107  virtual void setTimeout( timeout_t msec );
108  timeout_t getTimeout() const;
109 
110  void setWaiting(bool waiting);
111 
112  virtual unsigned char receiveByte();
113  virtual void sendByte(unsigned char x);
114 
115  virtual size_t receiveBlock( unsigned char* msg, size_t len );
116  virtual ssize_t sendBlock( unsigned char* msg, size_t len );
117 
118  void setBlocking(bool blocking);
119 
120  virtual void cleanupChannel();
121  virtual void reopen();
122 
123  protected:
124  void openPort();
125 
126  static const size_t BufSize = 8192;
127  unsigned char buf[BufSize];
128  ssize_t curSym = { 0 };
129  ssize_t bufLength = { 0 };
130  int fd = { -1 };
131  timeout_t uTimeout = { 0 };
132  bool waiting = { false };
133  Speed speed = ComSpeed38400;
134  std::string dev = { "" };
135 
136  virtual unsigned char m_receiveByte( bool wait );
137 
138  private:
139  struct termios oldTermios;
140  };
141  // -------------------------------------------------------------------------
142 } // end of uniset namespace
143 // --------------------------------------------------------------------------
144 #endif // COMPORT_H_
145 // --------------------------------------------------------------------------
Definition: CommonEventLoop.h:14
Definition: ComPort.h:29