public Task <object> GetDataAsync()
        {
            while (!modbusClientAlive)
            {
                try
                {
                    modbusClient.Init();
                    modbusClientAlive = true;
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"[EXCEPTION] Exception while instantiating Modbus client: {ex.Message}");
                    Console.WriteLine("\nSleeping for 15 seconds before retrying to talk to Modbus host...\n");
                    Task.Delay(TimeSpan.FromSeconds(15)).Wait();
                }
            }

            var           ct = new CancellationToken();
            Task <object> t  = Task.Run <object>(async() =>
            {
                var voltage    = Array.Empty <short>();
                var current    = Array.Empty <short>();
                var hardwareId = String.Empty;
                try
                {
                    voltage    = await modbusClient.ReadRegistersAsync(40001, 3);
                    current    = await modbusClient.ReadRegistersAsync(41001, 3);
                    hardwareId = "Function Code 0x2b (43)";
                }
                catch (Exception ex)
                {
                    Console.WriteLine(
                        $"[EXCEPTION] Exception while calling ReadRegistersAsync(): {ex.Message}");
                    Console.WriteLine(
                        "\nSleeping for 5 seconds before retrying...\n");
                    Task.Delay(TimeSpan.FromSeconds(5), ct).Wait(ct);
                }

                return(new
                {
                    deviceId,
                    voltage,
                    current,
                    hardwareId
                });
            }, ct);

            if (t.Wait(9000, ct))
            {
                return(t);
            }

            Console.WriteLine("Aborting modbus Task, took too long to return.");
            //Console.WriteLine("Restarting collector...\n");
            //Assembly a = Assembly.GetExecutingAssembly();
            //System.Diagnostics.Process.Start("dotnet.exe", a.Location);
            //Environment.Exit(-2);
            return(null);
        }
Beispiel #2
0
        public void SocketTimeoutTest()
        {
            Console.WriteLine("Running SocketTimeoutTest...");
            int TEST_TIMEOUT        = 6000;
            int MODBUS_CALL_TIMEOUT = 3000;

            Int32  port       = 50222;
            string v4Loopback = ("127.0.0.1");

            TcpListener server = new TcpListener(IPAddress.Parse(v4Loopback), port);

            server.Start();

            CancellationTokenSource cts = new CancellationTokenSource(TEST_TIMEOUT);
            CancellationToken       ct  = cts.Token;

            var t = Task.Run(() =>
            {
                server.AcceptTcpClientAsync();
                ModbusClient mc = new ModbusClient(v4Loopback, port, MODBUS_CALL_TIMEOUT);
                mc.Init();
                short[] r = mc.ReadRegistersAsync(4304, 2, 1).Result;
            }, ct);

            try
            {
                t.Wait(ct);
            }
            catch (OperationCanceledException)
            {
                Assert.True(false, "The call to ReadRegistersAsync() did not return " +
                            $"within expected time window of {MODBUS_CALL_TIMEOUT / 1000} seconds.");
            }
            catch (Exception ex)
            {
                if (ex.InnerException.InnerException is TimeoutException ||
                    ex.InnerException.InnerException is IOException)
                {
                    // We expect a TimeoutException or IOException if ReadRegistersAsync() adheres to our timeout
                    Assert.True(true);
                }
                else
                {
                    throw;
                }
            }
            finally
            {
                server.Server.Close();
                server.Server.Dispose();
            }
        }