snap7_buf.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #include "snap7_buf.h"
  2. #include <string>
  3. #include <string.h>
  4. Snap7_buf::Snap7_buf()
  5. {
  6. m_id = 0;
  7. m_start_index = 0;
  8. m_size = 0;
  9. mp_buf_obverse = nullptr;
  10. mp_buf_reverse = nullptr;
  11. m_communication_mode = NO_COMMUNICATION;
  12. }
  13. Snap7_buf::Snap7_buf(const Snap7_buf& other)
  14. {
  15. m_id = other.m_id;
  16. m_start_index = other.m_start_index;
  17. m_communication_mode = other.m_communication_mode;
  18. if ( other.m_size > 0 && other.mp_buf_obverse != nullptr && other.mp_buf_reverse != nullptr)
  19. {
  20. mp_buf_obverse = (void*)malloc(other.m_size);
  21. memcpy(mp_buf_obverse, other.mp_buf_obverse, other.m_size);
  22. mp_buf_reverse = (void*)malloc(other.m_size);
  23. memcpy(mp_buf_reverse, other.mp_buf_reverse, other.m_size);
  24. m_size = other.m_size;
  25. }
  26. }
  27. Snap7_buf& Snap7_buf::operator =(const Snap7_buf& other)
  28. {
  29. m_id = other.m_id;
  30. m_start_index = other.m_start_index;
  31. m_communication_mode = other.m_communication_mode;
  32. if ( other.m_size > 0 && other.mp_buf_obverse != nullptr && other.mp_buf_reverse != nullptr)
  33. {
  34. mp_buf_obverse = (void*)malloc(other.m_size);
  35. memcpy(mp_buf_obverse, other.mp_buf_obverse, other.m_size);
  36. mp_buf_reverse = (void*)malloc(other.m_size);
  37. memcpy(mp_buf_reverse, other.mp_buf_reverse, other.m_size);
  38. m_size = other.m_size;
  39. }
  40. }
  41. Snap7_buf::~Snap7_buf()
  42. {
  43. if ( mp_buf_obverse )
  44. {
  45. free(mp_buf_obverse);
  46. mp_buf_obverse = NULL;
  47. }
  48. if ( mp_buf_reverse )
  49. {
  50. free(mp_buf_reverse);
  51. mp_buf_reverse = NULL;
  52. }
  53. }
  54. Snap7_buf::Snap7_buf(int id, int start_index, int size,
  55. std::vector<Snap7_buf::Variable_information>& variable_information_vector,Communication_mode communication_mode)
  56. {
  57. m_id = id;
  58. m_start_index = start_index;
  59. m_communication_mode = communication_mode;
  60. if ( size > 0)
  61. {
  62. mp_buf_obverse = (void*)malloc(size);
  63. memset(mp_buf_obverse, 0, size);
  64. mp_buf_reverse = (void*)malloc(size);
  65. memset(mp_buf_reverse, 0, size);
  66. m_size = size;
  67. m_variable_information_vector=variable_information_vector;
  68. }
  69. }
  70. Snap7_buf::Snap7_buf(int id, int start_index, int size, void* p_buf_obverse, void* p_buf_reverse,
  71. std::vector<Snap7_buf::Variable_information>& variable_information_vector, Communication_mode communication_mode)
  72. {
  73. m_id = id;
  74. m_start_index = start_index;
  75. m_communication_mode = communication_mode;
  76. if ( size > 0 && p_buf_obverse != nullptr && p_buf_reverse != nullptr)
  77. {
  78. mp_buf_obverse = (void*)malloc(size);
  79. memcpy(mp_buf_obverse, p_buf_obverse, size);
  80. mp_buf_reverse = (void*)malloc(size);
  81. memcpy(mp_buf_reverse, p_buf_reverse, size);
  82. m_size = size;
  83. m_variable_information_vector=variable_information_vector;
  84. }
  85. }
  86. //正序数据 转为 倒序数据
  87. void Snap7_buf::obverse_to_reverse()
  88. {
  89. char *p_in=(char*)mp_buf_obverse;
  90. char *p_out=(char*)mp_buf_reverse;
  91. for ( auto &iter:m_variable_information_vector)
  92. {
  93. for (int i = 0; i < iter.m_variable_count; ++i)
  94. {
  95. for (int j = iter.m_variable_index*i; j < iter.m_variable_index*i+iter.m_variable_size ; ++j)
  96. {
  97. p_out[j] = p_in[iter.m_variable_index*i+iter.m_variable_size - j -1];
  98. }
  99. }
  100. }
  101. }
  102. //倒序数据 转为 正序数据
  103. void Snap7_buf::reverse_to_obverse()
  104. {
  105. char *p_in=(char*)mp_buf_reverse;
  106. char *p_out=(char*)mp_buf_obverse;
  107. for ( auto &iter:m_variable_information_vector)
  108. {
  109. for (int i = 0; i < iter.m_variable_count; ++i)
  110. {
  111. for (int j = iter.m_variable_index*i; j < iter.m_variable_index*i+iter.m_variable_size ; ++j)
  112. {
  113. p_out[j] = p_in[iter.m_variable_index*i+iter.m_variable_size - j -1];
  114. }
  115. }
  116. }
  117. }
  118. int Snap7_buf::get_id()
  119. {
  120. return m_id;
  121. }
  122. int Snap7_buf::get_start_index()
  123. {
  124. return m_start_index;
  125. }
  126. int Snap7_buf::get_size()
  127. {
  128. return m_size;
  129. }
  130. void* Snap7_buf::get_buf_obverse()
  131. {
  132. return mp_buf_obverse;
  133. }
  134. void* Snap7_buf::get_buf_reverse()
  135. {
  136. return mp_buf_reverse;
  137. }
  138. Snap7_buf::Communication_mode Snap7_buf::get_communication_mode()
  139. {
  140. return m_communication_mode;
  141. }
  142. void Snap7_buf::set_communication_mode(Communication_mode communication_mode)
  143. {
  144. m_communication_mode = communication_mode;
  145. }