ModBusTCPClient.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. 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 Dictionary<string, string> returnvalue = new Dictionary<string, string>();
  16. public bool IsOpen
  17. {
  18. get
  19. {
  20. return isOpen;
  21. }
  22. set
  23. {
  24. isOpen = value;
  25. }
  26. }
  27. public string IP
  28. {
  29. get
  30. {
  31. return iP;
  32. }
  33. set
  34. {
  35. iP = value;
  36. }
  37. }
  38. public string Port
  39. {
  40. get
  41. {
  42. return port;
  43. }
  44. set
  45. {
  46. port = value;
  47. }
  48. }
  49. public bool ReceiveData
  50. {
  51. get
  52. {
  53. return receiveData;
  54. }
  55. set
  56. {
  57. receiveData = value;
  58. }
  59. }
  60. public Dictionary<string, string> Returnvalue
  61. {
  62. get
  63. {
  64. return returnvalue;
  65. }
  66. set
  67. {
  68. returnvalue = value;
  69. }
  70. }
  71. private string iP;
  72. private string port;
  73. string decode = "";
  74. string dename = "";
  75. public ModBusTCPClient(string IP, int Port,string Decode,string Dename)
  76. {
  77. decode = Decode;
  78. dename = Dename;
  79. socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  80. serverFullAddr = new IPEndPoint(IPAddress.Parse(IP), Port);//设置IP,端口
  81. try
  82. {
  83. socket.Connect(serverFullAddr);
  84. }
  85. catch (Exception)
  86. {
  87. LogicHandler.UpdateDeviceStatus(decode, dename,"disconnect");
  88. }
  89. }
  90. public void Send(string Command)
  91. {
  92. try
  93. {
  94. if (!socket.Connected)
  95. {
  96. socket.Close();
  97. socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  98. socket.Connect(serverFullAddr);
  99. }
  100. Command = Command.Replace(" ", "");
  101. byte[] arr = new byte[Command.Length / 2];
  102. for (int i = 0; i < Command.Length / 2; i++)
  103. {
  104. arr[i] = (byte)Convert.ToInt32(Command.Substring(i * 2, 2), 16);
  105. }
  106. socket.Send(arr);
  107. byte[] receive = new byte[1024 * 1024];
  108. LogicHandler.UpdateDeviceStatus(decode, dename, "noanswer");
  109. int length = socket.Receive(receive);
  110. LogicHandler.UpdateDeviceStatus(decode, dename, "running");
  111. if (!returnvalue.ContainsKey(socket.RemoteEndPoint.ToString()))
  112. {
  113. returnvalue.Add(socket.RemoteEndPoint.ToString(), BaseUtil.ByteToHexadecimalString(receive, length));
  114. }
  115. }
  116. catch (Exception e)
  117. {
  118. LogicHandler.UpdateDeviceStatus(decode, dename, "disconnect");
  119. }
  120. }
  121. }
  122. }