MOOS 0.2375
/home/toby/moos-ivp/MOOS-2375-Oct0611/Core/MOOSGenLib/MOOSSerialPort.h
Go to the documentation of this file.
00001 
00002 //
00003 //   MOOS - Mission Oriented Operating Suite
00004 //
00005 //   A suit of Applications and Libraries for Mobile Robotics Research
00006 //   Copyright (C) 2001-2005 Massachusetts Institute of Technology and
00007 //   Oxford University.
00008 //
00009 //   This software was written by Paul Newman at MIT 2001-2002 and Oxford
00010 //   University 2003-2005. email: pnewman@robots.ox.ac.uk.
00011 //
00012 //   This file is part of a  MOOS Core Component.
00013 //
00014 //   This program is free software; you can redistribute it and/or
00015 //   modify it under the terms of the GNU General Public License as
00016 //   published by the Free Software Foundation; either version 2 of the
00017 //   License, or (at your option) any later version.
00018 //
00019 //   This program is distributed in the hope that it will be useful,
00020 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
00021 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00022 //   General Public License for more details.
00023 //
00024 //   You should have received a copy of the GNU General Public License
00025 //   along with this program; if not, write to the Free Software
00026 //   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
00027 //   02111-1307, USA.
00028 //
00030 // MOOSSerialPort.h: interface for the CMOOSSerialPort class.
00031 //
00033 
00036 #if !defined(MOOSERIALPORTH)
00037 #define MOOSSERIALPORTH
00038 
00039 #if _MSC_VER > 1000
00040 #pragma once
00041 #endif // _MSC_VER > 1000
00042 
00043 #ifdef _WIN32
00044     #define DEFAULT_PORT  "COM1"
00045 #else
00046     #define DEFAULT_PORT  "/dev/ttyS0"
00047 #endif
00048 
00049 #define DEFAULT_BAUDRATE 9600
00050 #define TELEGRAM_LEN 1000
00051 
00052 #include "MOOSLock.h"
00053 
00054 #include <string>
00055 #include <list>
00056 //using namespace std;
00057 
00058 typedef std::list<std::string> STRING_LIST;
00059 
00061 
00065 class CMOOSSerialPort
00066 {
00067 public:
00068     std::string GetPortName();
00069     virtual bool Close();
00070     char GetTermCharacter();
00071     void SetTermCharacter(char cTermChar);
00072     int GetBaudRate(){return m_nBaudRate;};
00073 
00074     virtual int Flush(){return -1;};
00075 
00076     bool IsStreaming();
00077     bool IsVerbose(){return m_bVerbose;};
00078     bool GetLatest(std::string & sWhat,double & dfWhen);
00079 
00080     bool GetEarliest(std::string &sWhat, double &dfWhen);
00081 
00082 
00083     class CMOOSSerialTelegram
00084     {
00085     public:
00086         CMOOSSerialTelegram(const std::string & sWhat,double dfWhen):
00087           m_sTelegram(sWhat),m_dfTime(dfWhen){};
00088 
00089         std::string m_sTelegram;
00090         double m_dfTime;
00091     };
00092     typedef std::list<CMOOSSerialTelegram> TELEGRAM_LIST;
00093 
00094     TELEGRAM_LIST m_InBox;
00095     TELEGRAM_LIST m_OutBox;
00096     CMOOSLock m_InBoxLock;
00097     CMOOSLock m_OutBoxLock;
00098     CMOOSLock m_PortLock;
00099 
00100     bool CommsLoop();
00101 
00102     CMOOSSerialPort();
00103     virtual ~CMOOSSerialPort();
00104 
00105 
00106     virtual bool Configure(STRING_LIST sParams);
00107 
00108     //crucial functions...
00109     virtual bool Create(const char * pPortNum=DEFAULT_PORT, int nBaudRate=DEFAULT_BAUDRATE)=0;
00110     virtual int  ReadNWithTimeOut(char * pBuff, int  nBufferLen,double Timeout=0.5, double* pTime = NULL);
00111     virtual int  Write(const char* Str,int nLen, double* pTime=NULL)=0;
00112 
00113     //returns a complete telegram in sTelegram (block of terminated characters)
00114     bool GetTelegram(std::string &sTelegram,double dfTimeOut,double *pTime=NULL);
00115     
00120     bool GetTelegramOrAccumulate(std::string &sTelegram,double dfTimeOut,double *pTime=NULL);
00121     
00122     // This version of ReadNWithTimeOut returns a negative value to signal an error
00123     virtual int  ReadNWithTimeOut2(char * pBuff, int nBufferLen, double Timeout=0.5, double* pTime = NULL);
00124 
00125     void SetIsCompleteReplyCallBack(bool (*pfn)(char *pData, int nLen, int nRead) );
00126 
00127     virtual void Break();
00128 
00129 protected:
00130     //termination character the serial port is interested in
00131     char m_cTermCharacter;
00132 
00134 #ifdef _WIN32
00135     HANDLE m_hCommsThread;
00136 #endif
00137 
00138 
00139 #ifdef _WIN32
00140     typedef unsigned long THREAD_ID;
00141 #else
00142     typedef pthread_t THREAD_ID;
00143 #endif
00144 
00145 
00147     THREAD_ID        m_nCommsThreadID;
00148 
00149 
00150     bool StartThreads();
00151 
00152     bool m_bStreaming;
00153 
00154     bool m_bVerbose;
00155 
00156     virtual int GrabN(char * pBuffer,int nRequired)=0;
00157 
00158     bool (*m_pfnUserIsCompleteReplyCallBack)( char *pData, int nLen, int nRead);
00159 
00160     bool IsCompleteReply(char * pData,int nLen, int nRead);
00161 
00163        bool m_bHandShaking;
00164 
00166     std::string m_sPort;
00167 
00169     int m_nBaudRate;
00170 
00171     bool m_bQuit;
00172 
00174         bool m_bUseCsmExt;
00175 
00176 };
00177 
00178 #endif //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines