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.

199 lines
4.5 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: SocketVM.cpp
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file of SocketVM.cpp
  13. //----------------------------------------------------------------------------
  14. // SocketVM.cpp: implementation of the CSocketVM class.
  15. //
  16. //////////////////////////////////////////////////////////////////////
  17. #include "stdafx.h"
  18. //#include "chicago.h"
  19. #include "SocketVM.h"
  20. #include "SensorArray.h"
  21. #include "..\..\LgLib\Version.h"
  22. #ifdef _DEBUG
  23. #undef THIS_FILE
  24. static char THIS_FILE[]=__FILE__;
  25. #define new DEBUG_NEW
  26. #endif
  27. //////////////////////////////////////////////////////////////////////
  28. // Construction/Destruction
  29. //////////////////////////////////////////////////////////////////////
  30. CSocketVM::CSocketVM()
  31. {
  32. m_nType = SOCKET_VM;
  33. for( int i = 0; i < MAX_DEVICE; i++ )
  34. m_ppDevice[ i ] = NULL;
  35. m_nDeviceCount = 0;
  36. m_nDeviceCurrent = 0;
  37. m_bRunning = TRUE;
  38. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CSocketVM::CSocketVM") );
  39. }
  40. CSocketVM::~CSocketVM()
  41. {
  42. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CSocketVM::~CSocketVM") );
  43. }
  44. int CSocketVM::OnCommand()
  45. {
  46. BOOL bWait = FALSE;
  47. for( int i = 0; i < m_nDeviceCount; i++ )
  48. {
  49. if( GetTickCount() > m_pTime[ m_nDeviceCurrent ] )
  50. {
  51. BOOL bPause = FALSE;
  52. DWORD dwTime;
  53. if( m_ppDevice[ m_nDeviceCurrent ]->GetCommand( m_pBuffer, dwTime, bPause ) )
  54. {
  55. if( !bPause )
  56. {
  57. m_pTime[ m_nDeviceCurrent ] = 0UL;
  58. return CMD_WRITE;
  59. }
  60. else
  61. {
  62. m_pTime[ m_nDeviceCurrent ] = GetTickCount() + dwTime; // m_nRCount is pause time
  63. bWait = TRUE;
  64. }
  65. }
  66. else
  67. m_nDeviceCurrent = (m_nDeviceCurrent + 1) % m_nDeviceCount;
  68. }
  69. else
  70. {
  71. m_nDeviceCurrent = (m_nDeviceCurrent + 1) % m_nDeviceCount;
  72. bWait = TRUE;
  73. }
  74. }
  75. if( bWait )
  76. return CMD_PAUSE;
  77. return CMD_STOP;
  78. /*
  79. for( int i = 0; i < m_nDeviceCount; i++ )
  80. {
  81. if( m_ppDevice[ m_nDeviceCurrent ]->GetCommand( m_pBuffer ) )
  82. {
  83. return CMD_WRITE;
  84. }
  85. else
  86. m_nDeviceCurrent = (m_nDeviceCurrent + 1) % m_nDeviceCount;
  87. }
  88. return CMD_STOP;
  89. */
  90. }
  91. void CSocketVM::OnBeforeWrite( LPBYTE pBuffer, DWORD& nCount )
  92. {
  93. nCount = MSG_LENGTH;
  94. ::CopyMemory( pBuffer, m_pBuffer, MSG_LENGTH );
  95. ::EscapeCommFunction( m_hCommPort, SETRTS );
  96. }
  97. int CSocketVM::OnAfterWrite()
  98. {
  99. return CMD_READ;
  100. }
  101. void CSocketVM::OnBeforeRead( DWORD& nCount, DWORD& dwTime )
  102. {
  103. nCount = MSG_LENGTH;
  104. dwTime = 50UL;
  105. ::EscapeCommFunction( m_hCommPort, CLRRTS );
  106. }
  107. int CSocketVM::OnAfterRead( LPBYTE pBuffer, DWORD nCount )
  108. {
  109. if( m_ppDevice[ m_nDeviceCurrent ]->SetAnswer( pBuffer, nCount ) )
  110. m_nCountSuccess++;
  111. else
  112. m_nCountFailure++;
  113. return CMD_COMMAND;
  114. }
  115. BOOL CSocketVM::DeviceAttach( CDeviceVMs *pDevice )
  116. {
  117. if( m_nDeviceCount < MAX_DEVICE )
  118. {
  119. for( int i = 0; i < m_nDeviceCount; i++ )
  120. if( m_ppDevice[ i ] == pDevice )
  121. return FALSE;
  122. m_pTime[ m_nDeviceCount ] = 0UL;
  123. m_ppDevice[ m_nDeviceCount++ ] = pDevice;
  124. pDevice->SetSocket( this );
  125. pDevice->SetLogicalID( m_nDeviceCount );
  126. return TRUE;
  127. }
  128. return FALSE;
  129. }
  130. BOOL CSocketVM::DeviceDetach( CDeviceVMs *pDevice )
  131. {
  132. for( int i = 0; i < m_nDeviceCount; i++ )
  133. {
  134. if( m_ppDevice[ i ] == pDevice )
  135. {
  136. m_ppDevice[ i ] = m_ppDevice[ --m_nDeviceCount ];
  137. if( m_nDeviceCurrent >= m_nDeviceCount )
  138. m_nDeviceCurrent = 0;
  139. return TRUE;
  140. }
  141. }
  142. return FALSE;
  143. }
  144. int CSocketVM::OnPause( DWORD &dwTime )
  145. {
  146. dwTime = 1UL;
  147. return CMD_COMMAND;
  148. }
  149. void CSocketVM::CommandLoopStart()
  150. {
  151. if( !m_bRunning )
  152. {
  153. m_bRunning = TRUE;
  154. CSocketRS232::Start();
  155. }
  156. }
  157. void CSocketVM::CommandLoopStop()
  158. {
  159. if( m_bRunning )
  160. m_bRunning = FALSE;
  161. }
  162. BOOL CSocketVM::Create(CSensorArray *pSensorArray, LPCTSTR pPort)
  163. {
  164. if( !CSocketRS232::Create( pPort, 9600, 8, NOPARITY, ONESTOPBIT ) )
  165. {
  166. pSensorArray->GetEventQueue()->PostEvent( LGS_EVENT_SOCKET_COMMUNICATION, FALSE, m_nType, m_nNumber, RGB( 0xFF, 0, 0 ), "Can't create video matrix line" );
  167. return m_bInitiated = FALSE;
  168. }
  169. else
  170. pSensorArray->GetEventQueue()->PostEvent( LGS_EVENT_SOCKET_COMMUNICATION, TRUE, m_nType, m_nNumber, RGB( 0, 0xFF, 0 ), "Video matrix line created" );
  171. return TRUE;
  172. }