Old fashion design for PC-104
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.

480 lines
13 KiB

4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. //----------------------------------------------------------------------------
  2. // Laser Guard Ltd.
  3. // Copyright � 2000-2002. All Rights Reserved.
  4. // Version 1.0 01/01/2000
  5. // Last modification: 03/09/2001
  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 "RS232.h"
  16. CConnectionRS232::CConnectionRS232()
  17. {
  18. m_hCommPort = INVALID_HANDLE_VALUE;
  19. m_dwCommMask = 0;
  20. m_hEventQuit = NULL;
  21. ::ZeroMemory( &m_ovWrite, sizeof( OVERLAPPED ) );
  22. ::ZeroMemory( &m_ovWriteWait, sizeof( OVERLAPPED ) );
  23. ::ZeroMemory( &m_ovRead, sizeof( OVERLAPPED ) );
  24. ::ZeroMemory( &m_ovReadWait, sizeof( OVERLAPPED ) );
  25. }
  26. CConnectionRS232::~CConnectionRS232()
  27. {
  28. Destroy();
  29. }
  30. BOOL CConnectionRS232::Create( LPCTSTR pPort, INT nBaudRate, INT nBitCount, INT nParity, INT nStopBit, DWORD dwReadBufferSize, DWORD dwWriteBufferSize )
  31. {
  32. Destroy();
  33. m_CreateParams.dwReadBufferSize = dwReadBufferSize;
  34. m_CreateParams.dwWriteBufferSize = dwWriteBufferSize;
  35. m_CreateParams.nBaudRate = nBaudRate;
  36. m_CreateParams.nBitCount = nBitCount;
  37. m_CreateParams.nParity = nParity;
  38. m_CreateParams.nStopBit = nStopBit;
  39. _tcscpy( m_CreateParams.pPort, pPort );
  40. m_hEventQuit = ::CreateEvent( NULL, FALSE, FALSE, NULL );
  41. if( !m_hEventQuit )
  42. return FALSE;
  43. m_ovReadWait.hEvent = ::CreateEvent( NULL, TRUE, FALSE, NULL );
  44. if( !m_ovReadWait.hEvent )
  45. return FALSE;
  46. m_ovWriteWait.hEvent = ::CreateEvent( NULL, TRUE, FALSE, NULL );
  47. if( !m_ovWriteWait.hEvent )
  48. return FALSE;
  49. m_ovRead.hEvent = ::CreateEvent( NULL, TRUE, FALSE, NULL );
  50. if( !m_ovRead.hEvent )
  51. return FALSE;
  52. m_ovWrite.hEvent = ::CreateEvent( NULL, TRUE, FALSE, NULL );
  53. if( !m_ovWrite.hEvent )
  54. return FALSE;
  55. return Create();
  56. }
  57. BOOL CConnectionRS232::Create()
  58. {
  59. // close previous communication port handle
  60. if( m_hCommPort != INVALID_HANDLE_VALUE )
  61. {
  62. ::CloseHandle( m_hCommPort );
  63. m_hCommPort = INVALID_HANDLE_VALUE;
  64. }
  65. // open new one
  66. m_hCommPort = ::CreateFile( m_CreateParams.pPort, GENERIC_READ | GENERIC_WRITE,
  67. 0UL, NULL, OPEN_EXISTING,
  68. FILE_FLAG_OVERLAPPED, NULL );
  69. if( m_hCommPort == INVALID_HANDLE_VALUE )
  70. return FALSE;
  71. DCB dcb;
  72. ::ZeroMemory( &dcb, sizeof( DCB ) );
  73. dcb.DCBlength = sizeof( DCB );
  74. ::GetCommState( m_hCommPort, &dcb );
  75. dcb.BaudRate = m_CreateParams.nBaudRate;
  76. dcb.ByteSize = m_CreateParams.nBitCount;
  77. dcb.Parity = m_CreateParams.nParity;
  78. dcb.StopBits = m_CreateParams.nStopBit;
  79. dcb.EvtChar = 0;
  80. dcb.fParity = ( m_CreateParams.nParity != NOPARITY );
  81. dcb.fOutxCtsFlow = FALSE;
  82. dcb.fOutxDsrFlow = FALSE;
  83. dcb.fDtrControl = DTR_CONTROL_DISABLE;
  84. dcb.fDsrSensitivity = FALSE;
  85. dcb.fOutX = FALSE;
  86. dcb.fInX = FALSE;
  87. dcb.fErrorChar = FALSE;
  88. dcb.fNull = FALSE;
  89. dcb.fRtsControl = RTS_CONTROL_DISABLE;
  90. dcb.fAbortOnError = FALSE;
  91. // setup communication port buffers
  92. if( !::SetupComm( m_hCommPort, m_CreateParams.dwReadBufferSize, m_CreateParams.dwWriteBufferSize ) )
  93. return FALSE;
  94. // setup communication port parameters
  95. if( !::SetCommState( m_hCommPort, &dcb ) )
  96. return FALSE;
  97. COMMTIMEOUTS ct;
  98. ct.ReadIntervalTimeout = 0UL;
  99. ct.ReadTotalTimeoutMultiplier = 0UL;
  100. ct.ReadTotalTimeoutConstant = 0UL;
  101. ct.WriteTotalTimeoutMultiplier = 0UL;
  102. ct.WriteTotalTimeoutConstant = 0UL;
  103. // setup communication port timeouts
  104. if( !::SetCommTimeouts( m_hCommPort, &ct ) )
  105. return FALSE;
  106. // clear communication port
  107. ::PurgeComm( m_hCommPort, PURGE_TXCLEAR | PURGE_RXCLEAR | PURGE_TXABORT | PURGE_RXABORT );
  108. // setup communication port timeouts
  109. if( !::EscapeCommFunction( m_hCommPort, CLRRTS ) )
  110. return FALSE;
  111. return TRUE;
  112. }
  113. void CConnectionRS232::Destroy()
  114. {
  115. if( m_hEventQuit )
  116. {
  117. ::CloseHandle( m_hEventQuit );
  118. m_hEventQuit = NULL;
  119. }
  120. if( m_hCommPort != INVALID_HANDLE_VALUE )
  121. {
  122. ::CloseHandle( m_hCommPort );
  123. m_hCommPort = INVALID_HANDLE_VALUE;
  124. }
  125. if( m_ovReadWait.hEvent )
  126. {
  127. ::CloseHandle( m_ovReadWait.hEvent );
  128. m_ovReadWait.hEvent = NULL;
  129. }
  130. if( m_ovWriteWait.hEvent )
  131. {
  132. ::CloseHandle( m_ovWriteWait.hEvent );
  133. m_ovWriteWait.hEvent = NULL;
  134. }
  135. if( m_ovWrite.hEvent )
  136. {
  137. ::CloseHandle( m_ovWrite.hEvent );
  138. m_ovWrite.hEvent = NULL;
  139. }
  140. if( m_ovRead.hEvent )
  141. {
  142. ::CloseHandle( m_ovRead.hEvent );
  143. m_ovRead.hEvent = NULL;
  144. }
  145. }
  146. DWORD CConnectionRS232::Read( IN PBYTE pBuffer, IN DWORD dwSize, IN DWORD dwTimeout )
  147. {
  148. // used for retrieve port information
  149. COMSTAT comstat;
  150. // placeholder for result
  151. DWORD dwRead = 0UL;
  152. // event array useable in Wait... functions
  153. HANDLE hEventsWait[] = { m_hEventQuit, m_ovReadWait.hEvent };
  154. HANDLE hEventsRead[] = { m_hEventQuit, m_ovRead.hEvent };
  155. // if previous operation was not read then re-set event mask
  156. if( m_dwCommMask != EV_RXCHAR )
  157. ::SetCommMask( m_hCommPort, m_dwCommMask = EV_RXCHAR );
  158. // remember read start time
  159. DWORD dwStartTime = ::GetTickCount();
  160. while( TRUE )
  161. {
  162. DWORD dwErrors;
  163. // check communication port for errors
  164. if( ::ClearCommError( m_hCommPort, &dwErrors, &comstat ) )
  165. {
  166. if( dwErrors & CE_RXOVER ) ::PurgeComm( m_hCommPort, PURGE_RXCLEAR );
  167. if( dwErrors & CE_BREAK ) ::ClearCommBreak( m_hCommPort );
  168. }
  169. else
  170. {
  171. // recreate commport and continue reading
  172. Create();
  173. }
  174. // calculate time eplaced since start of read
  175. DWORD dwSubTime = ::GetTickCount() - dwStartTime;
  176. // check amount of bytes in input buffer and remaining time
  177. if( (comstat.cbInQue < dwSize) && (dwSubTime < dwTimeout) )
  178. {
  179. DWORD dwEvtMask = 0UL;
  180. DWORD dwWaitResult;
  181. // wait for event EV_RXCHAR
  182. if( !::WaitCommEvent( m_hCommPort, &dwEvtMask, &m_ovReadWait ) )
  183. {
  184. DWORD dwError = ::GetLastError();
  185. switch( dwError )
  186. {
  187. case ERROR_IO_PENDING:
  188. // wait for overlapped completion
  189. switch( ::WaitForMultipleObjects( 2, hEventsWait, FALSE, dwTimeout - dwSubTime ) )
  190. {
  191. // request to abort
  192. case WAIT_OBJECT_0 + 0:
  193. // abort waiting
  194. ::CancelIo( m_hCommPort );
  195. // check result (nessessary by default)
  196. ::GetOverlappedResult( m_hCommPort, &m_ovReadWait, &dwWaitResult, FALSE );
  197. goto READ_ABORT;
  198. // timeout eplaced but event did not received
  199. case WAIT_TIMEOUT:
  200. ::CancelIo( m_hCommPort );
  201. // event received (or timeout eplaced - see above)
  202. case WAIT_OBJECT_0 + 1:
  203. ::GetOverlappedResult( m_hCommPort, &m_ovReadWait, &dwWaitResult, FALSE );
  204. break;
  205. }
  206. break;
  207. default:
  208. // recreate commport and continue reading
  209. Create();
  210. break;
  211. }
  212. }
  213. }
  214. else break;
  215. }
  216. // actual data reading (from internal buffers)
  217. if( !::ReadFile( m_hCommPort, pBuffer, min( dwSize, comstat.cbInQue), &dwRead, &m_ovRead ) )
  218. {
  219. DWORD dwError = ::GetLastError();
  220. switch( dwError )
  221. {
  222. case ERROR_IO_PENDING:
  223. // wait for overlapped completion
  224. switch( ::WaitForMultipleObjects( 2, hEventsRead, FALSE, 0 ) )
  225. {
  226. // request to abort
  227. case WAIT_OBJECT_0 + 0:
  228. // stop read operation
  229. ::PurgeComm( m_hCommPort, PURGE_RXABORT );
  230. // abort reading
  231. ::CancelIo( m_hCommPort );
  232. // check result for receive number of bytes have readed to this time
  233. ::GetOverlappedResult( m_hCommPort, &m_ovRead, &dwRead, FALSE );
  234. goto READ_ABORT;
  235. // timeout eplaced but event did not received
  236. case WAIT_TIMEOUT:
  237. ::CancelIo( m_hCommPort );
  238. // event received (or timeout eplaced - see above)
  239. case WAIT_OBJECT_0 + 1:
  240. // check result of operation
  241. if( !::GetOverlappedResult( m_hCommPort, &m_ovRead, &dwRead, FALSE ) )
  242. {
  243. DWORD dwError = ::GetLastError();
  244. switch( dwError )
  245. {
  246. // stop read operation
  247. case ERROR_IO_INCOMPLETE:
  248. ::PurgeComm( m_hCommPort, PURGE_RXABORT );
  249. break;
  250. // operation was aborted by something
  251. case ERROR_OPERATION_ABORTED:
  252. break;
  253. }
  254. }
  255. break;
  256. }
  257. break;
  258. default:
  259. // recreate commport and continue reading
  260. Create();
  261. break;
  262. }
  263. }
  264. READ_ABORT:
  265. return dwRead;
  266. }
  267. DWORD CConnectionRS232::Write( IN PBYTE pBuffer, IN DWORD dwSize, IN DWORD dwTimeout )
  268. {
  269. // used for retrieve port information
  270. COMSTAT comstat;
  271. DWORD dwErrors;
  272. // placeholder for result
  273. DWORD dwWrite = 0UL;
  274. // placeholder for event mask
  275. DWORD dwEvtMask = 0UL;
  276. // event array useable in Wait... functions
  277. HANDLE hEventsWait[] = { m_hEventQuit, m_ovWriteWait.hEvent };
  278. HANDLE hEventsWrite[] = { m_hEventQuit, m_ovWrite.hEvent };
  279. DWORD dwStartTime = GetTickCount();
  280. DWORD dwWaitTime;
  281. DWORD dwEplacedTime;
  282. // check communication port for errors
  283. if( ::ClearCommError( m_hCommPort, &dwErrors, &comstat ) )
  284. {
  285. if( dwErrors & CE_RXOVER ) ::PurgeComm( m_hCommPort, PURGE_RXCLEAR );
  286. if( dwErrors & CE_BREAK ) ::ClearCommBreak( m_hCommPort );
  287. }
  288. else
  289. {
  290. // recreate commport and continue reading
  291. Create();
  292. }
  293. // if previous operation was not Write then re-set event mask
  294. if( m_dwCommMask != EV_TXEMPTY )
  295. ::SetCommMask( m_hCommPort, m_dwCommMask = EV_TXEMPTY );
  296. BOOL bWaitEstablished = FALSE;
  297. while( !bWaitEstablished && ( GetTickCount() - dwStartTime ) < dwTimeout )
  298. {
  299. // wait for event EV_TXEMPTY
  300. if( !::WaitCommEvent( m_hCommPort, &dwEvtMask, &m_ovWriteWait ) )
  301. {
  302. DWORD dwError = ::GetLastError();
  303. switch( dwError )
  304. {
  305. case ERROR_IO_PENDING:
  306. bWaitEstablished = TRUE;
  307. break;
  308. default:
  309. Create();
  310. break;
  311. }
  312. }
  313. }
  314. // can't begin write
  315. if( !bWaitEstablished )
  316. return FALSE;
  317. // actual data writing
  318. if( !::WriteFile( m_hCommPort, pBuffer, dwSize, &dwWrite, &m_ovWrite ) )
  319. {
  320. DWORD dwError = ::GetLastError();
  321. switch( dwError )
  322. {
  323. case ERROR_IO_PENDING:
  324. // store current time
  325. dwEplacedTime = GetTickCount() - dwStartTime;
  326. // maximum wait timeout
  327. dwWaitTime = ( dwTimeout >= dwEplacedTime ) ? dwTimeout - dwEplacedTime : 0UL;
  328. // wait for overlapped completion
  329. switch( ::WaitForMultipleObjects( 2, hEventsWrite, FALSE, dwWaitTime ) )
  330. {
  331. // request to abort
  332. case WAIT_OBJECT_0 + 0:
  333. // stop write operation
  334. ::PurgeComm( m_hCommPort, PURGE_TXABORT | PURGE_TXCLEAR );
  335. // abort writing
  336. ::CancelIo( m_hCommPort );
  337. // check result for receive number of bytes have Writeed to this time
  338. ::GetOverlappedResult( m_hCommPort, &m_ovWrite, &dwWrite, FALSE );
  339. goto WRITE_ABORT;
  340. // timeout eplaced but event did not received
  341. case WAIT_TIMEOUT:
  342. ::CancelIo( m_hCommPort );
  343. // event received (or timeout eplaced - see above)
  344. case WAIT_OBJECT_0 + 1:
  345. // check result of operation
  346. if( !::GetOverlappedResult( m_hCommPort, &m_ovWrite, &dwWrite, FALSE ) )
  347. {
  348. DWORD dwError = ::GetLastError();
  349. switch( dwError )
  350. {
  351. // stop write operation
  352. case ERROR_IO_INCOMPLETE:
  353. // stop write operation
  354. ::PurgeComm( m_hCommPort, PURGE_TXABORT | PURGE_TXCLEAR );
  355. // abort writing
  356. ::CancelIo( m_hCommPort );
  357. break;
  358. // operation was aborted by something
  359. case ERROR_OPERATION_ABORTED:
  360. break;
  361. }
  362. }
  363. break;
  364. }
  365. break;
  366. default:
  367. // recreate commport and continue writeing
  368. Create();
  369. break;
  370. }
  371. }
  372. WRITE_ABORT:
  373. // store current time
  374. dwEplacedTime = GetTickCount() - dwStartTime;
  375. // maximum wait timeout
  376. dwWaitTime = ( dwTimeout >= dwEplacedTime ) ? dwTimeout - dwEplacedTime : 0UL;
  377. DWORD dwWaitResult;
  378. // wait for end of writing
  379. switch( ::WaitForMultipleObjects( 2, hEventsWait, FALSE, dwWaitTime ) )
  380. {
  381. // request to abort
  382. case WAIT_OBJECT_0 + 0:
  383. // timeout eplaced but event did not received
  384. case WAIT_TIMEOUT:
  385. // abort waiting
  386. ::CancelIo( m_hCommPort );
  387. // clear result
  388. dwWrite = 0UL;
  389. // event received (or timeout eplaced - see above)
  390. case WAIT_OBJECT_0 + 1:
  391. // check result (nessessary by default)
  392. if( !::GetOverlappedResult( m_hCommPort, &m_ovWriteWait, &dwWaitResult, FALSE ) )
  393. {
  394. DWORD dwError = ::GetLastError();
  395. switch( dwError )
  396. {
  397. // stop write operation
  398. case ERROR_IO_INCOMPLETE:
  399. // stop write operation
  400. ::PurgeComm( m_hCommPort, PURGE_TXABORT | PURGE_TXCLEAR );
  401. // abort writing
  402. ::CancelIo( m_hCommPort );
  403. break;
  404. // operation was aborted by something
  405. case ERROR_OPERATION_ABORTED:
  406. break;
  407. }
  408. }
  409. break;
  410. }
  411. // check write result
  412. if( ( dwEvtMask & EV_TXEMPTY ) != EV_TXEMPTY )
  413. {
  414. dwWrite = 0UL;
  415. }
  416. return dwWrite;
  417. }