CommRS232.cpp 4.6 KB

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