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.

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