snap7_communication_base.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. //
  2. // Created by huli on 2020/9/25.
  3. //
  4. #include "snap7_communication_base.h"
  5. Snap7_communication_base::Snap7_communication_base() {
  6. m_communication_status = SNAP7_COMMUNICATION_UNKNOWN;
  7. m_communication_delay_time_ms = SNAP7_COMMUNICATION_DELAY_TIME_MS;
  8. mp_communication_thread = NULL;
  9. }
  10. Snap7_communication_base::~Snap7_communication_base() {
  11. communication_uninit();
  12. }
  13. //初始化 通信 模块。如下三选一
  14. Error_manager Snap7_communication_base::communication_init() {
  15. plcJsonConfig config(ETC_PATH"/etc/plc.json");
  16. return communication_init(config.ip());
  17. }
  18. Error_manager Snap7_communication_base::communication_init(std::string ip) {
  19. m_ip_string = ip;
  20. Error_manager t_error = communication_connect(m_ip_string);
  21. if (t_error != Error_code::SUCCESS) {
  22. //连接失败, 不要直接返回, 而是改为断连, 后面继续启动线程, (线程内部有重连功能)
  23. printf("---Debug %s %d : CONNECT ERROR !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n", __func__, __LINE__);
  24. m_communication_status = SNAP7_COMMUNICATION_DISCONNECT;
  25. } else {
  26. printf("---Debug %s %d : CONNECT SUCCESS !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n", __func__, __LINE__);
  27. m_communication_status = SNAP7_COMMUNICATION_READY;
  28. }
  29. return communication_run();
  30. }
  31. //反初始化 通信 模块。
  32. Error_manager Snap7_communication_base::communication_uninit() {
  33. //关闭线程并回收资源
  34. if (mp_communication_thread) {
  35. m_communication_condition.kill_all();
  36. }
  37. if (mp_communication_thread) {
  38. mp_communication_thread->join();
  39. delete mp_communication_thread;
  40. mp_communication_thread = NULL;
  41. }
  42. //清空map
  43. {
  44. std::unique_lock<std::mutex> t_lock(m_receive_buf_lock);
  45. m_receive_buf_map.clear();
  46. }
  47. {
  48. std::unique_lock<std::mutex> t_lock(m_send_buf_lock);
  49. m_send_buf_map.clear();
  50. }
  51. communication_disconnect();
  52. m_communication_status = SNAP7_COMMUNICATION_UNKNOWN;
  53. return Error_code::SUCCESS;
  54. }
  55. //唤醒s7通信线程
  56. Error_manager Snap7_communication_base::communication_start() {
  57. m_communication_condition.notify_all(true);
  58. return Error_code::SUCCESS;
  59. }
  60. //停止s7通信线程
  61. Error_manager Snap7_communication_base::communication_stop() {
  62. m_communication_condition.notify_all(false);
  63. return Error_code::SUCCESS;
  64. }
  65. Snap7_communication_base::Snap7_communication_statu Snap7_communication_base::get_status() {
  66. return m_communication_status;
  67. }
  68. //通信连接
  69. Error_manager Snap7_communication_base::communication_connect(std::string ip_string) {
  70. std::unique_lock<std::mutex> t_lock(m_communication_lock);
  71. int result = m_snap7_client.ConnectTo(ip_string.c_str(), 0, 1);
  72. std::this_thread::sleep_for(std::chrono::milliseconds(m_communication_delay_time_ms));
  73. printf("---Debug %s %d : connect %s result is %d \n", __func__, __LINE__, ip_string.c_str(), result);
  74. if (result == 0) {
  75. return Error_code::SUCCESS;
  76. } else {
  77. return Error_manager(Error_code::SNAP7_CONNECT_ERROR, Error_level::MINOR_ERROR,
  78. " Snap7_communication_base::communication_connect error ");
  79. }
  80. }
  81. //启动通信, run thread
  82. Error_manager Snap7_communication_base::communication_run() {
  83. //启动4个线程。
  84. //接受线程默认循环, 内部的nn_recv进行等待, 超时1ms
  85. m_communication_condition.reset(false, false, false);
  86. mp_communication_thread = new std::thread(&Snap7_communication_base::communication_thread, this);
  87. return Error_code::SUCCESS;
  88. }
  89. //通信断连
  90. Error_manager Snap7_communication_base::communication_disconnect() {
  91. std::unique_lock<std::mutex> t_lock(m_communication_lock);
  92. int result = m_snap7_client.Disconnect();
  93. std::this_thread::sleep_for(std::chrono::milliseconds(m_communication_delay_time_ms));
  94. if (result == 0) {
  95. return Error_code::SUCCESS;
  96. } else {
  97. return Error_manager(Error_code::SNAP7_DISCONNECT_ERROR, Error_level::MINOR_ERROR,
  98. " Snap7_communication_base::communication_disconnect error ");
  99. }
  100. return Error_code::SUCCESS;
  101. }
  102. //mp_communication_thread线程的执行函数, 负责进行s7的通信
  103. void Snap7_communication_base::communication_thread() {
  104. //LOG(INFO) << " ---Snap7_communication_base::communication_thread()--- "<< this;
  105. Error_manager t_error;
  106. while (m_communication_condition.is_alive()) {
  107. m_communication_condition.wait_for_millisecond(1);
  108. //s7的通信时间较长, 所以将发送和接受分开
  109. //发送多个时, 必须加锁后一起发送, 不允许分段写入, 防止数据错误
  110. if (m_communication_condition.is_alive()) {
  111. std::this_thread::sleep_for(std::chrono::milliseconds(m_communication_delay_time_ms));
  112. std::this_thread::yield();
  113. switch (m_communication_status) {
  114. case SNAP7_COMMUNICATION_READY:
  115. case SNAP7_COMMUNICATION_RECEIVE: {
  116. {
  117. std::unique_lock<std::mutex> t_lock(m_receive_buf_lock);
  118. auto iter = m_receive_buf_map.begin();
  119. for (; iter != m_receive_buf_map.end(); ++iter) {
  120. //接受数据, 读取DB块,
  121. t_error = read_data_buf(iter->second);
  122. if (t_error == Error_code::SNAP7_READ_ERROR) {
  123. m_communication_status = SNAP7_COMMUNICATION_DISCONNECT;
  124. printf("---Debug %s %d : t_error = %s \n", __func__, __LINE__, t_error.to_string().c_str());
  125. break;
  126. }
  127. }
  128. if (iter != m_receive_buf_map.end()) {
  129. break;
  130. }
  131. }
  132. //注:数据更新放在锁的外面, 防止重复加锁....
  133. if(updata_receive_buf() != Error_code::SUCCESS) {
  134. m_communication_status = SNAP7_COMMUNICATION_DISCONNECT;
  135. }
  136. else {
  137. m_communication_status = SNAP7_COMMUNICATION_SEND;
  138. }
  139. break;
  140. }
  141. case SNAP7_COMMUNICATION_SEND: {
  142. //注:数据更新放在锁的外面, 防止重复加锁....
  143. updata_send_buf();
  144. {
  145. std::unique_lock<std::mutex> t_lock(m_send_buf_lock);
  146. auto iter = m_send_buf_map.begin();
  147. for (; iter != m_send_buf_map.end(); ++iter) {
  148. //发送数据, 写入DB块,
  149. t_error = write_data_buf(iter->second);
  150. if (t_error == Error_code::SNAP7_WRITE_ERROR) {
  151. m_communication_status = SNAP7_COMMUNICATION_DISCONNECT;
  152. printf("---Debug %s %d : t_error = %s \n", __func__, __LINE__, t_error.to_string().c_str());
  153. break;
  154. }
  155. }
  156. if (iter != m_send_buf_map.end()) {
  157. break;
  158. }
  159. }
  160. m_communication_status = SNAP7_COMMUNICATION_RECEIVE;
  161. break;
  162. }
  163. case SNAP7_COMMUNICATION_DISCONNECT: {
  164. //重连
  165. printf("---Debug %s %d : find plc connection error, trying to reconnect.\n", __func__, __LINE__);
  166. communication_disconnect();
  167. std::this_thread::sleep_for(std::chrono::milliseconds(m_communication_delay_time_ms));
  168. t_error = communication_connect(m_ip_string);
  169. if (t_error != Error_code::SUCCESS) {
  170. //连接失败, 不要直接返回, 而是改为断连, 后面继续启动线程, (线程内部有重连功能)
  171. m_communication_status = SNAP7_COMMUNICATION_DISCONNECT;
  172. } else {
  173. m_communication_status = SNAP7_COMMUNICATION_READY;
  174. }
  175. std::this_thread::sleep_for(std::chrono::milliseconds(m_communication_delay_time_ms));
  176. break;
  177. }
  178. default: {
  179. break;
  180. }
  181. }
  182. }
  183. }
  184. //LOG(INFO) << " Communication_socket_base::send_data_thread end "<< this;
  185. return;
  186. }
  187. //接受数据, 读取DB块,
  188. Error_manager Snap7_communication_base::read_data_buf(Snap7_buf &snap7_buf) {
  189. Error_manager t_error;
  190. if (snap7_buf.m_communication_mode != Snap7_buf::NO_COMMUNICATION) {
  191. if (snap7_buf.m_communication_mode == Snap7_buf::ONCE_COMMUNICATION) {
  192. snap7_buf.m_communication_mode = Snap7_buf::NO_COMMUNICATION;
  193. }
  194. std::unique_lock<std::mutex> lck(m_communication_lock);
  195. // printf("---Debug %s %d : snap7_buf,m_id %d \n", __func__, __LINE__, snap7_buf.m_id);
  196. // printf("---Debug %s %d : snap7_buf,m_start_index %d\n", __func__, __LINE__, snap7_buf.m_start_index);
  197. // printf("---Debug %s %d : snap7_buf,m_size %d\n", __func__, __LINE__, snap7_buf.m_size);
  198. int result = m_snap7_client.AsDBRead(snap7_buf.m_id, snap7_buf.m_start_index, snap7_buf.m_size,
  199. snap7_buf.mp_buf_reverse);
  200. // std::this_thread::sleep_for(std::chrono::milliseconds(10));
  201. if (result == 0) {
  202. m_snap7_client.WaitAsCompletion(100);
  203. //倒序数据 转为 正序数据
  204. snap7_buf.reverse_to_obverse();
  205. } else {
  206. std::cout << " huli test :::: " << " result = " << result << std::endl;
  207. return Error_manager(Error_code::SNAP7_READ_ERROR, Error_level::MINOR_ERROR,
  208. " Snap7_communication_base::read_data_buf error ");
  209. }
  210. }
  211. return Error_code::SUCCESS;
  212. }
  213. //发送数据, 写入DB块,
  214. Error_manager Snap7_communication_base::write_data_buf(Snap7_buf &snap7_buf) {
  215. Error_manager t_error;
  216. if (snap7_buf.m_communication_mode != Snap7_buf::NO_COMMUNICATION) {
  217. if (snap7_buf.m_communication_mode == Snap7_buf::ONCE_COMMUNICATION) {
  218. snap7_buf.m_communication_mode = Snap7_buf::NO_COMMUNICATION;
  219. }
  220. //正序数据 转为 倒序数据
  221. snap7_buf.obverse_to_reverse();
  222. std::unique_lock<std::mutex> lck(m_communication_lock);
  223. unsigned short a = 0;
  224. memcpy(&a, snap7_buf.mp_buf_reverse, 2);
  225. //printf("id %d start %d size :%d value :%d\n",snap7_buf.m_id, snap7_buf.m_start_index, snap7_buf.m_size,a);
  226. int result = m_snap7_client.AsDBWrite(snap7_buf.m_id, snap7_buf.m_start_index, snap7_buf.m_size,
  227. snap7_buf.mp_buf_reverse);
  228. // std::this_thread::sleep_for(std::chrono::milliseconds(10));
  229. if (result == 0) {
  230. if (0 != m_snap7_client.WaitAsCompletion(1000)) {
  231. // printf("---Debug %s %d : snap7_buf,m_id %d \n", __func__, __LINE__, snap7_buf.m_id);
  232. // printf("---Debug %s %d : snap7_buf,m_start_index %d\n", __func__, __LINE__, snap7_buf.m_start_index);
  233. // printf("---Debug %s %d : snap7_buf,m_size %d\n", __func__, __LINE__, snap7_buf.m_size);
  234. // return Error_manager(Error_code::SNAP7_WRITE_ERROR, Error_level::MINOR_ERROR,
  235. // " Snap7_communication_base::write_data_buf error ");
  236. }
  237. } else {
  238. printf("---Debug %s %d : snap7_buf,m_id %d \n", __func__, __LINE__, snap7_buf.m_id);
  239. printf("---Debug %s %d : snap7_buf,m_start_index %d\n", __func__, __LINE__, snap7_buf.m_start_index);
  240. printf("---Debug %s %d : snap7_buf,m_size %d\n", __func__, __LINE__, snap7_buf.m_size);
  241. return Error_manager(Error_code::SNAP7_WRITE_ERROR, Error_level::MINOR_ERROR,
  242. " Snap7_communication_base::write_data_buf error ");
  243. }
  244. }
  245. return Error_code::SUCCESS;
  246. }
  247. //数据颠倒
  248. Error_manager Snap7_communication_base::reverse_byte(void *p_buf_in, void *p_buf_out, int size) {
  249. if (p_buf_in == NULL || p_buf_out == NULL) {
  250. return Error_manager(Error_code::POINTER_IS_NULL, Error_level::MINOR_ERROR,
  251. " POINTER IS NULL ");
  252. }
  253. char *tp_in = (char *) p_buf_in;
  254. char *tp_out = (char *) p_buf_out;
  255. // for(int i=0;i<size;++i)
  256. // {
  257. // tp_out[i]=tp_in[size-i-1];
  258. // }
  259. for (int i = 0; i < size; ++i) {
  260. tp_out[i] = tp_in[i];
  261. }
  262. return Error_code::SUCCESS;
  263. }
  264. //更新数据
  265. Error_manager Snap7_communication_base::updata_receive_buf() {
  266. return Error_code::SUCCESS;
  267. }
  268. Error_manager Snap7_communication_base::updata_send_buf() {
  269. return Error_code::SUCCESS;
  270. }