MRPT  1.9.9
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-2018, 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
22 // platform-independent they are put here
23 #include <stdlib.h>
24 #include <string.h>
25 #include <stddef.h>
26 #define _strnicmp strncasecmp
27 #endif
28 
29 #include <stdio.h>
30 
31 //! The namespace of all Xsens software since 2006.
32 namespace xsens
33 {
34 #ifndef CMTLOG
35 #if defined(_DEBUG) || defined(_LOG_ALWAYS)
36 void CMTLOG(const char* str, ...);
37 #define CMTEXITLOG(str) \
38  JanitorFunc2<const char*, XsensResultValue> _cmtExitLog( \
39  CMTLOG, str " returns %u", m_lastResult);
40 #else
41 #define CMTLOG(...)
42 #define CMTEXITLOG(...)
43 #endif
44 #endif
45 
46 #ifndef _WIN32
47 int _wcsnicmp(const wchar_t* s1, const wchar_t* s2, int count);
48 #endif
49 
50 //////////////////////////////////////////////////////////////////////////////////////////
51 ///////////////////////////////////////// Cmt1s
52 ////////////////////////////////////////////
53 //////////////////////////////////////////////////////////////////////////////////////////
54 
55 /*! \brief The low-level serial communication class.
56 */
57 class Cmt1s
58 {
59  private:
60  //! This object cannot be copied, so this function is not implemented.
61  Cmt1s(const Cmt1s& ref);
62 
63 #ifdef _LOG_RX_TX
64  FILE* rx_log;
65  FILE* tx_log;
66 #endif
67 
68  /*! \brief The bytes received function.
69 
70  This function is automatically called every time binary data is read
71  from the
72  connected COM port.
73  */
75  //! Custom, user supplied parameter for the OnBytesReceived callback
76  //! function, passed as the first argument
78  //! Custom, user supplied parameter for the OnBytesReceived callback
79  //! function, passed as the last argument
81 
82  protected:
83  //! The baudrate that was last set to be used by the port
85  //! The time at which an operation will end in ms, used by several
86  //! functions.
88  //! Indicates if the port is open or not
89  bool m_isOpen;
90  //! The last result of an operation
92  //! The opened COM port nr
94  char m_portname[260];
95  /*! The default timeout value to use during blocking operations.
96  A value of 0 means that all operations become non-blocking.
97  */
99 
100 #ifdef _WIN32
101  /** Stored settings about the serial port */
103  /** The serial port handle */
104  HANDLE m_handle;
105 #else
106  /** Stored settings about the serial port */
107  termios m_commState;
108  /** The serial port handle */
110  typedef int32_t HANDLE;
111 #endif
112  public:
113  //! Default constructor, initializes all members to their default values.
114  Cmt1s();
115  //! Destructor, de-initializes, frees memory allocated for buffers, etc.
116  ~Cmt1s();
117  //! \brief Close the serial communication port.
118  XsensResultValue close(void);
119  /*! \brief Manipulate the Serial control lines
120 
121  The function manipulates the serial control lines that are indicated by
122  the
123  mask parameter. Note that only the DTR and RTS lines can be set by
124  win32.
125  \param mask Indicates which lines are to be manipulated and which
126  should be
127  left alone.
128  \param state Contains the new state of the control lines.
129  */
131  const CmtControlLine mask, const CmtControlLine state);
132  /*! \brief Flush all data to be transmitted / received.
133 
134  This function tries to send and receive any remaining data immediately
135  and does not return until the buffers are empty.
136  */
138  //! Return the baudrate that is currently being used by the port
139  uint32_t getBaudrate(void) const { return m_baudrate; }
140  //! Return the handle of the port
141  HANDLE getHandle(void) const { return m_handle; }
142  //! Retrieve the port number that was last successfully opened.
143  uint8_t getPortNr(void) const { return m_port; }
144  //! Retrieve the port name that was last successfully opened.
145  void getPortName(char* portname) const
146  {
147  sprintf(portname, "%s", m_portname);
148  }
149  //! Return the error code of the last operation.
151  //! Return the current timeout value
152  uint32_t getTimeout(void) const { return m_timeout; }
153  //! Return whether the communication port is open or not.
154  bool isOpen(void) const { return m_isOpen; }
155  /*! \brief Open a communcation channel to the given serial port name.
156 
157  The port is automatically initialized to the given baudrate.
158  If the baudrate is set to 0, the baud rate is automatically detected. If
159  possible.
160  */
162  const char* portName, const uint32_t baudRate = CMT_DEFAULT_BAUD_RATE,
164  uint32_t writeBufSize = CMT_DEFAULT_WRITE_BUFFER_SIZE);
165 
166 #ifdef _WIN32
167  /*! \brief Open a communication channel to the given COM port number.
168 
169  The port is automatically initialized to the given baud rate.
170  If the baudrate is set to 0, the baud rate is automatically detected. If
171  possible.
172  */
174  const uint32_t portNumber,
175  const uint32_t baudRate = CMT_DEFAULT_BAUD_RATE,
177  uint32_t writeBufSize = CMT_DEFAULT_WRITE_BUFFER_SIZE);
178 #endif
179  /*! \brief Read data from the serial port and put it into the data buffer.
180 
181  This function reads as much data as possible from the com port
182  (non-blocking) and
183  put as much data as will fit into the data buffer. Any excess data is
184  stored in
185  the \c m_readBuffer member variable. If there was enough data in
186  m_readBuffer to
187  fulfill the request, the data parameter is first filled and the port is
188  polled
189  afterwards.
190  \param maxLength The maximum amount of data read.
191  \param data Pointer to a buffer that will store the received
192  data.
193  \param length The number of bytes placed into \c data.
194  */
196  const uint32_t maxLength, uint8_t* data, uint32_t* length = nullptr);
197  //! Set the callback function for when bytes have been received
200  void* param);
201  /*! \brief Set the default timeout value to use in blocking operations.
202 
203  This function sets the value of m_timeout. There is no infinity value.
204  The value 0
205  means that all blocking operations now become polling (non-blocking)
206  operations.
207  If the value is set to or from 0, the low-level serial port settings may
208  be
209  changed in addition to the m_timeout value.
210  */
212  /*! \brief Wait for data to arrive or a timeout to occur.
213 
214  The function waits until \c maxLength data is available or until a
215  timeout occurs.
216  The function returns success if data is available or
217  XsensResultValue::TIMEOUT if a
218  timeout occurred. A timeout value of 0 indicates that the default
219  timeout stored
220  in the class should be used.
221  */
223  const uint32_t maxLength, uint8_t* data, uint32_t* length = nullptr);
224  /*! \brief Write the data to the serial port.
225 
226  The function writes the given data to the connected COM port.
227  The default timeout is respected in this operation.
228  */
230  const uint32_t length, const uint8_t* data, uint32_t* written);
231 };
232 
233 //////////////////////////////////////////////////////////////////////////////////////////
234 ///////////////////////////////////////// Cmt1f
235 ////////////////////////////////////////////
236 //////////////////////////////////////////////////////////////////////////////////////////
237 
238 /*! \brief The low-level file communication class.
239 */
240 class Cmt1f
241 {
242  private:
243  //! This object cannot be copied, so this function is not implemented.
244  Cmt1f(const Cmt1f& ref);
245 
246  protected:
247  //! The file handle
248  FILE* m_handle;
249  //! Contains the size of the file
251  //! The last read position in the file
253  //! The last write position in the file
255  //! The last result of an operation
257  //! Contains the name of the file that was last successfully opened.
259  //! Contains the name of the file that was last successfully opened using
260  //! unicode.
262  //! Indicates if the file is open or not.
263  bool m_isOpen;
264  //! Indicates if we're using the unicode filename or the regular filename
265  bool m_unicode;
266  /*! \brief Indicates whether the last operation was a read or write
267  operation. This value is used to check whether or not a seek is
268  required to perform a requested read or write operation.
269  */
270  bool m_reading;
271  //! Indicates if the file was opened in read-only mode
273  //! Change from writing to reading mode
274  void gotoRead(void);
275  //! Change from reading to writing mode
276  void gotoWrite(void);
277 
278  public:
279  //! Default constructor, initializes all members to their default values.
280  Cmt1f();
281  //! Destructor.
282  ~Cmt1f();
283  /*! \brief Write data to the end of the file.
284 
285  The function writes the given data to the file at the end. The current
286  write
287  position is also moved to the end of the file.
288  */
289  XsensResultValue appendData(const uint32_t length, const void* data);
290  //! Close the file.
291  XsensResultValue close(void);
292  //! Close the file and delete it.
294  //! Open an empty file.
295  XsensResultValue create(const char* filename);
296  //! Open an empty file using a unicode path + filename.
297  XsensResultValue create(const wchar_t* filename);
298  /*! \brief Delete the given data from the file.
299  The function erases the given data from the file at the given write
300  position. This
301  operation may take a while to complete, but is faster than insertData.
302 
303  The write position is not changed and the read position is checked
304  for validity.
305  */
307  /*! \brief Find a string of bytes in the file
308 
309  The function searches from the current read position until the given \c
310  needle is
311  found. If the needle is not found, XsensResultValue::NOT_FOUND is
312  returned. The function
313  will update the seek position to the first character of the found
314  needle.
315  \param needle The byte string to find.
316  \param needleLength The length of the byte string.
317  \param pos Out: The position where the needle was found. This
318  will point
319  to the first character of the found needle.
320  */
322  const void* needle, const uint32_t needleLength, CmtFilePos& pos);
323  /*! \brief Flush all data to be written.
324  This function writes any remaining data immediately and does not return
325  until this is done.
326  */
328  //! Return the size of the file.
329  CmtFilePos getFileSize(void) const { return m_fileSize; }
330  //! Return the result code of the last operation.
332  /*! \brief Retrieve the filename that was last successfully opened.
333 
334  \param filename A buffer for storing the filename. The buffer should
335  be able
336  to hold the filename. A safe size is to make it at least 256
337  bytes.
338  */
339  XsensResultValue getName(char* filename) const;
340  /*! \brief Retrieve the filename that was last successfully opened in
341  unicode.
342 
343  \param filename A buffer for storing the filename. The buffer should
344  be able
345  to hold the filename. A safe size is to make it at least 256
346  wide characters.
347  */
348  XsensResultValue getName(wchar_t* filename) const;
349  //! Return the current read position.
350  CmtFilePos getReadPos(void) const { return m_readPos; }
351  //! Return the current write position.
352  CmtFilePos getWritePos(void) const { return m_writePos; }
353  /*! \brief Insert the given data into the file.
354  The function writes the given data to the file at the current write
355  position. This
356  operation may take a while to complete.
357 
358  The write position is placed at the end of the inserted data.
359  */
361  const CmtFilePos start, const uint32_t length, const void* data);
362  //! Return whether the file is open or not.
363  bool isOpen(void) const { return m_isOpen; }
364  //! Return whether the file is readonly or not.
365  bool isReadOnly(void) const { return !m_isOpen || m_readOnly; }
366  //! Open a file.
368  const char* filename, const bool create, const bool readOnly);
369  //! Open a file using a unicode filename.
371  const wchar_t* filename, const bool create, const bool readOnly);
372  /*! \brief Read data from the file and put it into the data buffer.
373 
374  This function reads exactly the number of bytes as requested from the
375  file.
376  \param maxLength The amount of data that will be read.
377  \param data Pointer to a buffer that will store the read data.
378  \param length pointer to a variable that will store the number of
379  bytes
380  that were actually read. The parameter may be NULL.
381  */
383  const uint32_t maxLength, void* data, uint32_t* length);
384  /*! \brief Read data from the file and put it into the data buffer.
385 
386  This function reads upp to the number of bytes as requested from the
387  file.
388  The function will also stop if the given terminator character is
389  encountered.
390  The terminator is included in the output buffer.
391  \param maxLength The amount of data that will be read.
392  \param data Pointer to a buffer that will store the read data.
393  \param terminator A character that will end the read operation if
394  encountered.
395  \param length The actual number of bytes read, including the
396  terminator
397  character, if encountered.
398  */
400  const uint32_t maxLength, const char terminator, void* data,
401  uint32_t* length);
402  /*! \brief Set the new absolute read position
403 
404  The read position is checked against the filesize before committing.
405  */
407  /*! \brief Set the new absolute write position
408 
409  The write position is checked against the filesize before committing.
410  */
411  XsensResultValue setWritePos(const CmtFilePos pos = -1);
412 
413  /*! \brief Write data to the file.
414 
415  The function writes the given data to the file at the current write
416  position.
417  */
418  XsensResultValue writeData(const uint32_t length, const void* data);
419 };
420 
421 } // end of xsens namespace
422 
423 #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:77
XsensResultValue waitForData(const uint32_t maxLength, uint8_t *data, uint32_t *length=nullptr)
Wait for data to arrive or a timeout to occur.
Definition: cmt1.cpp:605
GLuint GLuint GLsizei count
Definition: glext.h:3528
GLenum GLint GLuint mask
Definition: glext.h:4050
Cmt1s()
Default constructor, initializes all members to their default values.
Definition: cmt1.cpp:185
__int64 CmtFilePos
Definition: cmtf.h:25
XsensResultValue appendData(const uint32_t length, const void *data)
Write data to the end of the file.
Definition: cmt1.cpp:694
void getPortName(char *portname) const
Retrieve the port name that was last successfully opened.
Definition: cmt1.h:145
XsensResultValue setReadPos(const CmtFilePos pos)
Set the new absolute read position.
Definition: cmt1.cpp:1272
XsensResultValue writeData(const uint32_t length, const uint8_t *data, uint32_t *written)
Write the data to the serial port.
Definition: cmt1.cpp:633
XsensResultValue getName(char *filename) const
Retrieve the filename that was last successfully opened.
Definition: cmt1.cpp:1010
CmtCallbackFunction m_onBytesReceived
The bytes received function.
Definition: cmt1.h:74
void gotoRead(void)
Change from writing to reading mode.
Definition: cmt1.cpp:1030
GLenum GLint ref
Definition: glext.h:4050
#define CMT_MAX_FILENAME_LENGTH
Definition: cmtdef.h:926
bool isOpen(void) const
Return whether the file is open or not.
Definition: cmt1.h:363
#define CMTLOG(...)
Definition: cmt1.h:41
uint32_t m_baudrate
The baudrate that was last set to be used by the port.
Definition: cmt1.h:84
#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:350
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:1220
XsensResultValue writeData(const uint32_t length, const void *data)
Write data to the file.
Definition: cmt1.cpp:1312
XsensResultValue closeAndDelete(void)
Close the file and delete it.
Definition: cmt1.cpp:737
CmtControlLine
Definition: cmtdef.h:821
XsensResultValue deleteData(const CmtFilePos start, const uint32_t length)
Delete the given data from the file. The function erases the given data from the file at the given wr...
Definition: cmt1.cpp:867
CmtFilePos getWritePos(void) const
Return the current write position.
Definition: cmt1.h:352
XsensResultValue readData(const uint32_t maxLength, uint8_t *data, uint32_t *length=nullptr)
Read data from the serial port and put it into the data buffer.
Definition: cmt1.cpp:485
unsigned char uint8_t
Definition: rptypes.h:41
CmtFilePos m_readPos
The last read position in the file.
Definition: cmt1.h:252
XsensResultValue open(const char *filename, const bool create, const bool readOnly)
Open a file.
Definition: cmt1.cpp:1116
HANDLE getHandle(void) const
Return the handle of the port.
Definition: cmt1.h:141
FILE * m_handle
The file handle.
Definition: cmt1.h:248
XsensResultValue setTimeout(const uint32_t ms=CMT1_DEFAULT_TIMEOUT)
Set the default timeout value to use in blocking operations.
Definition: cmt1.cpp:561
XsensResultValue insertData(const CmtFilePos start, const uint32_t length, const void *data)
Insert the given data into the file. The function writes the given data to the file at the current wr...
Definition: cmt1.cpp:1050
char m_filename[CMT_MAX_FILENAME_LENGTH]
Contains the name of the file that was last successfully opened.
Definition: cmt1.h:258
XsensResultValue
Xsens return values.
Definition: xsens_std.h:31
XsensResultValue(__cdecl * CmtCallbackFunction)(int32_t, CmtCallbackSelector, void *, void *)
Definition: cmtdef.h:1160
XsensResultValue find(const void *needle, const uint32_t needleLength, CmtFilePos &pos)
Find a string of bytes in the file.
Definition: cmt1.cpp:941
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
CmtFilePos m_fileSize
Contains the size of the file.
Definition: cmt1.h:250
~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:676
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:261
bool m_isOpen
Indicates if the file is open or not.
Definition: cmt1.h:263
XsensResultValue close(void)
Close the file.
Definition: cmt1.cpp:713
XsensResultValue escape(const CmtControlLine mask, const CmtControlLine state)
Manipulate the Serial control lines.
Definition: cmt1.cpp:245
void gotoWrite(void)
Change from reading to writing mode.
Definition: cmt1.cpp:1040
bool m_unicode
Indicates if we&#39;re using the unicode filename or the regular filename.
Definition: cmt1.h:265
XsensResultValue getLastResult(void) const
Return the result code of the last operation.
Definition: cmt1.h:331
#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:57
#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:89
CmtFilePos m_writePos
The last write position in the file.
Definition: cmt1.h:254
~Cmt1f()
Destructor.
Definition: cmt1.cpp:691
__int32 int32_t
Definition: rptypes.h:46
XsensResultValue setCallbackFunction(CmtCallbackSelector tp, int32_t instance, CmtCallbackFunction func, void *param)
Set the callback function for when bytes have been received.
Definition: cmt1.cpp:545
char m_portname[260]
Definition: cmt1.h:94
uint8_t getPortNr(void) const
Retrieve the port number that was last successfully opened.
Definition: cmt1.h:143
HANDLE m_handle
The serial port handle.
Definition: cmt1.h:104
bool m_reading
Indicates whether the last operation was a read or write operation. This value is used to check wheth...
Definition: cmt1.h:270
uint32_t getTimeout(void) const
Return the current timeout value.
Definition: cmt1.h:152
CmtFilePos getFileSize(void) const
Return the size of the file.
Definition: cmt1.h:329
void * m_onBytesReceivedParam
Custom, user supplied parameter for the OnBytesReceived callback function, passed as the last argumen...
Definition: cmt1.h:80
uint32_t m_endTime
The time at which an operation will end in ms, used by several functions.
Definition: cmt1.h:87
GLuint GLsizei GLsizei * length
Definition: glext.h:4064
bool isOpen(void) const
Return whether the communication port is open or not.
Definition: cmt1.h:154
XsensResultValue m_lastResult
The last result of an operation.
Definition: cmt1.h:91
The low-level file communication class.
Definition: cmt1.h:240
CmtCallbackSelector
Definition: cmtdef.h:1120
The namespace of all Xsens software since 2006.
Definition: cmt1.cpp:95
bool m_readOnly
Indicates if the file was opened in read-only mode.
Definition: cmt1.h:272
uint32_t m_timeout
Definition: cmt1.h:98
XsensResultValue create(const char *filename)
Open an empty file.
Definition: cmt1.cpp:789
XsensResultValue close(void)
Close the serial communication port.
Definition: cmt1.cpp:205
GLsizei maxLength
Definition: glext.h:4932
bool isReadOnly(void) const
Return whether the file is readonly or not.
Definition: cmt1.h:365
GLuint start
Definition: glext.h:3528
#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:93
unsigned __int32 uint32_t
Definition: rptypes.h:47
GLfloat param
Definition: glext.h:3831
GLsizei GLsizei GLenum GLenum const GLvoid * data
Definition: glext.h:3546
XsensResultValue setWritePos(const CmtFilePos pos=-1)
Set the new absolute write position.
Definition: cmt1.cpp:1287
XsensResultValue getLastResult(void) const
Return the error code of the last operation.
Definition: cmt1.h:150
int sprintf(char *buf, size_t bufSize, const char *format,...) noexcept 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:189
DCB m_commState
Stored settings about the serial port.
Definition: cmt1.h:102
XsensResultValue flushData(void)
Flush all data to be written. This function writes any remaining data immediately and does not return...
Definition: cmt1.cpp:1001
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:256
uint32_t getBaudrate(void) const
Return the baudrate that is currently being used by the port.
Definition: cmt1.h:139



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020