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-2018, 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/io/CStream.h>
12 #include <mrpt/system/CTicTac.h>
13 
14 namespace mrpt::comms
15 {
16 /** A communications serial port built as an implementation of a utils::CStream.
17  * On communication errors (eg. the given port number does not exist,
18  * timeouts,...), most of the methods will
19  * raise an exception of the class `std::exception`
20  *
21  * The serial port to open is passed in the constructor in the form of a string
22  * description, which is platform dependent.
23  *
24  * In Windows they are numbered "COM1"-"COM4" and "\\.\COMXXX" for numbers
25  * above. It is recomended to always use the prefix "\\.\" despite the actual
26  * port number.
27  *
28  * In Linux the name must refer to the device, for example: "ttyUSB0","ttyS0".
29  * If the name string does not start with "/" (an absolute path), the
30  * constructor will assume the prefix "/dev/".
31  *
32  * History:
33  * - 1/DEC/2005: (JLBC) First version
34  * - 20/DEC/2006: (JLBC) Integration into the MRPT framework
35  * - 12/DEC/2007: (JLBC) Added support for Linux.
36  * - 22/AUG/2017: (JLBC) Moved to new module mrpt-comms
37  *
38  * \todo Add the internal buffer to the Windows implementation also
39  * \ingroup mrpt_comms_grp
40  */
42 {
44 
45  public:
46  /** Constructor
47  * \param portName The serial port to open. See comments at the begining of
48  * this page.
49  * \param openNow Whether to try to open the port now. If not selected, the
50  * port should be open later with "open()".
51  *
52  */
53  CSerialPort(const std::string& portName, bool openNow = true);
54 
55  /** Default constructor: it does not open any port - later you must call
56  * "setSerialPortName" and then "open"
57  */
58  CSerialPort();
59 
60  /** Destructor
61  */
62  virtual ~CSerialPort();
63 
64  /** Sets the serial port to open (it is an error to try to change this while
65  * open yet).
66  * \sa open, close
67  */
68  void setSerialPortName(const std::string& COM_name);
69 
70  /** Open the port. If is already open results in no action.
71  * \exception std::exception On communication errors
72  */
73  void open();
74 
75  /** Open the given serial port. If it is already open and the name does not
76  * match, an exception is raised.
77  * \exception std::exception On communication errors or a different serial
78  * port already open.
79  */
80  void open(const std::string& COM_name);
81 
82  /** Close the port. If is already closed, results in no action.
83  */
84  void close();
85 
86  /** Returns if port has been correctly open.
87  */
88  bool isOpen() const;
89 
90  /** Purge tx and rx buffers.
91  * \exception std::exception On communication errors
92  */
93  void purgeBuffers();
94 
95  /** Changes the configuration of the port.
96  * \param parity 0:No parity, 1:Odd, 2:Even (WINDOWS ONLY: 3:Mark, 4:Space)
97  * \param baudRate The desired baud rate Accepted values: 50 - 230400
98  * \param bits Bits per word (typ. 8) Accepted values: 5,6,7,8.
99  * \param nStopBits Stop bits (typ. 1) Accepted values: 1,2
100  * \param enableFlowControl Whether to enable the hardware flow control
101  * (RTS/CTS) (default=no)
102  * \exception std::exception On communication errors
103  */
104  void setConfig(
105  int baudRate, int parity = 0, int bits = 8, int nStopBits = 1,
106  bool enableFlowControl = false);
107 
108  /** Changes the timeouts of the port, in milliseconds.
109  * \exception std::exception On communication errors
110  */
111  void setTimeouts(
112  int ReadIntervalTimeout, int ReadTotalTimeoutMultiplier,
113  int ReadTotalTimeoutConstant, int WriteTotalTimeoutMultiplier,
114  int WriteTotalTimeoutConstant);
115 
116  /** Implements the virtual method responsible for reading from the stream -
117  * Unlike CStream::ReadBuffer, this method will not raise an exception on
118  * zero bytes read, as long as there is not any fatal error in the
119  * communications.
120  * \exception std::exception On communication errors
121  */
122  size_t Read(void* Buffer, size_t Count);
123 
124  /** Reads one text line from the serial port in POSIX "canonical mode".
125  * This method reads from the serial port until one of the characters in
126  * \a eol are found.
127  * \param eol_chars A line reception is finished when one of these
128  * characters is found. Default: LF (10), CR (13).
129  * \param total_timeout_ms If >0, the maximum number of milliseconds to
130  * wait.
131  * \param out_timeout If provided, will hold true on return if a timeout
132  * ocurred, false on a valid read.
133  * \return The read string, without the final
134  * \exception std::exception On communication errors
135  */
137  const int total_timeout_ms = -1, bool* out_timeout = nullptr,
138  const char* eol_chars = "\r\n");
139 
140  // See base class docs
141  size_t Write(const void* Buffer, size_t Count) override;
142  /** not applicable in a serial port */
143  uint64_t Seek(
144  int64_t off, CStream::TSeekOrigin o = sFromBeginning) override;
145  /** not applicable in a serial port */
146  uint64_t getTotalBytesCount() const override;
147  /** not applicable in a serial port */
148  uint64_t getPosition() const override;
149 
150  protected:
151  /** The complete name of the serial port device (i.e.
152  * "\\.\COM10","/dev/ttyS2",...)
153  */
158 #ifdef _WIN32
159  // WINDOWS
160  void* hCOM;
161 #else
162  // LINUX
163  /** The file handle (-1: Not open)
164  */
165  int hCOM;
166 #endif
167 }; // end of class
168 }
169 
A communications serial port built as an implementation of a utils::CStream.
Definition: CSerialPort.h:41
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".
A high-performance stopwatch, with typical resolution of nanoseconds.
void open()
Open the port.
uint64_t getTotalBytesCount() const override
not applicable in a serial port
void setConfig(int baudRate, int parity=0, int bits=8, int nStopBits=1, bool enableFlowControl=false)
Changes the configuration of the port.
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: io/CStream.h:28
__int64 int64_t
Definition: rptypes.h:49
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.cpp:97
bool isOpen() const
Returns if port has been correctly open.
size_t Write(const void *Buffer, size_t Count) override
Introduces a pure virtual method responsible for writing to the stream.
void purgeBuffers()
Purge tx and rx buffers.
mrpt::system::CTicTac m_timer
Definition: CSerialPort.h:157
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.
unsigned __int64 uint64_t
Definition: rptypes.h:50
friend class PosixSignalDispatcherImpl
Definition: CSerialPort.h:43
std::string m_serialName
The complete name of the serial port device (i.e.
Definition: CSerialPort.h:154
uint64_t Seek(int64_t off, CStream::TSeekOrigin o=sFromBeginning) override
not applicable in a serial port
Serial and networking devices and utilities.
CSerialPort()
Default constructor: it does not open any port - later you must call "setSerialPortName" and then "op...
Definition: CSerialPort.cpp:68
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:81
uint64_t getPosition() const override
not applicable in a serial port



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020