CommRS232.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. // CommRS232.cpp: implementation of the CCommRS232 class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "CommRS232.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. CCommRS232::CCommRS232() : CCommAsyn()
  15. {
  16. m_hCom = INVALID_HANDLE_VALUE;
  17. m_bOpen=FALSE;
  18. }
  19. CCommRS232::~CCommRS232()
  20. {
  21. if( m_hCom != INVALID_HANDLE_VALUE )
  22. {
  23. CloseHandle( m_hCom );
  24. m_hCom = INVALID_HANDLE_VALUE;
  25. }
  26. // sio_close(m_nPort);
  27. }
  28. BOOL CCommRS232::InitParam(PPORTPARAM pPortParam)
  29. {
  30. ASSERT(pPortParam!=NULL);
  31. if(pPortParam==NULL)
  32. {
  33. return FALSE;
  34. }
  35. if( !CCommAsyn::InitParam(pPortParam) )
  36. {
  37. return FALSE;
  38. }
  39. if( !InitComm(pPortParam) )
  40. {
  41. return FALSE;
  42. }
  43. return TRUE;
  44. }
  45. BOOL CCommRS232::InitComm(PPORTPARAM pPortParam)
  46. {
  47. m_bOpen=FALSE;
  48. ASSERT(pPortParam!=NULL);
  49. if(pPortParam==NULL)
  50. return FALSE;
  51. COMMTIMEOUTS timeout;
  52. DCB dcb;
  53. BOOL result;
  54. int temp=0;
  55. char tmp[20]="\\\\.\\com";
  56. char buffer[20];
  57. m_nPort=pPortParam->PortNo;
  58. _itoa(m_nPort,buffer,10);
  59. strcat(tmp,buffer);
  60. m_hCom = CreateFile(tmp, GENERIC_READ | GENERIC_WRITE,
  61. 0, NULL, OPEN_EXISTING, NULL , NULL);
  62. if(m_hCom==INVALID_HANDLE_VALUE)
  63. {
  64. //AfxMessageBox( "¶Ë¿Ú²»´æÔÚ»òÒÑ»µ£¡" , MB_OK , 0 );
  65. // ErrorProcess();
  66. return FALSE;
  67. }
  68. result=SetupComm(m_hCom,1024,1024);
  69. ASSERT(result);
  70. if(!result)
  71. return FALSE;
  72. result=PurgeComm(m_hCom,PURGE_TXCLEAR|PURGE_RXCLEAR);
  73. ASSERT(result);
  74. if(!result)
  75. return FALSE;
  76. result=GetCommState(m_hCom, &dcb);
  77. ASSERT(result);
  78. if(!result)
  79. return FALSE;
  80. dcb.Parity=pPortParam->Parity;
  81. if(dcb.Parity==NOPARITY)
  82. dcb.fParity=FALSE;
  83. else
  84. dcb.fParity=TRUE;
  85. dcb.BaudRate=pPortParam->BaudRate;
  86. dcb.ByteSize=pPortParam->ByteSize;
  87. dcb.StopBits=pPortParam->StopBits;
  88. if(dcb.ByteSize==8)
  89. dcb.StopBits=ONESTOPBIT;
  90. result=SetCommState(m_hCom, &dcb);
  91. ASSERT(result);
  92. if(!result)
  93. return FALSE;
  94. result=GetCommTimeouts(m_hCom, &timeout);
  95. ASSERT(result);
  96. if(!result)
  97. return FALSE;
  98. timeout.ReadIntervalTimeout=MAXDWORD;
  99. timeout.ReadTotalTimeoutMultiplier=0;
  100. timeout.ReadTotalTimeoutConstant=0;
  101. timeout.WriteTotalTimeoutMultiplier=0;
  102. timeout.WriteTotalTimeoutConstant=0;
  103. result=SetCommTimeouts(m_hCom,&timeout);
  104. ASSERT(result);
  105. if(!result)
  106. return FALSE;
  107. m_bOpen=TRUE;
  108. return TRUE;
  109. }
  110. BOOL CCommRS232::CloseComm()
  111. {
  112. m_bOpen=FALSE;
  113. if(m_hCom!=INVALID_HANDLE_VALUE)
  114. {
  115. BOOL result=CloseHandle(m_hCom);
  116. if(!result)
  117. return FALSE;
  118. }
  119. return TRUE;
  120. }
  121. int CCommRS232::Read(BYTE *pBuf, int len)
  122. {
  123. DWORD nTick = GetTickCount();
  124. if(m_hCom == INVALID_HANDLE_VALUE)
  125. {
  126. return 0;
  127. }
  128. if( pBuf == NULL || !::AfxIsValidAddress(pBuf, len, FALSE ) )
  129. {
  130. return 0;
  131. }
  132. if( !m_bOpen )
  133. {
  134. return 0;
  135. }
  136. BOOL bReadStatus;
  137. DWORD dwBytesRead, dwErrorFlags;
  138. COMSTAT ComStat;
  139. ClearCommError( m_hCom, &dwErrorFlags, &ComStat );
  140. while( ComStat.cbInQue != len )
  141. {
  142. if( GetTickCount() - nTick > 3000 ) break;
  143. ClearCommError( m_hCom, &dwErrorFlags, &ComStat );
  144. Sleep(100);
  145. }
  146. //if( !ComStat.cbInQue ) return( 0 );
  147. dwBytesRead = (DWORD) ComStat.cbInQue;
  148. if( len < (int) dwBytesRead ) dwBytesRead = (DWORD) len;
  149. bReadStatus = ReadFile( m_hCom, pBuf, dwBytesRead, &dwBytesRead , NULL);
  150. if( !bReadStatus )
  151. {
  152. DWORD dwError;
  153. dwError = GetLastError();
  154. CString strMsg;
  155. strMsg.Format("ReadFile Error! ErrorCode = %d", dwError);
  156. //AfxMessageBox(strMsg);
  157. PurgeComm(m_hCom, PURGE_TXABORT|
  158. PURGE_RXABORT|PURGE_TXCLEAR|PURGE_RXCLEAR);
  159. return 0;
  160. }
  161. PurgeComm(m_hCom, PURGE_TXABORT|
  162. PURGE_RXABORT|PURGE_TXCLEAR|PURGE_RXCLEAR);
  163. return( (int) dwBytesRead );
  164. }
  165. int CCommRS232::Write(BYTE *pBuf, int len)
  166. {
  167. if( !m_bOpen )
  168. {
  169. return 0;
  170. }
  171. if(m_hCom == INVALID_HANDLE_VALUE || len<=0)
  172. {
  173. return 0;
  174. }
  175. DWORD dwResult = 0;
  176. BOOL bResult=WriteFile( m_hCom, pBuf, (DWORD)len, &dwResult, NULL );
  177. if(bResult)
  178. {
  179. return dwResult;
  180. }
  181. else
  182. {
  183. return 0;
  184. }
  185. }