snap7_communication_base.cpp 12 KB

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