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.

514 lines
11 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: 08/10/2003
  6. // PROJECT:
  7. // SUBSYSTEM:
  8. // FILE: SocketRS232.cpp
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file of SocketRS232.cpp
  13. //----------------------------------------------------------------------------
  14. // SocketRS232.cpp: implementation of the CSocketRS232 class.
  15. //
  16. //////////////////////////////////////////////////////////////////////
  17. #include "stdafx.h"
  18. #include "SocketRS232.h"
  19. #include "..\..\LgLib\Version.h"
  20. #ifdef _DEBUG
  21. #undef THIS_FILE
  22. static char THIS_FILE[]=__FILE__;
  23. #define new DEBUG_NEW
  24. #endif
  25. //////////////////////////////////////////////////////////////////////
  26. // Construction/Destruction
  27. //////////////////////////////////////////////////////////////////////
  28. CSocketRS232::CSocketRS232()
  29. {
  30. m_hThread = NULL;
  31. m_hEventQuit = NULL;
  32. m_hEventStart = NULL;
  33. m_hCommPort = INVALID_HANDLE_VALUE;
  34. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CSocketRS232::CSocketRS232") );
  35. }
  36. CSocketRS232::~CSocketRS232()
  37. {
  38. Destroy();
  39. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CSocketRS232::~CSocketRS232") );
  40. }
  41. BOOL CSocketRS232::Create( LPCTSTR pPort, INT nBaudRate, INT nBitCount, INT nParity, INT nStopBit )
  42. {
  43. Destroy();
  44. _tcscpy( m_Port.pPort, pPort );
  45. m_Port.nBaudRate = nBaudRate;
  46. m_Port.nBitCount = nBitCount;
  47. m_Port.nParity = nParity;
  48. m_Port.nStopBit = nStopBit;
  49. if( !CreateCommPort() )
  50. return FALSE;
  51. m_hEventStart = ::CreateEvent( NULL, FALSE, FALSE, NULL );
  52. if( !m_hEventStart )
  53. return FALSE;
  54. m_hEventQuit = ::CreateEvent( NULL, FALSE, FALSE, NULL );
  55. if( !m_hEventQuit )
  56. return FALSE;
  57. DWORD dwThreadId;
  58. m_hThread = ::CreateThread( NULL, 0UL, ThreadProcSocketRS232, this, 0UL, &dwThreadId );
  59. if( !m_hThread )
  60. return FALSE;
  61. return TRUE;
  62. }
  63. void CSocketRS232::Destroy()
  64. {
  65. if( m_hThread )
  66. {
  67. ::SetEvent( m_hEventQuit );
  68. ::WaitForSingleObject( m_hThread, INFINITE );
  69. ::CloseHandle( m_hThread );
  70. m_hThread = NULL;
  71. }
  72. if( m_hEventQuit )
  73. {
  74. ::CloseHandle( m_hEventQuit );
  75. m_hEventQuit = NULL;
  76. }
  77. if( m_hEventStart )
  78. {
  79. ::CloseHandle( m_hEventStart );
  80. m_hEventStart = NULL;
  81. }
  82. if( m_hCommPort != INVALID_HANDLE_VALUE )
  83. {
  84. ::CloseHandle( m_hCommPort );
  85. m_hCommPort = INVALID_HANDLE_VALUE;
  86. }
  87. }
  88. BOOL CSocketRS232::CreateCommPort()
  89. {
  90. if( m_hCommPort != INVALID_HANDLE_VALUE )
  91. ::CloseHandle( m_hCommPort );
  92. m_hCommPort = ::CreateFile( m_Port.pPort, GENERIC_READ | GENERIC_WRITE,
  93. 0UL, NULL, OPEN_EXISTING,
  94. FILE_FLAG_OVERLAPPED, NULL );
  95. if( m_hCommPort == INVALID_HANDLE_VALUE )
  96. {
  97. m_hCommPort = NULL;
  98. return FALSE;
  99. }
  100. DCB dcb;
  101. ::ZeroMemory( &dcb, sizeof( DCB ) );
  102. dcb.DCBlength = sizeof( DCB );
  103. ::GetCommState( m_hCommPort, &dcb );
  104. dcb.BaudRate = m_Port.nBaudRate;
  105. dcb.ByteSize = m_Port.nBitCount;
  106. dcb.Parity = m_Port.nParity;
  107. dcb.StopBits = m_Port.nStopBit;
  108. dcb.EvtChar = 0;
  109. dcb.fParity = ( m_Port.nParity != NOPARITY );
  110. dcb.fOutxCtsFlow = FALSE;
  111. dcb.fOutxDsrFlow = FALSE;
  112. dcb.fDtrControl = DTR_CONTROL_DISABLE;
  113. dcb.fDsrSensitivity = FALSE;
  114. dcb.fOutX = FALSE;
  115. dcb.fInX = FALSE;
  116. dcb.fErrorChar = FALSE;
  117. dcb.fNull = FALSE;
  118. dcb.fRtsControl = RTS_CONTROL_DISABLE;
  119. dcb.fAbortOnError = FALSE;
  120. if( !::SetupComm( m_hCommPort, MAX_BUFFER_SIZE, MAX_BUFFER_SIZE ) )
  121. return FALSE;
  122. if( !::SetCommState( m_hCommPort, &dcb ) )
  123. return FALSE;
  124. COMMTIMEOUTS ct;
  125. ct.ReadIntervalTimeout = 0UL;
  126. ct.ReadTotalTimeoutMultiplier = 0UL;
  127. ct.ReadTotalTimeoutConstant = 0UL;
  128. ct.WriteTotalTimeoutMultiplier = 0UL;
  129. ct.WriteTotalTimeoutConstant = 0UL;
  130. if( !::SetCommTimeouts( m_hCommPort, &ct ) )
  131. return FALSE;
  132. ::PurgeComm( m_hCommPort, PURGE_TXCLEAR | PURGE_RXCLEAR |
  133. PURGE_TXABORT | PURGE_RXABORT );
  134. return TRUE;
  135. }
  136. DWORD CSocketRS232::ThreadProc()
  137. {
  138. DWORD dwErrors;
  139. COMSTAT comstat;
  140. DWORD dwWritten;
  141. OVERLAPPED ovWrite;
  142. ovWrite.hEvent = ::CreateEvent( NULL, TRUE, FALSE, NULL );
  143. ovWrite.Internal = ovWrite.InternalHigh = ovWrite.Offset = ovWrite.OffsetHigh = 0UL;
  144. DWORD dwEvtMask = 0;
  145. OVERLAPPED ovWait;
  146. ovWait.hEvent = ::CreateEvent( NULL, TRUE, FALSE, NULL );
  147. ovWait.Internal = ovWait.InternalHigh = ovWait.Offset = ovWait.OffsetHigh = 0UL;
  148. DWORD dwRead;
  149. OVERLAPPED ovRead;
  150. ovRead.hEvent = ::CreateEvent( NULL, TRUE, FALSE, NULL );
  151. ovRead.Internal = ovRead.InternalHigh = ovRead.Offset = ovRead.OffsetHigh = 0UL;
  152. HANDLE hEventsStart[] = { m_hEventQuit, m_hEventStart };
  153. HANDLE hEventsWrite[] = { m_hEventQuit, ovWrite.hEvent };
  154. HANDLE hEventsWait[] = { m_hEventQuit, ovWait.hEvent };
  155. HANDLE hEventsRead[] = { m_hEventQuit, ovRead.hEvent };
  156. BYTE pBuffer[ MAX_BUFFER_SIZE ];
  157. DWORD nCount;
  158. DWORD dwTime;
  159. DWORD dwStartTime;
  160. DWORD dwSubTime;
  161. DWORD dwCommMask = 0;
  162. DWORD dwWaitTime;
  163. DWORD dwPauseTime;
  164. INT nError;
  165. INT nStage = CMD_COMMAND;
  166. while( TRUE )
  167. {
  168. switch( ::WaitForMultipleObjects( 2, hEventsStart, FALSE, nStage == CMD_STOP ? INFINITE : 0UL ) )
  169. {
  170. case WAIT_OBJECT_0:
  171. goto THREAD_QUIT;
  172. case WAIT_OBJECT_0+1:
  173. if( nStage == CMD_STOP )
  174. nStage = CMD_COMMAND;
  175. break;
  176. }
  177. switch( nStage )
  178. {
  179. case CMD_COMMAND:
  180. switch( ::WaitForSingleObject( m_hEventQuit, 0UL ) )
  181. {
  182. case WAIT_OBJECT_0:
  183. goto THREAD_QUIT;
  184. case WAIT_TIMEOUT:
  185. nStage = OnCommand();
  186. break;
  187. }
  188. break;
  189. case CMD_PAUSE:
  190. nStage = OnPause( dwPauseTime );
  191. switch( ::WaitForMultipleObjects( 2, hEventsStart, FALSE, dwPauseTime ) )
  192. {
  193. case WAIT_OBJECT_0:
  194. goto THREAD_QUIT;
  195. case WAIT_OBJECT_0+1:
  196. nStage = CMD_COMMAND;
  197. break;
  198. }
  199. break;
  200. case CMD_WRITE:
  201. OnBeforeWrite( pBuffer, nCount );
  202. if( ::ClearCommError( m_hCommPort, &dwErrors, &comstat ) )
  203. {
  204. if( dwErrors & (CE_RXOVER|CE_BREAK) )
  205. {
  206. if( dwErrors & CE_RXOVER ) ::PurgeComm( m_hCommPort, PURGE_RXCLEAR );
  207. if( dwErrors & CE_BREAK ) ::ClearCommBreak( m_hCommPort );
  208. }
  209. }
  210. else
  211. {
  212. nError = 0;
  213. goto RECREATE_COMMPORT;
  214. }
  215. if( dwCommMask != EV_TXEMPTY )
  216. ::SetCommMask( m_hCommPort, dwCommMask = EV_TXEMPTY );
  217. dwWaitTime = ((DWORD)m_Port.nBaudRate * (DWORD)nCount ) >> 8;
  218. if( !::WriteFile( m_hCommPort, pBuffer, nCount, &dwWritten, &ovWrite ) )
  219. {
  220. DWORD dwError = ::GetLastError();
  221. switch( dwError )
  222. {
  223. case ERROR_IO_PENDING:
  224. switch( ::WaitForMultipleObjects( 2, hEventsWrite, FALSE, dwWaitTime ) )
  225. {
  226. case WAIT_OBJECT_0 + 0:
  227. goto THREAD_QUIT;
  228. case WAIT_OBJECT_0 + 1:
  229. case WAIT_TIMEOUT:
  230. ::GetOverlappedResult( m_hCommPort, &ovWrite, &dwWritten, FALSE );
  231. break;
  232. }
  233. break;
  234. default:
  235. nError = 1;
  236. goto RECREATE_COMMPORT;
  237. }
  238. if( nCount != dwWritten )
  239. {
  240. if( !::WaitCommEvent( m_hCommPort, &dwEvtMask, &ovWait ) )
  241. {
  242. DWORD dwError = ::GetLastError();
  243. switch( dwError )
  244. {
  245. case ERROR_IO_PENDING:
  246. switch( ::WaitForMultipleObjects( 2, hEventsWait, FALSE, dwWaitTime ) )
  247. {
  248. case WAIT_OBJECT_0 + 0:
  249. goto THREAD_QUIT;
  250. case WAIT_OBJECT_0 + 1:
  251. case WAIT_TIMEOUT:
  252. ::GetOverlappedResult( m_hCommPort, &ovWait, &dwEvtMask, FALSE );
  253. break;
  254. }
  255. break;
  256. default:
  257. nError = 2;
  258. goto RECREATE_COMMPORT;
  259. }
  260. }
  261. if( !( dwEvtMask & EV_TXEMPTY ) )
  262. {
  263. nError = 3;
  264. goto RECREATE_COMMPORT;
  265. }
  266. }
  267. }
  268. ::PurgeComm( m_hCommPort, PURGE_TXCLEAR | PURGE_TXABORT );
  269. if( dwWritten != nCount )
  270. {
  271. nError = 4;
  272. goto RECREATE_COMMPORT;
  273. }
  274. nStage = OnAfterWrite();
  275. break;
  276. case CMD_READ:
  277. OnBeforeRead( nCount, dwTime );
  278. ::ZeroMemory( pBuffer, MAX_BUFFER_SIZE );
  279. dwStartTime = ::GetTickCount();
  280. if( dwCommMask != EV_RXCHAR )
  281. ::SetCommMask( m_hCommPort, dwCommMask = EV_RXCHAR );
  282. while( TRUE )
  283. {
  284. if( ::ClearCommError( m_hCommPort, &dwErrors, &comstat ) )
  285. {
  286. if( dwErrors & (CE_RXOVER|CE_BREAK) )
  287. {
  288. if( dwErrors & CE_RXOVER ) ::PurgeComm( m_hCommPort, PURGE_RXCLEAR );
  289. if( dwErrors & CE_BREAK ) ::ClearCommBreak( m_hCommPort );
  290. }
  291. }
  292. else
  293. {
  294. nError = 5;
  295. goto RECREATE_COMMPORT;
  296. }
  297. if( comstat.cbInQue < nCount && ( dwSubTime = ( ::GetTickCount() - dwStartTime ) ) < dwTime )
  298. {
  299. dwEvtMask = 0UL;
  300. if( !::WaitCommEvent( m_hCommPort, &dwEvtMask, &ovWait ) )
  301. {
  302. DWORD dwError = ::GetLastError();
  303. switch( dwError )
  304. {
  305. case ERROR_IO_PENDING:
  306. switch( ::WaitForMultipleObjects( 2, hEventsWait, FALSE, dwSubTime ) )
  307. {
  308. case WAIT_OBJECT_0 + 0:
  309. goto THREAD_QUIT;
  310. case WAIT_TIMEOUT:
  311. ::CancelIo( m_hCommPort );
  312. case WAIT_OBJECT_0 + 1:
  313. ::GetOverlappedResult( m_hCommPort, &ovWait, &dwEvtMask, FALSE );
  314. break;
  315. }
  316. break;
  317. default:
  318. nError = 6;
  319. goto RECREATE_COMMPORT;
  320. }
  321. }
  322. }
  323. else break;
  324. }
  325. if( !::ReadFile( m_hCommPort, pBuffer, comstat.cbInQue, &dwRead, &ovRead ) )
  326. {
  327. DWORD dwError = ::GetLastError();
  328. switch( dwError )
  329. {
  330. case ERROR_IO_PENDING:
  331. switch( ::WaitForMultipleObjects( sizeof(hEventsRead)/sizeof(*hEventsRead), hEventsRead, FALSE, 0 ) )
  332. {
  333. case WAIT_OBJECT_0 + 0:
  334. goto THREAD_QUIT;
  335. case WAIT_TIMEOUT:
  336. ::CancelIo( m_hCommPort );
  337. case WAIT_OBJECT_0 + 1:
  338. if( !::GetOverlappedResult( m_hCommPort, &ovRead, &dwRead, FALSE ) )
  339. {
  340. DWORD dwError = ::GetLastError();
  341. switch( dwError )
  342. {
  343. case ERROR_IO_INCOMPLETE:
  344. ::PurgeComm( m_hCommPort, PURGE_RXABORT );
  345. break;
  346. case ERROR_OPERATION_ABORTED:
  347. break;
  348. }
  349. }
  350. break;
  351. }
  352. break;
  353. default:
  354. nError = 7;
  355. goto RECREATE_COMMPORT;
  356. }
  357. }
  358. nStage = OnAfterRead( pBuffer, dwRead );
  359. break;
  360. case CMD_WAIT:
  361. OnBeforeWait( dwTime, dwEvtMask );
  362. nStage = OnAfterWait( dwEvtMask );
  363. break;
  364. }
  365. continue;
  366. RECREATE_COMMPORT:
  367. TRACE3( "Recreate port (%d-%d)(%d)\n", nCount, dwWaitTime, nError );
  368. OnRecreate( nError );
  369. if( !CreateCommPort() )
  370. goto THREAD_QUIT;
  371. }
  372. THREAD_QUIT:
  373. ::PurgeComm( m_hCommPort, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR );
  374. ::CloseHandle( ovWrite.hEvent );
  375. ::CloseHandle( ovWait.hEvent );
  376. ::CloseHandle( ovRead.hEvent );
  377. return 0;
  378. }
  379. int CSocketRS232::OnCommand()
  380. {
  381. return CMD_COMMAND;
  382. }
  383. void CSocketRS232::OnRecreate( int nError )
  384. {
  385. }
  386. void CSocketRS232::OnBeforeWrite( LPBYTE pBuffer, DWORD& nCount )
  387. {
  388. nCount = 0UL;
  389. }
  390. int CSocketRS232::OnAfterWrite()
  391. {
  392. return CMD_COMMAND;
  393. }
  394. void CSocketRS232::OnBeforeWait( DWORD& dwTime, DWORD& dwEvent )
  395. {
  396. dwTime = 0UL;
  397. dwEvent = 0UL;
  398. }
  399. int CSocketRS232::OnAfterWait( DWORD dwEvent )
  400. {
  401. return CMD_COMMAND;
  402. }
  403. void CSocketRS232::OnBeforeRead( DWORD& nCount, DWORD& dwTime )
  404. {
  405. nCount = 0UL;
  406. dwTime = 0UL;
  407. }
  408. int CSocketRS232::OnAfterRead( LPBYTE pBuffer, DWORD nCount )
  409. {
  410. return CMD_COMMAND;
  411. }
  412. int CSocketRS232::OnPause( DWORD &dwTime )
  413. {
  414. dwTime = 0UL;
  415. return CMD_COMMAND;
  416. }