snap7_buf.cpp 6.0 KB

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