snap7_communication_base.cpp 9.4 KB

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