CommRS232.cpp 4.0 KB

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