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.

193 lines
4.4 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: PTZPG3AD.cpp
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file of PTZPG3AD.cpp
  13. //----------------------------------------------------------------------------
  14. #include "stdafx.h"
  15. #ifdef __BORLANDC__
  16. #include "..\LgLib\Comm\PTZPG3AD.h"
  17. #else
  18. #include "PTZPG3AD.h"
  19. #endif
  20. CProtocolPTZPG3AD::CProtocolPTZPG3AD()
  21. {
  22. m_pConnection = NULL;
  23. m_nCount = 0;
  24. m_pBufferPtr = m_pBuffer;
  25. m_pAddress[0] = m_pAddress[1] = 0;
  26. m_bReadAnyAddress = FALSE;
  27. }
  28. CProtocolPTZPG3AD::~CProtocolPTZPG3AD()
  29. {
  30. Destroy();
  31. }
  32. BOOL CProtocolPTZPG3AD::Create( CConnection *pConnection )
  33. {
  34. Destroy();
  35. m_pConnection = pConnection;
  36. m_pBufferPtr = m_pBuffer;
  37. m_nCount = 0;
  38. return TRUE;
  39. }
  40. void CProtocolPTZPG3AD::Destroy()
  41. {
  42. m_pConnection = NULL;
  43. m_pBufferPtr = m_pBuffer;
  44. m_nCount = 0;
  45. }
  46. void CProtocolPTZPG3AD::SetAddress( WORD bAddress )
  47. {
  48. m_pAddress[0] = PTZ_PHILIPS_G3_AUTODOME::To14bit( bAddress, 0 );
  49. m_pAddress[1] = PTZ_PHILIPS_G3_AUTODOME::To14bit( bAddress, 1 );
  50. }
  51. BOOL CProtocolPTZPG3AD::Read( PBYTE *ppBuffer, PINT pCount, DWORD dwTimeout, BOOL bHost )
  52. {
  53. bHost;
  54. BOOL bResult = FALSE;
  55. while( TRUE )
  56. {
  57. // assume without read repeating
  58. BOOL bRepeat = FALSE;
  59. // copy remaining part
  60. if( ( m_pBuffer != m_pBufferPtr ) && m_nCount )
  61. memcpy( m_pBuffer, m_pBufferPtr, m_nCount );
  62. DWORD nReadCount = 0;
  63. // try to read command header
  64. if( m_nCount < HEADER_SIZE )
  65. {
  66. // read new data
  67. DWORD nCount = HEADER_SIZE - m_nCount;
  68. m_nCount += ( m_pConnection->Read( m_pBuffer + m_nCount, nCount, dwTimeout ) );
  69. }
  70. if( m_nCount >= HEADER_SIZE )
  71. {
  72. // check header tag
  73. if( ( m_pBuffer[ LENGTH_POS ] & HEADER_TAG ) )
  74. {
  75. // validate possibility of lenght
  76. WORD bLength = ( m_pBuffer[LENGTH_POS] & 0x7F ) + 1;
  77. if( bLength <= MAX_COMMAND_SIZE )
  78. {
  79. if( m_bReadAnyAddress ||
  80. ( m_pBuffer[ ADDRESS_POS_0 ] == m_pAddress[0] &&
  81. m_pBuffer[ ADDRESS_POS_1 ] == m_pAddress[1] ) )
  82. {
  83. nReadCount = bLength - m_nCount;
  84. }
  85. else
  86. nReadCount = MAX_COMMAND_SIZE - m_nCount;
  87. }
  88. else
  89. nReadCount = MAX_COMMAND_SIZE - m_nCount;
  90. }
  91. else
  92. nReadCount = MAX_COMMAND_SIZE - m_nCount;
  93. }
  94. else
  95. nReadCount = MAX_COMMAND_SIZE - m_nCount;
  96. // read new data
  97. m_nCount += ( m_pConnection->Read( m_pBuffer + m_nCount, nReadCount, dwTimeout ) );
  98. // initialize pointer
  99. m_pBufferPtr = m_pBuffer;
  100. // parse input bytes
  101. while( m_nCount >= MIN_COMMAND_SIZE )
  102. {
  103. // check header tag
  104. if( m_pBuffer[ LENGTH_POS ] & HEADER_TAG )
  105. {
  106. // validate possibility of lenght
  107. WORD bLength = ( m_pBuffer[LENGTH_POS] & 0x7F ) + 1;
  108. if( bLength <= MAX_COMMAND_SIZE )
  109. {
  110. // check current lenght
  111. if( bLength > m_nCount )
  112. return FALSE;
  113. // check address
  114. if( m_bReadAnyAddress ||
  115. ( m_pBuffer[ ADDRESS_POS_0 ] == m_pAddress[0] &&
  116. m_pBuffer[ ADDRESS_POS_1 ] == m_pAddress[1] ) )
  117. {
  118. // length without checksum
  119. BYTE bSubLength = bLength - 1;
  120. // test checksum
  121. BYTE bCheckSum = m_pBufferPtr[ 0 ];
  122. for( INT i = 1; i < bSubLength; i++ )
  123. bCheckSum += m_pBufferPtr[ i ];
  124. bCheckSum &= 0x7F;
  125. // if checksum is ok
  126. if( bCheckSum == m_pBufferPtr[ i ] )
  127. {
  128. // store length
  129. (*pCount) = (INT)bLength;
  130. (*ppBuffer) = m_pBufferPtr;
  131. // shift pointers
  132. m_pBufferPtr += bSubLength;
  133. m_nCount -= bSubLength;
  134. // at least one command received
  135. bResult = TRUE;
  136. }
  137. }
  138. }
  139. }
  140. // shift pointers
  141. m_pBufferPtr ++;
  142. m_nCount --;
  143. }
  144. // if not need to repeat reading
  145. if( !bRepeat )
  146. break;
  147. }
  148. return bResult;
  149. }
  150. BOOL CProtocolPTZPG3AD::Write( PBYTE pBuffer, INT nCount, DWORD dwTimeout, BOOL bHost )
  151. {
  152. bHost;
  153. // validate data length
  154. if( nCount > MAX_COMMAND_SIZE )
  155. return FALSE;
  156. // clear receiver
  157. m_nCount = 0;
  158. m_pBufferPtr = m_pBuffer;
  159. // write data
  160. DWORD dwDataSize = nCount;
  161. return (BOOL)(m_pConnection->Write( pBuffer, dwDataSize, dwTimeout ) == dwDataSize);
  162. }