ETH_S7SimensPLCLib.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 ETH_S7SimensPLCLib
  12. {
  13. public static string cncIP;
  14. int receiveData;
  15. byte[] sendCommands;
  16. byte[] buffer;//发送报文容器
  17. byte[] datas;//接收报文容器
  18. static IAsyncResult connResult = null;
  19. Func<byte[], int, object> func;
  20. public ETH_S7SimensPLCLib()
  21. {
  22. }
  23. public Socket PLCSocket { get; set; }
  24. /// <summary>
  25. /// 链接函数
  26. /// </summary>
  27. public bool ETH_S7SimensConnect(string adrip, ushort port, int overtime)
  28. {
  29. cncIP = adrip;
  30. IPAddress ip = IPAddress.Parse(adrip);
  31. int ports = Convert.ToInt32(port);
  32. PLCSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  33. connResult = PLCSocket.BeginConnect(ip, ports, null, null);
  34. Thread.Sleep(200);
  35. connResult.AsyncWaitHandle.WaitOne(5, true); //等待2秒
  36. if (connResult.IsCompleted)
  37. {
  38. PLCHandShark();
  39. return true;
  40. }
  41. else
  42. {
  43. return false;
  44. }
  45. }
  46. /// <summary>
  47. /// 断开设备连接
  48. /// </summary>
  49. /// <param name="no">设备编号</param>
  50. public void ETH_S7SimensDisConnect(string no)
  51. {
  52. PLCSocket.Shutdown(SocketShutdown.Both);
  53. PLCSocket.Close();
  54. }
  55. /// <summary>
  56. /// 三次握手
  57. /// </summary>
  58. /// <summary>
  59. /// plc握手
  60. /// </summary>
  61. void PLCHandShark()
  62. {
  63. PLCSocket.Send(ETH_S7SimensPLCProtocol.PLC_FIRST_HAND_SHANK);
  64. buffer = new byte[1024];
  65. receiveData = PLCSocket.Receive(buffer);
  66. PLCSocket.Send(ETH_S7SimensPLCProtocol.PLC_SECONDE_HAND_SHANK);
  67. buffer = new byte[1024];
  68. receiveData = PLCSocket.Receive(buffer);
  69. }
  70. /// <summary>
  71. /// 获取报警信息
  72. /// </summary>
  73. /// <param name="no"></param>
  74. /// <returns></returns>
  75. public bool ETH_S7SimensGetPLCAlarmMsg(int i, ref object result)
  76. {
  77. ETH_S7SimensPLCProtocol.PLC_READBYTE[ETH_S7SimensPLCProtocol.PLC_READBYTE.Length - 1] = (byte)(i * 8);
  78. PLCSocket.Send(ETH_S7SimensPLCProtocol.PLC_READBYTE);
  79. Thread.Sleep(10);
  80. buffer = new byte[1024 * 8];
  81. receiveData = PLCSocket.Receive(buffer);
  82. if (receiveData > 0)
  83. {
  84. datas = new byte[receiveData];
  85. Array.Copy(buffer, datas, receiveData);
  86. var s = BitConverter.ToInt16(datas.Skip(datas.Length - 2).Take(2).ToArray(), 0);
  87. if (s > 0)
  88. {
  89. if ((s & 0x01) == 1)
  90. {
  91. result=((700000 + i * 8).ToString() + "\r\n");
  92. }
  93. if ((s & 0x02) == 0x02)
  94. {
  95. result=((700001 + i * 8).ToString() + "\r\n");
  96. }
  97. if ((s & 0x04) == 0x04)
  98. {
  99. result=((700002 + i * 8).ToString() + "\r\n");
  100. }
  101. if ((s & 0x08) == 0x08)
  102. {
  103. result=((700003 + i * 8).ToString() + "\r\n");
  104. }
  105. if ((s & 0x10) == 0x10)
  106. {
  107. result=((700004 + i * 8).ToString() + "\r\n");
  108. }
  109. if ((s & 0x20) == 0x20)
  110. {
  111. result=((700005 + i * 8).ToString() + "\r\n");
  112. }
  113. if ((s & 0x40) == 0x40)
  114. {
  115. result=((700006 + i * 8).ToString() + "\r\n");
  116. }
  117. if ((s & 0x80) == 0x80)
  118. {
  119. result=((700007 + i * 8).ToString() + "\r\n");
  120. }
  121. };
  122. return true;
  123. }
  124. else
  125. {
  126. return false;
  127. }
  128. }
  129. }
  130. }