Apollo3zehn / FluentModbus

Lightweight and fast client and server implementation of the Modbus protocol (TCP/RTU).
MIT License
188 stars 69 forks source link

Instances affect each other in case of disconnects #90

Open JeremyMahieu opened 1 year ago

JeremyMahieu commented 1 year ago

It's not impossible that it's due to my own code but I'm having the following problem: I connect to 5 modbus tcp server in a foreach loop. I try to connect and then read some values. The code is surrounded by try catches. Imagine server 1, 2, 3, and 5 is available but 4 is not available. After I get an exception on server 4, I don't get anything out of server 5 either. If I swap the place of 4 and 5, I can read values from server 5.

I don't think my "ModbusService" had any statics or dependency injected things that might be singleton. So I believe the problem must be in the library.

Any tips on how I could diagnose this?

        public DirigentService()
        {
            ModbusDevice dev1 = new ModbusDevice();
            dev1.IPEndPoint = new(IPAddress.Parse("192.168.0.2"), 502);
            dev1.ModbusService = new ModbusService();
            devices.Add(dev1);

            ModbusDevice dev2 = new ModbusDevice();
            dev2.IPEndPoint = new(IPAddress.Parse("192.168.0.3"), 502);
            dev2.ModbusService = new ModbusService();
            devices.Add(dev2);

            (...) add more devices

            // Set up a timer to read values every few seconds
            Log.Debug("Seting up read timer");
            timer = new System.Timers.Timer(2000);
            timer.Elapsed += (s, e) => DoRead();
            timer.Start();
        }

        private void DoRead()
        {
            foreach (var dev in in devices)
            {
                try
                {
                    dev.ConnectAndReadAllValues();
                }
                catch { }
            }

        }
Apollo3zehn commented 4 months ago

Sorry for being so late. FluentModbus has no (known) interdependencies between individual instances so they should not affect each other. I have no clue what could cause this behavior :-(