Main MRPT website > C++ reference for MRPT 1.9.9
List of all members | Public Member Functions | Protected Attributes | Private Attributes
SerialInterface Class Reference

Detailed Description

The low-level serial communication class.

Definition at line 27 of file serialinterface.h.

#include <xSens_MT4/xcommunication/include/xsens/serialinterface.h>

Inheritance diagram for SerialInterface:
Inheritance graph

Public Member Functions

 SerialInterface ()
 Default constructor, initializes all members to their default values. More...
 
virtual ~SerialInterface ()
 Destructor, de-initializes, frees memory allocated for buffers, etc. More...
 
XsResultValue close (void)
 Close the serial communication port. More...
 
XsResultValue closeLive (void)
 Close the serial communication port. More...
 
XsResultValue flushData (void)
 Flush all data in the buffers to and from the device. More...
 
bool isOpen (void) const
 Return whether the communication port is open or not. More...
 
XsResultValue getLastResult (void) const
 Return the error code of the last operation. More...
 
XsResultValue writeData (const XsByteArray &data, XsSize *written=0)
 Write the data contained in data to the device. More...
 
XsResultValue readData (XsSize maxLength, XsByteArray &data)
 Read data from the serial port and put it into the data buffer. More...
 
void cancelIo (void) const
 Cancel any pending io requests. More...
 
XsResultValue escape (XsControlLine mask, XsControlLine state)
 Manipulate the Serial control lines. More...
 
XsBaudRate getBaudrate (void) const
 Return the baudrate that is currently being used by the port. More...
 
XsIoHandle getHandle (void) const
 Return the handle of the port. More...
 
uint16_t getPortNumber (void) const
 Retrieve the port number that was last successfully opened. More...
 
void getPortName (XsString &portname) const
 Retrieve the port name that was last successfully opened. More...
 
uint32_t getTimeout (void) const
 Return the current timeout value. More...
 
XsResultValue open (const XsPortInfo &portInfo, uint32_t readBufSize=XS_DEFAULT_READ_BUFFER_SIZE, uint32_t writeBufSize=XS_DEFAULT_WRITE_BUFFER_SIZE)
 Open a communication channel to the given port info. More...
 
XsResultValue setTimeout (uint32_t ms)
 Set the default timeout value to use in blocking operations. More...
 
XsResultValue waitForData (XsSize maxLength, XsByteArray &data)
 Wait for data to arrive or a timeout to occur. More...
 
virtual XsResultValue open (const XsString &filename, bool createNew, bool readOnly)
 Open a file.
More...
 
virtual XsResultValue appendData (const XsByteArray &bdata)
 Write data to the end of the file.
More...
 
virtual XsResultValue closeAndDelete (void)
 Close the file and delete it.
More...
 
virtual XsResultValue create (const XsString &filename)
 Create an empty file.
More...
 
virtual XsResultValue deleteData (XsFilePos start, XsSize length)
 Delete the given data from the file.
More...
 
virtual XsResultValue find (const XsByteArray &needleV, XsFilePos &pos)
 Find a string of bytes in the file.
More...
 
virtual XsFilePos getFileSize (void) const
 Return the size of the file.
More...
 
virtual XsResultValue getName (XsString &filename) const
 Retrieve the filename that was last successfully opened.
More...
 
virtual XsFilePos getReadPosition (void) const
 Return the current read position.
More...
 
virtual XsFilePos getWritePosition (void) const
 Return the current write position.
More...
 
virtual XsResultValue insertData (XsFilePos start, const XsByteArray &data)
 Insert the given data into the file.
More...
 
virtual bool isReadOnly (void) const
 Return whether the file is readonly or not.
More...
 
virtual XsResultValue setReadPosition (XsFilePos pos)
 Set the new absolute read position.
More...
 
virtual XsResultValue setWritePosition (XsFilePos pos=-1)
 Set the new absolute write position.
More...
 

Protected Attributes

XsBaudRate m_baudrate
 The baudrate that was last set to be used by the port. More...
 
uint32_t m_endTime
 The time at which an operation will end in ms, used by several functions. More...
 
XsResultValue m_lastResult
 The last result of an operation. More...
 
uint16_t m_port
 The opened COM port nr. More...
 
char m_portname [32]
 The name of the open serial port. More...
 
uint32_t m_timeout
 
XsIoHandle m_handle
 The serial port handle, also indicates if the port is open or not. More...
 

Private Attributes

XsFileHandlerx_log
 
XsFileHandletx_log
 

Constructor & Destructor Documentation

◆ SerialInterface()

SerialInterface::SerialInterface ( )

Default constructor, initializes all members to their default values.

Definition at line 60 of file serialinterface.cpp.

References m_baudrate, m_endTime, m_handle, m_lastResult, m_port, m_portname, m_timeout, rx_log, tx_log, XBR_Invalid, and XRV_OK.

◆ ~SerialInterface()

SerialInterface::~SerialInterface ( )
virtual

Destructor, de-initializes, frees memory allocated for buffers, etc.

Definition at line 80 of file serialinterface.cpp.

References closeLive().

Here is the call graph for this function:

Member Function Documentation

◆ appendData()

XsResultValue IoInterface::appendData ( const XsByteArray bdata)
virtualinherited

Write data to the end of the file.

The function writes the given data to the file at the end. The current write position is also moved to the end of the file.

Parameters
bdataThe byte data to append to the file
Returns
XRV_OK if the write was successful

Reimplemented in IoInterfaceFile.

Definition at line 77 of file iointerface.cpp.

References void(), and XRV_INVALIDOPERATION.

Here is the call graph for this function:

◆ cancelIo()

void SerialInterface::cancelIo ( void  ) const
virtual

Cancel any pending io requests.

Reimplemented from IoInterface.

Definition at line 755 of file serialinterface.cpp.

◆ close()

XsResultValue SerialInterface::close ( void  )
virtual

Close the serial communication port.

Implements IoInterface.

Definition at line 92 of file serialinterface.cpp.

References closeLive().

Referenced by closeLive().

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

◆ closeAndDelete()

XsResultValue IoInterface::closeAndDelete ( void  )
virtualinherited

Close the file and delete it.

Returns
XRV_OK if the file was closed and deleted successfully

Reimplemented in IoInterfaceFile.

Definition at line 83 of file iointerface.cpp.

References XRV_INVALIDOPERATION.

◆ closeLive()

XsResultValue SerialInterface::closeLive ( void  )

Close the serial communication port.

Definition at line 94 of file serialinterface.cpp.

References close(), mrpt::system::os::fclose(), flushData(), isOpen(), m_endTime, m_handle, m_lastResult, rx_log, tx_log, XRV_ERROR, XRV_NOPORTOPEN, and XRV_OK.

Referenced by close(), open(), and ~SerialInterface().

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

◆ create()

XsResultValue IoInterface::create ( const XsString filename)
virtualinherited

Create an empty file.

Parameters
filenameThe desired (path+)name of the file
Returns
XRV_OK if the file was created successfully

Reimplemented in IoInterfaceFile.

Definition at line 85 of file iointerface.cpp.

References void(), and XRV_INVALIDOPERATION.

Here is the call graph for this function:

◆ deleteData()

XsResultValue IoInterface::deleteData ( XsFilePos  start,
XsSize  length 
)
virtualinherited

Delete the given data from the file.

The function erases the given data from the file at the given write position. This operation may take a while to complete, but is faster than insertData.

The write position is not changed and the read position is checked for validity upon function exit.

Parameters
startThe offset of the first byte to delete
lengthThe total number of bytes to delete
Returns
XRV_OK if the data was deleted successfully

Reimplemented in IoInterfaceFile.

Definition at line 91 of file iointerface.cpp.

References void(), and XRV_INVALIDOPERATION.

Here is the call graph for this function:

◆ escape()

XsResultValue SerialInterface::escape ( XsControlLine  mask,
XsControlLine  state 
)

Manipulate the Serial control lines.

The function manipulates the serial control lines that are indicated by the mask parameter. Note that only the DTR and RTS lines can be set by win32.

Parameters
maskIndicates which lines are to be manipulated and which should be left alone.
stateContains the new state of the control lines.
Returns
XRV_OK if the function succeeded

Definition at line 156 of file serialinterface.cpp.

References isOpen(), m_handle, m_lastResult, status, XCL_DTR, XCL_RTS, XRV_ERROR, XRV_NOPORTOPEN, and XRV_OK.

Here is the call graph for this function:

◆ find()

XsResultValue IoInterface::find ( const XsByteArray needleV,
XsFilePos pos 
)
virtualinherited

Find a string of bytes in the file.

The function searches from the current read position until the given needle is found. If the needle is not found, XsResultValue::NOT_FOUND is returned. The function will update the seek position to the first character of the found needle.

Parameters
needleVThe byte string to find.
posThe position where needleV was found. This will point to the first character of the found needleV.
Returns
XRV_OK if the data was found, XRV_ENDOFFILE if it wasn't found

Reimplemented in IoInterfaceFile.

Definition at line 98 of file iointerface.cpp.

References void(), and XRV_INVALIDOPERATION.

Here is the call graph for this function:

◆ flushData()

XsResultValue SerialInterface::flushData ( void  )
virtual

Flush all data in the buffers to and from the device.

Returns
XRV_OK if the data was flushed successfully
Note
This function tries to send and receive any remaining data immediately and does not return until the buffers are empty.

Implements IoInterface.

Definition at line 222 of file serialinterface.cpp.

References m_endTime, m_handle, m_lastResult, XRV_ERROR, and XRV_OK.

Referenced by closeLive().

Here is the caller graph for this function:

◆ getBaudrate()

XsBaudRate SerialInterface::getBaudrate ( void  ) const

Return the baudrate that is currently being used by the port.

Definition at line 237 of file serialinterface.cpp.

References isOpen(), m_baudrate, and XBR_Invalid.

Here is the call graph for this function:

◆ getFileSize()

XsFilePos IoInterface::getFileSize ( void  ) const
virtualinherited

Return the size of the file.

Returns
The size of the file.

Reimplemented in IoInterfaceFile.

Definition at line 105 of file iointerface.cpp.

◆ getHandle()

XsIoHandle SerialInterface::getHandle ( void  ) const

Return the handle of the port.

Definition at line 243 of file serialinterface.cpp.

References m_handle.

◆ getLastResult()

XsResultValue SerialInterface::getLastResult ( void  ) const
virtual

Return the error code of the last operation.

Implements IoInterface.

Definition at line 252 of file serialinterface.cpp.

References m_lastResult.

◆ getName()

XsResultValue IoInterface::getName ( XsString filename) const
virtualinherited

Retrieve the filename that was last successfully opened.

Parameters
filenameThe XsString which will contain the filename.
Returns
XRV_OK

Reimplemented in IoInterfaceFile.

Definition at line 107 of file iointerface.cpp.

References void(), and XRV_INVALIDOPERATION.

Here is the call graph for this function:

◆ getPortName()

void SerialInterface::getPortName ( XsString portname) const

Retrieve the port name that was last successfully opened.

Definition at line 247 of file serialinterface.cpp.

References m_portname.

◆ getPortNumber()

uint16_t SerialInterface::getPortNumber ( void  ) const

Retrieve the port number that was last successfully opened.

Definition at line 245 of file serialinterface.cpp.

References m_port.

◆ getReadPosition()

XsFilePos IoInterface::getReadPosition ( void  ) const
virtualinherited

Return the current read position.

Returns
The current read position.

Reimplemented in IoInterfaceFile.

Definition at line 113 of file iointerface.cpp.

◆ getTimeout()

uint32_t SerialInterface::getTimeout ( void  ) const
virtual

Return the current timeout value.

Implements StreamInterface.

Definition at line 257 of file serialinterface.cpp.

References m_timeout.

◆ getWritePosition()

XsFilePos IoInterface::getWritePosition ( void  ) const
virtualinherited

Return the current write position.

Returns
The current write position.

Reimplemented in IoInterfaceFile.

Definition at line 115 of file iointerface.cpp.

◆ insertData()

XsResultValue IoInterface::insertData ( XsFilePos  start,
const XsByteArray data 
)
virtualinherited

Insert the given data into the file.

The function writes the given data to the file at the current write position. This operation may take a while to complete.

The write position is placed at the end of the inserted data.

Parameters
startThe offset in the file to write the first byte
dataThe data to insert in the file
Returns
XRV_OK if the data was inserted successfully

Reimplemented in IoInterfaceFile.

Definition at line 117 of file iointerface.cpp.

References void(), and XRV_INVALIDOPERATION.

Here is the call graph for this function:

◆ isOpen()

bool SerialInterface::isOpen ( void  ) const
virtual

Return whether the communication port is open or not.

Implements IoInterface.

Definition at line 259 of file serialinterface.cpp.

References m_handle.

Referenced by closeLive(), escape(), getBaudrate(), open(), readData(), setTimeout(), and writeData().

Here is the caller graph for this function:

◆ isReadOnly()

bool IoInterface::isReadOnly ( void  ) const
virtualinherited

Return whether the file is readonly or not.

Returns
true if the file is readonly

Reimplemented in IoInterfaceFile.

Definition at line 124 of file iointerface.cpp.

◆ open() [1/2]

XsResultValue SerialInterface::open ( const XsPortInfo portInfo,
uint32_t  readBufSize = XS_DEFAULT_READ_BUFFER_SIZE,
uint32_t  writeBufSize = XS_DEFAULT_WRITE_BUFFER_SIZE 
)
virtual

Open a communication channel to the given port info.

If the baudrate in portInfo is set to XBR_Invalid, the baud rate is automatically detected if possible.

Parameters
portInfoThe details of the port that should be opened. Depending on the type of interface, parts of this parameter may be ignored.
readBufSizeThe size of the read buffer in bytes (if appliccable to the device)
writeBufSizeThe size of the write buffer in bytes (if appliccable to the device)
Returns
XRV_OK if the device was opened successfully

Stored settings about the serial port

Reimplemented from IoInterface.

Definition at line 281 of file serialinterface.cpp.

References closeLive(), FALSE, isOpen(), JLALERT, JLDEBUG, m_baudrate, m_endTime, m_handle, m_lastResult, m_port, m_portname, m_timeout, setTimeout(), mrpt::system::os::sprintf(), void(), XRV_ALREADYOPEN, XRV_ERROR, XRV_INPUTCANNOTBEOPENED, and XRV_OK.

Here is the call graph for this function:

◆ open() [2/2]

XsResultValue IoInterface::open ( const XsString filename,
bool  createNew,
bool  readOnly 
)
virtualinherited

Open a file.

Parameters
filenameThe name of the file to open
createNewWhen true, the file will be created if it doesn't exist yet
readOnlyWhen true, the file will be marked as read only for IoInterfaceFile, preventing accidental writes to the file.
Returns
XRV_OK if the file was opened successfully
See also
createFile

Reimplemented in IoInterfaceFile.

Definition at line 126 of file iointerface.cpp.

References void(), and XRV_INVALIDOPERATION.

Here is the call graph for this function:

◆ readData()

XsResultValue SerialInterface::readData ( XsSize  maxLength,
XsByteArray data 
)
virtual

Read data from the serial port and put it into the data buffer.

This function reads up to maxLength bytes from the port (non-blocking) and puts it into the data buffer.

Parameters
maxLengthThe maximum amount of data read.
dataThe buffer that will store the received data.
Returns
XRV_OK if no error occurred. It can be that no data is available and XRV_OK will be returned. Check data.size() for the number of bytes that were read.

Implements IoInterface.

Definition at line 517 of file serialinterface.cpp.

References mrpt::system::os::fopen(), isOpen(), JLALERT, JLTRACE, m_baudrate, m_handle, m_lastResult, m_port, m_portname, m_timeout, makeFilenameUnique(), rx_log, mrpt::system::os::sprintf(), XRV_ERROR, XRV_NOFILEORPORTOPEN, XRV_NOPORTOPEN, XRV_OK, XRV_TIMEOUT, and XS_MAX_FILENAME_LENGTH.

Referenced by waitForData().

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

◆ setReadPosition()

XsResultValue IoInterface::setReadPosition ( XsFilePos  pos)
virtualinherited

Set the new absolute read position.

The read position is checked against the filesize first.

Parameters
posThe new read position
Returns
XRV_OK if the read position was updated successfully

Reimplemented in IoInterfaceFile.

Definition at line 135 of file iointerface.cpp.

References void(), and XRV_INVALIDOPERATION.

Here is the call graph for this function:

◆ setTimeout()

XsResultValue SerialInterface::setTimeout ( uint32_t  ms)
virtual

Set the default timeout value to use in blocking operations.

This function sets the value of m_timeout. There is no infinity value. The value 0 means that all blocking operations now become polling (non-blocking) operations. If the value is set to or from 0, the low-level serial port settings may be changed in addition to the m_timeout value.

Parameters
msThe new timeout in milliseconds
Returns
XRV_OK if the function succeeded

Implements StreamInterface.

Definition at line 610 of file serialinterface.cpp.

References isOpen(), JLDEBUG, m_handle, m_lastResult, m_timeout, XRV_ERROR, and XRV_OK.

Referenced by open().

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

◆ setWritePosition()

XsResultValue IoInterface::setWritePosition ( XsFilePos  pos = -1)
virtualinherited

Set the new absolute write position.

The write position is checked against the filesize first.

Parameters
posThe new write position
Returns
XRV_OK if the write position was updated successfully

Reimplemented in IoInterfaceFile.

Definition at line 141 of file iointerface.cpp.

References void(), and XRV_INVALIDOPERATION.

Here is the call graph for this function:

◆ waitForData()

XsResultValue SerialInterface::waitForData ( XsSize  maxLength,
XsByteArray data 
)
virtual

Wait for data to arrive or a timeout to occur.

The function waits until maxLength data is available or until a timeout occurs. The function returns success if data is available or XsResultValue::TIMEOUT if a timeout occurred. A timeout value of 0 indicates that the default timeout stored in the class should be used.

Parameters
maxLengthThe maximum number of bytes to read before returning
dataThe buffer to put the read data in.
Returns
XRV_OK if maxLength bytes were read, XRV_TIMEOUT if less was read, XRV_TIMEOUTNODATA if nothing was read

Reimplemented from IoInterface.

Definition at line 669 of file serialinterface.cpp.

References JLTRACE, m_lastResult, m_timeout, readData(), XRV_OK, XRV_TIMEOUT, and XsTime_getTimeOfDay().

Here is the call graph for this function:

◆ writeData()

XsResultValue SerialInterface::writeData ( const XsByteArray data,
XsSize written = 0 
)
virtual

Write the data contained in data to the device.

Parameters
dataThe data to write to the device.
writtenAn optional XsSize value that will receive the number of bytes that were actually written.
Returns
XRV_OK if the data was written successfully
Note
The default timeout is respected in this operation.

Implements IoInterface.

Definition at line 702 of file serialinterface.cpp.

References mrpt::system::os::fopen(), isOpen(), m_baudrate, m_handle, m_lastResult, m_port, m_portname, makeFilenameUnique(), mrpt::system::os::sprintf(), tx_log, XRV_ERROR, XRV_NOPORTOPEN, XRV_OK, and XS_MAX_FILENAME_LENGTH.

Here is the call graph for this function:

Member Data Documentation

◆ m_baudrate

XsBaudRate SerialInterface::m_baudrate
protected

The baudrate that was last set to be used by the port.

Definition at line 37 of file serialinterface.h.

Referenced by getBaudrate(), open(), readData(), SerialInterface(), and writeData().

◆ m_endTime

uint32_t SerialInterface::m_endTime
protected

The time at which an operation will end in ms, used by several functions.

Definition at line 40 of file serialinterface.h.

Referenced by closeLive(), flushData(), open(), and SerialInterface().

◆ m_handle

XsIoHandle SerialInterface::m_handle
protected

The serial port handle, also indicates if the port is open or not.

Definition at line 54 of file serialinterface.h.

Referenced by closeLive(), escape(), flushData(), getHandle(), isOpen(), open(), readData(), SerialInterface(), setTimeout(), and writeData().

◆ m_lastResult

XsResultValue SerialInterface::m_lastResult
mutableprotected

The last result of an operation.

Definition at line 42 of file serialinterface.h.

Referenced by closeLive(), escape(), flushData(), getLastResult(), open(), readData(), SerialInterface(), setTimeout(), waitForData(), and writeData().

◆ m_port

uint16_t SerialInterface::m_port
protected

The opened COM port nr.

Definition at line 44 of file serialinterface.h.

Referenced by getPortNumber(), open(), readData(), SerialInterface(), and writeData().

◆ m_portname

char SerialInterface::m_portname[32]
protected

The name of the open serial port.

Definition at line 46 of file serialinterface.h.

Referenced by getPortName(), open(), readData(), SerialInterface(), and writeData().

◆ m_timeout

uint32_t SerialInterface::m_timeout
protected

The default timeout value to use during blocking operations. A value of 0 means that all operations become non-blocking.

Definition at line 50 of file serialinterface.h.

Referenced by getTimeout(), open(), readData(), SerialInterface(), setTimeout(), and waitForData().

◆ rx_log

XsFileHandle* SerialInterface::rx_log
private

Definition at line 32 of file serialinterface.h.

Referenced by closeLive(), readData(), and SerialInterface().

◆ tx_log

XsFileHandle* SerialInterface::tx_log
private

Definition at line 33 of file serialinterface.h.

Referenced by closeLive(), SerialInterface(), and writeData().




Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019