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.

212 lines
5.2 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: 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. for( INT i = 1; i < bSubLength; i++ )
  126. bCheckSum ^= m_pBufferPtr[ i ];
  127. bCheckSum |= 0x80;
  128. // if checksum ok
  129. if( bCheckSum == m_pBufferPtr[ i ] )
  130. {
  131. BOOL bFromCamera = m_pBufferPtr[ CFLAG_POS ] != PTZ_MIC_1_300::CF_FULL_DUPLEX &&
  132. m_pBufferPtr[ CFLAG_POS ] != PTZ_MIC_1_300::CF_HALF_DUPLEX &&
  133. m_pBufferPtr[ CFLAG_POS ] != PTZ_MIC_1_300::CF_SIMPLEX;
  134. // if right direction
  135. if( bFromCamera )
  136. {
  137. // store length
  138. (*pCount) = (INT)bLength;
  139. (*ppBuffer) = m_pBufferPtr;
  140. // at least one command received
  141. bResult = TRUE;
  142. }
  143. else
  144. {
  145. // probably received echo - repeat reading
  146. bRepeat = TRUE;
  147. }
  148. // shift pointers
  149. m_pBufferPtr += bSubLength;
  150. m_nCount -= bSubLength;
  151. // at least one command received
  152. bResult = TRUE;
  153. }
  154. }
  155. }
  156. }
  157. // shift pointers
  158. m_pBufferPtr ++;
  159. m_nCount --;
  160. }
  161. // if not need to repeat reading
  162. if( !bRepeat )
  163. break;
  164. }
  165. return bResult;
  166. }
  167. BOOL CProtocolMic1300PTZ::Write( PBYTE pBuffer, INT nCount, DWORD dwTimeout, BOOL bHost )
  168. {
  169. bHost;
  170. // validate data length
  171. if( nCount > MAX_COMMAND_SIZE )
  172. return FALSE;
  173. // clear receiver
  174. m_nCount = 0;
  175. m_pBufferPtr = m_pBuffer;
  176. // write data
  177. DWORD dwDataSize = nCount;
  178. return (BOOL)(m_pConnection->Write( pBuffer, dwDataSize, dwTimeout ) == dwDataSize);
  179. }