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



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST