MRPT
1.9.9
|
A communications serial port built as an implementation of a utils::CStream.
On communication errors (eg. the given port number does not exist, timeouts,...), most of the methods will raise an exception of the class std::exception
The serial port to open is passed in the constructor in the form of a string description, which is platform dependent.
In Windows they are numbered "COM1"-"COM4" and "\\.\COMXXX" for numbers above. It is recomended to always use the prefix "\\.\" despite the actual port number.
In Linux the name must refer to the device, for example: "ttyUSB0","ttyS0". If the name string does not start with "/" (an absolute path), the constructor will assume the prefix "/dev/".
History:
Definition at line 41 of file CSerialPort.h.
#include <mrpt/comms/CSerialPort.h>
Public Types | |
enum | TSeekOrigin { sFromBeginning = 0, sFromCurrent = 1, sFromEnd = 2 } |
Used in CStream::Seek. More... | |
Public Member Functions | |
CSerialPort (const std::string &portName, bool openNow=true) | |
Constructor. More... | |
CSerialPort ()=default | |
Default constructor: it does not open any port - later you must call "setSerialPortName" and then "open". More... | |
~CSerialPort () override | |
Destructor. More... | |
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). More... | |
void | open () |
Open the port. More... | |
void | open (const std::string &COM_name) |
Open the given serial port. More... | |
void | close () |
Close the port. More... | |
bool | isOpen () const |
Returns if port has been correctly open. More... | |
void | purgeBuffers () |
Purge tx and rx buffers. More... | |
void | setConfig (int baudRate, int parity=0, int bits=8, int nStopBits=1, bool enableFlowControl=false) |
Changes the configuration of the port. More... | |
void | setTimeouts (int ReadIntervalTimeout, int ReadTotalTimeoutMultiplier, int ReadTotalTimeoutConstant, int WriteTotalTimeoutMultiplier, int WriteTotalTimeoutConstant) |
Changes the timeouts of the port, in milliseconds. More... | |
size_t | Read (void *Buffer, size_t Count) override |
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. More... | |
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". More... | |
size_t | Write (const void *Buffer, size_t Count) override |
Introduces a pure virtual method responsible for writing to the stream. More... | |
uint64_t | Seek (int64_t off, CStream::TSeekOrigin o=sFromBeginning) override |
not applicable in a serial port More... | |
uint64_t | getTotalBytesCount () const override |
not applicable in a serial port More... | |
uint64_t | getPosition () const override |
not applicable in a serial port More... | |
virtual size_t | ReadBufferImmediate (void *Buffer, size_t Count) |
Reads a block of bytes from the stream into Buffer, and returns the amound of bytes actually read, without waiting for more extra bytes to arrive (just those already enqued in the stream). More... | |
virtual uint64_t | Seek (int64_t Offset, CStream::TSeekOrigin Origin=sFromBeginning)=0 |
Introduces a pure virtual method for moving to a specified position in the streamed resource. More... | |
virtual int | printf (const char *fmt,...) MRPT_printf_format_check(2 |
Writes a string to the stream in a textual form. More... | |
template<typename CONTAINER_TYPE > | |
virtual int void | printf_vector (const char *fmt, const CONTAINER_TYPE &V, char separator=',') |
Prints a vector in the format [A,B,C,...] using CStream::printf, and the fmt string for each vector element T . More... | |
bool | getline (std::string &out_str) |
Reads from the stream until a ' ' character is found ('' characters are ignored). More... | |
Protected Attributes | |
std::string | m_serialName |
The complete name of the serial port device (i.e. More... | |
int | m_baudRate {0} |
int | m_totalTimeout_ms {0} |
int | m_interBytesTimeout_ms {0} |
mrpt::system::CTicTac | m_timer |
void * | hCOM {nullptr} |
Friends | |
class | PosixSignalDispatcherImpl |
|
inherited |
Used in CStream::Seek.
Enumerator | |
---|---|
sFromBeginning | |
sFromCurrent | |
sFromEnd |
Definition at line 32 of file io/CStream.h.
CSerialPort::CSerialPort | ( | const std::string & | portName, |
bool | openNow = true |
||
) |
Constructor.
portName | The serial port to open. See comments at the begining of this page. |
openNow | Whether to try to open the port now. If not selected, the port should be open later with "open()". |
Definition at line 54 of file CSerialPort.cpp.
References open().
|
default |
Default constructor: it does not open any port - later you must call "setSerialPortName" and then "open".
|
override |
Destructor.
Definition at line 61 of file CSerialPort.cpp.
References close(), mrpt::exception_to_str(), and isOpen().
void CSerialPort::close | ( | ) |
Close the port.
If is already closed, results in no action.
Definition at line 625 of file CSerialPort.cpp.
References hCOM, MRPT_END, and MRPT_START.
Referenced by mrpt::hwdrivers::CNTRIPEmitter::initialize(), Read(), ReadString(), mrpt::hwdrivers::CGillAnemometer::~CGillAnemometer(), mrpt::hwdrivers::CNTRIPEmitter::~CNTRIPEmitter(), mrpt::hwdrivers::CRaePID::~CRaePID(), and ~CSerialPort().
|
inherited |
Reads from the stream until a '
' character is found ('' characters are ignored).
Definition at line 69 of file CStream.cpp.
|
overridevirtual |
not applicable in a serial port
Implements mrpt::io::CStream.
Definition at line 938 of file CSerialPort.cpp.
References MRPT_END, MRPT_START, and THROW_EXCEPTION.
|
overridevirtual |
not applicable in a serial port
Implements mrpt::io::CStream.
Definition at line 930 of file CSerialPort.cpp.
References MRPT_END, MRPT_START, and THROW_EXCEPTION.
bool CSerialPort::isOpen | ( | ) | const |
Returns if port has been correctly open.
Definition at line 190 of file CSerialPort.cpp.
References hCOM.
Referenced by mrpt::hwdrivers::CNTRIPEmitter::doProcess(), mrpt::hwdrivers::CNTRIPEmitter::initialize(), open(), purgeBuffers(), Read(), ReadString(), setConfig(), setSerialPortName(), setTimeouts(), mrpt::hwdrivers::CCANBusReader::tryToOpenComms(), Write(), mrpt::hwdrivers::CNTRIPEmitter::~CNTRIPEmitter(), and ~CSerialPort().
void CSerialPort::open | ( | ) |
Open the port.
If is already open results in no action.
std::exception | On communication errors |
Definition at line 93 of file CSerialPort.cpp.
References hCOM, m_serialName, MRPT_END, MRPT_START, THROW_EXCEPTION, and THROW_EXCEPTION_FMT.
Referenced by CSerialPort(), mrpt::hwdrivers::CNTRIPEmitter::initialize(), and mrpt::hwdrivers::CCANBusReader::tryToOpenComms().
void CSerialPort::open | ( | const std::string & | COM_name | ) |
Open the given serial port.
If it is already open and the name does not match, an exception is raised.
std::exception | On communication errors or a different serial port already open. |
Definition at line 73 of file CSerialPort.cpp.
References isOpen(), m_serialName, open(), setSerialPortName(), and THROW_EXCEPTION.
Referenced by open().
|
virtualinherited |
Writes a string to the stream in a textual form.
Definition at line 30 of file CStream.cpp.
References MRPT_END, MRPT_START, and mrpt::system::os::vsnprintf().
Referenced by mrpt::hmtslam::CTopLCDetector_GridMatching::computeTopologicalObservationModel(), mrpt::apps::MonteCarloLocalization_Base::do_pf_localization(), mrpt::io::CStream::printf_vector(), mrpt::apps::CGridMapAlignerApp::run(), mrpt::apps::RBPF_SLAM_App_Base::run(), and mrpt::apps::ICP_SLAM_App_Base::run().
|
inlineinherited |
Prints a vector in the format [A,B,C,...] using CStream::printf, and the fmt string for each vector element T
.
CONTAINER_TYPE | can be any vector<T>, deque<T> or alike. |
Definition at line 102 of file io/CStream.h.
References mrpt::io::CStream::printf().
Referenced by mrpt::hmtslam::CTopLCDetector_GridMatching::computeTopologicalObservationModel().
void CSerialPort::purgeBuffers | ( | ) |
Purge tx and rx buffers.
std::exception | On communication errors |
Definition at line 899 of file CSerialPort.cpp.
References hCOM, isOpen(), MRPT_END, MRPT_START, THROW_EXCEPTION, and THROW_EXCEPTION_FMT.
Referenced by mrpt::hwdrivers::CNTRIPEmitter::initialize(), mrpt::hwdrivers::CHokuyoURG::purgeBuffers(), and mrpt::hwdrivers::CCANBusReader::setupSerialComms().
|
overridevirtual |
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.
std::exception | On communication errors |
Implements mrpt::io::CStream.
Definition at line 648 of file CSerialPort.cpp.
References close(), hCOM, isOpen(), m_interBytesTimeout_ms, m_timer, m_totalTimeout_ms, MRPT_END, MRPT_START, mrpt::system::CTicTac::Tac(), THROW_EXCEPTION, and mrpt::system::CTicTac::Tic().
Referenced by mrpt::hwdrivers::CNTRIPEmitter::doProcess(), mrpt::hwdrivers::CCANBusReader::waitACK(), mrpt::hwdrivers::CCANBusReader::waitContinuousSampleFrame(), mrpt::hwdrivers::CCANBusReader::waitForVersion(), and mrpt::hwdrivers::CCANBusReader::waitIncomingFrame().
|
inlinevirtualinherited |
Reads a block of bytes from the stream into Buffer, and returns the amound of bytes actually read, without waiting for more extra bytes to arrive (just those already enqued in the stream).
Note that this method will fallback to ReadBuffer() in most CStream classes but in some hardware-related classes.
std::exception | On any error, or if ZERO bytes are read. |
Reimplemented in mrpt::comms::CInterfaceFTDI.
Definition at line 62 of file io/CStream.h.
References mrpt::io::CStream::Read().
std::string CSerialPort::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".
This method reads from the serial port until one of the characters in eol are found.
eol_chars | A line reception is finished when one of these characters is found. Default: LF (10), CR (13). |
total_timeout_ms | If >0, the maximum number of milliseconds to wait. |
out_timeout | If provided, will hold true on return if a timeout ocurred, false on a valid read. |
std::exception | On communication errors |
This method reads from the serial port until one of the characters in eol are found.
Definition at line 739 of file CSerialPort.cpp.
References ASSERT_, close(), hCOM, isOpen(), m_timer, MRPT_TRY_END, MRPT_TRY_START, mrpt::system::CTicTac::Tac(), THROW_EXCEPTION, and mrpt::system::CTicTac::Tic().
|
pure virtualinherited |
Introduces a pure virtual method for moving to a specified position in the streamed resource.
he Origin parameter indicates how to interpret the Offset parameter. Origin should be one of the following values:
Implemented in mrpt::io::CPipeBaseEndPoint, mrpt::io::CFileStream, mrpt::io::CFileGZInputStream, mrpt::io::CFileGZOutputStream, mrpt::io::CMemoryStream, mrpt::io::CFileOutputStream, and mrpt::io::CFileInputStream.
|
override |
not applicable in a serial port
Definition at line 920 of file CSerialPort.cpp.
References MRPT_END, MRPT_START, and THROW_EXCEPTION.
void CSerialPort::setConfig | ( | int | baudRate, |
int | parity = 0 , |
||
int | bits = 8 , |
||
int | nStopBits = 1 , |
||
bool | enableFlowControl = false |
||
) |
Changes the configuration of the port.
parity | 0:No parity, 1:Odd, 2:Even (WINDOWS ONLY: 3:Mark, 4:Space) |
baudRate | The desired baud rate Accepted values: 50 - 230400 |
bits | Bits per word (typ. 8) Accepted values: 5,6,7,8. |
nStopBits | Stop bits (typ. 1) Accepted values: 1,2 |
enableFlowControl | Whether to enable the hardware flow control (RTS/CTS) (default=no) |
std::exception | On communication errors |
Definition at line 202 of file CSerialPort.cpp.
References ASSERT_, hCOM, isOpen(), m_baudRate, MRPT_END, MRPT_START, THROW_EXCEPTION, and THROW_EXCEPTION_FMT.
Referenced by mrpt::hwdrivers::CNTRIPEmitter::initialize(), mrpt::hwdrivers::CCANBusReader::setupSerialComms(), mrpt::hwdrivers::CCANBusReader::tryToOpenComms(), and mrpt::hwdrivers::CHokuyoURG::turnOn().
void CSerialPort::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 at line 84 of file CSerialPort.cpp.
References isOpen(), m_serialName, and THROW_EXCEPTION.
Referenced by open(), mrpt::hwdrivers::CCANBusReader::tryToOpenComms(), and mrpt::hwdrivers::CSickLaserSerial::tryToOpenComms().
void CSerialPort::setTimeouts | ( | int | ReadIntervalTimeout, |
int | ReadTotalTimeoutMultiplier, | ||
int | ReadTotalTimeoutConstant, | ||
int | WriteTotalTimeoutMultiplier, | ||
int | WriteTotalTimeoutConstant | ||
) |
Changes the timeouts of the port, in milliseconds.
std::exception | On communication errors |
Definition at line 564 of file CSerialPort.cpp.
References hCOM, isOpen(), m_interBytesTimeout_ms, m_totalTimeout_ms, MRPT_END, MRPT_START, THROW_EXCEPTION, and THROW_EXCEPTION_FMT.
Referenced by mrpt::hwdrivers::CNTRIPEmitter::initialize(), and mrpt::hwdrivers::CCANBusReader::tryToOpenComms().
|
overridevirtual |
Introduces a pure virtual method responsible for writing to the stream.
Write attempts to write up to Count bytes to Buffer, and returns the number of bytes actually written.
Implements mrpt::io::CStream.
Definition at line 834 of file CSerialPort.cpp.
References mrpt::containers::end(), hCOM, isOpen(), MRPT_END, MRPT_START, THROW_EXCEPTION, and THROW_EXCEPTION_FMT.
Referenced by mrpt::hwdrivers::CNTRIPEmitter::doProcess(), and mrpt::hwdrivers::CCANBusReader::sendCommandToCANReader().
|
friend |
Definition at line 43 of file CSerialPort.h.
|
protected |
Definition at line 160 of file CSerialPort.h.
Referenced by close(), isOpen(), open(), purgeBuffers(), Read(), ReadString(), setConfig(), setTimeouts(), and Write().
|
protected |
Definition at line 155 of file CSerialPort.h.
Referenced by setConfig().
|
protected |
Definition at line 156 of file CSerialPort.h.
Referenced by Read(), and setTimeouts().
|
protected |
The complete name of the serial port device (i.e.
"\\.\COM10","/dev/ttyS2",...)
Definition at line 154 of file CSerialPort.h.
Referenced by open(), and setSerialPortName().
|
protected |
Definition at line 157 of file CSerialPort.h.
Referenced by Read(), and ReadString().
|
protected |
Definition at line 156 of file CSerialPort.h.
Referenced by Read(), and setTimeouts().
Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: c7a3bec24 Sun Mar 29 18:33:13 2020 +0200 at dom mar 29 18:50:38 CEST 2020 |