Beispiel #1
0
        public async Task ListenOnPort()
        {
            var dacs7Server = new Dacs7Server(5011, SimulationPlcDataProvider.Instance);
            await dacs7Server.ConnectAsync();

            //var dacstClient = new Dacs7Client("127.0.0.1:5011");
            //await dacstClient.ConnectAsync();

            await Task.Delay(30000);

            //await dacstClient.DisconnectAsync();

            await dacs7Server.DisconnectAsync();
        }
Beispiel #2
0
        private static async Task <int> Serve(ServerOptions options, ILoggerFactory loggerFactory)
        {
            Dacs7Client      client   = null;
            IPlcDataProvider provider = null;

            if (string.IsNullOrWhiteSpace(options.DataProvider) || options.DataProvider.Equals("Simulation", StringComparison.InvariantCultureIgnoreCase))
            {
                if (options.Tags != null)
                {
                    foreach (var item in options.Tags)
                    {
                        var ri = ReadItem.CreateFromTag(item);
                        Console.WriteLine($"Register tag {item}");

                        SimulationPlcDataProvider.Instance.Register(ri.Area, ri.NumberOfItems, ri.DbNumber);
                    }
                }
                Console.WriteLine("Using Simulation Provider!");
                provider = SimulationPlcDataProvider.Instance;
            }
            else if (options.DataProvider.Equals("Relay", StringComparison.InvariantCultureIgnoreCase))
            {
                client = new Dacs7Client(options.Address, PlcConnectionType.Pg, 5000, loggerFactory)
                {
                    MaxAmQCalled  = (ushort)options.MaxJobs,
                    MaxAmQCalling = (ushort)options.MaxJobs,
                    PduSize       = options.MaxPduSize
                };
                RelayPlcDataProvider.Instance.UseClient(client);
                Console.WriteLine("Using Relay Provider!");
                provider = RelayPlcDataProvider.Instance;
            }

            var server = new Dacs7Server(options.Port, provider, loggerFactory)
            {
                MaxAmQCalled  = (ushort)options.MaxJobs,
                MaxAmQCalling = (ushort)options.MaxJobs,
                PduSize       = (ushort)480
            };
            var logger = loggerFactory?.CreateLogger("Dacs7Cli.Serve");

            try
            {
                Console.WriteLine($"Started serving on port {options.Port} !");
                await server.ConnectAsync();

                if (client != null)
                {
                    await client.ConnectAsync();
                }

                Console.WriteLine("Please press any key to stop serving!");
                Console.ReadKey();
            }
            catch (Exception ex)
            {
                logger?.LogError($"An error occured in Serve: {ex.Message} - {ex.InnerException?.Message}");
                return(1);
            }
            finally
            {
                if (client != null)
                {
                    await client.DisconnectAsync();
                }
                await server.DisconnectAsync();
            }

            return(0);
        }