CommRS232.cpp 3.8 KB

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