rossmann-engineering / EasyModbusTCP.NET

Modbus TCP, Modbus UDP and Modbus RTU client/server library for .NET implementations
947 stars 412 forks source link

When the server is actively disconnected(当服务器端主动断开时) #74

Open jkh404 opened 2 years ago

jkh404 commented 2 years ago

我用的EasyModbusTCP.NET v5.6 当TCP服务器断开连接时,客户端并不能获取到反馈,依然能够读取和写入(显然这是错误的)。 不过,我查看EasyModbusTCP.NET 项目的 ModbusClient.cs 源代码后, 将Connected的代码改为以下代码即可得到正确的反馈。

public bool Connected
{
    get{
                if (serialport != null)
                {
                    return (serialport.IsOpen);
                }

                if (udpFlag & tcpClient != null)
                    return true;
                if (tcpClient == null)
                    return false;
                else
                {
                    return connected && tcpClient.Connected;

                }

    }
}

image 也许这不是最好的处理方法,但经过我的测试确实解决了这个问题。哈哈😁 en: I use EasyModbusTCP.NET v5.6 When the TCP server is disconnected, the client cannot get feedback and can still read and write (obviously wrong). However, after I looked at the ModbusClient.cs source code of the EasyModbusTCP.NET project, I changed the Connected code to the following code to get the correct feedback. Maybe this is not the best way to deal with it, but my tests did solve the problem.😁

jkh404 commented 2 years ago

这是最终效果 QQ录屏20220125125504

wmx1033 commented 7 months ago

的确实现了,顶一个