ModBusTCPClient.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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.Connect(serverFullAddr);
  97. }
  98. catch (Exception ex)
  99. {
  100. LogicHandler.UpdateDeviceStatus(decode, dename, ex.Message, DeviceStatus.Disconnect, -1);
  101. LogicHandler.DoDevicePollingLog(decode, false);
  102. LogManager.DoLog(ex.Message + ex.StackTrace);
  103. }
  104. }
  105. public void Send(string Command)
  106. {
  107. string str = "";
  108. try
  109. {
  110. if (!socket.Connected || ResetCount == 5)
  111. {
  112. socket.Close();
  113. socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  114. socket.Connect(serverFullAddr);
  115. }
  116. Command = Command.Replace(" ", "");
  117. byte[] arr = new byte[Command.Length / 2];
  118. for (int i = 0; i < Command.Length / 2; i++)
  119. {
  120. arr[i] = (byte)Convert.ToInt32(Command.Substring(i * 2, 2), 16);
  121. }
  122. socket.Send(arr);
  123. byte[] receive = new byte[1024];
  124. LogicHandler.UpdateDeviceStatus(decode, dename, "设备运行准备获取数据", DeviceStatus.Noanswer, 0);
  125. LogicHandler.DoDevicePollingLog(decode, true);
  126. int length = socket.Receive(receive);
  127. LogicHandler.UpdateDeviceStatus(decode, dename, "设备运行获取到数据返回数据", DeviceStatus.Running, 0);
  128. str = BaseUtil.ByteToHexadecimalString(receive, length);
  129. LogicHandler.DoDevicePollingLog(decode, BaseUtil.ASCIIToString(str));
  130. if (!returnvalue.ContainsKey(socket.RemoteEndPoint.ToString()))
  131. {
  132. returnvalue.Add(socket.RemoteEndPoint.ToString(), str);
  133. }
  134. ResetCount = ResetCount + 1;
  135. }
  136. catch (Exception ex)
  137. {
  138. LogicHandler.UpdateDeviceStatus(decode, dename, ex.Message, DeviceStatus.Disconnect, -1);
  139. LogicHandler.DoDevicePollingLog(decode, false);
  140. LogManager.DoLog(ex.Message + ex.StackTrace + "\n" + decode + " " + str);
  141. }
  142. }
  143. }
  144. }