snap7_communication_base.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. //
  2. // Created by huli on 2020/9/25.
  3. //
  4. #include "snap7_communication_base.h"
  5. Snap7_communication_base::Snap7_communication_base() {
  6. m_communication_status = SNAP7_COMMUNICATION_UNKNOWN;
  7. m_communication_delay_time_ms = SNAP7_COMMUNICATION_DELAY_TIME_MS;
  8. mp_communication_thread = nullptr;
  9. }
  10. Snap7_communication_base::~Snap7_communication_base() {
  11. communication_uninit();
  12. }
  13. //初始化 通信 模块。如下三选一
  14. Error_manager Snap7_communication_base::communication_init(std::string ip) {
  15. m_ip_string = ip;
  16. Error_manager t_error = communication_connect(m_ip_string);
  17. if (t_error != Error_code::SUCCESS) {
  18. //连接失败, 不要直接返回, 而是改为断连, 后面继续启动线程, (线程内部有重连功能)
  19. printf("---Debug %s %d : CONNECT ERROR !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n", __func__, __LINE__);
  20. m_communication_status = SNAP7_COMMUNICATION_DISCONNECT;
  21. } else {
  22. printf("---Debug %s %d : CONNECT SUCCESS !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n", __func__, __LINE__);
  23. m_communication_status = SNAP7_COMMUNICATION_READY;
  24. }
  25. return communication_run();
  26. }
  27. //反初始化 通信 模块。
  28. Error_manager Snap7_communication_base::communication_uninit() {
  29. //关闭线程并回收资源
  30. if (mp_communication_thread) {
  31. m_communication_condition.kill_all();
  32. }
  33. if (mp_communication_thread) {
  34. mp_communication_thread->join();
  35. delete mp_communication_thread;
  36. mp_communication_thread = nullptr;
  37. }
  38. //清空map
  39. {
  40. std::unique_lock<std::mutex> t_lock(m_receive_buf_lock);
  41. m_receive_buf_map.clear();
  42. }
  43. {
  44. std::unique_lock<std::mutex> t_lock(m_send_buf_lock);
  45. m_send_buf_map.clear();
  46. }
  47. communication_disconnect();
  48. m_communication_status = SNAP7_COMMUNICATION_UNKNOWN;
  49. return Error_code::SUCCESS;
  50. }
  51. //唤醒s7通信线程
  52. Error_manager Snap7_communication_base::communication_start() {
  53. m_communication_condition.notify_all(true);
  54. return Error_code::SUCCESS;
  55. }
  56. //停止s7通信线程
  57. Error_manager Snap7_communication_base::communication_stop() {
  58. m_communication_condition.notify_all(false);
  59. return Error_code::SUCCESS;
  60. }
  61. Snap7_communication_base::Snap7_communication_statu Snap7_communication_base::get_status() {
  62. return m_communication_status;
  63. }
  64. //通信连接
  65. Error_manager Snap7_communication_base::communication_connect(std::string ip_string) {
  66. std::unique_lock<std::mutex> t_lock(m_communication_lock);
  67. int result = m_snap7_client.ConnectTo(ip_string.c_str(), 0, 1);
  68. std::this_thread::sleep_for(std::chrono::milliseconds(m_communication_delay_time_ms));
  69. printf("---Debug %s %d : connect %s result is %d \n", __func__, __LINE__, ip_string.c_str(), result);
  70. if (result == 0) {
  71. return Error_code::SUCCESS;
  72. } else {
  73. return {Error_code::SNAP7_CONNECT_ERROR, Error_level::MINOR_ERROR,
  74. " Snap7_communication_base::communication_connect error "};
  75. }
  76. }
  77. //启动通信, run thread
  78. Error_manager Snap7_communication_base::communication_run() {
  79. //启动4个线程。
  80. //接受线程默认循环, 内部的nn_recv进行等待, 超时1ms
  81. m_communication_condition.reset(false, false, false);
  82. mp_communication_thread = new std::thread(&Snap7_communication_base::communication_thread, this);
  83. return Error_code::SUCCESS;
  84. }
  85. //通信断连
  86. Error_manager Snap7_communication_base::communication_disconnect() {
  87. std::unique_lock<std::mutex> t_lock(m_communication_lock);
  88. int result = m_snap7_client.Disconnect();
  89. std::this_thread::sleep_for(std::chrono::milliseconds(m_communication_delay_time_ms));
  90. if (result == 0) {
  91. return Error_code::SUCCESS;
  92. } else {
  93. return {Error_code::SNAP7_DISCONNECT_ERROR, Error_level::MINOR_ERROR,
  94. " Snap7_communication_base::communication_disconnect error "};
  95. }
  96. return Error_code::SUCCESS;
  97. }
  98. //mp_communication_thread线程的执行函数, 负责进行s7的通信
  99. void Snap7_communication_base::communication_thread() {
  100. //LOG(INFO) << " ---Snap7_communication_base::communication_thread()--- "<< this;
  101. Error_manager t_error;
  102. while (m_communication_condition.is_alive()) {
  103. m_communication_condition.wait_for_millisecond(1);
  104. //s7的通信时间较长, 所以将发送和接受分开
  105. //发送多个时, 必须加锁后一起发送, 不允许分段写入, 防止数据错误
  106. if (m_communication_condition.is_alive()) {
  107. std::this_thread::sleep_for(std::chrono::milliseconds(m_communication_delay_time_ms));
  108. std::this_thread::yield();
  109. switch (m_communication_status) {
  110. case SNAP7_COMMUNICATION_READY:
  111. case SNAP7_COMMUNICATION_RECEIVE: {
  112. {
  113. std::unique_lock<std::mutex> t_lock(m_receive_buf_lock);
  114. auto iter = m_receive_buf_map.begin();
  115. for (; iter != m_receive_buf_map.end(); ++iter) {
  116. //接受数据, 读取DB块,
  117. t_error = read_data_buf(iter->second);
  118. if (t_error == Error_code::SNAP7_READ_ERROR) {
  119. m_communication_status = SNAP7_COMMUNICATION_DISCONNECT;
  120. printf("---Debug %s %d : t_error = %s \n", __func__, __LINE__, t_error.to_string().c_str());
  121. break;
  122. }
  123. }
  124. if (iter != m_receive_buf_map.end()) {
  125. break;
  126. }
  127. }
  128. //注:数据更新放在锁的外面, 防止重复加锁....
  129. if(updata_receive_buf() != Error_code::SUCCESS) {
  130. m_communication_status = SNAP7_COMMUNICATION_DISCONNECT;
  131. }
  132. else {
  133. m_communication_status = SNAP7_COMMUNICATION_SEND;
  134. }
  135. break;
  136. }
  137. case SNAP7_COMMUNICATION_SEND: {
  138. //注:数据更新放在锁的外面, 防止重复加锁....
  139. updata_send_buf();
  140. {
  141. std::unique_lock<std::mutex> t_lock(m_send_buf_lock);
  142. auto iter = m_send_buf_map.begin();
  143. for (; iter != m_send_buf_map.end(); ++iter) {
  144. //发送数据, 写入DB块,
  145. t_error = write_data_buf(iter->second);
  146. if (t_error == Error_code::SNAP7_WRITE_ERROR) {
  147. m_communication_status = SNAP7_COMMUNICATION_DISCONNECT;
  148. printf("---Debug %s %d : t_error = %s \n", __func__, __LINE__, t_error.to_string().c_str());
  149. break;
  150. }
  151. }
  152. if (iter != m_send_buf_map.end()) {
  153. break;
  154. }
  155. }
  156. m_communication_status = SNAP7_COMMUNICATION_RECEIVE;
  157. break;
  158. }
  159. case SNAP7_COMMUNICATION_DISCONNECT: {
  160. //重连
  161. printf("---Debug %s %d : find plc connection error, trying to reconnect.\n", __func__, __LINE__);
  162. communication_disconnect();
  163. std::this_thread::sleep_for(std::chrono::milliseconds(m_communication_delay_time_ms));
  164. t_error = communication_connect(m_ip_string);
  165. if (t_error != Error_code::SUCCESS) {
  166. //连接失败, 不要直接返回, 而是改为断连, 后面继续启动线程, (线程内部有重连功能)
  167. m_communication_status = SNAP7_COMMUNICATION_DISCONNECT;
  168. } else {
  169. m_communication_status = SNAP7_COMMUNICATION_READY;
  170. }
  171. std::this_thread::sleep_for(std::chrono::milliseconds(m_communication_delay_time_ms));
  172. break;
  173. }
  174. default: {
  175. break;
  176. }
  177. }
  178. }
  179. }
  180. //LOG(INFO) << " Communication_socket_base::send_data_thread end "<< this;
  181. }
  182. //接受数据, 读取DB块,
  183. Error_manager Snap7_communication_base::read_data_buf(Snap7_buf &snap7_buf) {
  184. Error_manager t_error;
  185. if (snap7_buf.m_communication_mode != Snap7_buf::NO_COMMUNICATION) {
  186. if (snap7_buf.m_communication_mode == Snap7_buf::ONCE_COMMUNICATION) {
  187. snap7_buf.m_communication_mode = Snap7_buf::NO_COMMUNICATION;
  188. }
  189. std::unique_lock<std::mutex> lck(m_communication_lock);
  190. // printf("---Debug %s %d : snap7_buf,m_id %d \n", __func__, __LINE__, snap7_buf.m_id);
  191. // printf("---Debug %s %d : snap7_buf,m_start_index %d\n", __func__, __LINE__, snap7_buf.m_start_index);
  192. // printf("---Debug %s %d : snap7_buf,m_size %d\n", __func__, __LINE__, snap7_buf.m_size);
  193. int result = m_snap7_client.AsDBRead(snap7_buf.m_id, snap7_buf.m_start_index, snap7_buf.m_size,
  194. snap7_buf.mp_buf_reverse);
  195. // std::this_thread::sleep_for(std::chrono::milliseconds(10));
  196. if (result == 0) {
  197. m_snap7_client.WaitAsCompletion(100);
  198. //倒序数据 转为 正序数据
  199. snap7_buf.reverse_to_obverse();
  200. } else {
  201. std::cout << " huli test :::: " << " result = " << result << std::endl;
  202. return {Error_code::SNAP7_READ_ERROR, Error_level::MINOR_ERROR,
  203. " Snap7_communication_base::read_data_buf error "};
  204. }
  205. }
  206. return Error_code::SUCCESS;
  207. }
  208. //发送数据, 写入DB块,
  209. Error_manager Snap7_communication_base::write_data_buf(Snap7_buf &snap7_buf) {
  210. Error_manager t_error;
  211. if (snap7_buf.m_communication_mode != Snap7_buf::NO_COMMUNICATION) {
  212. if (snap7_buf.m_communication_mode == Snap7_buf::ONCE_COMMUNICATION) {
  213. snap7_buf.m_communication_mode = Snap7_buf::NO_COMMUNICATION;
  214. }
  215. //正序数据 转为 倒序数据
  216. snap7_buf.obverse_to_reverse();
  217. std::unique_lock<std::mutex> lck(m_communication_lock);
  218. unsigned short a = 0;
  219. memcpy(&a, snap7_buf.mp_buf_reverse, 2);
  220. //printf("id %d start %d size :%d value :%d\n",snap7_buf.m_id, snap7_buf.m_start_index, snap7_buf.m_size,a);
  221. int result = m_snap7_client.AsDBWrite(snap7_buf.m_id, snap7_buf.m_start_index, snap7_buf.m_size,
  222. snap7_buf.mp_buf_reverse);
  223. if (result == 0) {
  224. if (0 != m_snap7_client.WaitAsCompletion(1000)) {
  225. }
  226. } else {
  227. printf("---Debug %s %d : snap7_buf,m_id %d \n", __func__, __LINE__, snap7_buf.m_id);
  228. printf("---Debug %s %d : snap7_buf,m_start_index %d\n", __func__, __LINE__, snap7_buf.m_start_index);
  229. printf("---Debug %s %d : snap7_buf,m_size %d\n", __func__, __LINE__, snap7_buf.m_size);
  230. return {Error_code::SNAP7_WRITE_ERROR, Error_level::MINOR_ERROR,
  231. " Snap7_communication_base::write_data_buf error "};
  232. }
  233. }
  234. return Error_code::SUCCESS;
  235. }
  236. //数据颠倒
  237. Error_manager Snap7_communication_base::reverse_byte(void *p_buf_in, void *p_buf_out, int size) {
  238. if (p_buf_in == nullptr || p_buf_out == nullptr) {
  239. return {Error_code::POINTER_IS_NULL, Error_level::MINOR_ERROR,
  240. " POINTER IS NULL "};
  241. }
  242. char *tp_in = (char *) p_buf_in;
  243. char *tp_out = (char *) p_buf_out;
  244. for (int i = 0; i < size; ++i) {
  245. tp_out[i] = tp_in[i];
  246. }
  247. return Error_code::SUCCESS;
  248. }
  249. //更新数据
  250. Error_manager Snap7_communication_base::updata_receive_buf() {
  251. return Error_code::SUCCESS;
  252. }
  253. Error_manager Snap7_communication_base::updata_send_buf() {
  254. return Error_code::SUCCESS;
  255. }