예제 #1
0
        static void Main(string[] args)
        {
            RawTcp(); return;//

            byte[]        buffer = new byte[] { 0x01, 0x00, 0x13, 0x00, 0x13 };
            ModbusMessage mm     = ModbusRequest.ParseBuffer(buffer);

            return;

            //ReflectMyClasses();return;
            //TestMessage();return;
            ModbusTcpClient client = new ModbusTcpClient();

            Console.WriteLine($"Keep Alive          : {client.KeepAlive}");
            Console.WriteLine($"Receive Buffer Size : {client.ReceiveBufferSize = 1024}");
            Console.WriteLine($"Send Buffer Size    : {client.SendBufferSize = 1024}");
            Console.WriteLine($"Receive Timeout     : {client.ReceiveTimeout = 500}");
            Console.WriteLine($"Send Timeout        : {client.SendTimeout = 500}");
            Console.WriteLine($"No Delay            : {client.NoDelay}");
            Console.WriteLine($"Reuse Address       : {client.ReuseAddress = true}");
            client.Connect("localhost");
            bool running = true;

            Task.Run(() =>
            {
                try
                {
                    while (running && client.Connected)
                    {
                        int length = client.SendMessage(new WriteSingleRegisterRequest(9999, 1000));

                        Console.WriteLine($"{length} bytes sent");
                        Thread.Sleep(200);
                    }
                    if (client.Connected)
                    {
                        Console.WriteLine("Stopped");
                    }
                    else
                    {
                        Console.WriteLine("Disconnected");
                    }
                }
                catch (Exception x)
                {
                    Console.WriteLine($"Error: {x.Message}");
                    client = new ModbusTcpClient("localhost");
                }
            });

            Console.ReadLine();
            running = false;
            //new ArraySegment<byte>()
        }