Exemple #1
0
        /// <summary>
        /// socket方式
        /// </summary>
        public static void TestSocket()
        {
            var socketWatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            var endpoint    = new IPEndPoint(IPAddress.Parse("172.16.10.40"), 2317);

            socketWatch.Bind(endpoint);
            socketWatch.Listen(20);
            Socket socConnection = socketWatch.Accept();

            socConnection.Send(ModBus.HexStrToHexByte("06 05 00 10 FF 00 8C 48"));
            Thread.Sleep(5000);
            socConnection.Send(ModBus.HexStrToHexByte("06 05 00 10 00 00 CD B8"));
            Console.ReadKey();
        }
Exemple #2
0
        private static string WriteRead(string hexdata, Socket serverSocket)
        {
            byte[] recByte = new byte[1024];
            var    sd      = ModBus.HexStrToHexByte(hexdata);//开

            serverSocket.Send(sd, sd.Length, 0);
            int bytes  = serverSocket.Receive(recByte, recByte.Length, 0);
            var rcvbt1 = new byte[bytes];

            Array.Copy(recByte, 0, rcvbt1, 0, bytes);
            var rst = ModBus.HexByteToHexStr(rcvbt1);
            var s   = rst.Replace("7777772E7573722E636E", "");

            if (s == "")//排除第一个空
            {
                bytes  = serverSocket.Receive(recByte, recByte.Length, 0);
                rcvbt1 = new byte[bytes];
                Array.Copy(recByte, 0, rcvbt1, 0, bytes);
                rst = ModBus.HexByteToHexStr(rcvbt1);
                s   = rst.Replace("7777772E7573722E636E", "");
            }
            return(s);
        }