snap7_communication_base.cpp 9.6 KB

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