CommProcess.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. // CommProcess.cpp: implementation of the CCommProcess class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "CommProcess.h"
  6. #ifdef _DEBUG
  7. #undef THIS_FILE
  8. static char THIS_FILE[]=__FILE__;
  9. #define new DEBUG_NEW
  10. #endif
  11. //////////////////////////////////////////////////////////////////////
  12. // Construction/Destruction
  13. //////////////////////////////////////////////////////////////////////
  14. CCommProcess::CCommProcess()
  15. {
  16. m_hCom = INVALID_HANDLE_VALUE;
  17. m_bOpen=FALSE;
  18. }
  19. CCommProcess::~CCommProcess()
  20. {
  21. }
  22. BOOL CCommProcess::OpenComm(PPORTPARAM pPortParam)
  23. {
  24. m_bOpen=FALSE;
  25. ASSERT(pPortParam!=NULL);
  26. if(pPortParam==NULL)
  27. return FALSE;
  28. COMMTIMEOUTS timeout;
  29. DCB dcb;
  30. BOOL result;
  31. int temp=0;
  32. char tmp[20]="\\\\.\\com";
  33. char buffer[20];
  34. m_nPort=pPortParam->PortNo;
  35. _itoa(m_nPort,buffer,10);
  36. strcat(tmp,buffer);
  37. m_hCom = CreateFile(tmp, GENERIC_READ | GENERIC_WRITE,
  38. 0, NULL, OPEN_EXISTING, NULL , NULL);
  39. int ilastEr = GetLastError();
  40. //LOG4C((LOG_NOTICE , "CCommProcess::OpenComm Com%d Er = %d",m_nPort,ilastEr));
  41. if(m_hCom==INVALID_HANDLE_VALUE)
  42. {
  43. //AfxMessageBox( "端口不存在或已坏!" , MB_OK , 0 );
  44. // ErrorProcess();
  45. LOG4C((LOG_NOTICE , "CCommProcess::端口不存在或已坏"));
  46. return FALSE;
  47. }
  48. result=SetupComm(m_hCom,1024,1024);
  49. ASSERT(result);
  50. if(!result)
  51. return FALSE;
  52. result=PurgeComm(m_hCom,PURGE_TXCLEAR|PURGE_RXCLEAR);
  53. ASSERT(result);
  54. if(!result)
  55. return FALSE;
  56. result=GetCommState(m_hCom, &dcb);
  57. ASSERT(result);
  58. if(!result)
  59. return FALSE;
  60. dcb.Parity=pPortParam->Parity;
  61. if(dcb.Parity==NOPARITY)
  62. dcb.fParity=FALSE;
  63. else
  64. dcb.fParity=TRUE;
  65. //ONESTOPBIT;
  66. //ONE5STOPBITS;
  67. //TWOSTOPBITS;
  68. dcb.BaudRate=pPortParam->BaudRate;
  69. dcb.ByteSize=pPortParam->ByteSize;
  70. dcb.StopBits=pPortParam->StopBits;
  71. if(dcb.ByteSize==8)
  72. dcb.StopBits=ONESTOPBIT;
  73. result=SetCommState(m_hCom, &dcb);
  74. ASSERT(result);
  75. if(!result)
  76. return FALSE;
  77. result=GetCommTimeouts(m_hCom, &timeout);
  78. ASSERT(result);
  79. if(!result)
  80. return FALSE;
  81. timeout.ReadIntervalTimeout=MAXDWORD;
  82. timeout.ReadTotalTimeoutMultiplier=0;
  83. timeout.ReadTotalTimeoutConstant=0;
  84. timeout.WriteTotalTimeoutMultiplier=0;
  85. timeout.WriteTotalTimeoutConstant=0;
  86. result=SetCommTimeouts(m_hCom,&timeout);
  87. ASSERT(result);
  88. if(!result)
  89. return FALSE;
  90. m_bOpen=TRUE;
  91. return TRUE;
  92. }
  93. BOOL CCommProcess::CloseComm()
  94. {
  95. m_bOpen=FALSE;
  96. if( m_hCom != INVALID_HANDLE_VALUE )
  97. {
  98. BOOL bResult = CloseHandle( m_hCom );
  99. if( !bResult )
  100. {
  101. return FALSE;
  102. }
  103. }
  104. m_hCom = INVALID_HANDLE_VALUE;
  105. return TRUE;
  106. }
  107. int CCommProcess::Read(BYTE *pBuf, int len)
  108. {
  109. DWORD nTick = GetTickCount();
  110. if(m_hCom == INVALID_HANDLE_VALUE)
  111. {
  112. return 0;
  113. }
  114. if( pBuf == NULL || !::AfxIsValidAddress(pBuf, len, FALSE ) )
  115. {
  116. return 0;
  117. }
  118. if( !m_bOpen )
  119. {
  120. return 0;
  121. }
  122. BOOL bReadStatus;
  123. DWORD dwBytesRead, dwErrorFlags;
  124. COMSTAT ComStat;
  125. ClearCommError( m_hCom, &dwErrorFlags, &ComStat );
  126. while( ComStat.cbInQue != len )
  127. {
  128. if( GetTickCount() - nTick > 3000 ) break;
  129. ClearCommError( m_hCom, &dwErrorFlags, &ComStat );
  130. Sleep(150);
  131. }
  132. //if( !ComStat.cbInQue ) return( 0 );
  133. dwBytesRead = (DWORD) ComStat.cbInQue;
  134. if( len < (int) dwBytesRead ) dwBytesRead = (DWORD) len;
  135. bReadStatus = ReadFile( m_hCom, pBuf, dwBytesRead, &dwBytesRead , NULL);
  136. if( !bReadStatus )
  137. {
  138. DWORD dwError;
  139. dwError = GetLastError();
  140. CString strMsg;
  141. strMsg.Format("ReadFile Error! ErrorCode = %d", dwError);
  142. PurgeComm(m_hCom, PURGE_TXABORT|
  143. PURGE_RXABORT|PURGE_TXCLEAR|PURGE_RXCLEAR);
  144. return 0;
  145. }
  146. PurgeComm(m_hCom, PURGE_TXABORT|
  147. PURGE_RXABORT|PURGE_TXCLEAR|PURGE_RXCLEAR);
  148. return( (int) dwBytesRead );
  149. }
  150. int CCommProcess::Write(BYTE *pBuf, int len)
  151. {
  152. if( !m_bOpen )
  153. {
  154. return 0;
  155. }
  156. if(m_hCom == INVALID_HANDLE_VALUE || len<=0)
  157. {
  158. return 0;
  159. }
  160. Sleep(100);
  161. DWORD dwResult = 0;
  162. BOOL bResult=WriteFile( m_hCom, pBuf, (DWORD)len, &dwResult, NULL );
  163. if(bResult)
  164. {
  165. return dwResult;
  166. }
  167. else
  168. {
  169. return 0;
  170. }
  171. }