cmt2.h

Go to the documentation of this file.
00001 #ifndef _CMT_MONOLITHIC
00002 /*! \file
00003         \brief        Contains the CMT Level 2 interface
00004 
00005         This level contains the message- and log-file interfaces of Cmt.
00006         All code should be OS-independent.
00007 
00008         \section FileCopyright Copyright Notice 
00009         Copyright (C) Xsens Technologies B.V., 2006.  All rights reserved.
00010 
00011         This source code is intended for use only by Xsens Technologies BV and
00012         those that have explicit written permission to use it from
00013         Xsens Technologies BV.
00014 
00015         THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
00016         KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
00017         IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
00018         PARTICULAR PURPOSE.
00019 */
00020 #endif
00021 #ifndef _CMT2_H_2006_04_13
00022 #define _CMT2_H_2006_04_13
00023 
00024 #ifndef _CMT_MONOLITHIC
00025 #        include "cmt1.h"
00026 #        include "cmtmessage.h"
00027 #        include "xsens_fifoqueue.h"
00028 #endif
00029 
00030 namespace xsens {
00031 
00032 //////////////////////////////////////////////////////////////////////////////////////////
00033 /////////////////////////////////// Support  functions ///////////////////////////////////
00034 //////////////////////////////////////////////////////////////////////////////////////////
00035 
00036 //! Find a valid message in the given buffer. If nothing is found, the function returns -1. Otherwise the index of the first character of the message is returned.
00037 int32_t findValidMessage(const uint8_t* buffer, const uint16_t bufferLength);
00038 
00039 
00040 //////////////////////////////////////////////////////////////////////////////////////////
00041 ///////////////////////////////////////// Cmt2s  /////////////////////////////////////////
00042 //////////////////////////////////////////////////////////////////////////////////////////
00043 /*! \brief Mid-level serial communication class.
00044 
00045         The class uses CMT level 1, but does not inherit from it. If software needs to access 
00046         the level 1 component, it needs to be done through the getCmt1s() function.
00047 */
00048 class Cmt2s {
00049 private:
00050         //! This object cannot be copied, so this function is not implemented.
00051         Cmt2s(const Cmt2s& ref);
00052 
00053         /*! \brief The message received function.
00054         
00055                 This function is automatically called every time a complete message was read from the 
00056                 connected COM port.
00057         */
00058         CmtCallbackFunction m_onMessageReceived;
00059         //! Custom, user supplied parameter for the OnMessageReceived callback function, passed as the first argument
00060         int32_t m_onMessageReceivedInstance;
00061         //! Custom, user supplied parameter for the OnMessageReceived callback function, passed as the last argument
00062         void* m_onMessageReceivedParam;
00063 
00064         /*! \brief The message sent function.
00065         
00066                 This function is automatically called every time a complete message was sent to the 
00067                 connected COM port.
00068         */
00069         CmtCallbackFunction m_onMessageSent;
00070         //! Custom, user supplied parameter for the OnMessageSent callback function, passed as the first argument
00071         int32_t m_onMessageSentInstance;
00072         //! Custom, user supplied parameter for the OnMessageSent callback function, passed as the last argument
00073         void* m_onMessageSentParam;
00074 
00075 protected:
00076                 //! The baudrate that was last set to be used by the port
00077         uint32_t m_baudrate;
00078                 //! The CMT level 1 object that this class operates on
00079         Cmt1s m_cmt1s;
00080                 //! The last result of an operation
00081         mutable XsensResultValue m_lastResult;
00082                 //! Buffer for reading data until a valid message is read. Should be rarely used.
00083         uint8_t m_readBuffer[CMT_DEFAULT_READ_BUFFER_SIZE];
00084                 //! The number of valid bytes in the readBuffer.
00085         uint16_t m_readBufferCount;
00086                 //! Timeout in ms for blocking operations
00087         uint32_t m_timeout;
00088                 //! The timestamp at which to end an operation
00089         uint32_t m_toEnd;
00090 public:
00091                 //! Default constructor, initialize all members to their default values.
00092         Cmt2s();
00093 
00094                 //! Destructor, de-initialize, free memory allocated for buffers, etc.
00095         ~Cmt2s();
00096 
00097                 //! Close the serial communication port.
00098         XsensResultValue close(void);
00099 
00100                 //! Return the baudrate that is currently being used by the port
00101         uint32_t getBaudrate(void) { return (m_baudrate = m_cmt1s.getBaudrate()); }
00102         /*! \brief Return a reference to the embedded Cmt1s object.
00103         
00104                 Any manipulation of the object should be done through Cmt2s. Cmt2s integrity is 
00105                 not guaranteed if the Cmt1s object is manipulated directly.
00106         */
00107         Cmt1s* getCmt1s(void) { return &m_cmt1s; }
00108                 //! Return the error code of the last operation.
00109         XsensResultValue getLastResult(void) const { return m_lastResult; }
00110                 //! Retrieve the port that the object is connected to.
00111         XsensResultValue getPortNr(uint16_t& port) const;
00112         XsensResultValue getPortNr(int32_t& port) const;
00113         XsensResultValue getPortName(char *portname) const;
00114                 //! Return the current timeout value in ms.
00115         uint32_t getTimeout(void) const { return m_timeout; }
00116                 //! Return whether the communication port is open or not.
00117         bool isOpen (void) const { return (m_cmt1s.isOpen()); }
00118                 //! Open a communication channel to the given serial port name.
00119         XsensResultValue open(const char *portName, 
00120                                                         const uint32_t baudRate = CMT_DEFAULT_BAUD_RATE,
00121                                                         uint32_t readBufSize = CMT_DEFAULT_READ_BUFFER_SIZE,
00122                                                         uint32_t writeBufSize = CMT_DEFAULT_WRITE_BUFFER_SIZE);
00123 #ifdef _WIN32
00124                 //! Open a communication channel to the given COM port number.
00125         XsensResultValue open(const uint32_t portNumber,
00126                                                         const uint32_t baudRate = CMT_DEFAULT_BAUD_RATE,
00127                                                         uint32_t readBufSize = CMT_DEFAULT_READ_BUFFER_SIZE,
00128                                                         uint32_t writeBufSize = CMT_DEFAULT_WRITE_BUFFER_SIZE);
00129 #endif        
00130         /*! \brief Read a message from the COM port.
00131         
00132                 The function reads data from the embedded Cmt1s object. The data is then converted
00133                 into a Message object.
00134                 If an error occurred, a NULL pointer is returned and the error code can be
00135                 retrieved with getLastError().
00136         */
00137         XsensResultValue readMessage(Message* rcv);
00138 
00139         //! Set the callback function for when a message has been received or sent
00140         XsensResultValue setCallbackFunction(CmtCallbackType tp, int32_t instance, CmtCallbackFunction func, void* param);
00141 
00142         /*! \brief Set the default timeout value to use in blocking operations.
00143 
00144                 This function sets the level 2 timeout value. The L1 value is set to half the given
00145                 timeout value.
00146         */
00147         XsensResultValue setTimeout(const uint32_t ms = CMT2_DEFAULT_TIMEOUT);
00148 
00149         /*! \brief Wait for a message to arrive.
00150         
00151                 The function waits for a message to arrive or until a timeout occurs.
00152                 If the msgId parameter is set to a value other than 0, the function will wait
00153                 until a message has arrived with that particular msgId.
00154                 
00155                 \note msgId 0 is the ReqDeviceID MID, but that is an outgoing only message. It is
00156                 illogical to wait for a message that will never be sent to the host. So 0 is a
00157                 safe value for the 'all' messages option.
00158                 
00159                 \note If an error message is received, the contents are stored in the m_lastResult
00160                 field and a NULL value is returned immediately.
00161         */
00162         XsensResultValue waitForMessage(Message* rcv, const uint8_t msgId, uint32_t timeoutOverride, bool acceptErrorMessage);
00163         /*! \brief Send a message over the COM port.
00164         
00165                 The function attempts to write the message over the connected COM port.
00166                 \param msg        The message to send.
00167         */
00168         XsensResultValue writeMessage(Message* msg);
00169 };
00170 
00171 //////////////////////////////////////////////////////////////////////////////////////////
00172 ///////////////////////////////////////// Cmt2f  /////////////////////////////////////////
00173 //////////////////////////////////////////////////////////////////////////////////////////
00174 
00175 /*! \brief The mid-level file communication class.
00176 
00177         The class uses CMT level 1, but does not inherit from it. If software needs to access 
00178         the level 1 component, it needs to be done through the getCmt1f() function.
00179 */
00180 class Cmt2f {
00181 private:
00182                 //! This object cannot be copied, so this function is not implemented.
00183         Cmt2f(const Cmt2f& ref);
00184 
00185 protected:
00186                 //! The Cmt1f object that is used for the low-level operations
00187         Cmt1f m_cmt1f;
00188                 //! The last result of an operation
00189         mutable XsensResultValue m_lastResult;
00190 
00191         bool m_readOnly;                                //!< When set to true, the file is read-only and attempts to write to it will fail
00192 
00193 public:
00194                 //! Default constructor
00195         Cmt2f();
00196                 //! Destructor.
00197         ~Cmt2f();
00198                 //! Close the file.
00199         XsensResultValue close(void);
00200                 //! Close the file and delete it.
00201         XsensResultValue closeAndDelete(void);
00202                 //! Create a new file with level 2 header
00203         XsensResultValue create(const char* filename);
00204                 //! Create a new file with level 2 header
00205         XsensResultValue create(const wchar_t* filename);
00206                 //! Get a reference to the embedded Cmt1f object
00207         Cmt1f* getCmt1f(void);
00208 
00209                 //! Return the error code of the last operation.
00210         XsensResultValue getLastResult(void) const;
00211                 //! Retrieve the filename that was last successfully opened.
00212         XsensResultValue getName(char* filename) const;
00213                 //! Retrieve the filename that was last successfully opened.
00214         XsensResultValue getName(wchar_t* filename) const;
00215                 //! Return whether the file is open or not.
00216         bool isOpen(void) const;
00217                 //! Open a file and read the header
00218         XsensResultValue open(const char* filename, const bool readOnly = false);
00219                 //! Open a file and read the header
00220         XsensResultValue open(const wchar_t* filename, const bool readOnly = false);
00221                 //! Read the next message from the file, when msgId is non-zero, the first matching message will be returned
00222         XsensResultValue readMessage(Message* msg, const uint8_t msgId = 0);
00223                 //! Get the current file size
00224         CmtFilePos getFileSize(void);
00225                 //! Get the current read position
00226         CmtFilePos getReadPosition(void);
00227                 //! Set the read position to the given position
00228         XsensResultValue setReadPosition(CmtFilePos pos);
00229                 //! Write a message to the end of the file
00230         XsensResultValue writeMessage(const Message* msg);
00231 };
00232 
00233 } // end of xsens namespace
00234 
00235 #endif        // _CMT2_H_2006_04_13
Generated on Sun May 8 08:05:57 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3