Main MRPT website > C++ reference for MRPT 1.9.9
CSerialPort.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #pragma once
10 
11 #include <mrpt/config.h>
12 #include <mrpt/utils/CStream.h>
13 #include <mrpt/utils/CTicTac.h>
14 
15 namespace mrpt
16 {
17 namespace comms
18 {
19 /** A communications serial port built as an implementation of a utils::CStream.
20  * On communication errors (eg. the given port number does not exist,
21  * timeouts,...), most of the methods will
22  * raise an exception of the class "std::exception"
23  *
24  * The serial port to open is passed in the constructor in the form of a string
25  * description,
26  * which is platform dependent.
27  *
28  * In windows they are numbered "COM1"-"COM4" and "\\.\COMXXX" for numbers
29  * above.
30  * It is recomended to always use the prefix "\\.\" despite the actual port
31  * number.
32  *
33  * In Linux the name must refer to the device, for example: "ttyUSB0","ttyS0".
34  * If the name string does not
35  * start with "/" (an absolute path), the constructor will assume the prefix
36  * "/dev/".
37  *
38  * History:
39  * - 1/DEC/2005: (JLBC) First version
40  * - 20/DEC/2006: (JLBC) Integration into the MRPT framework
41  * - 12/DEC/2007: (JLBC) Added support for Linux.
42  * - 22/AUG/2017: (JLBC) Moved to new module mrpt-comms
43  *
44  * \todo Add the internal buffer to the Windows implementation also
45  * \ingroup mrpt_comms_grp
46  */
48 {
50 
51  public:
52  /** Constructor
53  * \param portName The serial port to open. See comments at the begining of
54  * this page.
55  * \param openNow Whether to try to open the port now. If not selected, the
56  * port should be open later with "open()".
57  *
58  */
59  CSerialPort(const std::string& portName, bool openNow = true);
60 
61  /** Default constructor: it does not open any port - later you must call
62  * "setSerialPortName" and then "open"
63  */
64  CSerialPort();
65 
66  /** Destructor
67  */
68  virtual ~CSerialPort();
69 
70  /** Sets the serial port to open (it is an error to try to change this while
71  * open yet).
72  * \sa open, close
73  */
74  void setSerialPortName(const std::string& COM_name)
75  {
76  if (isOpen()) THROW_EXCEPTION("Cannot change serial port while open");
77  m_serialName = COM_name;
78  }
79 
80  /** Open the port. If is already open results in no action.
81  * \exception std::exception On communication errors
82  */
83  void open();
84 
85  /** Open the given serial port. If it is already open and the name does not
86  * match, an exception is raised.
87  * \exception std::exception On communication errors or a different serial
88  * port already open.
89  */
90  void open(const std::string& COM_name)
91  {
92  if (isOpen() && m_serialName != COM_name)
93  THROW_EXCEPTION("Cannot change serial port while open");
94  if (!isOpen())
95  {
96  setSerialPortName(COM_name);
97  open();
98  }
99  }
100 
101  /** Close the port. If is already closed, results in no action.
102  */
103  void close();
104 
105  /** Returns if port has been correctly open.
106  */
107  bool isOpen() const;
108 
109  /** Purge tx and rx buffers.
110  * \exception std::exception On communication errors
111  */
112  void purgeBuffers();
113 
114  /** Changes the configuration of the port.
115  * \param parity 0:No parity, 1:Odd, 2:Even (WINDOWS ONLY: 3:Mark, 4:Space)
116  * \param baudRate The desired baud rate Accepted values: 50 - 230400
117  * \param bits Bits per word (typ. 8) Accepted values: 5,6,7,8.
118  * \param nStopBits Stop bits (typ. 1) Accepted values: 1,2
119  * \param enableFlowControl Whether to enable the hardware flow control
120  * (RTS/CTS) (default=no)
121  * \exception std::exception On communication errors
122  */
123  void setConfig(
124  int baudRate, int parity = 0, int bits = 8, int nStopBits = 1,
125  bool enableFlowControl = false);
126 
127  /** Changes the timeouts of the port, in milliseconds.
128  * \exception std::exception On communication errors
129  */
130  void setTimeouts(
131  int ReadIntervalTimeout, int ReadTotalTimeoutMultiplier,
132  int ReadTotalTimeoutConstant, int WriteTotalTimeoutMultiplier,
133  int WriteTotalTimeoutConstant);
134 
135  /** Implements the virtual method responsible for reading from the stream -
136  * Unlike CStream::ReadBuffer, this method will not raise an exception on
137  * zero bytes read, as long as there is not any fatal error in the
138  * communications.
139  * \exception std::exception On communication errors
140  */
141  size_t Read(void* Buffer, size_t Count);
142 
143  /** Reads one text line from the serial port in POSIX "canonical mode".
144  * This method reads from the serial port until one of the characters in
145  * \a eol are found.
146  * \param eol_chars A line reception is finished when one of these
147  * characters is found. Default: LF (10), CR (13).
148  * \param total_timeout_ms If >0, the maximum number of milliseconds to
149  * wait.
150  * \param out_timeout If provided, will hold true on return if a timeout
151  * ocurred, false on a valid read.
152  * \return The read string, without the final
153  * \exception std::exception On communication errors
154  */
156  const int total_timeout_ms = -1, bool* out_timeout = nullptr,
157  const char* eol_chars = "\r\n");
158 
159  /** Implements the virtual method responsible for writing to the stream.
160  * Write attempts to write up to Count bytes to Buffer, and returns the
161  * number of bytes actually written.
162  * \exception std::exception On communication errors
163  */
164  size_t Write(const void* Buffer, size_t Count);
165 
166  /** Introduces a pure virtual method for moving to a specified position in
167  *the streamed resource.
168  * he Origin parameter indicates how to interpret the Offset parameter.
169  *Origin should be one of the following values:
170  * - sFromBeginning (Default) Offset is from the beginning of the
171  *resource. Seek moves to the position Offset. Offset must be >= 0.
172  * - sFromCurrent Offset is from the current position in the resource.
173  *Seek moves to Position + Offset.
174  * - sFromEnd Offset is from the end of the resource. Offset must
175  *be
176  *<= 0 to indicate a number of bytes before the end of the file.
177  * \return Seek returns the new value of the Position property.
178  */
179  uint64_t Seek(uint64_t Offset, CStream::TSeekOrigin Origin = sFromBeginning)
180  {
181  MRPT_START
182  MRPT_UNUSED_PARAM(Origin);
183  MRPT_UNUSED_PARAM(Offset);
185  "Method not applicable to serial communications port CStream!");
186  MRPT_END
187  }
188 
189  /** Returns the total amount of bytes in the stream.
190  */
192  {
193  MRPT_START
195  "Method not applicable to serial communications port CStream!");
196  MRPT_END
197  }
198 
199  /** Method for getting the current cursor position, where 0 is the first
200  * byte and TotalBytesCount-1 the last one.
201  */
203  {
204  MRPT_START
206  "Method not applicable to serial communications port CStream!");
207  MRPT_END
208  }
209 
210  protected:
211  /** The complete name of the serial port device (i.e.
212  * "\\.\COM10","/dev/ttyS2",...)
213  */
217 
218  /** Used only in \a ReadString */
220 
221 #ifdef MRPT_OS_WINDOWS
222  // WINDOWS
223  void* hCOM;
224 #else
225  // LINUX
226  /** The file handle (-1: Not open)
227  */
228  int hCOM;
229 // size_t ReadUnbuffered(void *Buffer, size_t Count); // JL: Remove??
230 #endif
231 
232 }; // end of class
233 
234 } // end of namespace
235 } // end of namespace
void open(const std::string &COM_name)
Open the given serial port.
Definition: CSerialPort.h:90
void setSerialPortName(const std::string &COM_name)
Sets the serial port to open (it is an error to try to change this while open yet).
Definition: CSerialPort.h:74
size_t Write(const void *Buffer, size_t Count)
Implements the virtual method responsible for writing to the stream.
A communications serial port built as an implementation of a utils::CStream.
Definition: CSerialPort.h:47
uint64_t Seek(uint64_t Offset, CStream::TSeekOrigin Origin=sFromBeginning)
Introduces a pure virtual method for moving to a specified position in the streamed resource...
Definition: CSerialPort.h:179
std::string ReadString(const int total_timeout_ms=-1, bool *out_timeout=nullptr, const char *eol_chars="\")
Reads one text line from the serial port in POSIX "canonical mode".
uint64_t getPosition()
Method for getting the current cursor position, where 0 is the first byte and TotalBytesCount-1 the l...
Definition: CSerialPort.h:202
uint64_t getTotalBytesCount()
Returns the total amount of bytes in the stream.
Definition: CSerialPort.h:191
#define THROW_EXCEPTION(msg)
void open()
Open the port.
Definition: CSerialPort.cpp:88
void setConfig(int baudRate, int parity=0, int bits=8, int nStopBits=1, bool enableFlowControl=false)
Changes the configuration of the port.
mrpt::utils::CTicTac m_timer
Used only in ReadString.
Definition: CSerialPort.h:219
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:41
#define MRPT_END
#define MRPT_UNUSED_PARAM(a)
Can be used to avoid "not used parameters" warnings from the compiler.
This class implements a high-performance stopwatch.
Definition: CTicTac.h:23
bool isOpen() const
Returns if port has been correctly open.
void purgeBuffers()
Purge tx and rx buffers.
GLsizei const GLchar ** string
Definition: glext.h:4101
void close()
Close the port.
void setTimeouts(int ReadIntervalTimeout, int ReadTotalTimeoutMultiplier, int ReadTotalTimeoutConstant, int WriteTotalTimeoutMultiplier, int WriteTotalTimeoutConstant)
Changes the timeouts of the port, in milliseconds.
#define MRPT_START
unsigned __int64 uint64_t
Definition: rptypes.h:50
friend class PosixSignalDispatcherImpl
Definition: CSerialPort.h:49
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
std::string m_serialName
The complete name of the serial port device (i.e.
Definition: CSerialPort.h:214
CSerialPort()
Default constructor: it does not open any port - later you must call "setSerialPortName" and then "op...
Definition: CSerialPort.cpp:67
int hCOM
The file handle (-1: Not open)
Definition: CSerialPort.h:228
size_t Read(void *Buffer, size_t Count)
Implements the virtual method responsible for reading from the stream - Unlike CStream::ReadBuffer, this method will not raise an exception on zero bytes read, as long as there is not any fatal error in the communications.
virtual ~CSerialPort()
Destructor.
Definition: CSerialPort.cpp:80



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019