ETHSimensAlarmLib.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. namespace SimensCNC
  10. {
  11. public class ETHSimensAlarmLib
  12. {
  13. bool flag=true;
  14. public static string cncIP;
  15. int receiveData;
  16. short alarmMsg =0;
  17. byte[] sendCommands;
  18. byte[] buffer;//发送报文容器
  19. byte[] datas;//接收报文容器
  20. static IAsyncResult connResult = null;
  21. public ETHSimensAlarmLib()
  22. {
  23. }
  24. public Socket AlarmSocket { get; set; }
  25. /// <summary>
  26. /// 链接函数
  27. /// </summary>
  28. public bool ETH_S7SimensConnect(string adrip, ushort port, int overtime)
  29. {
  30. cncIP = adrip;
  31. IPAddress ip = IPAddress.Parse(adrip);
  32. int ports = Convert.ToInt32(port);
  33. AlarmSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  34. connResult = AlarmSocket.BeginConnect(ip, ports, null, null);
  35. Thread.Sleep(5000);
  36. connResult.AsyncWaitHandle.WaitOne(5, true); //等待2秒
  37. if (connResult.IsCompleted)
  38. {
  39. HandShark();
  40. // ReceiveAlarmData();
  41. return true;
  42. }
  43. else
  44. {
  45. return false;
  46. }
  47. }
  48. /// <summary>
  49. /// 断开设备连接
  50. /// </summary>
  51. /// <param name="no">设备编号</param>
  52. public void ETH_S7SimensDisConnect(string no)
  53. {
  54. AlarmSocket.Shutdown(SocketShutdown.Both);
  55. AlarmSocket.Close();
  56. }
  57. /// <summary>
  58. /// 三次握手
  59. /// </summary>
  60. void HandShark()
  61. {
  62. AlarmSocket.Send(ETH_S7SimensCommands.FIRST_HAND_SHANK);
  63. buffer = new byte[1024];
  64. receiveData = AlarmSocket.Receive(buffer);
  65. AlarmSocket.Send(ETH_S7SimensCommands.SENCOND_HAND_SHARK);
  66. buffer = new byte[1024];
  67. receiveData = AlarmSocket.Receive(buffer);
  68. AlarmSocket.Send(ETH_S7SimensCommands.THREE_HAND_SHARK);
  69. buffer = new byte[1024];
  70. receiveData = AlarmSocket.Receive(buffer);
  71. }
  72. public void ReceiveAlarmData()
  73. {
  74. //事件监听线程
  75. Task.Factory.StartNew(new Action(() =>
  76. {
  77. while (true)
  78. {
  79. byte[] buffer = new byte[1024 * 8];
  80. try
  81. {
  82. receiveData = AlarmSocket.Receive(buffer);
  83. byte[] datas = new byte[receiveData];
  84. //监听到消息
  85. if (receiveData > 0)
  86. {
  87. Array.Copy(buffer, datas, receiveData);
  88. if ((datas[3] == 0xc3))//&& (datas[12] == 0x40))
  89. {
  90. if (datas[187] == 0x01)
  91. {
  92. alarmMsg = BitConverter.ToInt16(datas.Skip(39).Take(2).ToArray(), 0);
  93. var sss = String.Format("{0:X2}{0:X2}", datas[54], datas[55]);
  94. flag = true;
  95. }
  96. }
  97. else
  98. {
  99. flag = false ;
  100. }
  101. }
  102. else
  103. {
  104. Thread.Sleep(100);
  105. flag = false;
  106. }
  107. }
  108. catch
  109. { }
  110. }
  111. }));
  112. }
  113. /// <summary>
  114. /// 设备报警信息
  115. /// </summary>
  116. /// <param name="socket"></param>
  117. public bool AlarmMsg(ref object result)
  118. {
  119. AlarmSocket.Send(ETH_S7SimensCommands.CNC_ALARM);
  120. Thread.Sleep(10);
  121. buffer = new byte[1024 * 8];
  122. receiveData = AlarmSocket.Receive(buffer);
  123. if (receiveData > 0)
  124. {
  125. datas = new byte[receiveData];
  126. Array.Copy(buffer, datas, receiveData);
  127. if ((datas[3] == 0xc3))//&& (datas[12] == 0x40))
  128. {
  129. if (datas[187] == 0x01)
  130. {
  131. var ss = BitConverter.ToInt16(datas.Skip(39).Take(2).ToArray(), 0);
  132. var sss = String.Format("{0:X2}{0:X2}", datas[54], datas[55]);
  133. result = sss;
  134. }
  135. return true;
  136. }
  137. else
  138. {
  139. return false;
  140. }
  141. }
  142. else
  143. {
  144. return false;
  145. }
  146. }
  147. }
  148. }