Example #1
0
        public void ClientAndServerCommunication()
        {
            using var runtime = new Runtime(new RuntimeConfig { NumCoreThreads = 2 });
            var map = new DeviceMap();

            map.AddEndpoint(UNIT_ID, new WriteHandler(), new DatabaseUpdate((db) =>
            {
                for (ushort i = 0; i < NUM_POINTS; ++i)
                {
                    db.AddCoil(i, false);
                    db.AddDiscreteInput(i, false);
                    db.AddHoldingRegister(i, 0);
                    db.AddInputRegister(i, 0);
                }
            }));

            var server = Server.CreateTcpServer(runtime, ENDPOINT, 100, map);
            var client = Channel.CreateTcpClient(runtime, ENDPOINT, 10);

            // set a unique pattern to test reads
            server.Update(UNIT_ID, new DatabaseUpdate(db =>
            {
                db.UpdateDiscreteInput(3, true);
                db.UpdateInputRegister(4, 42);
            }));

            TestReadDiscreteInputs(client);
            TestReadInputRegisters(client);
            TestWriteSingleCoil(client);
            TestWriteSingleRegister(client);
            TestWriteMultipeCoils(client);
            TestWriteMultipeRegisters(client);
        }
Example #2
0
        // ANCHOR_END: auth_handler

        static void Main(string[] args)
        {
            // initialize logging with the default configuration
            Logging.Configure(
                new LoggingConfig(),
                new ConsoleLogger()
                );

            // initialize the runtime
            var runtime = new Runtime(new RuntimeConfig {
                NumCoreThreads = 4
            });

            // create the device map
            // ANCHOR: device_map_init
            var map = new DeviceMap();

            map.AddEndpoint(1, new WriteHandler(), db =>
            {
                for (ushort i = 0; i < 10; ++i)
                {
                    db.AddCoil(i, false);
                    db.AddDiscreteInput(i, false);
                    db.AddHoldingRegister(i, 0);
                    db.AddInputRegister(i, 0);
                }
            });
            // ANCHOR_END: device_map_init

            if (args.Length != 1)
            {
                Console.WriteLine("you must specify a transport type");
                Console.WriteLine("usage: server_example <channel> (tcp, rtu, tls-ca, tls-self-signed)");
                Environment.Exit(-1);
            }

            // create the TCP server
            var server = CreateServer(args[0], runtime, map);

            try
            {
                RunServer(server);
            }
            finally
            {
                runtime.Shutdown();
            }
        }
Example #3
0
        static void Main(string[] args)
        {
            Logging.SetMaxLogLevel(LogLevel.Info);
            Logging.SetHandler(new LogHandler());

            using var runtime = new Runtime(new RuntimeConfig { NumCoreThreads = 1 });
            var map = new DeviceMap();

            map.AddEndpoint(1, new WriteHandler(), new DatabaseUpdate((db) =>
            {
                for (ushort i = 0; i < 10; ++i)
                {
                    db.AddCoil(i, false);
                    db.AddDiscreteInput(i, false);
                    db.AddHoldingRegister(i, 0);
                    db.AddInputRegister(i, 0);
                }
            }));

            var server = Server.CreateTcpServer(runtime, "127.0.0.1:502", 10, map);

            ushort registerValue = 0;
            bool   bitValue      = false;

            while (true)
            {
                server.Update(1, new DatabaseUpdate((db) =>
                {
                    registerValue += 1;
                    bitValue       = !bitValue;
                    for (ushort i = 0; i < 10; ++i)
                    {
                        db.UpdateDiscreteInput(i, bitValue);
                        db.UpdateInputRegister(i, registerValue);
                    }
                }));
                Thread.Sleep(1000);
            }
        }