For DOS Protected Mode version of sensor core software
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.

213 lines
5.0 KiB

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