snap7_buf.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. m_variable_information_vector=other.m_variable_information_vector;
  26. }
  27. }
  28. Snap7_buf& Snap7_buf::operator =(const Snap7_buf& other)
  29. {
  30. m_id = other.m_id;
  31. m_start_index = other.m_start_index;
  32. m_communication_mode = other.m_communication_mode;
  33. if ( other.m_size > 0 && other.mp_buf_obverse != nullptr && other.mp_buf_reverse != nullptr)
  34. {
  35. mp_buf_obverse = (void*)malloc(other.m_size);
  36. memcpy(mp_buf_obverse, other.mp_buf_obverse, other.m_size);
  37. mp_buf_reverse = (void*)malloc(other.m_size);
  38. memcpy(mp_buf_reverse, other.mp_buf_reverse, other.m_size);
  39. m_size = other.m_size;
  40. m_variable_information_vector=other.m_variable_information_vector;
  41. }
  42. }
  43. Snap7_buf::~Snap7_buf()
  44. {
  45. if ( mp_buf_obverse )
  46. {
  47. free(mp_buf_obverse);
  48. mp_buf_obverse = NULL;
  49. }
  50. if ( mp_buf_reverse )
  51. {
  52. free(mp_buf_reverse);
  53. mp_buf_reverse = NULL;
  54. }
  55. }
  56. Snap7_buf::Snap7_buf(int id, int start_index, int size,
  57. std::vector<Snap7_buf::Variable_information>& variable_information_vector,Communication_mode communication_mode)
  58. {
  59. m_id = id;
  60. m_start_index = start_index;
  61. m_communication_mode = communication_mode;
  62. if ( size > 0)
  63. {
  64. mp_buf_obverse = (void*)malloc(size);
  65. memset(mp_buf_obverse, 0, size);
  66. mp_buf_reverse = (void*)malloc(size);
  67. memset(mp_buf_reverse, 0, size);
  68. m_size = size;
  69. m_variable_information_vector=variable_information_vector;
  70. }
  71. }
  72. Snap7_buf::Snap7_buf(int id, int start_index, int size, void* p_buf_obverse, void* p_buf_reverse,
  73. std::vector<Snap7_buf::Variable_information>& variable_information_vector, Communication_mode communication_mode)
  74. {
  75. m_id = id;
  76. m_start_index = start_index;
  77. m_communication_mode = communication_mode;
  78. if ( size > 0 && p_buf_obverse != nullptr && p_buf_reverse != nullptr)
  79. {
  80. mp_buf_obverse = (void*)malloc(size);
  81. memcpy(mp_buf_obverse, p_buf_obverse, size);
  82. mp_buf_reverse = (void*)malloc(size);
  83. memcpy(mp_buf_reverse, p_buf_reverse, size);
  84. m_size = size;
  85. m_variable_information_vector=variable_information_vector;
  86. }
  87. }
  88. void Snap7_buf::init(int id, int start_index, int size,
  89. std::vector<Snap7_buf::Variable_information>& variable_information_vector,Communication_mode communication_mode)
  90. {
  91. m_id = id;
  92. m_start_index = start_index;
  93. m_communication_mode = communication_mode;
  94. if ( mp_buf_obverse )
  95. {
  96. free(mp_buf_obverse);
  97. mp_buf_obverse = NULL;
  98. }
  99. if ( mp_buf_reverse )
  100. {
  101. free(mp_buf_reverse);
  102. mp_buf_reverse = NULL;
  103. }
  104. if ( size > 0)
  105. {
  106. mp_buf_obverse = (void*)malloc(size);
  107. memset(mp_buf_obverse, 0, size);
  108. mp_buf_reverse = (void*)malloc(size);
  109. memset(mp_buf_reverse, 0, size);
  110. m_size = size;
  111. m_variable_information_vector=variable_information_vector;
  112. }
  113. }
  114. void Snap7_buf::init(int id, int start_index, int size, void* p_buf_obverse, void* p_buf_reverse,
  115. std::vector<Snap7_buf::Variable_information>& variable_information_vector, Communication_mode communication_mode)
  116. {
  117. m_id = id;
  118. m_start_index = start_index;
  119. m_communication_mode = communication_mode;
  120. if ( mp_buf_obverse )
  121. {
  122. free(mp_buf_obverse);
  123. mp_buf_obverse = NULL;
  124. }
  125. if ( mp_buf_reverse )
  126. {
  127. free(mp_buf_reverse);
  128. mp_buf_reverse = NULL;
  129. }
  130. if ( size > 0 && p_buf_obverse != nullptr && p_buf_reverse != nullptr)
  131. {
  132. mp_buf_obverse = (void*)malloc(size);
  133. memcpy(mp_buf_obverse, p_buf_obverse, size);
  134. mp_buf_reverse = (void*)malloc(size);
  135. memcpy(mp_buf_reverse, p_buf_reverse, size);
  136. m_size = size;
  137. m_variable_information_vector=variable_information_vector;
  138. }
  139. }
  140. //正序数据 转为 倒序数据
  141. void Snap7_buf::obverse_to_reverse()
  142. {
  143. char *p_in=(char*)mp_buf_obverse;
  144. char *p_out=(char*)mp_buf_reverse;
  145. for ( auto &iter:m_variable_information_vector)
  146. {
  147. for (int i = 0; i < iter.m_variable_count; ++i)
  148. {
  149. for (int j = 0; j < iter.m_variable_size; ++j)
  150. {
  151. p_out[iter.m_variable_index+iter.m_variable_size*i+j] = p_in[iter.m_variable_index+iter.m_variable_size*(i+1)-j-1];
  152. }
  153. }
  154. // for (int i = 0; i < iter.m_variable_count; ++i)
  155. // {
  156. // for (int j = iter.m_variable_index*i; j < iter.m_variable_index*i+iter.m_variable_size ; ++j)
  157. // {
  158. // p_out[j] = p_in[iter.m_variable_index*i+iter.m_variable_size - j -1];
  159. // }
  160. // }
  161. }
  162. }
  163. //倒序数据 转为 正序数据
  164. void Snap7_buf::reverse_to_obverse()
  165. {
  166. char *p_in=(char*)mp_buf_reverse;
  167. char *p_out=(char*)mp_buf_obverse;
  168. for ( auto &iter:m_variable_information_vector)
  169. {
  170. for (int i = 0; i < iter.m_variable_count; ++i)
  171. {
  172. for (int j = 0; j < iter.m_variable_size; ++j)
  173. {
  174. p_out[iter.m_variable_index+iter.m_variable_size*i+j] = p_in[iter.m_variable_index+iter.m_variable_size*(i+1)-j-1];
  175. }
  176. }
  177. // for (int i = 0; i < iter.m_variable_count; ++i)
  178. // {
  179. // for (int j = iter.m_variable_index*i; j < iter.m_variable_index*i+iter.m_variable_size ; ++j)
  180. // {
  181. // p_out[j] = p_in[iter.m_variable_index*i+iter.m_variable_size - j -1];
  182. // }
  183. // }
  184. }
  185. }
  186. int Snap7_buf::get_id()
  187. {
  188. return m_id;
  189. }
  190. int Snap7_buf::get_start_index()
  191. {
  192. return m_start_index;
  193. }
  194. int Snap7_buf::get_size()
  195. {
  196. return m_size;
  197. }
  198. void* Snap7_buf::get_buf_obverse()
  199. {
  200. return mp_buf_obverse;
  201. }
  202. void* Snap7_buf::get_buf_reverse()
  203. {
  204. return mp_buf_reverse;
  205. }
  206. Snap7_buf::Communication_mode Snap7_buf::get_communication_mode()
  207. {
  208. return m_communication_mode;
  209. }
  210. void Snap7_buf::set_communication_mode(Communication_mode communication_mode)
  211. {
  212. m_communication_mode = communication_mode;
  213. }