Main MRPT website > C++ reference for MRPT 1.9.9
List of all members | Public Types | Public Member Functions | Protected Attributes | Friends
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:

Todo:
Add the internal buffer to the Windows implementation also

Definition at line 43 of file CSerialPort.h.

#include <mrpt/comms/CSerialPort.h>

Inheritance diagram for mrpt::comms::CSerialPort:
Inheritance graph

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 constructor: it does not open any port - later you must call "setSerialPortName" and then "open". More...
 
virtual ~CSerialPort ()
 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)
 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="\r\n")
 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 ('\r' characters are ignored). More...
 

Protected Attributes

std::string m_serialName
 The complete name of the serial port device (i.e. More...
 
int m_baudRate
 
int m_totalTimeout_ms
 
int m_interBytesTimeout_ms
 
mrpt::system::CTicTac m_timer
 
voidhCOM
 

Friends

class PosixSignalDispatcherImpl
 

Member Enumeration Documentation

◆ TSeekOrigin

Used in CStream::Seek.

Enumerator
sFromBeginning 
sFromCurrent 
sFromEnd 

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

◆ CSerialPort() [2/2]

CSerialPort::CSerialPort ( )

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

Definition at line 68 of file CSerialPort.cpp.

◆ ~CSerialPort()

CSerialPort::~CSerialPort ( )
virtual

Destructor.

Definition at line 81 of file CSerialPort.cpp.

References close(), and isOpen().

Member Function Documentation

◆ close()

void CSerialPort::close ( )

◆ getline()

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

Reads from the stream until a '
' character is found ('\r' 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 951 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 943 of file CSerialPort.cpp.

References MRPT_END, MRPT_START, and THROW_EXCEPTION.

◆ isOpen()

bool CSerialPort::isOpen ( ) const

◆ open() [1/2]

void CSerialPort::open ( )

◆ 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 86 of file CSerialPort.cpp.

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

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

◆ 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 104 of file io/CStream.h.

References mrpt::io::CStream::printf().

Referenced by mrpt::hmtslam::CTopLCDetector_GridMatching::computeTopologicalObservationModel().

◆ purgeBuffers()

void CSerialPort::purgeBuffers ( )

◆ Read()

size_t CSerialPort::Read ( void Buffer,
size_t  Count 
)
virtual

◆ 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 64 of file io/CStream.h.

References mrpt::io::CStream::Read().

◆ 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 752 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().

◆ Seek() [1/2]

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

not applicable in a serial port

Definition at line 933 of file CSerialPort.cpp.

References MRPT_END, MRPT_START, MRPT_UNUSED_PARAM, and THROW_EXCEPTION.

◆ Seek() [2/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::CFileGZInputStream, mrpt::io::CFileGZOutputStream, mrpt::io::CMemoryStream, mrpt::io::CFileOutputStream, mrpt::io::CFileInputStream, mrpt::io::CFileStream, and mrpt::io::CPipeBaseEndPoint.

◆ 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 215 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::CSickLaserSerial::LMS_setupSerialComms(), mrpt::hwdrivers::CCANBusReader::setupSerialComms(), mrpt::hwdrivers::CCANBusReader::tryToOpenComms(), mrpt::hwdrivers::CSickLaserSerial::tryToOpenComms(), and mrpt::hwdrivers::CHokuyoURG::turnOn().

◆ 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 97 of file CSerialPort.cpp.

References isOpen(), m_serialName, and THROW_EXCEPTION.

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

◆ setTimeouts()

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

◆ 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 847 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::CGPSInterface::JAVAD_sendMessage(), mrpt::hwdrivers::CGyroKVHDSP3000::resetIncrementalAngle(), mrpt::hwdrivers::CCANBusReader::sendCommandToCANReader(), and mrpt::hwdrivers::CSickLaserSerial::SendCommandToSICK().

Friends And Related Function Documentation

◆ PosixSignalDispatcherImpl

friend class PosixSignalDispatcherImpl
friend

Definition at line 45 of file CSerialPort.h.

Member Data Documentation

◆ hCOM

void* mrpt::comms::CSerialPort::hCOM
protected

Definition at line 162 of file CSerialPort.h.

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

◆ m_baudRate

int mrpt::comms::CSerialPort::m_baudRate
protected

Definition at line 157 of file CSerialPort.h.

Referenced by setConfig().

◆ m_interBytesTimeout_ms

int mrpt::comms::CSerialPort::m_interBytesTimeout_ms
protected

Definition at line 158 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 156 of file CSerialPort.h.

Referenced by open(), and setSerialPortName().

◆ m_timer

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

Definition at line 159 of file CSerialPort.h.

Referenced by Read(), and ReadString().

◆ m_totalTimeout_ms

int mrpt::comms::CSerialPort::m_totalTimeout_ms
protected

Definition at line 158 of file CSerialPort.h.

Referenced by Read(), and setTimeouts().




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