I'm facing similar problem as reported in Issue #50:
http://code.google.com/p/nmodbus/issues/detail?id=50&q=label%3APriority-Medium
The scenario is actually the same as in Issue #50, but I added a part
of a code that catches the excpetions and tries to recconect.
I see following exceptions recorded in log file:
===>
WARN Modbus.IO.ModbusTransport - IOException, 3 retries remaining -
System.IO.IOException: Unable to read data from the transport
connection: An existing connection was forcibly closed by the remote
host. ---> System.Net.Sockets.SocketException: An existing connection
was forcibly closed by the remote host
at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset,
Int32 size, SocketFlags socketFlags)
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32
offset, Int32 size)
--- End of inner exception stack trace ---
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32
offset, Int32 size)
at Modbus.IO.TcpClientAdapter.Read(Byte[] buffer, Int32 offset,
Int32 size)
at Modbus.IO.ModbusIpTransport.ReadRequestResponse(IStreamResource
streamResource)
at Modbus.IO.ModbusIpTransport.ReadResponse[T]()
at Modbus.IO.ModbusTransport.UnicastMessage[T](IModbusMessage
message)
<===
Right after, a write attempt causes InvalidOperationException (IOE):
===>
INFO Modbus.IO.ModbusIpTransport - TX: 1, 159, 0, 0, 0, 6, 0, 3, 0,
0, 0, 52
ERROR System.InvalidOperationException: The operation is not allowed
on non-connected sockets.
at System.Net.Sockets.TcpClient.GetStream()
at Modbus.IO.TcpClientAdapter.Write(Byte[] buffer, Int32 offset,
Int32 size)
at Modbus.IO.ModbusIpTransport.Write(IModbusMessage message)
at Modbus.IO.ModbusTransport.UnicastMessage[T](IModbusMessage
message)
at Modbus.Device.ModbusMaster.ReadRegisters(Byte functionCode, Byte
slaveAddress, UInt16 startAddress, UInt16 numberOfPoints)
at Modbus.Device.ModbusMaster.ReadHoldingRegisters(Byte
slaveAddress, UInt16 startAddress, UInt16 numberOfPoints)
at Modbus.Device.ModbusIpMaster.ReadHoldingRegisters(UInt16
startAddress, UInt16 numberOfPoints)
at XXX.ReadMessage(Int32& counter, UInt16[]& values)
<===
I catch this IOE and try to reconnect to remote host explicitly by
masterTcpClient.Connect(ipAddress.ToString(), portId), but this causes
SocketException (surprise) ...
===>
ERROR Error by reconnection:System.Net.Sockets.SocketException: A
connect request was made on an already connected socket
at System.Net.Sockets.TcpClient.Connect(String hostname, Int32
port)
at XXX.ReconnectToRemoteHost()
<===
Does it mean that underlying NModbus layers (Modbus.IO.ModbusTransport
etc.) reconnected the transport connection by themselves in the mean
time? But then, why IOE persits in case of read/write (Issue #50)?
Can you explain briefly what is going on and what behaviour is to be
expected? If there is a automatic recovery mechanism (reconnection -
how many attempts/time window?) is there any event fired on reconnect?
Any help, explanation will be very useful ...
Is there any safe-procedure to be applied in such cases e.g.:
- force closure of all TCP connections (or init stack) -->
masterTcpClient.Close();
- re-establish the connection:
masterTcpClient = new TcpClient(ipAddress.ToString(), portId);
master = ModbusIpMaster.CreateIp(masterTcpClient);
Could (only) such procedure (see above) work out in case we identify
the network failure or there is more elegant solution for
recconection ...
Thank you so much for your help!
Seasonal greetings & A Happy New Year!
David
still struggeling with reconnection after network failure - can anyone
help or at least comment on this issue?
Once NModbus stack enters the state with
"InvalidOperationException" (please see below, physical IP connection
fails and recovers after a while), I can't find a way how to reconnect
it with the remote host. The hard-way init as follows:
// force closure of all TCP connections
masterTcpClient.Close();
// re-establish the connection:
masterTcpClient = new TcpClient(ipAddress.ToString(), portId);
master = ModbusIpMaster.CreateIp(masterTcpClient);
... doesn't work either due to System.Net.Sockets.SocketException: A
connection attempt failed because the connected party did not properly
respond after a period of time, or established connection failed
because connected host has failed to respond 85.10.32.103:502,
precisely "Socket is already connected" (error-code == 10056)!
I'd be thankful for any hints/help/comment on this issue that would
lead to resolution or conclusion of any kind.
Regards,
David
On Dec 26 2009, 9:56 pm, dave <david.ba...@gmail.com> wrote:
> Hi,
>
> I'm facing similar problem as reported in Issue #50:
>
> http://code.google.com/p/nmodbus/issues/detail?id=50&q=label%3APriori...
the reconnection is successful on the way described. I've overlooked
another bug in my code that prevented proper execution.
So, the connection can be re-established automatically by handling the
exceptions at read/write (SocketException, InvalidOperationException
etc. on network failure) with reconnection to remote host:
while (true)
{
...
try
{
master.ReadX / WriteX (...);
...
}
catch (SocketExcpetion / InvalidOperationException e)
{
try {
// force closure of all TCP connections
masterTcpClient.Close();
// re-establish the connection:
masterTcpClient = new TcpClient(ipAddress.ToString(), portId);
master = ModbusIpMaster.CreateIp(masterTcpClient);
} catch (Exception) {}
}
...
}
It reconnects automatically after network connection is back.
Simple & quite ugly way, but it works at least, and life is brighter
again!
Maybe Issue #50 can be now closed as well ...
Regards,
David
> > David- Skrij navedeno besedilo -
>
> - Prikaži citirano besedilo -