Closed Zonciu closed 3 years ago
Thank you, I´ll check the later today. I was just about to publish an update so I can include a fix for this.
I think there is no way to solve this. ModbusTcpClient.IsConnected
checks for TcpClient.Connected
which again checks for Socket.Connected
, which states the following:
So I am afraid I cannot do something about it, except you have an alternative solution.
I see, I will use try-catch
to create a new ModbusTcpClient to avoid this issue.
Issue
See here
When
_tcpClient.ConnectAsync
success, at this moment I close theModbus Server
,_tcpClient.GetStream
will be null or disposed, causingObjectDisposedException
orNullReferenceException
while reading/writing data.At this time
ModbusTcpClient._tcpClient
is not null,ModbusTcpClient.IsConnected
is true, butModbusTcpClient._networkStream
isnull
ordisposed
. Useif(!ModbusTcpClient.IsConnected) { ModbusTcpClient.Connect(...) }
to reconnect will not work.For now I catch
ObjectDisposedException
andNullReferenceException
, then I create a newModbusTcpClient
to avoid this issue, rather than catch and callConnect
directly._networkStream is null:
_networkStream is disposed: