ams
cjs
2025-05-28 53f837bef54387950308db48a06de42663f3ae99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
using Hanhe.iWCS.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
 
namespace HH.WCS.JingmenGEMNorthProtocol
{
    public class TcpServer
    {
        public static Dictionary<string, Socket> clients = new Dictionary<string, Socket>();
        public static Dictionary<string, byte[]> buffers = new Dictionary<string, byte[]>();
 
        /// <summary>
        /// 重连PLC
        /// </summary>
        /// <param name="ip"></param> 
        /// <param name="port"></param>
        public static void ReConnect(string ip,int port)
        {
            Console.WriteLine($"设备重连:ip:{ip},Port:{port}");
            if (clients.Keys.Contains(ip))
            {
                // 关闭socket
                clients[ip].Close();
                clients.Remove(ip);
                Console.WriteLine($"Close Tcp :{ip},Port:{port}");
            }
 
            // 创建一个socket对象
            Socket socket = new Socket(SocketType.Stream, ProtocolType.Tcp);
            try
            {
                // 连接到远程IP
                socket.Connect(new IPEndPoint(IPAddress.Parse(ip), port));
 
                if (clients.Keys.Contains(ip)) clients[ip] = socket;
                else clients.Add(ip, socket);
                if (!buffers.Keys.Contains(ip)) buffers.Add(ip, new byte[1024]);
                Console.WriteLine($"重连成功:ip:{ip},Port:{port}");
            }
            catch (SocketException ex)
            {
                Console.WriteLine("Error: " + ex.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.Message);
            }
        }
 
        public static bool SendTcpMsg(string ip, int port, string msg)
        {
            try
            {
                //根据ip找到目标端口
                CMMLog.Info($"【SendTcpMsg】,ip:{ip},port:{port},msg:{msg}");
                if (clients.Keys.Contains(ip))
                {
                    CMMLog.Info("【SendTcpMsg】 111111");
                    var client = clients[ip];
                    client.ReceiveTimeout = 200;
                    if (client.Connected)
                    {
                        CMMLog.Info("【SendTcpMsg】 2222222222");
                        try
                        {
                            CMMLog.Info("【SendTcpMsg】 33333333");
                            //byte[] messageBytes = Encoding.UTF8.GetBytes(msg);
                            byte[] messageBytes = Hex2Bytes(msg);
                            CMMLog.Info("【SendTcpMsg】 4444444444");
                            client.Send(messageBytes);
                            CMMLog.Info("【SendTcpMsg】 5555555555");
                            Console.WriteLine("Send data: " + msg);
                            return true;
                        }
                        catch (SocketException ex)
                        {
                            CMMLog.Info("【SendTcpMsg】 66666666");
                            Console.WriteLine(ex.Message);
                            //clients[ip].Close();
                            clients.Remove(ip);
                        }
                    }
                    else
                    {
                        //clients[ip].Close();
                        clients.Remove(ip);
                    }
                }
                else
                {
                    CMMLog.Info("【SendTcpMsg】 777777777777");
                    Console.WriteLine("未找到设备的链接:" + ip);
                    ReConnect(ip, port);
 
                    SendTcpMsg(ip, port, msg);
                }
            }
            catch(Exception ex)
            {
                Console.WriteLine("err :"+ex.Message);
            }
            
            return false;
        }
 
        internal static byte[] Hex2Bytes(string hexString)
        {
            hexString = hexString.Replace(" ", "");
            if ((hexString.Length % 2) != 0)
                hexString += " ";
            byte[] returnBytes = new byte[hexString.Length / 2];
            for (int i = 0; i < returnBytes.Length; i++)
                returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
            return returnBytes;
        }
    }
}