xsens_imu
cmt1.h
Go to the documentation of this file.
1 #ifndef XSENS_MONOLITHIC
2 
20 #endif
21 #ifndef CMT1_H
22 #define CMT1_H
23 
24 #ifndef CMTDEF_H
25 # include "cmtdef.h"
26 #endif
27 
28 #include <stdlib.h>
29 #include <stdio.h>
30 #ifdef _WIN32
31 # include <windows.h>
32 //# include <sys/types.h>
33 #else
34 # include <termios.h>
35 // these are not required by level 1, but to keep the higher levels platform-independent they are put here
36 # include <string.h>
37 # include <stddef.h>
38 #define _strnicmp strncasecmp
39 #endif
40 
41 #include <stdio.h>
42 
44 namespace xsens {
45 
46 #ifndef CMTLOG
47 # if defined(XSENS_DEBUG) || defined(CMT_LOG_ALWAYS)
48  void CMTLOG(const char *str, ...);
49 # define CMTEXITLOG(str) JanitorFunc2R<const char*,XsensResultValue> _cmtExitLog(CMTLOG,str " returns %u",m_lastResult);
50 # else
51 # define CMTLOG(...)
52 # define CMTEXITLOG(...)
53 # endif
54 #endif
55 
56 #ifndef _WIN32
57 int _wcsnicmp(const wchar_t* s1, const wchar_t* s2,int count);
58 typedef int32_t HANDLE;
59 #endif
60 
64 
67 class Cmt1s {
68 private:
70  Cmt1s(const Cmt1s& ref);
71 
72  #ifdef LOG_RX_TX
73  FILE* rx_log;
74  FILE* tx_log;
75  #endif
76 
82  CmtCallbackFunction m_onBytesReceived;
84  int32_t m_onBytesReceivedInstance;
86  void* m_onBytesReceivedParam;
87 protected:
89  uint32_t m_baudrate;
91  uint32_t m_endTime;
93  bool m_isOpen;
97  uint16_t m_port;
98  char m_portname[32];
102  uint32_t m_timeout;
103 
104  #ifdef _WIN32
105  DCB m_commState;
106  HANDLE m_handle;
107  #else
108  termios m_commState;
109  int32_t m_handle;
110  #endif
111 public:
113  Cmt1s();
115  ~Cmt1s();
117  XsensResultValue close (void);
126  XsensResultValue escape (const CmtControlLine mask, const CmtControlLine state);
134  uint32_t getBaudrate(void) const;
136  HANDLE getHandle(void) const;
138  uint16_t getPortNr (void) const;
140  void getPortName(char *portname) const;
142  XsensResultValue getLastResult(void) const;
144  uint32_t getTimeout (void) const;
146  bool isOpen (void) const;
147 
153  XsensResultValue open ( const char *portName,
154  const uint32_t baudRate = CMT_DEFAULT_BAUD_RATE,
155  uint32_t readBufSize = CMT_DEFAULT_READ_BUFFER_SIZE,
156  uint32_t writeBufSize = CMT_DEFAULT_WRITE_BUFFER_SIZE);
157 
158 #ifdef _WIN32
159 
164  XsensResultValue open ( const uint32_t portNumber,
165  const uint32_t baudRate = CMT_DEFAULT_BAUD_RATE,
166  uint32_t readBufSize = CMT_DEFAULT_READ_BUFFER_SIZE,
167  uint32_t writeBufSize = CMT_DEFAULT_WRITE_BUFFER_SIZE);
168 #endif
169 
180  XsensResultValue readData (const uint32_t maxLength, uint8_t* data,
181  uint32_t* length = NULL);
183  XsensResultValue setCallbackFunction(CmtCallbackType tp, int32_t instance, CmtCallbackFunction func, void* param);
191  XsensResultValue setTimeout (const uint32_t ms = CMT1_DEFAULT_TIMEOUT);
199  XsensResultValue waitForData (const uint32_t maxLength, uint8_t* data,
200  uint32_t* length = NULL);
206  XsensResultValue writeData (const uint32_t length, const uint8_t* data,
207  uint32_t* written);
208 
209 };
210 
214 
217 class Cmt1f {
218 private:
220  Cmt1f(const Cmt1f& ref);
221 
222 protected:
224  FILE* m_handle;
234  char m_filename[CMT_MAX_FILENAME_LENGTH];
236  wchar_t m_filename_w[CMT_MAX_FILENAME_LENGTH];
238  bool m_isOpen;
240  bool m_unicode;
246  bool m_reading;
250  void gotoRead(void);
252  void gotoWrite(void);
253 public:
255  Cmt1f();
257  ~Cmt1f();
263  XsensResultValue appendData(const uint32_t length, const void* data);
265  XsensResultValue close(void);
267  XsensResultValue closeAndDelete(void);
269  XsensResultValue create(const char* filename);
271  XsensResultValue create(const wchar_t* filename);
279  XsensResultValue deleteData(const XsensFilePos start, const uint32_t length);
290  XsensResultValue find(const void* needle, const uint32_t needleLength, XsensFilePos& pos);
297  XsensFilePos getFileSize(void) const { return m_fileSize; }
305  XsensResultValue getName(char* filename) const;
311  XsensResultValue getName(wchar_t* filename) const;
313  XsensFilePos getReadPos(void) const { return m_readPos; }
315  XsensFilePos getWritePos(void) const { return m_writePos; }
323  XsensResultValue insertData(const XsensFilePos start, const uint32_t length,
324  const void* data);
326  bool isOpen(void) const { return m_isOpen; }
328  bool isReadOnly(void) const { return !m_isOpen || m_readOnly; }
330  XsensResultValue open(const char* filename, const bool create, const bool readOnly);
332  XsensResultValue open(const wchar_t* filename, const bool create, const bool readOnly);
341  XsensResultValue readData(const uint32_t maxLength, void* data, uint32_t* length);
353  XsensResultValue readData(const uint32_t maxLength, const char terminator, void* data, uint32_t* length);
358  XsensResultValue setReadPos(const XsensFilePos pos);
363  XsensResultValue setWritePos(const XsensFilePos pos = -1);
364 
369  XsensResultValue writeData(const uint32_t length, const void* data);
370 };
371 
372 } // end of xsens namespace
373 
374 #endif // CMT1_H
XsensResultValue flushData(void)
Flush all data to be transmitted / received.
Definition: cmt1.cpp:285
uint32_t m_endTime
The time at which an operation will end in ms, used by several functions.
Definition: cmt1.h:91
bool isReadOnly(void) const
Return whether the file is readonly or not.
Definition: cmt1.h:328
#define CMT_MAX_FILENAME_LENGTH
Definition: cmtdef.h:960
HANDLE getHandle(void) const
Return the handle of the port.
Definition: cmt1.cpp:300
#define CMTLOG(...)
Definition: cmt1.h:51
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:314
The low-level serial communication class.
Definition: cmt1.h:67
#define CMT_DEFAULT_BAUD_RATE
The default baud rate of the Cmt1s serial communication.
Definition: cmtdef.h:828
CmtCallbackType
Definition: cmtdef.h:1101
XsensResultValue writeData(const uint32_t length, const uint8_t *data, uint32_t *written)
Write the data to the serial port.
Definition: cmt1.cpp:634
CmtControlLine
Definition: cmtdef.h:853
XsensResultValue setCallbackFunction(CmtCallbackType tp, int32_t instance, CmtCallbackFunction func, void *param)
Set the callback function for when bytes have been received.
Definition: cmt1.cpp:547
bool isOpen(void) const
Return whether the communication port is open or not.
Definition: cmt1.cpp:310
XsensResultValue m_lastResult
The last result of an operation.
Definition: cmt1.h:232
int _wcsnicmp(const wchar_t *s1, const wchar_t *s2, int count)
Definition: cmt1.cpp:65
XsensResultValue
Xsens return values.
Definition: xsens_std.h:43
bool m_unicode
Indicates if we&#39;re using the unicode filename or the regular filename.
Definition: cmt1.h:240
bool m_isOpen
Indicates if the file is open or not.
Definition: cmt1.h:238
XsensResultValue escape(const CmtControlLine mask, const CmtControlLine state)
Manipulate the Serial control lines.
Definition: cmt1.cpp:226
XsensFilePos m_readPos
The last read position in the file.
Definition: cmt1.h:228
XsensFilePos getReadPos(void) const
Return the current read position.
Definition: cmt1.h:313
XsensFilePos m_writePos
The last write position in the file.
Definition: cmt1.h:230
int32_t HANDLE
Definition: cmt1.h:58
XsensResultValue close(void)
Close the serial communication port.
Definition: cmt1.cpp:186
~Cmt1s()
Destructor, de-initializes, frees memory allocated for buffers, etc.
Definition: cmt1.cpp:179
uint16_t m_port
The opened COM port nr.
Definition: cmt1.h:97
bool isOpen(void) const
Return whether the file is open or not.
Definition: cmt1.h:326
termios m_commState
Stored settings about the serial port.
Definition: cmt1.h:108
The low-level file communication class.
Definition: cmt1.h:217
uint32_t getBaudrate(void) const
Return the baudrate that is currently being used by the port.
Definition: cmt1.cpp:298
XsensFilePos getFileSize(void) const
Return the size of the file.
Definition: cmt1.h:297
Cmt1s()
Default constructor, initializes all members to their default values.
Definition: cmt1.cpp:161
FILE * m_handle
The file handle.
Definition: cmt1.h:224
#define CMT_DEFAULT_WRITE_BUFFER_SIZE
The default size of the serial write buffer in bytes.
Definition: cmtdef.h:826
#define CMT1_DEFAULT_TIMEOUT
The default timeout value for blocking CMT1s operations in ms.
Definition: cmtdef.h:835
uint32_t getTimeout(void) const
Return the current timeout value.
Definition: cmt1.cpp:308
uint16_t getPortNr(void) const
Retrieve the port number that was last successfully opened.
Definition: cmt1.cpp:302
void(__cdecl * CmtCallbackFunction)(int32_t instance, int32_t callbackType, void *param, void *userParam)
Function prototype for Cmt callback functions.
Definition: cmtdef.h:1134
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:471
int32_t m_handle
The serial port handle.
Definition: cmt1.h:109
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:606
Macros and types for use in the Xsens communication protocol and CMT classes.
The namespace of all Xsens software since 2006.
Definition: cmt1.cpp:62
XsensResultValue getLastResult(void) const
Return the result code of the last operation.
Definition: cmt1.h:299
XsensResultValue getLastResult(void) const
Return the error code of the last operation.
Definition: cmt1.cpp:306
void getPortName(char *portname) const
Retrieve the port name that was last successfully opened.
Definition: cmt1.cpp:304
bool m_isOpen
Indicates if the port is open or not.
Definition: cmt1.h:93
XsensResultValue m_lastResult
The last result of an operation.
Definition: cmt1.h:95
#define CMT_DEFAULT_READ_BUFFER_SIZE
The default size of the serial read buffer in bytes.
Definition: cmtdef.h:824
XsensFilePos m_fileSize
Contains the size of the file.
Definition: cmt1.h:226
XsensFilePos getWritePos(void) const
Return the current write position.
Definition: cmt1.h:315
XsensResultValue setTimeout(const uint32_t ms=CMT1_DEFAULT_TIMEOUT)
Set the default timeout value to use in blocking operations.
Definition: cmt1.cpp:561
uint32_t m_timeout
Definition: cmt1.h:102
bool m_readOnly
Indicates if the file was opened in read-only mode.
Definition: cmt1.h:248
bool m_reading
Indicates whether the last operation was a read or write operation.
Definition: cmt1.h:246
char m_portname[32]
Definition: cmt1.h:98
uint32_t m_baudrate
The baudrate that was last set to be used by the port.
Definition: cmt1.h:89
__off64_t XsensFilePos
Definition: xsens_file.h:21