snap7_communication_base.cpp 9.6 KB

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