The low-level serial communication class.
Definition at line 27 of file serialinterface.h.
#include <xSens_MT4/xcommunication/include/xsens/serialinterface.h>
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 | |
XsFileHandle * | rx_log |
XsFileHandle * | tx_log |
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.
|
virtual |
Destructor, de-initializes, frees memory allocated for buffers, etc.
Definition at line 80 of file serialinterface.cpp.
References closeLive().
|
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.
bdata | The byte data to append to the file |
Reimplemented in IoInterfaceFile.
Definition at line 77 of file iointerface.cpp.
References void(), and XRV_INVALIDOPERATION.
Cancel any pending io requests.
Reimplemented from IoInterface.
Definition at line 755 of file serialinterface.cpp.
|
virtual |
Close the serial communication port.
Implements IoInterface.
Definition at line 92 of file serialinterface.cpp.
References closeLive().
Referenced by closeLive().
|
virtualinherited |
Close the file and delete it.
Reimplemented in IoInterfaceFile.
Definition at line 83 of file iointerface.cpp.
References XRV_INVALIDOPERATION.
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().
|
virtualinherited |
Create an empty file.
filename | The desired (path+)name of the file |
Reimplemented in IoInterfaceFile.
Definition at line 85 of file iointerface.cpp.
References void(), and XRV_INVALIDOPERATION.
|
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.
start | The offset of the first byte to delete |
length | The total number of bytes to delete |
Reimplemented in IoInterfaceFile.
Definition at line 91 of file iointerface.cpp.
References void(), and XRV_INVALIDOPERATION.
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.
mask | Indicates which lines are to be manipulated and which should be left alone. |
state | Contains the new state of the control lines. |
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.
|
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.
needleV | The byte string to find. |
pos | The position where needleV was found. This will point to the first character of the found needleV. |
Reimplemented in IoInterfaceFile.
Definition at line 98 of file iointerface.cpp.
References void(), and XRV_INVALIDOPERATION.
|
virtual |
Flush all data in the buffers to and from the device.
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().
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.
Return the size of the file.
Reimplemented in IoInterfaceFile.
Definition at line 105 of file iointerface.cpp.
XsIoHandle SerialInterface::getHandle | ( | void | ) | const |
Return the handle of the port.
Definition at line 243 of file serialinterface.cpp.
References m_handle.
|
virtual |
Return the error code of the last operation.
Implements IoInterface.
Definition at line 252 of file serialinterface.cpp.
References m_lastResult.
|
virtualinherited |
Retrieve the filename that was last successfully opened.
filename | The XsString which will contain the filename. |
Reimplemented in IoInterfaceFile.
Definition at line 107 of file iointerface.cpp.
References void(), and XRV_INVALIDOPERATION.
Retrieve the port name that was last successfully opened.
Definition at line 247 of file serialinterface.cpp.
References m_portname.
Retrieve the port number that was last successfully opened.
Definition at line 245 of file serialinterface.cpp.
References m_port.
Return the current read position.
Reimplemented in IoInterfaceFile.
Definition at line 113 of file iointerface.cpp.
Return the current timeout value.
Implements StreamInterface.
Definition at line 257 of file serialinterface.cpp.
References m_timeout.
Return the current write position.
Reimplemented in IoInterfaceFile.
Definition at line 115 of file iointerface.cpp.
|
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.
start | The offset in the file to write the first byte |
data | The data to insert in the file |
Reimplemented in IoInterfaceFile.
Definition at line 117 of file iointerface.cpp.
References void(), and XRV_INVALIDOPERATION.
|
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().
|
virtualinherited |
Return whether the file is readonly or not.
Reimplemented in IoInterfaceFile.
Definition at line 124 of file iointerface.cpp.
|
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.
portInfo | The details of the port that should be opened. Depending on the type of interface, parts of this parameter may be ignored. |
readBufSize | The size of the read buffer in bytes (if appliccable to the device) |
writeBufSize | The size of the write buffer in bytes (if appliccable to the device) |
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.
|
virtualinherited |
Open a file.
filename | The name of the file to open |
createNew | When true, the file will be created if it doesn't exist yet |
readOnly | When true, the file will be marked as read only for IoInterfaceFile, preventing accidental writes to the file. |
Reimplemented in IoInterfaceFile.
Definition at line 126 of file iointerface.cpp.
References void(), and XRV_INVALIDOPERATION.
|
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.
maxLength | The maximum amount of data read. |
data | The buffer that will store the received data. |
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().
|
virtualinherited |
Set the new absolute read position.
The read position is checked against the filesize first.
pos | The new read position |
Reimplemented in IoInterfaceFile.
Definition at line 135 of file iointerface.cpp.
References void(), and XRV_INVALIDOPERATION.
|
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.
ms | The new timeout in milliseconds |
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().
|
virtualinherited |
Set the new absolute write position.
The write position is checked against the filesize first.
pos | The new write position |
Reimplemented in IoInterfaceFile.
Definition at line 141 of file iointerface.cpp.
References void(), and XRV_INVALIDOPERATION.
|
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.
maxLength | The maximum number of bytes to read before returning |
data | The buffer to put the read data in. |
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().
|
virtual |
Write the data contained in data to the device.
data | The data to write to the device. |
written | An optional XsSize value that will receive the number of bytes that were actually written. |
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.
|
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().
|
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().
|
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().
|
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().
|
protected |
The opened COM port nr.
Definition at line 44 of file serialinterface.h.
Referenced by getPortNumber(), open(), readData(), SerialInterface(), and writeData().
|
protected |
The name of the open serial port.
Definition at line 46 of file serialinterface.h.
Referenced by getPortName(), open(), readData(), SerialInterface(), and writeData().
|
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().
|
private |
Definition at line 32 of file serialinterface.h.
Referenced by closeLive(), readData(), and SerialInterface().
|
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 |