snap7_communication_base.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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::microseconds(1));
  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. std::unique_lock<std::mutex> lck(m_communication_lock);
  224. // int result = m_snap7_client.AsDBRead(snap7_buf.m_id, snap7_buf.m_start_index, snap7_buf.m_size, snap7_buf.mp_buf_reverse);
  225. char t_buf[20] ;
  226. memset(t_buf, 0, 20);
  227. int result = m_snap7_client.AsDBRead(123, 10, 30, t_buf);
  228. if ( result == 0 )
  229. {
  230. m_snap7_client.WaitAsCompletion(100);
  231. }
  232. unsigned char ccccc = *((unsigned char*)t_buf);
  233. std::cout << " huli test :::: " << " ccccc = " << (unsigned int)ccccc << std::endl;
  234. std::cout << " huli test :::: " << " AsDBRead result = " << result << std::endl;
  235. if ( snap7_buf.m_communication_mode == Snap7_buf::ONCE_COMMUNICATION )
  236. {
  237. snap7_buf.m_communication_mode = Snap7_buf::NO_COMMUNICATION;
  238. }
  239. std::this_thread::sleep_for(std::chrono::milliseconds(m_communication_delay_time_ms));
  240. if (result==0)
  241. {
  242. t_error = reverse_byte(snap7_buf.mp_buf_reverse, snap7_buf.mp_buf_obverse, snap7_buf.m_size);
  243. // unsigned int ccccc = *((unsigned int*)snap7_buf.mp_buf_reverse);
  244. //
  245. // std::cout << " huli test :::: " << " snap7_buf.m_id = " << snap7_buf.m_id << std::endl;
  246. // std::cout << " huli test :::: " << " snap7_buf.m_start_index = " << snap7_buf.m_start_index << std::endl;
  247. // std::cout << " huli test :::: " << " snap7_buf.m_size = " << snap7_buf.m_size << std::endl;
  248. // std::cout << " huli test :::: " << " ccccc = " << (unsigned int)ccccc << std::endl;
  249. if ( t_error != Error_code::SUCCESS )
  250. {
  251. return t_error;
  252. }
  253. std::this_thread::sleep_for(std::chrono::milliseconds(m_communication_delay_time_ms));
  254. return Error_code::SUCCESS;
  255. }
  256. else
  257. {
  258. return Error_manager(Error_code::SNAP7_READ_ERROR, Error_level::MINOR_ERROR,
  259. " Snap7_communication_base::read_data_buf error ");
  260. }
  261. }
  262. return Error_code::SUCCESS;
  263. }
  264. //发送数据, 写入DB块,
  265. Error_manager Snap7_communication_base::write_data_buf(Snap7_buf& snap7_buf)
  266. {
  267. Error_manager t_error;
  268. if ( snap7_buf.m_communication_mode != Snap7_buf::NO_COMMUNICATION)
  269. {
  270. t_error = reverse_byte(snap7_buf.mp_buf_obverse, snap7_buf.mp_buf_reverse, snap7_buf.m_size);
  271. if (t_error != Error_code::SUCCESS)
  272. {
  273. return t_error;
  274. }
  275. std::unique_lock<std::mutex> lck(m_communication_lock);
  276. int result = m_snap7_client.AsDBWrite(snap7_buf.m_id, snap7_buf.m_start_index, snap7_buf.m_size,
  277. snap7_buf.mp_buf_reverse);
  278. if ( result == 0 )
  279. {
  280. m_snap7_client.WaitAsCompletion(100);
  281. }
  282. std::cout << " huli test :::: " << " AsDBWrite result = " << result << std::endl;
  283. if ( snap7_buf.m_communication_mode != Snap7_buf::ONCE_COMMUNICATION )
  284. {
  285. snap7_buf.m_communication_mode = Snap7_buf::NO_COMMUNICATION;
  286. }
  287. std::this_thread::sleep_for(std::chrono::milliseconds(m_communication_delay_time_ms));
  288. if (result == 0)
  289. {
  290. std::this_thread::sleep_for(std::chrono::milliseconds(m_communication_delay_time_ms));
  291. return Error_code::SUCCESS;
  292. }
  293. else
  294. {
  295. return Error_manager(Error_code::SNAP7_WRITE_ERROR, Error_level::MINOR_ERROR,
  296. " Snap7_communication_base::write_data_buf error ");
  297. }
  298. }
  299. return Error_code::SUCCESS;
  300. }
  301. //数据颠倒
  302. Error_manager Snap7_communication_base::reverse_byte(void* p_buf_in, void* p_buf_out, int size)
  303. {
  304. if ( p_buf_in == NULL || p_buf_out == NULL )
  305. {
  306. return Error_manager(Error_code::POINTER_IS_NULL, Error_level::MINOR_ERROR,
  307. " POINTER IS NULL ");
  308. }
  309. char* tp_in=(char*)p_buf_in;
  310. char* tp_out=(char*)p_buf_out;
  311. // for(int i=0;i<size;++i)
  312. // {
  313. // tp_out[i]=tp_in[size-i-1];
  314. // }
  315. for(int i=0;i<size;++i)
  316. {
  317. tp_out[i]=tp_in[i];
  318. }
  319. return Error_code::SUCCESS;
  320. }
  321. //更新数据
  322. Error_manager Snap7_communication_base::updata_receive_buf()
  323. {
  324. return Error_code::SUCCESS;
  325. }
  326. Error_manager Snap7_communication_base::updata_send_buf()
  327. {
  328. return Error_code::SUCCESS;
  329. }