Main MRPT website > C++ reference for MRPT 1.5.7
cmt1.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +---------------------------------------------------------------------------+ */
9 #ifndef _CMT1_H_2006_04_12
10 #define _CMT1_H_2006_04_12
11 
12 #ifndef _CMTDEF_H_2006_05_01
13 # include "cmtdef.h"
14 #endif
15 
16 #ifdef _WIN32
17 # include <windows.h>
18 //# include <sys/types.h>
19 #else
20 # include <termios.h>
21 // these are not required by level 1, but to keep the higher levels platform-independent they are put here
22 # include <stdlib.h>
23 # include <string.h>
24 # include <stddef.h>
25 #define _strnicmp strncasecmp
26 #endif
27 
28 #include <stdio.h>
29 
30 //! The namespace of all Xsens software since 2006.
31 namespace xsens {
32 
33 #ifndef CMTLOG
34 # if defined(_DEBUG) || defined(_LOG_ALWAYS)
35  void CMTLOG(const char *str, ...);
36 # define CMTEXITLOG(str) JanitorFunc2<const char*,XsensResultValue> _cmtExitLog(CMTLOG,str " returns %u",m_lastResult);
37 # else
38 # define CMTLOG(...)
39 # define CMTEXITLOG(...)
40 # endif
41 #endif
42 
43 #ifndef _WIN32
44 int _wcsnicmp(const wchar_t* s1, const wchar_t* s2,int count);
45 #endif
46 
47 //////////////////////////////////////////////////////////////////////////////////////////
48 ///////////////////////////////////////// Cmt1s /////////////////////////////////////////
49 //////////////////////////////////////////////////////////////////////////////////////////
50 
51 /*! \brief The low-level serial communication class.
52 */
53 class Cmt1s {
54 private:
55  //! This object cannot be copied, so this function is not implemented.
56  Cmt1s(const Cmt1s& ref);
57 
58  #ifdef _LOG_RX_TX
59  FILE* rx_log;
60  FILE* tx_log;
61  #endif
62 
63  /*! \brief The bytes received function.
64 
65  This function is automatically called every time binary data is read from the
66  connected COM port.
67  */
69  //! Custom, user supplied parameter for the OnBytesReceived callback function, passed as the first argument
71  //! Custom, user supplied parameter for the OnBytesReceived callback function, passed as the last argument
73 protected:
74  //! The baudrate that was last set to be used by the port
76  //! The time at which an operation will end in ms, used by several functions.
78  //! Indicates if the port is open or not
79  bool m_isOpen;
80  //! The last result of an operation
82  //! The opened COM port nr
84  char m_portname[257];
85  /*! The default timeout value to use during blocking operations.
86  A value of 0 means that all operations become non-blocking.
87  */
89 
90  #ifdef _WIN32
91  DCB m_commState; //!< Stored settings about the serial port
92  HANDLE m_handle; //!< The serial port handle
93  #else
94  termios m_commState; //!< Stored settings about the serial port
95  int32_t m_handle; //!< The serial port handle
96  typedef int32_t HANDLE;
97  #endif
98 public:
99  //! Default constructor, initializes all members to their default values.
100  Cmt1s();
101  //! Destructor, de-initializes, frees memory allocated for buffers, etc.
102  ~Cmt1s();
103  //! \brief Close the serial communication port.
104  XsensResultValue close (void);
105  /*! \brief Manipulate the Serial control lines
106 
107  The function manipulates the serial control lines that are indicated by the
108  mask parameter. Note that only the DTR and RTS lines can be set by win32.
109  \param mask Indicates which lines are to be manipulated and which should be
110  left alone.
111  \param state Contains the new state of the control lines.
112  */
114  /*! \brief Flush all data to be transmitted / received.
115 
116  This function tries to send and receive any remaining data immediately
117  and does not return until the buffers are empty.
118  */
120  //! Return the baudrate that is currently being used by the port
121  uint32_t getBaudrate(void) const { return m_baudrate; }
122  //! Return the handle of the port
123  HANDLE getHandle(void) const { return m_handle; }
124  //! Retrieve the port number that was last successfully opened.
125  uint8_t getPortNr (void) const { return m_port; }
126  //! Retrieve the port name that was last successfully opened.
127  void getPortName(char *portname) const { sprintf(portname, "%s", m_portname); }
128  //! Return the error code of the last operation.
130  //! Return the current timeout value
131  uint32_t getTimeout (void) const { return m_timeout; }
132  //! Return whether the communication port is open or not.
133  bool isOpen (void) const { return m_isOpen; }
134 
135  /*! \brief Open a communcation channel to the given serial port name.
136 
137  The port is automatically initialized to the given baudrate.
138  If the baudrate is set to 0, the baud rate is automatically detected. If possible.
139  */
140  XsensResultValue open ( const char *portName,
141  const uint32_t baudRate = CMT_DEFAULT_BAUD_RATE,
143  uint32_t writeBufSize = CMT_DEFAULT_WRITE_BUFFER_SIZE);
144 
145 #ifdef _WIN32
146  /*! \brief Open a communication channel to the given COM port number.
147 
148  The port is automatically initialized to the given baud rate.
149  If the baudrate is set to 0, the baud rate is automatically detected. If possible.
150  */
151  XsensResultValue open ( const uint32_t portNumber,
152  const uint32_t baudRate = CMT_DEFAULT_BAUD_RATE,
154  uint32_t writeBufSize = CMT_DEFAULT_WRITE_BUFFER_SIZE);
155 #endif
156  /*! \brief Read data from the serial port and put it into the data buffer.
157 
158  This function reads as much data as possible from the com port (non-blocking) and
159  put as much data as will fit into the data buffer. Any excess data is stored in
160  the \c m_readBuffer member variable. If there was enough data in m_readBuffer to
161  fulfill the request, the data parameter is first filled and the port is polled
162  afterwards.
163  \param maxLength The maximum amount of data read.
164  \param data Pointer to a buffer that will store the received data.
165  \param length The number of bytes placed into \c data.
166  */
168  uint32_t* length = NULL);
169  //! Set the callback function for when bytes have been received
171  /*! \brief Set the default timeout value to use in blocking operations.
172 
173  This function sets the value of m_timeout. There is no infinity value. The value 0
174  means that all blocking operations now become polling (non-blocking) operations.
175  If the value is set to or from 0, the low-level serial port settings may be
176  changed in addition to the m_timeout value.
177  */
179  /*! \brief Wait for data to arrive or a timeout to occur.
180 
181  The function waits until \c maxLength data is available or until a timeout occurs.
182  The function returns success if data is available or XsensResultValue::TIMEOUT if a
183  timeout occurred. A timeout value of 0 indicates that the default timeout stored
184  in the class should be used.
185  */
187  uint32_t* length = NULL);
188  /*! \brief Write the data to the serial port.
189 
190  The function writes the given data to the connected COM port.
191  The default timeout is respected in this operation.
192  */
194  uint32_t* written);
195 
196 };
197 
198 //////////////////////////////////////////////////////////////////////////////////////////
199 ///////////////////////////////////////// Cmt1f /////////////////////////////////////////
200 //////////////////////////////////////////////////////////////////////////////////////////
201 
202 /*! \brief The low-level file communication class.
203 */
204 class Cmt1f {
205 private:
206  //! This object cannot be copied, so this function is not implemented.
207  Cmt1f(const Cmt1f& ref);
208 
209 protected:
210  //! The file handle
211  FILE* m_handle;
212  //! Contains the size of the file
214  //! The last read position in the file
216  //! The last write position in the file
218  //! The last result of an operation
220  //! Contains the name of the file that was last successfully opened.
222  //! Contains the name of the file that was last successfully opened using unicode.
224  //! Indicates if the file is open or not.
225  bool m_isOpen;
226  //! Indicates if we're using the unicode filename or the regular filename
227  bool m_unicode;
228  /*! \brief Indicates whether the last operation was a read or write operation.
229 
230  This value is used to check whether or not a seek is required to perform a
231  requested read or write operation.
232  */
233  bool m_reading;
234  //! Indicates if the file was opened in read-only mode
236  //! Change from writing to reading mode
237  void gotoRead(void);
238  //! Change from reading to writing mode
239  void gotoWrite(void);
240 public:
241  //! Default constructor, initializes all members to their default values.
242  Cmt1f();
243  //! Destructor.
244  ~Cmt1f();
245  /*! \brief Write data to the end of the file.
246 
247  The function writes the given data to the file at the end. The current write
248  position is also moved to the end of the file.
249  */
250  XsensResultValue appendData(const uint32_t length, const void* data);
251  //! Close the file.
252  XsensResultValue close(void);
253  //! Close the file and delete it.
255  //! Open an empty file.
256  XsensResultValue create(const char* filename);
257  //! Open an empty file using a unicode path + filename.
258  XsensResultValue create(const wchar_t* filename);
259  /*! \brief Delete the given data from the file.
260 
261  The function erases the given data from the file at the given write position. This
262  operation may take a while to complete, but is faster than insertData.
263 
264  The write position is not changed and the read position is checked for validity.
265  */
267  /*! \brief Find a string of bytes in the file
268 
269  The function searches from the current read position until the given \c needle is
270  found. If the needle is not found, XsensResultValue::NOT_FOUND is returned. The function
271  will update the seek position to the first character of the found needle.
272  \param needle The byte string to find.
273  \param needleLength The length of the byte string.
274  \param pos Out: The position where the needle was found. This will point
275  to the first character of the found needle.
276  */
277  XsensResultValue find(const void* needle, const uint32_t needleLength, CmtFilePos& pos);
278  /*! \brief Flush all data to be written.
279  This function writes any remaining data immediately and does not return
280  until this is done.
281  */
283  //! Return the size of the file.
284  CmtFilePos getFileSize(void) const { return m_fileSize; }
285  //! Return the result code of the last operation.
287  /*! \brief Retrieve the filename that was last successfully opened.
288 
289  \param filename A buffer for storing the filename. The buffer should be able
290  to hold the filename. A safe size is to make it at least 256 bytes.
291  */
292  XsensResultValue getName(char* filename) const;
293  /*! \brief Retrieve the filename that was last successfully opened in unicode.
294 
295  \param filename A buffer for storing the filename. The buffer should be able
296  to hold the filename. A safe size is to make it at least 256 wide characters.
297  */
298  XsensResultValue getName(wchar_t* filename) const;
299  //! Return the current read position.
300  CmtFilePos getReadPos(void) const { return m_readPos; }
301  //! Return the current write position.
302  CmtFilePos getWritePos(void) const { return m_writePos; }
303  /*! \brief Insert the given data into the file.
304 
305  The function writes the given data to the file at the current write position. This
306  operation may take a while to complete.
307 
308  The write position is placed at the end of the inserted data.
309  */
311  const void* data);
312  //! Return whether the file is open or not.
313  bool isOpen(void) const { return m_isOpen; }
314  //! Return whether the file is readonly or not.
315  bool isReadOnly(void) const { return !m_isOpen || m_readOnly; }
316  //! Open a file.
317  XsensResultValue open(const char* filename, const bool create, const bool readOnly);
318  //! Open a file using a unicode filename.
319  XsensResultValue open(const wchar_t* filename, const bool create, const bool readOnly);
320  /*! \brief Read data from the file and put it into the data buffer.
321 
322  This function reads exactly the number of bytes as requested from the file.
323  \param maxLength The amount of data that will be read.
324  \param data Pointer to a buffer that will store the read data.
325  \param length pointer to a variable that will store the number of bytes
326  that were actually read. The parameter may be NULL.
327  */
329  /*! \brief Read data from the file and put it into the data buffer.
330 
331  This function reads upp to the number of bytes as requested from the file.
332  The function will also stop if the given terminator character is encountered.
333  The terminator is included in the output buffer.
334  \param maxLength The amount of data that will be read.
335  \param data Pointer to a buffer that will store the read data.
336  \param terminator A character that will end the read operation if encountered.
337  \param length The actual number of bytes read, including the terminator
338  character, if encountered.
339  */
340  XsensResultValue readData(const uint32_t maxLength, const char terminator, void* data, uint32_t* length);
341  /*! \brief Set the new absolute read position
342 
343  The read position is checked against the filesize before committing.
344  */
346  /*! \brief Set the new absolute write position
347 
348  The write position is checked against the filesize before committing.
349  */
350  XsensResultValue setWritePos(const CmtFilePos pos = -1);
351 
352  /*! \brief Write data to the file.
353 
354  The function writes the given data to the file at the current write position.
355  */
356  XsensResultValue writeData(const uint32_t length, const void* data);
357 };
358 
359 } // end of xsens namespace
360 
361 #endif // _CMT1_H_2006_04_12
int32_t m_onBytesReceivedInstance
Custom, user supplied parameter for the OnBytesReceived callback function, passed as the first argume...
Definition: cmt1.h:70
GLuint GLuint GLsizei count
Definition: glext.h:3512
GLenum GLint GLuint mask
Definition: glext.h:3888
Cmt1s()
Default constructor, initializes all members to their default values.
Definition: cmt1.cpp:184
__int64 CmtFilePos
Definition: cmtf.h:24
XsensResultValue appendData(const uint32_t length, const void *data)
Write data to the end of the file.
Definition: cmt1.cpp:684
void getPortName(char *portname) const
Retrieve the port name that was last successfully opened.
Definition: cmt1.h:127
XsensResultValue setReadPos(const CmtFilePos pos)
Set the new absolute read position.
Definition: cmt1.cpp:1273
XsensResultValue writeData(const uint32_t length, const uint8_t *data, uint32_t *written)
Write the data to the serial port.
Definition: cmt1.cpp:618
XsensResultValue getName(char *filename) const
Retrieve the filename that was last successfully opened.
Definition: cmt1.cpp:1006
CmtCallbackFunction m_onBytesReceived
The bytes received function.
Definition: cmt1.h:68
void gotoRead(void)
Change from writing to reading mode.
Definition: cmt1.cpp:1026
GLenum GLint ref
Definition: glext.h:3888
#define CMT_MAX_FILENAME_LENGTH
Definition: cmtdef.h:915
bool isOpen(void) const
Return whether the file is open or not.
Definition: cmt1.h:313
#define CMTLOG(...)
Definition: cmt1.h:38
uint32_t m_baudrate
The baudrate that was last set to be used by the port.
Definition: cmt1.h:75
#define CMT_DEFAULT_BAUD_RATE
The default baud rate of the Cmt1s serial communication.
Definition: cmtdef.h:798
CmtFilePos getReadPos(void) const
Return the current read position.
Definition: cmt1.h:300
XsensResultValue readData(const uint32_t maxLength, void *data, uint32_t *length)
Read data from the file and put it into the data buffer.
Definition: cmt1.cpp:1218
XsensResultValue writeData(const uint32_t length, const void *data)
Write data to the file.
Definition: cmt1.cpp:1319
XsensResultValue closeAndDelete(void)
Close the file and delete it.
Definition: cmt1.cpp:729
CmtControlLine
Definition: cmtdef.h:821
char m_portname[257]
Definition: cmt1.h:84
XsensResultValue deleteData(const CmtFilePos start, const uint32_t length)
Delete the given data from the file.
Definition: cmt1.cpp:862
CmtFilePos getWritePos(void) const
Return the current write position.
Definition: cmt1.h:302
unsigned char uint8_t
Definition: rptypes.h:43
CmtFilePos m_readPos
The last read position in the file.
Definition: cmt1.h:215
XsensResultValue open(const char *filename, const bool create, const bool readOnly)
Open a file.
Definition: cmt1.cpp:1113
HANDLE getHandle(void) const
Return the handle of the port.
Definition: cmt1.h:123
FILE * m_handle
The file handle.
Definition: cmt1.h:211
XsensResultValue setTimeout(const uint32_t ms=CMT1_DEFAULT_TIMEOUT)
Set the default timeout value to use in blocking operations.
Definition: cmt1.cpp:545
XsensResultValue insertData(const CmtFilePos start, const uint32_t length, const void *data)
Insert the given data into the file.
Definition: cmt1.cpp:1048
char m_filename[CMT_MAX_FILENAME_LENGTH]
Contains the name of the file that was last successfully opened.
Definition: cmt1.h:221
XsensResultValue(__cdecl * CmtCallbackFunction)(int32_t, CmtCallbackSelector, void *, void *)
Definition: cmtdef.h:1091
XsensResultValue
Xsens return values.
Definition: xsens_std.h:30
XsensResultValue waitForData(const uint32_t maxLength, uint8_t *data, uint32_t *length=NULL)
Wait for data to arrive or a timeout to occur.
Definition: cmt1.cpp:590
XsensResultValue find(const void *needle, const uint32_t needleLength, CmtFilePos &pos)
Find a string of bytes in the file.
Definition: cmt1.cpp:938
XsensResultValue open(const char *portName, const uint32_t baudRate=CMT_DEFAULT_BAUD_RATE, uint32_t readBufSize=CMT_DEFAULT_READ_BUFFER_SIZE, uint32_t writeBufSize=CMT_DEFAULT_WRITE_BUFFER_SIZE)
Open a communcation channel to the given serial port name.
Definition: cmt1.cpp:322
XsensResultValue readData(const uint32_t maxLength, uint8_t *data, uint32_t *length=NULL)
Read data from the serial port and put it into the data buffer.
Definition: cmt1.cpp:478
CmtFilePos m_fileSize
Contains the size of the file.
Definition: cmt1.h:213
~Cmt1s()
Destructor, de-initializes, frees memory allocated for buffers, etc.
Definition: cmt1.cpp:202
Cmt1f()
Default constructor, initializes all members to their default values.
Definition: cmt1.cpp:662
wchar_t m_filename_w[CMT_MAX_FILENAME_LENGTH]
Contains the name of the file that was last successfully opened using unicode.
Definition: cmt1.h:223
bool m_isOpen
Indicates if the file is open or not.
Definition: cmt1.h:225
XsensResultValue close(void)
Close the file.
Definition: cmt1.cpp:705
XsensResultValue escape(const CmtControlLine mask, const CmtControlLine state)
Manipulate the Serial control lines.
Definition: cmt1.cpp:249
void gotoWrite(void)
Change from reading to writing mode.
Definition: cmt1.cpp:1037
bool m_unicode
Indicates if we&#39;re using the unicode filename or the regular filename.
Definition: cmt1.h:227
XsensResultValue getLastResult(void) const
Return the result code of the last operation.
Definition: cmt1.h:286
#define CMT_DEFAULT_WRITE_BUFFER_SIZE
The default size of the serial write buffer in bytes.
Definition: cmtdef.h:796
The low-level serial communication class.
Definition: cmt1.h:53
#define CMT1_DEFAULT_TIMEOUT
The default timeout value for blocking CMT1s operations in ms.
Definition: cmtdef.h:805
bool m_isOpen
Indicates if the port is open or not.
Definition: cmt1.h:79
CmtFilePos m_writePos
The last write position in the file.
Definition: cmt1.h:217
~Cmt1f()
Destructor.
Definition: cmt1.cpp:677
__int32 int32_t
Definition: rptypes.h:48
XsensResultValue setCallbackFunction(CmtCallbackSelector tp, int32_t instance, CmtCallbackFunction func, void *param)
Set the callback function for when bytes have been received.
Definition: cmt1.cpp:531
uint8_t getPortNr(void) const
Retrieve the port number that was last successfully opened.
Definition: cmt1.h:125
HANDLE m_handle
The serial port handle.
Definition: cmt1.h:92
bool m_reading
Indicates whether the last operation was a read or write operation.
Definition: cmt1.h:233
uint32_t getTimeout(void) const
Return the current timeout value.
Definition: cmt1.h:131
CmtFilePos getFileSize(void) const
Return the size of the file.
Definition: cmt1.h:284
void * m_onBytesReceivedParam
Custom, user supplied parameter for the OnBytesReceived callback function, passed as the last argumen...
Definition: cmt1.h:72
uint32_t m_endTime
The time at which an operation will end in ms, used by several functions.
Definition: cmt1.h:77
GLuint GLsizei GLsizei * length
Definition: glext.h:3900
bool isOpen(void) const
Return whether the communication port is open or not.
Definition: cmt1.h:133
XsensResultValue m_lastResult
The last result of an operation.
Definition: cmt1.h:81
The low-level file communication class.
Definition: cmt1.h:204
CmtCallbackSelector
Definition: cmtdef.h:1068
int BASE_IMPEXP sprintf(char *buf, size_t bufSize, const char *format,...) MRPT_NO_THROWS MRPT_printf_format_check(3
An OS-independent version of sprintf (Notice the bufSize param, which may be ignored in some compiler...
Definition: os.cpp:191
The namespace of all Xsens software since 2006.
Definition: cmt1.cpp:92
bool m_readOnly
Indicates if the file was opened in read-only mode.
Definition: cmt1.h:235
uint32_t m_timeout
Definition: cmt1.h:88
XsensResultValue create(const char *filename)
Open an empty file.
Definition: cmt1.cpp:781
XsensResultValue close(void)
Close the serial communication port.
Definition: cmt1.cpp:209
GLsizei maxLength
Definition: glext.h:4504
bool isReadOnly(void) const
Return whether the file is readonly or not.
Definition: cmt1.h:315
GLuint start
Definition: glext.h:3512
#define CMT_DEFAULT_READ_BUFFER_SIZE
The default size of the serial read buffer in bytes.
Definition: cmtdef.h:794
uint8_t m_port
The opened COM port nr.
Definition: cmt1.h:83
unsigned __int32 uint32_t
Definition: rptypes.h:49
GLfloat param
Definition: glext.h:3705
GLsizei GLsizei GLenum GLenum const GLvoid * data
Definition: glext.h:3520
XsensResultValue setWritePos(const CmtFilePos pos=-1)
Set the new absolute write position.
Definition: cmt1.cpp:1290
XsensResultValue getLastResult(void) const
Return the error code of the last operation.
Definition: cmt1.h:129
DCB m_commState
Stored settings about the serial port.
Definition: cmt1.h:91
XsensResultValue flushData(void)
Flush all data to be written. This function writes any remaining data immediately and does not return...
Definition: cmt1.cpp:997
XsensResultValue flushData(void)
Flush all data to be transmitted / received.
Definition: cmt1.cpp:308
XsensResultValue m_lastResult
The last result of an operation.
Definition: cmt1.h:219
uint32_t getBaudrate(void) const
Return the baudrate that is currently being used by the port.
Definition: cmt1.h:121



Page generated by Doxygen 1.8.14 for MRPT 1.5.7 Git: 5902e14cc Wed Apr 24 15:04:01 2019 +0200 at lun oct 28 01:39:17 CET 2019