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