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.

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