MRPT  1.9.9
mrpt::comms::CSerialPort Class Referenceabstract

Detailed Description

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:

  • 1/DEC/2005: (JLBC) First version
  • 20/DEC/2006: (JLBC) Integration into the MRPT framework
  • 12/DEC/2007: (JLBC) Added support for Linux.
  • 22/AUG/2017: (JLBC) Moved to new module mrpt-comms
Todo:
Add the internal buffer to the Windows implementation also

Definition at line 41 of file CSerialPort.h.

#include <mrpt/comms/CSerialPort.h>

Inheritance diagram for mrpt::comms::CSerialPort:

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
 
voidhCOM {nullptr}
 

Friends

class PosixSignalDispatcherImpl
 

Member Enumeration Documentation

◆ TSeekOrigin

Used in CStream::Seek.

Enumerator
sFromBeginning 
sFromCurrent 
sFromEnd 

Definition at line 32 of file io/CStream.h.

Constructor & Destructor Documentation

◆ CSerialPort() [1/2]

CSerialPort::CSerialPort ( const std::string portName,
bool  openNow = true 
)

Constructor.

Parameters
portNameThe serial port to open. See comments at the begining of this page.
openNowWhether 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().

Here is the call graph for this function:

◆ CSerialPort() [2/2]

mrpt::comms::CSerialPort::CSerialPort ( )
default

Default constructor: it does not open any port - later you must call "setSerialPortName" and then "open".

◆ ~CSerialPort()

CSerialPort::~CSerialPort ( )
override

Destructor.

Definition at line 61 of file CSerialPort.cpp.

References close(), and isOpen().

Here is the call graph for this function:

Member Function Documentation

◆ close()

void CSerialPort::close ( )

Close the port.

If is already closed, results in no action.

Definition at line 618 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::CGyroKVHDSP3000::~CGyroKVHDSP3000(), mrpt::hwdrivers::CNTRIPEmitter::~CNTRIPEmitter(), mrpt::hwdrivers::CRaePID::~CRaePID(), and ~CSerialPort().

Here is the caller graph for this function:

◆ getline()

bool CStream::getline ( std::string out_str)
inherited

Reads from the stream until a '
' character is found ('' characters are ignored).

Returns
false on EOF or any other read error.

Definition at line 69 of file CStream.cpp.

◆ getPosition()

uint64_t CSerialPort::getPosition ( ) const
overridevirtual

not applicable in a serial port

Implements mrpt::io::CStream.

Definition at line 931 of file CSerialPort.cpp.

References MRPT_END, MRPT_START, and THROW_EXCEPTION.

◆ getTotalBytesCount()

uint64_t CSerialPort::getTotalBytesCount ( ) const
overridevirtual

not applicable in a serial port

Implements mrpt::io::CStream.

Definition at line 923 of file CSerialPort.cpp.

References MRPT_END, MRPT_START, and THROW_EXCEPTION.

◆ isOpen()

bool CSerialPort::isOpen ( ) const

Returns if port has been correctly open.

Definition at line 183 of file CSerialPort.cpp.

References hCOM.

Referenced by mrpt::hwdrivers::CNTRIPEmitter::doProcess(), mrpt::hwdrivers::CNTRIPEmitter::initialize(), mrpt::hwdrivers::CGyroKVHDSP3000::initialize(), open(), purgeBuffers(), Read(), ReadString(), setConfig(), setSerialPortName(), setTimeouts(), mrpt::hwdrivers::CCANBusReader::tryToOpenComms(), Write(), mrpt::hwdrivers::CNTRIPEmitter::~CNTRIPEmitter(), and ~CSerialPort().

Here is the caller graph for this function:

◆ open() [1/2]

void CSerialPort::open ( )

Open the port.

If is already open results in no action.

Exceptions
std::exceptionOn communication errors

Definition at line 86 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(), open(), and mrpt::hwdrivers::CCANBusReader::tryToOpenComms().

Here is the caller graph for this function:

◆ open() [2/2]

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.

Exceptions
std::exceptionOn communication errors or a different serial port already open.

Definition at line 66 of file CSerialPort.cpp.

References isOpen(), m_serialName, open(), setSerialPortName(), and THROW_EXCEPTION.

Here is the call graph for this function:

◆ printf()

int CStream::printf ( const char *  fmt,
  ... 
)
virtualinherited

Writes a string to the stream in a textual form.

See also
CStdOutStream

Definition at line 30 of file CStream.cpp.

References MRPT_END, MRPT_START, and vsnprintf.

Referenced by mrpt::hmtslam::CTopLCDetector_GridMatching::computeTopologicalObservationModel(), and mrpt::io::CStream::printf_vector().

Here is the caller graph for this function:

◆ printf_vector()

template<typename CONTAINER_TYPE >
virtual int void mrpt::io::CStream::printf_vector ( const char *  fmt,
const CONTAINER_TYPE &  V,
char  separator = ',' 
)
inlineinherited

Prints a vector in the format [A,B,C,...] using CStream::printf, and the fmt string for each vector element T.

Template Parameters
CONTAINER_TYPEcan 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().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ purgeBuffers()

void CSerialPort::purgeBuffers ( )

Purge tx and rx buffers.

Exceptions
std::exceptionOn communication errors

Definition at line 892 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().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Read()

size_t CSerialPort::Read ( void Buffer,
size_t  Count 
)
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.

Exceptions
std::exceptionOn communication errors

Implements mrpt::io::CStream.

Definition at line 641 of file CSerialPort.cpp.

References close(), hCOM, isOpen(), m_interBytesTimeout_ms, m_timer, m_totalTimeout_ms, min, 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().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ReadBufferImmediate()

virtual size_t mrpt::io::CStream::ReadBufferImmediate ( void Buffer,
size_t  Count 
)
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.

Exceptions
std::exceptionOn 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().

Here is the call graph for this function:

◆ ReadString()

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.

Parameters
eol_charsA line reception is finished when one of these characters is found. Default: LF (10), CR (13).
total_timeout_msIf >0, the maximum number of milliseconds to wait.
out_timeoutIf provided, will hold true on return if a timeout ocurred, false on a valid read.
Returns
The read string, without the final
Exceptions
std::exceptionOn communication errors

This method reads from the serial port until one of the characters in eol are found.

Definition at line 732 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().

Referenced by mrpt::hwdrivers::CGyroKVHDSP3000::doProcess().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Seek() [1/2]

virtual uint64_t mrpt::io::CStream::Seek ( int64_t  Offset,
CStream::TSeekOrigin  Origin = sFromBeginning 
)
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:

  • sFromBeginning (Default) Offset is from the beginning of the resource. Seek moves to the position Offset. Offset must be >= 0.
  • sFromCurrent Offset is from the current position in the resource. Seek moves to Position + Offset.
  • sFromEnd Offset is from the end of the resource. Offset must be <= 0 to indicate a number of bytes before the end of the file.
    Returns
    Seek returns the new value of the Position property.

Implemented in mrpt::io::CPipeBaseEndPoint, mrpt::io::CFileStream, mrpt::io::CFileGZInputStream, mrpt::io::CMemoryStream, mrpt::io::CFileGZOutputStream, mrpt::io::CFileOutputStream, and mrpt::io::CFileInputStream.

◆ Seek() [2/2]

uint64_t CSerialPort::Seek ( int64_t  off,
CStream::TSeekOrigin  o = sFromBeginning 
)
override

not applicable in a serial port

Definition at line 913 of file CSerialPort.cpp.

References MRPT_END, MRPT_START, MRPT_UNUSED_PARAM, and THROW_EXCEPTION.

◆ setConfig()

void CSerialPort::setConfig ( int  baudRate,
int  parity = 0,
int  bits = 8,
int  nStopBits = 1,
bool  enableFlowControl = false 
)

Changes the configuration of the port.

Parameters
parity0:No parity, 1:Odd, 2:Even (WINDOWS ONLY: 3:Mark, 4:Space)
baudRateThe desired baud rate Accepted values: 50 - 230400
bitsBits per word (typ. 8) Accepted values: 5,6,7,8.
nStopBitsStop bits (typ. 1) Accepted values: 1,2
enableFlowControlWhether to enable the hardware flow control (RTS/CTS) (default=no)
Exceptions
std::exceptionOn communication errors

Definition at line 195 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::CGyroKVHDSP3000::initialize(), mrpt::hwdrivers::CCANBusReader::setupSerialComms(), mrpt::hwdrivers::CCANBusReader::tryToOpenComms(), and mrpt::hwdrivers::CHokuyoURG::turnOn().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setSerialPortName()

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).

See also
open, close

Definition at line 77 of file CSerialPort.cpp.

References isOpen(), m_serialName, and THROW_EXCEPTION.

Referenced by open(), mrpt::hwdrivers::CCANBusReader::tryToOpenComms(), and mrpt::hwdrivers::CSickLaserSerial::tryToOpenComms().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setTimeouts()

void CSerialPort::setTimeouts ( int  ReadIntervalTimeout,
int  ReadTotalTimeoutMultiplier,
int  ReadTotalTimeoutConstant,
int  WriteTotalTimeoutMultiplier,
int  WriteTotalTimeoutConstant 
)

Changes the timeouts of the port, in milliseconds.

Exceptions
std::exceptionOn communication errors

Definition at line 557 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().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Write()

size_t CSerialPort::Write ( const void Buffer,
size_t  Count 
)
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 827 of file CSerialPort.cpp.

References hCOM, isOpen(), MRPT_END, MRPT_START, THROW_EXCEPTION, and THROW_EXCEPTION_FMT.

Referenced by mrpt::hwdrivers::CGyroKVHDSP3000::changeMode(), mrpt::hwdrivers::CNTRIPEmitter::doProcess(), mrpt::hwdrivers::CGyroKVHDSP3000::resetIncrementalAngle(), and mrpt::hwdrivers::CCANBusReader::sendCommandToCANReader().

Here is the call graph for this function:
Here is the caller graph for this function:

Friends And Related Function Documentation

◆ PosixSignalDispatcherImpl

friend class PosixSignalDispatcherImpl
friend

Definition at line 43 of file CSerialPort.h.

Member Data Documentation

◆ hCOM

void* mrpt::comms::CSerialPort::hCOM {nullptr}
protected

Definition at line 160 of file CSerialPort.h.

Referenced by close(), isOpen(), open(), purgeBuffers(), Read(), ReadString(), setConfig(), setTimeouts(), and Write().

◆ m_baudRate

int mrpt::comms::CSerialPort::m_baudRate {0}
protected

Definition at line 155 of file CSerialPort.h.

Referenced by setConfig().

◆ m_interBytesTimeout_ms

int mrpt::comms::CSerialPort::m_interBytesTimeout_ms {0}
protected

Definition at line 156 of file CSerialPort.h.

Referenced by Read(), and setTimeouts().

◆ m_serialName

std::string mrpt::comms::CSerialPort::m_serialName
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().

◆ m_timer

mrpt::system::CTicTac mrpt::comms::CSerialPort::m_timer
protected

Definition at line 157 of file CSerialPort.h.

Referenced by Read(), and ReadString().

◆ m_totalTimeout_ms

int mrpt::comms::CSerialPort::m_totalTimeout_ms {0}
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: 8fe78517f Sun Jul 14 19:43:28 2019 +0200 at lun oct 28 02:10:00 CET 2019