Channel.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #include "stdafx.h"
  2. #include "kernel.h"
  3. #include "resource.h"
  4. #pragma warning (disable : 4244)
  5. //---------------------------------------------- CChannel ---------
  6. IMPLEMENT_SERIAL(CChannel, CObject, 0)
  7. CChannel::CChannel():CObject()
  8. {
  9. m_nCurDeviceNo = 0;
  10. m_nCommunicateType = 0; // 通信类型 0: 串口通信协议 1:网络通信协议
  11. }
  12. CChannel::~CChannel()
  13. {
  14. for (int i = 0; i < m_Devices.GetSize(); i++)
  15. {
  16. if( m_Devices[i] )
  17. {
  18. delete m_Devices[i];
  19. }
  20. }
  21. m_Devices.RemoveAll();
  22. }
  23. CChannel &CChannel::operator=(CChannel& comport)
  24. {
  25. m_nCommunicateType = comport.m_nCommunicateType;
  26. m_strName = comport.m_strName;
  27. return *this;
  28. }
  29. CChannel* CChannel::Clone(void)
  30. {
  31. CChannel* pComport = new CChannel;
  32. *pComport = *this;
  33. return pComport;
  34. }
  35. void CChannel::Serialize(CArchive& ar)
  36. {
  37. CObject::Serialize(ar);
  38. if(ar.IsStoring())
  39. {
  40. ar << m_nCommunicateType;
  41. ar << m_strName;
  42. }
  43. else
  44. {
  45. ar >> m_nCommunicateType;
  46. ar >> m_strName;
  47. }
  48. m_Devices.Serialize(ar);
  49. if ( !ar.IsStoring() )
  50. {
  51. int size = m_Devices.GetSize();
  52. for (int i = 0; i < size; i++)
  53. {
  54. CDevice* pDev = m_Devices.GetAt(i);
  55. pDev->m_pChannel = this;
  56. }
  57. }
  58. }
  59. // 取的使用的设备个数
  60. int CChannel::GetUsedDeviceNum()
  61. {
  62. int nUsedNum = 0;
  63. int size = m_Devices.GetSize();
  64. for (int i = 0; i < size; i++)
  65. {
  66. CDevice* pDev = m_Devices.GetAt(i);
  67. if (pDev->m_bUsed)
  68. {
  69. nUsedNum++;
  70. }
  71. }
  72. return nUsedNum;
  73. }
  74. // 找到下一个通讯的设备
  75. void CChannel::MoveToNextDevice()
  76. {
  77. // 有控制命令的话优先处理
  78. int size = m_Devices.GetSize();
  79. for (int i = 0; i < size; i++)
  80. {
  81. CDevice* pDev = m_Devices.GetAt(i);
  82. if (pDev->m_bUsed && pDev->m_bHasAction)
  83. {
  84. m_nCurDeviceNo = i;
  85. return;
  86. }
  87. }
  88. // 否则轮询到下一个设备
  89. BOOL bUsed = false;
  90. do
  91. {
  92. m_nCurDeviceNo++;
  93. m_nCurDeviceNo = m_nCurDeviceNo % (m_Devices.GetSize());
  94. bUsed = m_Devices[m_nCurDeviceNo]->m_bUsed;
  95. } while (!bUsed);
  96. }
  97. void CChannel::FreshData(void)
  98. {
  99. if (0 == GetUsedDeviceNum())
  100. {
  101. return;
  102. }
  103. }