ModBusTCPClient.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Net;
  4. using System.Net.Sockets;
  5. using UAS_PLCDataReader.DataOperate;
  6. using UAS_PLCDataReader.Entity;
  7. namespace UAS_PLCDataReader.PublicMethod
  8. {
  9. class ModBusTCPClient
  10. {
  11. private Socket socket;
  12. private IPEndPoint serverFullAddr;//完整终端地址
  13. private bool isOpen = false;
  14. private bool receiveData;
  15. private DataHelper dh;
  16. private Dictionary<string, string> returnvalue = new Dictionary<string, string>();
  17. public bool IsOpen
  18. {
  19. get
  20. {
  21. return isOpen;
  22. }
  23. set
  24. {
  25. isOpen = value;
  26. }
  27. }
  28. public string IP
  29. {
  30. get
  31. {
  32. return iP;
  33. }
  34. set
  35. {
  36. iP = value;
  37. }
  38. }
  39. public string Port
  40. {
  41. get
  42. {
  43. return port;
  44. }
  45. set
  46. {
  47. port = value;
  48. }
  49. }
  50. public bool ReceiveData
  51. {
  52. get
  53. {
  54. return receiveData;
  55. }
  56. set
  57. {
  58. receiveData = value;
  59. }
  60. }
  61. public Dictionary<string, string> Returnvalue
  62. {
  63. get
  64. {
  65. return returnvalue;
  66. }
  67. set
  68. {
  69. returnvalue = value;
  70. }
  71. }
  72. internal DataHelper Dh
  73. {
  74. get
  75. {
  76. return dh;
  77. }
  78. set
  79. {
  80. dh = value;
  81. }
  82. }
  83. private string iP;
  84. private string port;
  85. private int ResetCount = 0;
  86. string decode = "";
  87. string dename = "";
  88. public ModBusTCPClient(string IP, string Port, string Decode, string Dename)
  89. {
  90. try
  91. {
  92. decode = Decode;
  93. dename = Dename;
  94. socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  95. serverFullAddr = new IPEndPoint(IPAddress.Parse(IP), int.Parse(Port));//设置IP,端口
  96. socket.ReceiveTimeout = 2000;
  97. socket.Connect(serverFullAddr);
  98. }
  99. catch (Exception ex)
  100. {
  101. LogicHandler.UpdateDeviceStatus(decode, dename, ex.Message, DeviceStatus.Disconnect, -1);
  102. LogicHandler.DoDevicePollingLog(decode, false);
  103. LogManager.DoLog(ex.Message + ex.StackTrace);
  104. }
  105. }
  106. public void Send(string Command)
  107. {
  108. string str = "";
  109. try
  110. {
  111. if (!socket.Connected || ResetCount == 20)
  112. {
  113. socket.Close();
  114. socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  115. socket.ReceiveTimeout = 2000;
  116. socket.Connect(serverFullAddr);
  117. ResetCount = 0;
  118. }
  119. Command = Command.Replace(" ", "");
  120. byte[] arr = new byte[Command.Length / 2];
  121. for (int i = 0; i < Command.Length / 2; i++)
  122. {
  123. arr[i] = (byte)Convert.ToInt32(Command.Substring(i * 2, 2), 16);
  124. }
  125. socket.Send(arr);
  126. byte[] receive = new byte[1024 * 10];
  127. LogicHandler.UpdateDeviceStatus(decode, dename, "设备运行准备获取数据", DeviceStatus.Noanswer, 0);
  128. LogicHandler.DoDevicePollingLog(decode, true);
  129. int length = socket.Receive(receive);
  130. LogicHandler.UpdateDeviceStatus(decode, dename, "设备运行获取到数据返回数据", DeviceStatus.Running, 0);
  131. str = BaseUtil.ByteToHexadecimalString(receive, length);
  132. LogicHandler.DoDevicePollingLog(decode, BaseUtil.ASCIIToString(str));
  133. if (!returnvalue.ContainsKey(socket.RemoteEndPoint.ToString()))
  134. {
  135. returnvalue.Add(socket.RemoteEndPoint.ToString(), str);
  136. }
  137. ResetCount = ResetCount + 1;
  138. }
  139. catch (Exception ex)
  140. {
  141. LogicHandler.UpdateDeviceStatus(decode, dename, ex.Message, DeviceStatus.Disconnect, -1);
  142. LogicHandler.DoDevicePollingLog(decode, false);
  143. Console.WriteLine(ex.Message + ex.StackTrace + "\n" + decode + " " + str);
  144. }
  145. }
  146. }
  147. }