snap7_communication_base.cpp 10 KB

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