1
czw
2025-07-03 0bf4a3d86b017fc4cadddda4182e682386dfee82
2025Äê6ÔÂ12ÈÕ/AuxAllWCS/Build/Project/´úÂë/VS×Ô¶¨ÒåÀà/TcpServer.cs
@@ -8,13 +8,14 @@
using System.Net;
using System.Text;
using System.Threading.Tasks;
using GZ.Modular.Redis;
namespace GZ.Projects.AuxAllWCS
{
    public class TcpServer
    {
        public static Dictionary<string,string> TrayIps = new Dictionary<string,string>();
        public static Dictionary<string, string> TrayIps = new Dictionary<string, string>();
        public TcpServer(string ip)
        {
            Init(ip);
@@ -27,6 +28,7 @@
            {
                //将该socket绑定到主机上面的某个端口
                socket.Bind(new IPEndPoint(IPAddress.Parse(ip), 2025));
                Console.WriteLine($"TCPServer socket ç›‘听{ip}:{2025} ");
                //启动监听,并且设置一个最大的队列长度
                socket.Listen(30);
                //开始接受客户端连接请求
@@ -115,18 +117,27 @@
                        //}
                        //else
                        //{
                        Console.WriteLine($"【TCP信息协议异常 {DateTime.Now.Millisecond}】:IP:{remote_ip},MSG:{message}");
                        Console.WriteLine($"【TCP信息协议 {DateTime.Now.Millisecond}】:IP:{remote_ip},MSG:{message}");
                        var mg = Encoding.ASCII.GetString(PlcHelper.Hex2Bin(message));
                        Console.WriteLine(mg);
                        if (mg.StartsWith("DK") && mg.Trim().Length == "DK01000024".Length)
                        if (mg.Length > 10)
                        {
                           if(TrayIps.TryGetValue(remote_ip,out string traycode))
                            mg = mg.Substring(0, 10);
                        }
                        Console.WriteLine(mg);
                        if (mg.StartsWith("DK"))//&& mg.Trim().Length == "DK01000024".Length
                        {
                            LogHelper.Info($"扫码器 >{remote_ip} -{mg}");
                            if (TrayIps.TryGetValue(remote_ip, out string traycode))
                            {
                                TrayIps[remote_ip] = traycode;
                            }else TrayIps.Add(remote_ip, traycode);
                            Console.WriteLine("TOFF");
                            var mst = PlcHelper.Hex2Bin("544F4646");
                            TcpServer.TcpServerSend(remote_ip, mst);
                                TrayIps[remote_ip] = mg;
                            }
                            else TrayIps.Add(remote_ip, mg);
                            RedisHelper.Add("S扫码器" + (remote_ip.Split('.').LastOrDefault()), mg, out string msg);
                            RedisHelper.Add("S扫码器" + (remote_ip.Split('.').LastOrDefault()) + "#time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), out msg);
                            //Console.WriteLine("TOFF");
                            //var mst = PlcHelper.Hex2Bin("544F4646");
                            //TcpServer.TcpServerSend(remote_ip, mst);
                        }
                        //}
                    }
@@ -159,6 +170,7 @@
        public static bool TcpServerSend(string ip, byte[] msg)
        {
            LogHelper.Info($"TcpServerSend >{ip}:{msg}");
            if (clients.Keys.Contains(ip))
            {
                var client = clients[ip];
@@ -167,6 +179,7 @@
                    try
                    {
                        client.Send(msg);
                        LogHelper.Info($"TcpServerSend > å‘送成功。");
                        return true;
                    }
                    catch (SocketException ex)
@@ -219,6 +232,8 @@
            // è½¬æ¢å›žåè¿›åˆ¶
            return Convert.ToInt32(new string(binaryChars), 2);
        }
        public static List<string> GetStaticClients() => clients.Keys.ToList();
        public static Dictionary<string, string> GetStaticScan() => TrayIps;
    }
}