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.

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