TCPClient.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Net.Sockets;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace FileAnalysis
  10. {
  11. class TCPClient
  12. {
  13. private Socket socket;
  14. private IPEndPoint serverFullAddr;//完整终端地址
  15. private bool isOpen = false;
  16. private bool receiveData;
  17. private DataHelper dh;
  18. private Dictionary<string, string> returnvalue = new Dictionary<string, string>();
  19. public bool IsOpen
  20. {
  21. get
  22. {
  23. return isOpen;
  24. }
  25. set
  26. {
  27. isOpen = value;
  28. }
  29. }
  30. public string IP
  31. {
  32. get
  33. {
  34. return iP;
  35. }
  36. set
  37. {
  38. iP = value;
  39. }
  40. }
  41. public string Port
  42. {
  43. get
  44. {
  45. return port;
  46. }
  47. set
  48. {
  49. port = value;
  50. }
  51. }
  52. public bool ReceiveData
  53. {
  54. get
  55. {
  56. return receiveData;
  57. }
  58. set
  59. {
  60. receiveData = value;
  61. }
  62. }
  63. public Dictionary<string, string> Returnvalue
  64. {
  65. get
  66. {
  67. return returnvalue;
  68. }
  69. set
  70. {
  71. returnvalue = value;
  72. }
  73. }
  74. internal DataHelper Dh
  75. {
  76. get
  77. {
  78. return dh;
  79. }
  80. set
  81. {
  82. dh = value;
  83. }
  84. }
  85. private string iP;
  86. private string port;
  87. private int ResetCount = 0;
  88. string decode = "";
  89. string dename = "";
  90. public TCPClient(string IP, string Port, string Decode, string Dename)
  91. {
  92. try
  93. {
  94. decode = Decode;
  95. dename = Dename;
  96. socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  97. serverFullAddr = new IPEndPoint(IPAddress.Parse(IP), int.Parse(Port));//设置IP,端口
  98. socket.ReceiveTimeout = 2000;
  99. socket.Connect(serverFullAddr);
  100. }
  101. catch (Exception ex)
  102. {
  103. }
  104. }
  105. public void Send(string Command)
  106. {
  107. string str = "";
  108. try
  109. {
  110. if (!socket.Connected || ResetCount == 20)
  111. {
  112. socket.Close();
  113. socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  114. socket.ReceiveTimeout = 2000;
  115. socket.Connect(serverFullAddr);
  116. ResetCount = 0;
  117. }
  118. //byte[] arr = Encoding.ASCII.GetBytes(Command);
  119. byte[] arr = HexStringToBytes(Command);
  120. //Command = Command.Replace(" ", "");
  121. //byte[] arr = new byte[Command.Length / 2];
  122. //for (int i = 0; i < Command.Length / 2; i++)
  123. //{
  124. // arr[i] = (byte)Convert.(Command.Substring(i * 2, 2), 16);
  125. //}
  126. //byte[] data = { 0x01, 0x03, 0x00, 0x00, 0x00, 0x0A }; // 示例数据
  127. String crc = CalculateCRC(arr);
  128. arr = HexStringToBytes(Command+ crc);
  129. socket.Send(arr);
  130. byte[] receive = new byte[1024 * 10];
  131. int length = socket.Receive(receive);
  132. str = System.Text.Encoding.UTF8.GetString(receive,0,length);
  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. Console.WriteLine(ex.Message + ex.StackTrace + "\n" + decode + " " + str);
  142. }
  143. }
  144. public static String CalculateCRC(byte[] data)
  145. {
  146. ushort crc = 0xFFFF;
  147. foreach (byte b in data)
  148. {
  149. crc ^= b;
  150. for (int i = 0; i < 8; i++)
  151. {
  152. if ((crc & 0x0001) != 0)
  153. {
  154. crc >>= 1;
  155. crc ^= 0xA001;
  156. }
  157. else
  158. {
  159. crc >>= 1;
  160. }
  161. }
  162. }
  163. // Swap bytes to get the correct Modbus CRC16 format
  164. return ((ushort)((crc << 8) | (crc >> 8))).ToString("X").PadLeft(4, '0');
  165. }
  166. private byte[] HexStringToBytes(string hs)//十六进制字符串转byte
  167. {
  168. try
  169. {
  170. string a = hs.Replace(" ", "");
  171. int bytelength = 0;
  172. if (a.Length % 2 == 0)
  173. {
  174. bytelength = a.Length / 2;
  175. }
  176. else
  177. {
  178. bytelength = a.Length / 2 + 1;
  179. }
  180. byte[] b = new byte[bytelength];
  181. //逐个字符变为16进制字节数据
  182. for (int i = 0; i < bytelength; i++)
  183. {
  184. if (i == bytelength - 1)
  185. {
  186. b[i] = Convert.ToByte(a.Substring(i * 2), 16);
  187. }
  188. else
  189. {
  190. b[i] = Convert.ToByte(a.Substring(i * 2, 2), 16);
  191. }
  192. }
  193. //按照指定编码将字节数组变为字符串
  194. return b;
  195. }
  196. catch (Exception ex)
  197. {
  198. Console.WriteLine(ex.Message);
  199. return null;
  200. }
  201. }
  202. public static string ByteToHexadecimalString(byte[] b, int length)
  203. {
  204. string returnStr = "";
  205. if (b != null)
  206. {
  207. for (int i = 0; i < length; i++)
  208. {
  209. returnStr += Convert.ToString(b[i], 16);//ToString("X2") 为C#中的字符串格式控制符
  210. }
  211. }
  212. return returnStr.ToUpper();
  213. }
  214. }
  215. }