Serial.cpp
上传用户:pumpssky
上传日期:2007-12-07
资源大小:110k
文件大小:7k
源码类别:

MacOS编程

开发平台:

C/C++

  1. // Serial.cpp
  2. #include "stdafx.h"
  3. #include "Serial.h"
  4. #include "Gps.h"
  5. #include "Winbase.h"
  6. CSerial::CSerial()
  7. {
  8. m_hIDComDev = NULL;
  9. m_bOpened = FALSE;
  10. m_bNoRead = FALSE;
  11. }
  12. CSerial::~CSerial()
  13. if(m_hIDComDev != INVALID_HANDLE_VALUE)
  14.        Close();
  15. }
  16. BOOL CSerial::Open( int nPort, int nBaud)    //open serial port
  17. {   
  18. DWORD dwError,dwThreadID;
  19. if( m_bOpened ) 
  20. return( TRUE );
  21. TCHAR szPort[15];
  22. DCB dcb;
  23. wsprintf( szPort, _T("COM%d:"), nPort );
  24. //open serial device
  25. m_hIDComDev = CreateFile( szPort, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL );
  26. if( m_hIDComDev ==INVALID_HANDLE_VALUE )
  27. {
  28. MessageBox (NULL, TEXT("unable to create serial device"), TEXT("Error"), MB_OK);
  29. return( FALSE );
  30.     }
  31. //configure timeout
  32. COMMTIMEOUTS CommTimeOuts;
  33.     GetCommTimeouts (m_hIDComDev, &CommTimeOuts);
  34.     CommTimeOuts.ReadIntervalTimeout = MAXDWORD;
  35. CommTimeOuts.ReadTotalTimeoutMultiplier = 0;
  36. CommTimeOuts.ReadTotalTimeoutConstant = 400;
  37. CommTimeOuts.WriteTotalTimeoutMultiplier = 0;
  38. CommTimeOuts.WriteTotalTimeoutConstant = 5000;
  39.     
  40. if (!SetCommTimeouts (m_hIDComDev, &CommTimeOuts))
  41. MessageBox (NULL, TEXT("Unable to set the time-out parameters"), TEXT("Error"), MB_OK);
  42. dwError = GetLastError ();
  43. return FALSE;
  44.     }
  45. //point port detect event
  46.     SetCommMask (m_hIDComDev, EV_RXCHAR);
  47. //configure serial device
  48.     
  49. dcb.DCBlength = sizeof( DCB );
  50. GetCommState( m_hIDComDev, &dcb );
  51. dcb.BaudRate = nBaud;
  52. dcb.ByteSize = 8;
  53.     dcb.Parity = NOPARITY;           
  54.     dcb.StopBits = ONESTOPBIT;
  55. //sleep to let dcb be effect
  56. Sleep(1000);
  57. if( !SetCommState( m_hIDComDev, &dcb ) ||
  58. !SetupComm( m_hIDComDev, COM_MAX_INBUFSIZE, COM_MAX_OUTBUFSIZE ))
  59. {
  60.         
  61. MessageBox (NULL, TEXT("Unable to configure the serial port"), TEXT("Error"), MB_OK);
  62. dwError = GetLastError();
  63. CloseHandle( m_hIDComDev );
  64. return( FALSE );
  65. }
  66. //Clear the input and output buffer
  67. PurgeComm(m_hIDComDev,PURGE_TXCLEAR|PURGE_RXCLEAR);
  68.        
  69. //create read port thread
  70. if( !m_bNoRead )
  71. {
  72. if (hReadThread = CreateThread (NULL, 0, ReadPortThread, this , 0, &dwThreadID))
  73. {
  74. // SetThreadPriority( hReadThread, THREAD_PRIORITY_HIGHEST );
  75. // CeSetThreadPriority(hReadThread, 252);
  76. CeSetThreadPriority(hReadThread, 0);
  77. // CeSetThreadQuantum(hReadThread, 0);
  78. ResumeThread( hReadThread );
  79. }
  80. else
  81. {
  82.    MessageBox (NULL, TEXT("Unable to create the read thread"), TEXT("Error"), MB_OK);
  83.    dwError = GetLastError ();
  84.    return FALSE;
  85. }
  86. }
  87. m_bOpened = TRUE;
  88. return( m_bOpened );
  89. }
  90. BOOL CSerial::Close( void )                   //close serial port
  91. {
  92. if( !m_bOpened || m_hIDComDev == NULL ) 
  93. return( TRUE );
  94. m_bOpened = FALSE;
  95. //end waiting for WaitCommEvent
  96.     SetCommMask(m_hIDComDev,0);
  97. //end read port thread
  98.       
  99. if(hReadThread)
  100.      {
  101.          TerminateThread(hReadThread,0);
  102.          CloseHandle(hReadThread);
  103.       }
  104.     
  105. EscapeCommFunction(m_hIDComDev,CLRDTR);
  106. PurgeComm(m_hIDComDev,PURGE_TXCLEAR|PURGE_RXCLEAR);
  107.     
  108. CloseHandle( m_hIDComDev );
  109. m_hIDComDev = NULL;
  110. return( TRUE );
  111. }
  112. BOOL CSerial::WriteCommByte( unsigned char ucByte )  //serial port sends data by byte
  113. {
  114. BOOL bWriteStat;
  115. DWORD dwBytesWritten;
  116. bWriteStat = WriteFile( m_hIDComDev, (LPSTR) &ucByte, 1, &dwBytesWritten, NULL);
  117. if( !bWriteStat && ( GetLastError() == ERROR_IO_PENDING ) )
  118. {   
  119. //MessageBox(NULL,TEXT("Can't Write String to Comm"),TEXT("Error"),MB_OK);
  120. dwBytesWritten = 0;
  121. }
  122. return( dwBytesWritten!=0 );
  123. }
  124. int CSerial::SendData( const char *buffer, int size )   //serial port sends data
  125. {
  126. if( !m_bOpened || m_hIDComDev == NULL ) return( 0 );
  127. DWORD dwBytesWritten = 0;
  128. #if 1
  129. int i;
  130. for( i=0; i<size; i++ )
  131. {
  132. if(WriteCommByte( buffer[i] ))
  133. {
  134. dwBytesWritten++;
  135. }
  136. }
  137. #else
  138. //write to serial port in one shot
  139. BOOL bWriteStat;
  140. bWriteStat = WriteFile( m_hIDComDev, (LPSTR) buffer, size, &dwBytesWritten, NULL);
  141. if( !bWriteStat && ( GetLastError() == ERROR_IO_PENDING ) )
  142. {   
  143. //MessageBox(NULL,TEXT("Can't Write String to Comm"),TEXT("Error"),MB_OK);
  144. dwBytesWritten = 0;
  145. }
  146. #endif
  147. return( (int) dwBytesWritten );
  148. }
  149. void CSerial::SetGps(CGps *pGps)
  150. {
  151. m_pGps = pGps;
  152. }
  153. #if 0
  154. DWORD WINAPI ReadPortThread(LPVOID lpvoid)  //Read Port Thread
  155. {
  156.     
  157. DWORD dwCommModemStatus;
  158. BOOL bReadStatus;
  159. DWORD dwBytesRead, dwErrorFlags;
  160. COMSTAT ComStat;
  161. CSerial * pSerial = (CSerial *)lpvoid;
  162.     
  163. while (pSerial->m_hIDComDev!=INVALID_HANDLE_VALUE)
  164. {
  165. Sleep(100);
  166. SetCommMask( pSerial->m_hIDComDev, EV_RXCHAR );
  167. WaitCommEvent( pSerial->m_hIDComDev, &dwCommModemStatus, 0 );
  168. if (dwCommModemStatus & EV_RXCHAR) 
  169. {
  170. ClearCommError( pSerial->m_hIDComDev, &dwErrorFlags, &ComStat );
  171. if( !ComStat.cbInQue ) continue;
  172. dwBytesRead = (DWORD) ComStat.cbInQue;
  173.  if(dwBytesRead>0)
  174.  {
  175. char *buf = new char[COM_MAX_INBUFSIZE+1];
  176. memset( buf, 0, COM_MAX_INBUFSIZE + 1 );
  177. bReadStatus=ReadFile(pSerial->m_hIDComDev,buf,dwBytesRead+100,&dwBytesRead,NULL);
  178. if(!bReadStatus)
  179. {
  180. //MessageBox(NULL,TEXT("Error in read from serial port"),TEXT("Read Error"),MB_OK);
  181. }
  182. else
  183. {
  184. pSerial->m_pGps->OnReceived( (unsigned char *)buf,(int)dwBytesRead );
  185. }
  186. }     
  187. GetCommModemStatus (pSerial->m_hIDComDev, &dwCommModemStatus);
  188. }
  189. else
  190. {
  191. //MessageBox(pSerial->m_pGps->GetViewHandle(), _T("Other event occured"), _T(""), MB_OK);
  192. }
  193. }
  194.  return 0;
  195. }
  196. #else
  197. DWORD WINAPI ReadPortThread(LPVOID lpvoid)  //Read Port Thread
  198. {
  199.     
  200. DWORD dwCommModemStatus;
  201. BOOL bReadStatus;
  202. DWORD dwBytesRead;
  203. CSerial * pSerial = (CSerial *)lpvoid;
  204.     
  205. while (pSerial->m_hIDComDev!=INVALID_HANDLE_VALUE)
  206. {
  207. //pSerial->m_pGps->m_pLog->Write("Before WaitCommEvent");
  208. WaitCommEvent( pSerial->m_hIDComDev, &dwCommModemStatus, 0 );
  209. SetCommMask( pSerial->m_hIDComDev, EV_RXCHAR );
  210. if (dwCommModemStatus & EV_RXCHAR) 
  211. {
  212. char *buf = new char[COM_MAX_INBUFSIZE+1];
  213. memset( buf, 0, COM_MAX_INBUFSIZE + 1 );
  214. //pSerial->m_pGps->m_pLog->Write("Before ReadFile");
  215. bReadStatus=ReadFile(pSerial->m_hIDComDev,buf,COM_MAX_INBUFSIZE,&dwBytesRead,NULL);
  216. //pSerial->m_pGps->m_pLog->Write("AfterReadFile");
  217. if( dwBytesRead > 0 )
  218. {
  219. pSerial->m_pGps->OnReceived( (unsigned char *)buf, dwBytesRead );
  220. }
  221. else
  222. {
  223. delete buf;
  224. }
  225. }
  226. }
  227.  return 0;
  228. }
  229. #endif