snap7_communication_base.cpp 10 KB

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