For DOS Protected Mode version of sensor core software
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

757 lines
21 KiB

3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. //----------------------------------------------------------------------------
  2. // Laser Guard Ltd.
  3. // Copyright � 2000-2002. All Rights Reserved
  4. // Version 1.0 01/01/2000
  5. // Last modification: 08/10/2003
  6. // PROJECT:
  7. // SUBSYSTEM:
  8. // FILE: RS232.cpp
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file of Rs232.cpp
  13. //----------------------------------------------------------------------------
  14. #include "StdAfx.h"
  15. #include "..\Version.h"
  16. #include "RS232.h"
  17. CConnectionRS232::CConnectionRS232()
  18. {
  19. m_hCommPort = INVALID_HANDLE_VALUE;
  20. m_dwCommMask = 0;
  21. m_hEventQuit = NULL;
  22. ::ZeroMemory( &m_ovWrite, sizeof( OVERLAPPED ) );
  23. ::ZeroMemory( &m_ovWriteWait, sizeof( OVERLAPPED ) );
  24. ::ZeroMemory( &m_ovRead, sizeof( OVERLAPPED ) );
  25. ::ZeroMemory( &m_ovReadWait, sizeof( OVERLAPPED ) );
  26. }
  27. CConnectionRS232::~CConnectionRS232()
  28. {
  29. Destroy();
  30. }
  31. BOOL CConnectionRS232::Create( LPCTSTR pPort, INT nBaudRate, INT nBitCount, INT nParity, INT nStopBit, DWORD dwReadBufferSize, DWORD dwWriteBufferSize )
  32. {
  33. Destroy();
  34. m_CreateParams.dwReadBufferSize = dwReadBufferSize;
  35. m_CreateParams.dwWriteBufferSize = dwWriteBufferSize;
  36. m_CreateParams.nBaudRate = nBaudRate;
  37. m_CreateParams.nBitCount = nBitCount;
  38. m_CreateParams.nParity = nParity;
  39. m_CreateParams.nStopBit = nStopBit;
  40. sprintf(m_CreateParams.pPort,"\\\\.\\%s",pPort);
  41. //_tcscpy( m_CreateParams.pPort, pPort ); com<10
  42. m_hEventQuit = ::CreateEvent( NULL, FALSE, FALSE, NULL );
  43. if( !m_hEventQuit )
  44. return FALSE;
  45. m_ovReadWait.hEvent = ::CreateEvent( NULL, TRUE, FALSE, NULL );
  46. if( !m_ovReadWait.hEvent )
  47. return FALSE;
  48. m_ovWriteWait.hEvent = ::CreateEvent( NULL, TRUE, FALSE, NULL );
  49. if( !m_ovWriteWait.hEvent )
  50. return FALSE;
  51. m_ovRead.hEvent = ::CreateEvent( NULL, TRUE, FALSE, NULL );
  52. if( !m_ovRead.hEvent )
  53. return FALSE;
  54. m_ovWrite.hEvent = ::CreateEvent( NULL, TRUE, FALSE, NULL );
  55. if( !m_ovWrite.hEvent )
  56. return FALSE;
  57. return Create();
  58. }
  59. BOOL CConnectionRS232::Create()
  60. {
  61. // close previous communication port handle
  62. if( m_hCommPort != INVALID_HANDLE_VALUE )
  63. {
  64. ::CloseHandle( m_hCommPort );
  65. m_hCommPort = INVALID_HANDLE_VALUE;
  66. }
  67. // open new one
  68. m_hCommPort = ::CreateFile( m_CreateParams.pPort, GENERIC_READ | GENERIC_WRITE,
  69. 0UL, NULL, OPEN_EXISTING,
  70. FILE_FLAG_OVERLAPPED | FILE_FLAG_NO_BUFFERING, NULL );
  71. if( m_hCommPort == INVALID_HANDLE_VALUE )
  72. {
  73. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_WARNING, this, _T("CConnectionRS232::Create"), _T("CreateFile(%s) failed with 0x%08X"), m_CreateParams.pPort, GetLastError() );
  74. BYTE cnt = 10;
  75. while( cnt && m_hCommPort == INVALID_HANDLE_VALUE ){
  76. Sleep( 100 ); // 1/10th of sec
  77. // open new one
  78. m_hCommPort = ::CreateFile( m_CreateParams.pPort, GENERIC_READ | GENERIC_WRITE,
  79. 0UL, NULL, OPEN_EXISTING,
  80. FILE_FLAG_OVERLAPPED | FILE_FLAG_NO_BUFFERING, NULL );
  81. if( m_hCommPort == INVALID_HANDLE_VALUE ) LGLOG( LOG_LEVEL_LOW, LOG_TYPE_WARNING, this, _T("CConnectionRS232::Create"), _T("CreateFile(%s) failed with 0x%08X"), m_CreateParams.pPort, GetLastError() );
  82. cnt--;
  83. };
  84. if( m_hCommPort == INVALID_HANDLE_VALUE ) return FALSE;
  85. }
  86. DCB dcb;
  87. ::ZeroMemory( &dcb, sizeof( DCB ) );
  88. dcb.DCBlength = sizeof( DCB );
  89. ::GetCommState( m_hCommPort, &dcb );
  90. dcb.BaudRate = m_CreateParams.nBaudRate;
  91. dcb.ByteSize = m_CreateParams.nBitCount;
  92. dcb.Parity = m_CreateParams.nParity;
  93. dcb.StopBits = m_CreateParams.nStopBit;
  94. dcb.EvtChar = 0;
  95. dcb.fParity = ( m_CreateParams.nParity != NOPARITY );
  96. dcb.fOutxCtsFlow = FALSE;
  97. dcb.fOutxDsrFlow = FALSE;
  98. dcb.fDtrControl = DTR_CONTROL_DISABLE;
  99. dcb.fDsrSensitivity = FALSE;
  100. dcb.fOutX = FALSE;
  101. dcb.fInX = FALSE;
  102. dcb.fErrorChar = FALSE;
  103. dcb.fNull = FALSE;
  104. // dcb.fRtsControl = RTS_CONTROL_DISABLE;
  105. dcb.fRtsControl = RTS_CONTROL_ENABLE;
  106. // dcb.fRtsControl = RTS_CONTROL_TOGGLE; // specially for PROMAG USB->RS485 converter
  107. dcb.fAbortOnError = FALSE;
  108. dcb.fBinary = TRUE;
  109. dcb.fTXContinueOnXoff = TRUE;
  110. // setup communication port buffers
  111. if( !::SetupComm( m_hCommPort, m_CreateParams.dwReadBufferSize, m_CreateParams.dwWriteBufferSize ) )
  112. {
  113. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CConnectionRS232::Create"), _T("SetupComm failed with 0x%08X"), GetLastError() );
  114. return FALSE;
  115. }
  116. // setup communication port parameters
  117. if( !::SetCommState( m_hCommPort, &dcb ) )
  118. {
  119. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CConnectionRS232::Create"), _T("SetCommState failed with 0x%08X"), GetLastError() );
  120. return FALSE;
  121. }
  122. COMMTIMEOUTS ct;
  123. ct.ReadIntervalTimeout = 0UL;
  124. ct.ReadTotalTimeoutMultiplier = 0UL;
  125. ct.ReadTotalTimeoutConstant = 0UL;
  126. ct.WriteTotalTimeoutMultiplier = 0UL;
  127. ct.WriteTotalTimeoutConstant = 0UL;
  128. // setup communication port timeouts
  129. if( !::SetCommTimeouts( m_hCommPort, &ct ) )
  130. {
  131. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CConnectionRS232::Create"), _T("SetCommTimeouts failed with 0x%08X"), GetLastError() );
  132. return FALSE;
  133. }
  134. // clear communication port
  135. ::PurgeComm( m_hCommPort, PURGE_TXCLEAR | PURGE_RXCLEAR | PURGE_TXABORT | PURGE_RXABORT );
  136. // setup communication port timeouts
  137. // ::EscapeCommFunction( m_hCommPort, SETRTS );
  138. return TRUE;
  139. }
  140. void CConnectionRS232::Destroy(){
  141. BOOL RetVal;
  142. BYTE cnt;
  143. if( m_hEventQuit ){
  144. ::CloseHandle( m_hEventQuit );
  145. m_hEventQuit = NULL;
  146. };
  147. if( m_ovReadWait.hEvent ){
  148. ::CloseHandle( m_ovReadWait.hEvent );
  149. m_ovReadWait.hEvent = NULL;
  150. };
  151. if( m_ovWriteWait.hEvent ){
  152. ::CloseHandle( m_ovWriteWait.hEvent );
  153. m_ovWriteWait.hEvent = NULL;
  154. };
  155. if( m_ovWrite.hEvent ){
  156. ::CloseHandle( m_ovWrite.hEvent );
  157. m_ovWrite.hEvent = NULL;
  158. };
  159. if( m_ovRead.hEvent ){
  160. ::CloseHandle( m_ovRead.hEvent );
  161. m_ovRead.hEvent = NULL;
  162. };
  163. Flush();
  164. if( m_hCommPort != INVALID_HANDLE_VALUE ){
  165. ::PurgeComm( m_hCommPort, PURGE_TXCLEAR | PURGE_RXCLEAR | PURGE_TXABORT | PURGE_RXABORT );
  166. RetVal = ::CloseHandle( m_hCommPort );
  167. cnt = 10; // 10 tryes
  168. while( cnt && RetVal==0 ) {
  169. Sleep( 100 ); // 1/10th of sec.
  170. RetVal = ::CloseHandle( m_hCommPort );
  171. cnt--;
  172. };
  173. m_hCommPort = INVALID_HANDLE_VALUE;
  174. };
  175. }
  176. BOOL CConnectionRS232::SetCommState( LPDCB pDCB )
  177. {
  178. return ::SetCommState( m_hCommPort, pDCB );
  179. }
  180. BOOL CConnectionRS232::GetCommState( LPDCB pDCB )
  181. {
  182. return ::GetCommState( m_hCommPort, pDCB );
  183. }
  184. BOOL CConnectionRS232::ClearCommError( LPDWORD lpErrors, LPCOMSTAT lpStat )
  185. {
  186. return ::ClearCommError( m_hCommPort, lpErrors, lpStat );
  187. }
  188. DWORD CConnectionRS232::Read( PBYTE pBuffer, DWORD dwSize, DWORD dwTimeout )
  189. {
  190. return CommRead( pBuffer, dwSize, dwTimeout );
  191. }
  192. DWORD CConnectionRS232::Write( PBYTE pBuffer, DWORD dwSize, DWORD dwTimeout )
  193. {
  194. return CommWrite( pBuffer, dwSize, dwTimeout );
  195. }
  196. BOOL CConnectionRS232::SetRTS( BOOL bOn )
  197. {
  198. ::EscapeCommFunction( m_hCommPort, bOn ? SETRTS : CLRRTS );
  199. return TRUE;
  200. }
  201. BOOL CConnectionRS232::SetDTR( BOOL bOn )
  202. {
  203. ::EscapeCommFunction( m_hCommPort, bOn ? SETDTR : CLRDTR );
  204. return TRUE;
  205. }
  206. DWORD CConnectionRS232::CommRead( PBYTE pBuffer, DWORD dwSize, DWORD dwTimeout )
  207. {
  208. __try
  209. {
  210. // used for retrieve port information
  211. COMSTAT comstat;
  212. // placeholder for result
  213. DWORD dwRead = 0UL;
  214. // event array useable in Wait... functions
  215. HANDLE hEventsWait[] = { m_hEventQuit, m_ovReadWait.hEvent };
  216. HANDLE hEventsRead[] = { m_hEventQuit, m_ovRead.hEvent };
  217. m_ovReadWait.Internal = m_ovReadWait.InternalHigh = m_ovReadWait.Offset = m_ovReadWait.OffsetHigh = 0;
  218. m_ovRead.Internal = m_ovRead.InternalHigh = m_ovRead.Offset = m_ovRead.OffsetHigh = 0;
  219. // if previous operation was not read then re-set event mask
  220. if( m_dwCommMask != EV_RXCHAR )
  221. ::SetCommMask( m_hCommPort, m_dwCommMask = EV_RXCHAR );
  222. // remember read start time
  223. DWORD dwStartTime = ::GetTickCount();
  224. while( TRUE )
  225. {
  226. DWORD dwErrors;
  227. // check communication port for errors
  228. if( ::ClearCommError( m_hCommPort, &dwErrors, &comstat ) )
  229. {
  230. if( dwErrors & CE_RXOVER )
  231. ::PurgeComm( m_hCommPort, PURGE_RXCLEAR );
  232. if( dwErrors & CE_BREAK )
  233. ::ClearCommBreak( m_hCommPort );
  234. }
  235. else
  236. {
  237. // log error
  238. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CConnectionRS232::CommRead"), _T("ClearCommError failed") );
  239. // recreate commport and continue reading
  240. Create();
  241. }
  242. // calculate time eplaced since start of read
  243. DWORD dwSubTime = ::GetTickCount() - dwStartTime;
  244. // check amount of bytes in input buffer and remaining time
  245. if( (comstat.cbInQue < dwSize) && (dwSubTime < dwTimeout) )
  246. {
  247. DWORD dwEvtMask = 0UL;
  248. DWORD dwWaitResult;
  249. // wait for event EV_RXCHAR
  250. if( !::WaitCommEvent( m_hCommPort, &dwEvtMask, &m_ovReadWait ) )
  251. {
  252. DWORD dwError = ::GetLastError();
  253. switch( dwError )
  254. {
  255. case ERROR_IO_PENDING:
  256. // wait for overlapped completion
  257. switch( ::WaitForMultipleObjects( 2, hEventsWait, FALSE, dwTimeout - dwSubTime ) )
  258. {
  259. // request to abort
  260. case WAIT_OBJECT_0 + 0:
  261. // abort waiting
  262. ::CancelIo( m_hCommPort );
  263. // check result (nessessary by default)
  264. ::GetOverlappedResult( m_hCommPort, &m_ovReadWait, &dwWaitResult, FALSE );
  265. goto READ_ABORT;
  266. // timeout eplaced but event did not received
  267. case WAIT_TIMEOUT:
  268. ::CancelIo( m_hCommPort );
  269. // event received (or timeout eplaced - see above)
  270. case WAIT_OBJECT_0 + 1:
  271. ::GetOverlappedResult( m_hCommPort, &m_ovReadWait, &dwWaitResult, FALSE );
  272. break;
  273. }
  274. break;
  275. default:
  276. // log error
  277. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CConnectionRS232::CommRead"), _T("WaitCommEvent failed with 0x%08X"), dwError );
  278. // recreate commport and continue reading
  279. Create();
  280. break;
  281. }
  282. }
  283. }
  284. else break;
  285. }
  286. // actual data reading (from internal buffers)
  287. if( !::ReadFile( m_hCommPort, pBuffer, min( dwSize, comstat.cbInQue), &dwRead, &m_ovRead ) )
  288. {
  289. DWORD dwError = ::GetLastError();
  290. switch( dwError )
  291. {
  292. case ERROR_IO_PENDING:
  293. // wait for overlapped completion
  294. switch( ::WaitForMultipleObjects( 2, hEventsRead, FALSE, 0 ) )
  295. {
  296. // request to abort
  297. case WAIT_OBJECT_0 + 0:
  298. // stop read operation
  299. ::PurgeComm( m_hCommPort, PURGE_RXABORT );
  300. // abort reading
  301. ::CancelIo( m_hCommPort );
  302. // check result for receive number of bytes have readed to this time
  303. ::GetOverlappedResult( m_hCommPort, &m_ovRead, &dwRead, FALSE );
  304. goto READ_ABORT;
  305. // timeout eplaced but event did not received
  306. case WAIT_TIMEOUT:
  307. ::CancelIo( m_hCommPort );
  308. // event received (or timeout eplaced - see above)
  309. case WAIT_OBJECT_0 + 1:
  310. // check result of operation
  311. if( !::GetOverlappedResult( m_hCommPort, &m_ovRead, &dwRead, FALSE ) )
  312. {
  313. DWORD dwError = ::GetLastError();
  314. switch( dwError )
  315. {
  316. // stop read operation
  317. case ERROR_IO_INCOMPLETE:
  318. ::PurgeComm( m_hCommPort, PURGE_RXABORT );
  319. break;
  320. // operation was aborted by something
  321. case ERROR_OPERATION_ABORTED:
  322. break;
  323. }
  324. }
  325. break;
  326. }
  327. break;
  328. default:
  329. // log error
  330. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CConnectionRS232::CommRead"), _T("ReadFile failed with 0x%08X"), dwError );
  331. // recreate commport and continue reading
  332. Create();
  333. break;
  334. }
  335. }
  336. READ_ABORT:
  337. return dwRead;
  338. }
  339. __except( EXCEPTION_EXECUTE_HANDLER )
  340. {
  341. // log error
  342. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CConnectionRS232::CommRead"), _T("Exception 0x%08X"), _exception_code() );
  343. }
  344. return 0;
  345. }
  346. DWORD CConnectionRS232::CommWrite( PBYTE pBuffer, DWORD dwSize, DWORD dwTimeout )
  347. /*
  348. {
  349. __try
  350. {
  351. // used for retrieve port information
  352. COMSTAT comstat;
  353. DWORD dwErrors;
  354. // placeholder for result
  355. DWORD dwWrite = 0UL;
  356. // placeholder for event mask
  357. DWORD dwEvtMask = 0UL;
  358. // event array useable in Wait... functions
  359. // HANDLE hEventsWait[] = { m_hEventQuit, m_ovWriteWait.hEvent };
  360. // HANDLE hEventsWrite[] = { m_hEventQuit, m_ovWrite.hEvent };
  361. m_ovWriteWait.Internal = m_ovWriteWait.InternalHigh = m_ovWriteWait.Offset = m_ovWriteWait.OffsetHigh = 0;
  362. m_ovWrite.Internal = m_ovWrite.InternalHigh = m_ovWrite.Offset = m_ovWrite.OffsetHigh = 0;
  363. // check communication port for errors
  364. if( ::ClearCommError( m_hCommPort, &dwErrors, &comstat ) )
  365. {
  366. if( dwErrors & CE_RXOVER ) ::PurgeComm( m_hCommPort, PURGE_RXCLEAR );
  367. if( dwErrors & CE_BREAK ) ::ClearCommBreak( m_hCommPort );
  368. }
  369. else
  370. {
  371. // recreate commport and continue reading
  372. Create();
  373. }
  374. // if previous operation was not Write then re-set event mask
  375. if( m_dwCommMask != EV_TXEMPTY )
  376. {
  377. m_dwCommMask = EV_TXEMPTY;
  378. ::SetCommMask( m_hCommPort, EV_TXEMPTY );
  379. }
  380. DWORD dwStartTime = ::GetTickCount();
  381. DWORD dwWaitTime;
  382. DWORD dwEplacedTime;
  383. BOOL bWaitEstablished = FALSE;
  384. while( !bWaitEstablished )
  385. {
  386. // wait for event EV_TXEMPTY
  387. if( !::WaitCommEvent( m_hCommPort, &dwEvtMask, &m_ovWriteWait ) )
  388. {
  389. DWORD dwError = ::GetLastError();
  390. switch( dwError )
  391. {
  392. case ERROR_IO_PENDING:
  393. bWaitEstablished = TRUE;
  394. break;
  395. default:
  396. Create();
  397. break;
  398. }
  399. }
  400. // calc eplaced time
  401. dwEplacedTime = ::GetTickCount() - dwStartTime;
  402. if( dwEplacedTime > dwTimeout )
  403. break;
  404. }
  405. // can't begin write
  406. if( !bWaitEstablished )
  407. return 0;
  408. // actual data writing
  409. if( !::WriteFile( m_hCommPort, pBuffer, dwSize, &dwWrite, &m_ovWrite ) )
  410. {
  411. // HANDLE hEventsWait[] = { m_hEventQuit, m_ovWriteWait.hEvent };
  412. HANDLE hEventsWrite[] = { m_hEventQuit, m_ovWrite.hEvent };
  413. DWORD dwError = ::GetLastError();
  414. switch( dwError )
  415. {
  416. case ERROR_IO_PENDING:
  417. // store current time
  418. dwEplacedTime = GetTickCount() - dwStartTime;
  419. // maximum wait timeout
  420. dwWaitTime = ( dwTimeout >= dwEplacedTime ) ? dwTimeout - dwEplacedTime : 0UL;
  421. // wait for overlapped completion
  422. switch( ::WaitForMultipleObjects( 2, hEventsWrite, FALSE, dwWaitTime ) )
  423. {
  424. default:
  425. LGTRACE( "Write pending wait error\n" );
  426. break;
  427. // request to abort
  428. case WAIT_OBJECT_0 + 0:
  429. // stop write operation
  430. ::PurgeComm( m_hCommPort, PURGE_TXABORT | PURGE_TXCLEAR );
  431. // abort writing
  432. ::CancelIo( m_hCommPort );
  433. // check result for receive number of bytes have Writeed to this time
  434. ::GetOverlappedResult( m_hCommPort, &m_ovWrite, &dwWrite, FALSE );
  435. goto WRITE_ABORT;
  436. // timeout eplaced but event did not received
  437. case WAIT_TIMEOUT:
  438. LGTRACE( "Write timeout\n" );
  439. ::CancelIo( m_hCommPort );
  440. // event received (or timeout eplaced - see above)
  441. case WAIT_OBJECT_0 + 1:
  442. // check result of operation
  443. if( !::GetOverlappedResult( m_hCommPort, &m_ovWrite, &dwWrite, FALSE ) )
  444. {
  445. DWORD dwError = ::GetLastError();
  446. switch( dwError )
  447. {
  448. // stop write operation
  449. case ERROR_IO_INCOMPLETE:
  450. // stop write operation
  451. ::PurgeComm( m_hCommPort, PURGE_TXABORT | PURGE_TXCLEAR );
  452. // abort writing
  453. ::CancelIo( m_hCommPort );
  454. break;
  455. // operation was aborted by something
  456. case ERROR_OPERATION_ABORTED:
  457. break;
  458. }
  459. }
  460. break;
  461. }
  462. break;
  463. default:
  464. LGTRACE( "Error Write, %d (%p, %p, %p) ", GetLastError(), m_hEventQuit, hEventsWrite[0], hEventsWrite[1] );
  465. // recreate commport and continue writeing
  466. Create();
  467. break;
  468. }
  469. }
  470. WRITE_ABORT:
  471. // store current time
  472. dwEplacedTime = GetTickCount() - dwStartTime;
  473. // maximum wait timeout
  474. dwWaitTime = ( dwTimeout >= dwEplacedTime ) ? dwTimeout - dwEplacedTime : 0UL;
  475. DWORD dwWaitResult;
  476. HANDLE hEventsWait[] = { m_hEventQuit, m_ovWriteWait.hEvent };
  477. // HANDLE hEventsWrite[] = { m_hEventQuit, m_ovWrite.hEvent };
  478. // wait for end of writing
  479. switch( ::WaitForMultipleObjects( 2, hEventsWait, FALSE, dwWaitTime ) )
  480. {
  481. default:
  482. LGTRACE( "ErrorWait, %d (%p, %p, %p) ", GetLastError(), m_hEventQuit, hEventsWait[0], hEventsWait[1] );
  483. break;
  484. // request to abort
  485. case WAIT_OBJECT_0 + 0:
  486. // timeout eplaced but event did not received
  487. case WAIT_TIMEOUT:
  488. // abort waiting
  489. ::CancelIo( m_hCommPort );
  490. // clear result
  491. dwWrite = 0UL;
  492. // event received (or timeout eplaced - see above)
  493. case WAIT_OBJECT_0 + 1:
  494. // check result (nessessary by default)
  495. if( !::GetOverlappedResult( m_hCommPort, &m_ovWriteWait, &dwWaitResult, FALSE ) )
  496. {
  497. DWORD dwError = ::GetLastError();
  498. switch( dwError )
  499. {
  500. // stop write operation
  501. case ERROR_IO_INCOMPLETE:
  502. // stop write operation
  503. ::PurgeComm( m_hCommPort, PURGE_TXABORT | PURGE_TXCLEAR );
  504. // abort writing
  505. ::CancelIo( m_hCommPort );
  506. break;
  507. // operation was aborted by something
  508. case ERROR_OPERATION_ABORTED:
  509. break;
  510. }
  511. }
  512. break;
  513. }
  514. // check write result
  515. if( ( dwEvtMask & EV_TXEMPTY ) != EV_TXEMPTY )
  516. {
  517. LGTRACE( "[%p] NO TXEMPTY EVENT (%d)\n", this, dwWrite );
  518. dwWrite = 0UL;
  519. }
  520. return dwWrite;
  521. }
  522. __except(1)
  523. {
  524. MessageBox( NULL, "Error in CConnectionRS232::Write", "Error in CConnectionRS232::Write", MB_OK );
  525. }
  526. return 0;
  527. }
  528. */
  529. {
  530. m_dwCommMask = EV_TXEMPTY;
  531. // store operation's start time
  532. DWORD dwStartTime = ::GetTickCount();
  533. // set comm wait mask
  534. if( !::SetCommMask( m_hCommPort, EV_TXEMPTY ) ) {
  535. //MessageBox( NULL, "Disconnected USB Plug?", "Error in CConnectionRS232::Write", MB_OK );
  536. ::PurgeComm( m_hCommPort, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT );
  537. ::SetLastError( ERROR_IO_INCOMPLETE );
  538. return Create();//0;
  539. };
  540. // check internal buffers and errors
  541. COMSTAT comstat;
  542. DWORD dwErrors;
  543. if( ::ClearCommError( m_hCommPort, &dwErrors, &comstat ) ) {
  544. // check for UART errors
  545. if( dwErrors & CE_RXOVER ){
  546. //MessageBox( NULL, "Error in CConnectionRS232::Write 1 ", "Error in CConnectionRS232::Write RXOVER", MB_OK );
  547. ::PurgeComm( m_hCommPort, PURGE_RXCLEAR );
  548. };
  549. if( dwErrors & CE_BREAK ) {
  550. //MessageBox( NULL, "Error in CConnectionRS232::Write 2", "Error in CConnectionRS232::Write BREAK", MB_OK );
  551. ::ClearCommBreak( m_hCommPort );
  552. };
  553. // if port did not finish to transmit previous packet
  554. if( comstat.cbOutQue ){
  555. // cancel it
  556. ::CancelIo( m_hCommPort );
  557. ::PurgeComm( m_hCommPort, PURGE_TXCLEAR | PURGE_TXABORT );
  558. //MessageBox( NULL, "Error in CConnectionRS232::Write 3", "Error in CConnectionRS232::Write PCKGLATE", MB_OK );
  559. }
  560. } else {
  561. //MessageBox( NULL, "Error in CConnectionRS232::Write 4", "Error in CConnectionRS232::Write", MB_OK );
  562. // error occured
  563. return 0;
  564. };
  565. // establish wait operation
  566. DWORD dwEvtMask = 0;
  567. if( !::WaitCommEvent( m_hCommPort, &dwEvtMask, &m_ovWriteWait ) ){
  568. DWORD dwError = ::GetLastError();
  569. if( dwError != ERROR_IO_PENDING ){
  570. //MessageBox( NULL, "Error in CConnectionRS232::Write", "Error in CConnectionRS232::Write Error Ocured", MB_OK );
  571. // error occured
  572. SetLastError( dwError );
  573. return 0;
  574. }
  575. }
  576. // write packet
  577. DWORD dwWritten = 0;
  578. if( !::WriteFile( m_hCommPort, pBuffer, dwSize, &dwWritten, &m_ovWrite ) ){
  579. DWORD dwError = ::GetLastError();
  580. if( dwError != ERROR_IO_PENDING ){
  581. // cancel wait operation
  582. ::SetCommMask( m_hCommPort, 0 );
  583. // error occured
  584. ::SetLastError( dwError );
  585. //MessageBox( NULL, "Error in CConnectionRS232::Write", "Error in CConnectionRS232::Write PENDING", MB_OK );
  586. return 0;
  587. }
  588. // wait for completion
  589. HANDLE hEvents[] = { m_ovWrite.hEvent, m_hEventQuit };
  590. DWORD dwOverlappedWriteResult = 0;
  591. switch( ::WaitForMultipleObjects( 2, hEvents, FALSE, dwTimeout ) ){
  592. // if write complette
  593. case WAIT_OBJECT_0:
  594. if( !::GetOverlappedResult( m_hCommPort, &m_ovWrite, &dwOverlappedWriteResult, FALSE ) ){
  595. DWORD dwError = ::GetLastError();
  596. // cancel write operation
  597. ::CancelIo( m_hCommPort );
  598. ::PurgeComm( m_hCommPort, PURGE_TXCLEAR | PURGE_TXABORT );
  599. // cancel wait operation
  600. ::SetCommMask( m_hCommPort, 0 );
  601. // error occured
  602. if( dwError != ERROR_IO_INCOMPLETE )
  603. ::SetLastError( dwError );
  604. return 0;
  605. };
  606. // store amount of written bytes
  607. dwWritten = dwOverlappedWriteResult;
  608. break;
  609. // if write canceled or error occured
  610. case WAIT_OBJECT_0+1:
  611. default:
  612. // cancel write operation
  613. ::CancelIo( m_hCommPort );
  614. ::PurgeComm( m_hCommPort, PURGE_TXCLEAR | PURGE_TXABORT );
  615. // cancel wait operation
  616. ::SetCommMask( m_hCommPort, 0 );
  617. return 0;
  618. }
  619. }
  620. // calculate eplaced time
  621. DWORD dwEplacedTime = ::GetTickCount() - dwStartTime;
  622. // calculate remaining time
  623. DWORD dwRemaingTime = ( dwTimeout > dwEplacedTime ) ? ( dwTimeout - dwEplacedTime ) : 0UL;
  624. // wait for UART's TX is EMPTY
  625. HANDLE hEvents[] = { m_ovWriteWait.hEvent, m_hEventQuit };
  626. DWORD dwOverlappedWaitResult = 0;
  627. switch( ::WaitForMultipleObjects( 2, hEvents, FALSE, dwRemaingTime ) )
  628. {
  629. // if write complette
  630. case WAIT_OBJECT_0:
  631. if( !::GetOverlappedResult( m_hCommPort, &m_ovWriteWait, &dwOverlappedWaitResult, FALSE ) )
  632. {
  633. DWORD dwError = ::GetLastError();
  634. // cancel write operation
  635. ::CancelIo( m_hCommPort );
  636. ::PurgeComm( m_hCommPort, PURGE_TXCLEAR | PURGE_TXABORT );
  637. // cancel wait operation
  638. ::SetCommMask( m_hCommPort, 0 );
  639. // error occured
  640. if( dwError != ERROR_IO_INCOMPLETE )
  641. ::SetLastError( dwError );
  642. return 0;
  643. }
  644. break;
  645. // if wait canceled or error occured
  646. default:
  647. case WAIT_OBJECT_0+1:
  648. // cancel write operation
  649. ::CancelIo( m_hCommPort );
  650. ::PurgeComm( m_hCommPort, PURGE_TXCLEAR | PURGE_TXABORT );
  651. // cancel wait operation
  652. ::SetCommMask( m_hCommPort, 0 );
  653. return 0;
  654. }
  655. return dwWritten;
  656. }
  657. BOOL CConnectionRS232::Flush()
  658. {
  659. if( m_hCommPort == INVALID_HANDLE_VALUE )
  660. return FALSE;
  661. return ::FlushFileBuffers( m_hCommPort );
  662. }