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.

235 lines
5.5 KiB

4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. //----------------------------------------------------------------------------
  2. // Laser Guard Ltd.
  3. // Copyright � 2000-2002. All Rights Reserved.
  4. // Version 1.0 19/04/2000
  5. // Last modification: 08/10/2003
  6. // Project Intruder Position Indicator
  7. // SUBSYSTEM: Fence
  8. // FILE: Fence.cpp
  9. // AUTHOR: Chizhevskiy Sergey
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file of class CFence
  13. //----------------------------------------------------------------------------
  14. #include "stdafx.h"
  15. #include "..\LgLib\Version.h"
  16. #include "Application.h"
  17. #include "Fence.h"
  18. CFence::CFence()
  19. {
  20. ZeroMemory(this, sizeof(*this));
  21. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CFence::CFence" );
  22. }
  23. CFence::~CFence()
  24. {
  25. if(m_hThread)
  26. {
  27. ::SetEvent(m_hEventClose);
  28. ::WaitForSingleObject(m_hThread, 1000UL);
  29. ::CloseHandle(m_hThread);
  30. }
  31. if(m_hEventClose)
  32. ::CloseHandle(m_hEventClose);
  33. if(m_hCommPort)
  34. ::CloseHandle(m_hCommPort);
  35. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CFence::~CFence" );
  36. }
  37. BOOL CFence::Create(CApplication *pApplication, LPCTSTR pCommPort)
  38. {
  39. COMMTIMEOUTS ct;
  40. DCB dcb;
  41. m_pApplication = pApplication;
  42. m_hCommPort = CreateFile(pCommPort, GENERIC_READ | GENERIC_WRITE,
  43. 0UL, NULL, OPEN_EXISTING,
  44. FILE_FLAG_OVERLAPPED | FILE_FLAG_NO_BUFFERING, NULL);
  45. if(m_hCommPort == INVALID_HANDLE_VALUE)
  46. return FALSE;
  47. ::ZeroMemory(&dcb, sizeof(dcb));
  48. dcb.DCBlength = sizeof( dcb );
  49. ::GetCommState(m_hCommPort, &dcb);
  50. dcb.BaudRate = CBR_1200;
  51. dcb.ByteSize = 8;
  52. dcb.Parity = NOPARITY;
  53. dcb.StopBits = ONESTOPBIT;
  54. dcb.fBinary = TRUE;
  55. dcb.fOutxCtsFlow = FALSE;
  56. dcb.fOutxDsrFlow = FALSE;
  57. dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
  58. dcb.fDsrSensitivity = FALSE;
  59. if(!::SetCommState(m_hCommPort, &dcb))
  60. {
  61. return FALSE;
  62. }
  63. ZeroMemory(&ct, sizeof(ct));
  64. ::SetCommTimeouts(m_hCommPort, &ct);
  65. ::SetCommMask(m_hCommPort, EV_CTS | EV_DSR);
  66. ::EscapeCommFunction(m_hCommPort, SETRTS);
  67. m_hEventClose = ::CreateEvent(NULL, FALSE, FALSE, NULL);
  68. DWORD dwThreadId;
  69. m_hThread = ::CreateThread(NULL, 0UL, ThreadProcFence, (LPVOID)this, 0UL, &dwThreadId);
  70. if(!m_hThread)
  71. return FALSE;
  72. return TRUE;
  73. }
  74. /*
  75. DWORD CFence::ThreadProc()
  76. {
  77. COMSTAT comStat;
  78. DWORD dwErrors;
  79. DWORD dwEvtMask;
  80. DWORD dwStatus;
  81. OVERLAPPED waitOV = { 0UL, 0UL, 0UL, 0UL, ::CreateEvent(NULL, TRUE, FALSE, NULL) };
  82. HANDLE phHandle[] = { m_hEventClose, waitOV.hEvent };
  83. while(TRUE)
  84. {
  85. ::SetCommMask(m_hCommPort, EV_CTS | EV_DSR);
  86. if(!::ClearCommError(m_hCommPort, &dwErrors, &comStat))
  87. {
  88. CMessageBox::MessageBox(NULL, "CFence::ClearCommError: ", NULL, MB_OK);
  89. continue;
  90. }
  91. if(!::WaitCommEvent(m_hCommPort, &dwEvtMask, &waitOV))
  92. {
  93. switch(::GetLastError())
  94. {
  95. case ERROR_IO_PENDING:
  96. switch(::WaitForMultipleObjects(2, phHandle, FALSE, INFINITE))
  97. {
  98. case WAIT_TIMEOUT:
  99. continue;
  100. case WAIT_OBJECT_0 + 0:
  101. goto THREAD_EXIT;
  102. case WAIT_OBJECT_0 + 1:
  103. if(!::GetOverlappedResult(m_hCommPort, &waitOV, &dwEvtMask, FALSE) )
  104. {
  105. CMessageBox::MessageBox(NULL, "CFence::WaitCommEvent: ", NULL, MB_OK);
  106. continue;
  107. }
  108. break;
  109. }
  110. break;
  111. default:
  112. CMessageBox::MessageBox(NULL, "CFence::WaitCommEvent: ", NULL, MB_OK);
  113. continue;
  114. } // end of switch(::GetLastError())
  115. } // end of if(!::WaitCommEvent(m_hCommPort, &dwEvtMask, &waitOV))
  116. ::GetCommModemStatus(m_hCommPort, &dwStatus);
  117. if((dwEvtMask & EV_DSR) && (!(dwStatus & MS_DSR_ON)))
  118. if(m_bEnable)
  119. m_pApplication->AddFenceEvent(0);
  120. if((dwEvtMask & EV_CTS) && (dwStatus & MS_CTS_ON))
  121. if(m_bEnable)
  122. m_pApplication->AddFenceEvent(1);
  123. } // end of while(TRUE)
  124. THREAD_EXIT:
  125. ::CloseHandle(waitOV.hEvent);
  126. return 70UL;
  127. }
  128. */
  129. DWORD CALLBACK ThreadProcFence(LPVOID lpvParam)
  130. {
  131. COMSTAT comStat;
  132. DWORD dwErrors;
  133. DWORD dwEvtMask;
  134. DWORD dwStatus;
  135. OVERLAPPED waitOV = { 0UL, 0UL, 0UL, 0UL, ::CreateEvent(NULL, TRUE, FALSE, NULL) };
  136. CFence* ptr = (CFence*)lpvParam;
  137. HANDLE phHandle[] = { ptr->m_hEventClose, waitOV.hEvent };
  138. while(TRUE)
  139. {
  140. ::SetCommMask(ptr->m_hCommPort, EV_CTS | EV_DSR);
  141. if(!::ClearCommError(ptr->m_hCommPort, &dwErrors, &comStat))
  142. {
  143. CMessageBox::MessageBox(NULL, "CFence::ClearCommError: ", NULL, MB_OK);
  144. continue;
  145. }
  146. if(!::WaitCommEvent(ptr->m_hCommPort, &dwEvtMask, &waitOV))
  147. {
  148. switch(::GetLastError())
  149. {
  150. case ERROR_IO_PENDING:
  151. switch(::WaitForMultipleObjects(2, phHandle, FALSE, INFINITE))
  152. {
  153. case WAIT_TIMEOUT:
  154. continue;
  155. case WAIT_OBJECT_0 + 0:
  156. goto THREAD_EXIT;
  157. case WAIT_OBJECT_0 + 1:
  158. if(!::GetOverlappedResult(ptr->m_hCommPort, &waitOV, &dwEvtMask, FALSE) )
  159. {
  160. CMessageBox::MessageBox(NULL, "CFence::WaitCommEvent: ", NULL, MB_OK);
  161. continue;
  162. }
  163. break;
  164. }
  165. break;
  166. default:
  167. CMessageBox::MessageBox(NULL, "CFence::WaitCommEvent: ", NULL, MB_OK);
  168. continue;
  169. } // end of switch(::GetLastError())
  170. } // end of if(!::WaitCommEvent(m_hCommPort, &dwEvtMask, &waitOV))
  171. ::GetCommModemStatus(ptr->m_hCommPort, &dwStatus);
  172. if((dwEvtMask & EV_DSR) && (!(dwStatus & MS_DSR_ON)))
  173. if(ptr->m_bEnable)
  174. ptr->m_pApplication->AddFenceEvent(0);
  175. if((dwEvtMask & EV_CTS) && (dwStatus & MS_CTS_ON))
  176. if(ptr->m_bEnable)
  177. ptr->m_pApplication->AddFenceEvent(1);
  178. } // end of while(TRUE)
  179. THREAD_EXIT:
  180. ::CloseHandle(waitOV.hEvent);
  181. return 70UL;
  182. }