MOOS 0.2375
/home/toby/moos-ivp/MOOS-2375-Oct0611/Core/MOOSGenLib/MOOSNTSerialPort.cpp
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 
00031 #include "MOOSGenLibGlobalHelper.h"
00032 #include "MOOSNTSerialPort.h"
00033 #include <iostream>
00034 using namespace std;
00035 #include <assert.h>
00036 
00037 
00038 CMOOSNTSerialPort::CMOOSNTSerialPort()
00039 {
00040     m_pfnUserIsCompleteReplyCallBack = NULL;
00041 }
00042 
00043 
00044 bool CMOOSNTSerialPort::Create(const char * pPortNum, 
00045                                int nBaudRate)
00046 {
00047 
00048     LONG    lLastError = ERROR_SUCCESS;
00049 
00050     // Attempt to open the serial port (COM1)
00051     lLastError = CNTSerial::Open(pPortNum);
00052     
00053     if (lLastError != ERROR_SUCCESS)
00054     {
00055         MOOSTrace("Unable to open COM-port\n");
00056         return false;
00057     }
00058     
00059     EBaudrate eBaud;
00060     
00061     
00062     // ARH 14/05/2005
00063     // added check for CSM PCMCIA serial card
00064     if (!m_bUseCsmExt)
00065     {
00066         // Standard Serial port
00067         switch(nBaudRate)
00068         {
00069         case 500000:    eBaud=CNTSerial::EBaud500000;    break;
00070         case 256000:    eBaud=CNTSerial::EBaud256000;    break;
00071         case 128000:    eBaud=CNTSerial::EBaud128000;    break;
00072         case 115200:    eBaud=CNTSerial::EBaud115200;    break;
00073         case 57600:     eBaud=CNTSerial::EBaud57600;     break;
00074         case 38400:     eBaud=CNTSerial::EBaud38400;       break;
00075         case 19200:     eBaud=CNTSerial::EBaud19200;       break;
00076         case 9600:      eBaud=CNTSerial::EBaud9600;        break;
00077         case 4800:        eBaud=CNTSerial::EBaud4800;       break;
00078         case 2400:        eBaud=CNTSerial::EBaud2400;       break;
00079         case 1200:        eBaud=CNTSerial::EBaud1200;       break;
00080         case 600:        eBaud=CNTSerial::EBaud600;       break;
00081         case 300:        eBaud=CNTSerial::EBaud300;       break;
00082             
00083         default :   
00084             printf("unsupported baud rate\n");
00085             return false;
00086             break;
00087             
00088         }
00089     }
00090     else
00091     {
00092         // ARH 14/05/2005
00093         // CSM PCMCIA serial card, with non-standard clock
00094         switch(nBaudRate)
00095         {
00096         case 500000: eBaud=CNTSerial::EBaudCSM500000; break;
00097         case 38400:  eBaud=CNTSerial::EBaudCSM38400;  break;
00098         case 19200:  eBaud=CNTSerial::EBaudCSM19200;  break;
00099         case 9600:   eBaud=CNTSerial::EBaudCSM9600;   break;
00100             
00101         default :   
00102             printf("unsupported baud rate\n");
00103             return false;
00104             break;
00105         }
00106     }
00107 
00108     // Setup the serial port (9600,N81) using hardware handshaking
00109     lLastError = CNTSerial::Setup(  eBaud,
00110                                     CNTSerial::EData8,
00111                                     CNTSerial::EParNone,
00112                                     CNTSerial::EStop1);
00113 
00114     if (lLastError != ERROR_SUCCESS)
00115     {
00116         printf("Unable to set COM-port setting\n");
00117     }
00118 
00119     CNTSerial::EHandshake eHS = m_bHandShaking ? 
00120                                 CNTSerial::EHandshakeHardware :
00121                                 CNTSerial::EHandshakeOff;
00122 
00123 
00124     lLastError = CNTSerial::SetupHandshaking(eHS);
00125 
00126     if (lLastError != ERROR_SUCCESS)
00127     {
00128         printf("Unable to set COM-port handshaking\n");
00129         return false;
00130     }
00131 
00132     SetupReadTimeouts(CNTSerial::EReadTimeoutNonblocking);
00133 
00134     return true;
00135 }
00136 
00137 
00138 int CMOOSNTSerialPort::Write(const char *pData, int nLen,double *pTime)
00139 {
00140     LONG    lLastError = ERROR_SUCCESS;
00141 
00142     DWORD WriteTimeOut = 2000;
00143 
00144     //lock the port
00145     m_PortLock.Lock();
00146         lLastError = CNTSerial::Write((void*)pData,nLen,0,0,WriteTimeOut);
00147     m_PortLock.UnLock();
00148     
00149     if (lLastError != ERROR_SUCCESS)
00150     {
00151         printf("Unable to send data\n");
00152         return -1;
00153     }
00154 
00155     return nLen;
00156 }
00157 
00158 
00159 int CMOOSNTSerialPort::GrabN(char * pBuffer,int nRequired)
00160 {
00161     unsigned long dwRes = 0;
00162 
00163     m_PortLock.Lock();
00164         CNTSerial::NTRead(pBuffer,nRequired,&dwRes);
00165     m_PortLock.UnLock();
00166     
00167     return (int)dwRes;
00168 
00169 }
00170 
00171 bool CMOOSNTSerialPort::Close()
00172 {
00173     if(CMOOSSerialPort::Close())
00174     {
00175         ClosePort();
00176         return true;
00177     }
00178     else
00179     {
00180         return false;
00181     }
00182 
00183 }
00184 
00185 
00186 void CMOOSNTSerialPort::Break()
00187 {
00188     SetCommBreak(m_hFile);
00189     MOOSPause(500);
00190     ClearCommBreak(m_hFile);
00191 }    
00192 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines