snap7_buf.cpp 7.0 KB

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