snap7_communication_base.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. //修改通信延时, 单位ms
  102. Error_manager Snap7_communication_base::set_communication_delay_time_ms(int time)
  103. {
  104. m_communication_delay_time_ms = time;
  105. return Error_code::SUCCESS;
  106. }
  107. //通信连接
  108. Error_manager Snap7_communication_base::communication_connect(std::string ip_string)
  109. {
  110. std::unique_lock<std::mutex> t_lock(m_communication_lock);
  111. int result=m_snap7_client.ConnectTo(ip_string.c_str(),0,1);
  112. std::this_thread::sleep_for(std::chrono::milliseconds(m_communication_delay_time_ms));
  113. if (result==0)
  114. {
  115. return Error_code::SUCCESS;
  116. }
  117. else
  118. {
  119. return Error_manager(Error_code::SNAP7_CONNECT_ERROR, Error_level::MINOR_ERROR,
  120. " Snap7_communication_base::communication_connect error ");
  121. }
  122. return Error_code::SUCCESS;
  123. }
  124. //启动通信, run thread
  125. Error_manager Snap7_communication_base::communication_run()
  126. {
  127. //启动4个线程。
  128. //接受线程默认循环, 内部的nn_recv进行等待, 超时1ms
  129. m_communication_condition.reset(false, false, false);
  130. mp_communication_thread = new std::thread(&Snap7_communication_base::communication_thread, this);
  131. return Error_code::SUCCESS;
  132. }
  133. //通信断连
  134. Error_manager Snap7_communication_base::communication_disconnect()
  135. {
  136. std::unique_lock<std::mutex> t_lock(m_communication_lock);
  137. int result=m_snap7_client.Disconnect();
  138. std::this_thread::sleep_for(std::chrono::milliseconds(m_communication_delay_time_ms));
  139. if (result==0)
  140. {
  141. return Error_code::SUCCESS;
  142. }
  143. else
  144. {
  145. return Error_manager(Error_code::SNAP7_DISCONNECT_ERROR, Error_level::MINOR_ERROR,
  146. " Snap7_communication_base::communication_disconnect error ");
  147. }
  148. return Error_code::SUCCESS;
  149. }
  150. //mp_communication_thread线程的执行函数, 负责进行s7的通信
  151. void Snap7_communication_base::communication_thread()
  152. {
  153. LOG(INFO) << " ---Snap7_communication_base::communication_thread()--- "<< this;
  154. Error_manager t_error;
  155. while (m_communication_condition.is_alive())
  156. {
  157. m_communication_condition.wait_for_millisecond(1);
  158. //s7的通信时间较长, 所以将发送和接受分开
  159. //发送多个时, 必须加锁后一起发送, 不允许分段写入, 防止数据错误
  160. if ( m_communication_condition.is_alive() )
  161. {
  162. std::this_thread::sleep_for(std::chrono::milliseconds(m_communication_delay_time_ms));
  163. std::this_thread::yield();
  164. switch ( m_communication_status )
  165. {
  166. case SNAP7_COMMUNICATION_READY:
  167. case SNAP7_COMMUNICATION_RECEIVE:
  168. {
  169. {
  170. std::unique_lock<std::mutex> t_lock(m_receive_buf_lock);
  171. auto iter = m_receive_buf_map.begin();
  172. for (; iter != m_receive_buf_map.end(); ++iter)
  173. {
  174. //接受数据, 读取DB块,
  175. t_error = read_data_buf(iter->second);
  176. if (t_error == Error_code::SNAP7_READ_ERROR)
  177. {
  178. m_communication_status = SNAP7_COMMUNICATION_DISCONNECT;
  179. std::cout << " huli test :::: " << " t_error = " << t_error << std::endl;
  180. break;
  181. }
  182. }
  183. if ( iter != m_receive_buf_map.end() )
  184. {
  185. break;
  186. }
  187. }
  188. //注:数据更新放在锁的外面, 防止重复加锁....
  189. updata_receive_buf();
  190. m_communication_status = SNAP7_COMMUNICATION_SEND;
  191. break;
  192. }
  193. case SNAP7_COMMUNICATION_SEND:
  194. {
  195. //注:数据更新放在锁的外面, 防止重复加锁....
  196. updata_send_buf();
  197. {
  198. std::unique_lock<std::mutex> t_lock(m_send_buf_lock);
  199. auto iter = m_send_buf_map.begin();
  200. for (; iter != m_send_buf_map.end(); ++iter)
  201. {
  202. //发送数据, 写入DB块,
  203. t_error = write_data_buf(iter->second);
  204. if (t_error == Error_code::SNAP7_WRITE_ERROR)
  205. {
  206. m_communication_status = SNAP7_COMMUNICATION_DISCONNECT;
  207. std::cout << " huli test :::: " << " t_error = " << t_error << std::endl;
  208. break;
  209. }
  210. }
  211. if ( iter != m_send_buf_map.end() )
  212. {
  213. break;
  214. }
  215. }
  216. m_communication_status = SNAP7_COMMUNICATION_RECEIVE;
  217. break;
  218. }
  219. case SNAP7_COMMUNICATION_DISCONNECT:
  220. {
  221. //重连
  222. LOG(INFO) << "find plc connection error, trying to reconnect.";
  223. communication_disconnect();
  224. std::this_thread::sleep_for(std::chrono::milliseconds(m_communication_delay_time_ms));
  225. t_error = communication_connect(m_ip_string);
  226. if ( t_error != Error_code::SUCCESS )
  227. {
  228. //连接失败, 不要直接返回, 而是改为断连, 后面继续启动线程, (线程内部有重连功能)
  229. m_communication_status = SNAP7_COMMUNICATION_DISCONNECT;
  230. }
  231. else
  232. {
  233. m_communication_status = SNAP7_COMMUNICATION_READY;
  234. }
  235. std::this_thread::sleep_for(std::chrono::milliseconds(m_communication_delay_time_ms));
  236. break;
  237. }
  238. default:
  239. {
  240. break;
  241. }
  242. }
  243. }
  244. }
  245. LOG(INFO) << " Communication_socket_base::send_data_thread end "<< this;
  246. return;
  247. }
  248. //接受数据, 读取DB块,
  249. Error_manager Snap7_communication_base::read_data_buf(Snap7_buf& snap7_buf)
  250. {
  251. Error_manager t_error;
  252. if ( snap7_buf.m_communication_mode != Snap7_buf::NO_COMMUNICATION)
  253. {
  254. if ( snap7_buf.m_communication_mode == Snap7_buf::ONCE_COMMUNICATION )
  255. {
  256. snap7_buf.m_communication_mode = Snap7_buf::NO_COMMUNICATION;
  257. }
  258. std::unique_lock<std::mutex> lck(m_communication_lock);
  259. int result = m_snap7_client.AsDBRead(snap7_buf.m_id, snap7_buf.m_start_index, snap7_buf.m_size, snap7_buf.mp_buf_reverse);
  260. // std::this_thread::sleep_for(std::chrono::milliseconds(10));
  261. if ( result == 0 )
  262. {
  263. result = m_snap7_client.WaitAsCompletion(100);
  264. // if ( result !=0 )
  265. // {
  266. // return Error_manager(Error_code::SNAP7_READ_ERROR, Error_level::MINOR_ERROR,
  267. // " Snap7_communication_base::read_data_buf error ");
  268. // }
  269. //倒序数据 转为 正序数据
  270. snap7_buf.reverse_to_obverse();
  271. }
  272. else
  273. {
  274. std::cout << " huli test :::: " << " result = " << result << std::endl;
  275. return Error_manager(Error_code::SNAP7_READ_ERROR, Error_level::MINOR_ERROR,
  276. " Snap7_communication_base::read_data_buf error ");
  277. }
  278. }
  279. return Error_code::SUCCESS;
  280. }
  281. //发送数据, 写入DB块,
  282. Error_manager Snap7_communication_base::write_data_buf(Snap7_buf& snap7_buf)
  283. {
  284. Error_manager t_error;
  285. if ( snap7_buf.m_communication_mode != Snap7_buf::NO_COMMUNICATION)
  286. {
  287. if ( snap7_buf.m_communication_mode == Snap7_buf::ONCE_COMMUNICATION )
  288. {
  289. snap7_buf.m_communication_mode = Snap7_buf::NO_COMMUNICATION;
  290. }
  291. //正序数据 转为 倒序数据
  292. snap7_buf.obverse_to_reverse();
  293. std::unique_lock<std::mutex> lck(m_communication_lock);
  294. int result = m_snap7_client.AsDBWrite(snap7_buf.m_id, snap7_buf.m_start_index, snap7_buf.m_size,
  295. snap7_buf.mp_buf_reverse);
  296. // std::this_thread::sleep_for(std::chrono::milliseconds(10));
  297. if ( result == 0 )
  298. {
  299. result = m_snap7_client.WaitAsCompletion(100);
  300. // if ( result !=0 )
  301. // {
  302. // return Error_manager(Error_code::SNAP7_WRITE_ERROR, Error_level::MINOR_ERROR,
  303. // " Snap7_communication_base::write_data_buf error ");
  304. // }
  305. }
  306. else
  307. {
  308. return Error_manager(Error_code::SNAP7_WRITE_ERROR, Error_level::MINOR_ERROR,
  309. " Snap7_communication_base::write_data_buf error ");
  310. }
  311. }
  312. return Error_code::SUCCESS;
  313. }
  314. //数据颠倒
  315. Error_manager Snap7_communication_base::reverse_byte(void* p_buf_in, void* p_buf_out, int size)
  316. {
  317. if ( p_buf_in == NULL || p_buf_out == NULL )
  318. {
  319. return Error_manager(Error_code::POINTER_IS_NULL, Error_level::MINOR_ERROR,
  320. " POINTER IS NULL ");
  321. }
  322. char* tp_in=(char*)p_buf_in;
  323. char* tp_out=(char*)p_buf_out;
  324. // for(int i=0;i<size;++i)
  325. // {
  326. // tp_out[i]=tp_in[size-i-1];
  327. // }
  328. for(int i=0;i<size;++i)
  329. {
  330. tp_out[i]=tp_in[i];
  331. }
  332. return Error_code::SUCCESS;
  333. }
  334. //更新数据
  335. Error_manager Snap7_communication_base::updata_receive_buf()
  336. {
  337. return Error_code::SUCCESS;
  338. }
  339. Error_manager Snap7_communication_base::updata_send_buf()
  340. {
  341. return Error_code::SUCCESS;
  342. }