123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Net.Sockets;
- using System.Text;
- using System.Threading.Tasks;
- namespace FileAnalysis
- {
- class TCPClient
- {
- private Socket socket;
- private IPEndPoint serverFullAddr;//完整终端地址
- private bool isOpen = false;
- private bool receiveData;
- private DataHelper dh;
- private Dictionary<string, string> returnvalue = new Dictionary<string, string>();
- public bool IsOpen
- {
- get
- {
- return isOpen;
- }
- set
- {
- isOpen = value;
- }
- }
- public string IP
- {
- get
- {
- return iP;
- }
- set
- {
- iP = value;
- }
- }
- public string Port
- {
- get
- {
- return port;
- }
- set
- {
- port = value;
- }
- }
- public bool ReceiveData
- {
- get
- {
- return receiveData;
- }
- set
- {
- receiveData = value;
- }
- }
- public Dictionary<string, string> Returnvalue
- {
- get
- {
- return returnvalue;
- }
- set
- {
- returnvalue = value;
- }
- }
- internal DataHelper Dh
- {
- get
- {
- return dh;
- }
- set
- {
- dh = value;
- }
- }
- private string iP;
- private string port;
- private int ResetCount = 0;
- string decode = "";
- string dename = "";
- public TCPClient(string IP, string Port, string Decode, string Dename)
- {
- try
- {
- decode = Decode;
- dename = Dename;
- socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- serverFullAddr = new IPEndPoint(IPAddress.Parse(IP), int.Parse(Port));//设置IP,端口
- socket.ReceiveTimeout = 2000;
- socket.Connect(serverFullAddr);
- }
- catch (Exception ex)
- {
- }
- }
- public void Send(string Command)
- {
- string str = "";
- try
- {
- if (!socket.Connected || ResetCount == 20)
- {
- socket.Close();
- socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- socket.ReceiveTimeout = 2000;
- socket.Connect(serverFullAddr);
- ResetCount = 0;
- }
- //byte[] arr = Encoding.ASCII.GetBytes(Command);
- byte[] arr = HexStringToBytes(Command);
- //Command = Command.Replace(" ", "");
- //byte[] arr = new byte[Command.Length / 2];
- //for (int i = 0; i < Command.Length / 2; i++)
- //{
- // arr[i] = (byte)Convert.(Command.Substring(i * 2, 2), 16);
- //}
- //byte[] data = { 0x01, 0x03, 0x00, 0x00, 0x00, 0x0A }; // 示例数据
- String crc = CalculateCRC(arr);
- arr = HexStringToBytes(Command+ crc);
- socket.Send(arr);
- byte[] receive = new byte[1024 * 10];
- int length = socket.Receive(receive);
- str = System.Text.Encoding.UTF8.GetString(receive,0,length);
- //if (!returnvalue.ContainsKey(socket.RemoteEndPoint.ToString()))
- //{
- // returnvalue.Add(socket.RemoteEndPoint.ToString(), str);
- //}
- //ResetCount = ResetCount + 1;
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message + ex.StackTrace + "\n" + decode + " " + str);
- }
- }
- public static String CalculateCRC(byte[] data)
- {
- ushort crc = 0xFFFF;
- foreach (byte b in data)
- {
- crc ^= b;
- for (int i = 0; i < 8; i++)
- {
- if ((crc & 0x0001) != 0)
- {
- crc >>= 1;
- crc ^= 0xA001;
- }
- else
- {
- crc >>= 1;
- }
- }
- }
- // Swap bytes to get the correct Modbus CRC16 format
- return ((ushort)((crc << 8) | (crc >> 8))).ToString("X").PadLeft(4, '0');
- }
- private byte[] HexStringToBytes(string hs)//十六进制字符串转byte
- {
- try
- {
- string a = hs.Replace(" ", "");
- int bytelength = 0;
- if (a.Length % 2 == 0)
- {
- bytelength = a.Length / 2;
- }
- else
- {
- bytelength = a.Length / 2 + 1;
- }
- byte[] b = new byte[bytelength];
- //逐个字符变为16进制字节数据
- for (int i = 0; i < bytelength; i++)
- {
- if (i == bytelength - 1)
- {
- b[i] = Convert.ToByte(a.Substring(i * 2), 16);
- }
- else
- {
- b[i] = Convert.ToByte(a.Substring(i * 2, 2), 16);
- }
- }
- //按照指定编码将字节数组变为字符串
- return b;
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- return null;
-
- }
- }
- public static string ByteToHexadecimalString(byte[] b, int length)
- {
- string returnStr = "";
- if (b != null)
- {
- for (int i = 0; i < length; i++)
- {
- returnStr += Convert.ToString(b[i], 16);//ToString("X2") 为C#中的字符串格式控制符
- }
- }
- return returnStr.ToUpper();
- }
- }
- }
|