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.

127 lines
2.7 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: PTZPG3AD.cpp
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file of PTZPG3AD.cpp
  13. //----------------------------------------------------------------------------
  14. #include "stdafx.h"
  15. #ifdef __BORLANDC__
  16. #include "..\LgLib\Comm\PTZAD.h"
  17. #else
  18. #include "PTZAD.h"
  19. #endif
  20. CProtocolPTZAmericanDynamics::CProtocolPTZAmericanDynamics()
  21. {
  22. m_pConnection = NULL;
  23. m_nCount = 0;
  24. m_bAddress = 0;
  25. m_eReadType = READ_TYPE_ACK;
  26. }
  27. CProtocolPTZAmericanDynamics::~CProtocolPTZAmericanDynamics()
  28. {
  29. Destroy();
  30. }
  31. BOOL CProtocolPTZAmericanDynamics::Create( CConnection *pConnection )
  32. {
  33. Destroy();
  34. m_pConnection = pConnection;
  35. m_nCount = 0;
  36. return TRUE;
  37. }
  38. void CProtocolPTZAmericanDynamics::Destroy()
  39. {
  40. m_pConnection = NULL;
  41. m_nCount = 0;
  42. }
  43. void CProtocolPTZAmericanDynamics::SetAddress( BYTE bAddress )
  44. {
  45. m_bAddress = bAddress;
  46. }
  47. BOOL CProtocolPTZAmericanDynamics::Read( PBYTE *ppBuffer, PINT pCount, DWORD dwTimeout, BOOL bHost )
  48. {
  49. bHost;
  50. switch( m_eReadType )
  51. {
  52. case READ_TYPE_ACK:
  53. if( !m_pConnection->Read( m_pBuffer, 1, dwTimeout ) )
  54. return FALSE;
  55. if( m_pBuffer[0] == m_bAddress )
  56. {
  57. (*pCount) = 1;
  58. (*ppBuffer) = m_pBuffer;
  59. return TRUE;
  60. }
  61. break;
  62. case READ_TYPE_POSITION:
  63. while( TRUE )
  64. {
  65. if( m_nCount < 2 )
  66. m_nCount += m_pConnection->Read( m_pBuffer + m_nCount, 2 - m_nCount, dwTimeout );
  67. if( m_nCount < 2 )
  68. return FALSE;
  69. if( m_pBuffer[0] != m_bAddress || m_pBuffer[1] != m_bAddress )
  70. {
  71. memmove( m_pBuffer, m_pBuffer+1, --m_nCount );
  72. continue;
  73. }
  74. m_nCount += ( m_pConnection->Read( m_pBuffer + m_nCount, 13 - m_nCount, dwTimeout ) );
  75. if( m_nCount < 13 )
  76. return FALSE;
  77. BYTE bCheckSum = PTZ_AMERICAN_DYNAMICS::GetCheckSum( m_pBuffer+1, 12 );
  78. if( m_pBuffer[12] != bCheckSum )
  79. {
  80. memmove( m_pBuffer, m_pBuffer+1, --m_nCount );
  81. continue;
  82. }
  83. break;
  84. }
  85. m_nCount = 0;
  86. (*pCount) = 13;
  87. (*ppBuffer) = m_pBuffer;
  88. return TRUE;
  89. }
  90. return FALSE;
  91. }
  92. BOOL CProtocolPTZAmericanDynamics::Write( PBYTE pBuffer, INT nCount, DWORD dwTimeout, BOOL bHost )
  93. {
  94. bHost;
  95. // validate data length
  96. if( nCount > MAX_COMMAND_SIZE )
  97. return FALSE;
  98. // clear receiver
  99. m_nCount = 0;
  100. // write data
  101. DWORD dwDataSize = nCount;
  102. return (BOOL)(m_pConnection->Write( pBuffer, dwDataSize, dwTimeout ) == dwDataSize);
  103. }