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.

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