I'm working on my first project using NModbus, and so far it's been a
great tool to work with.
I have run into one problem. When polling a device using ModbusTCP,
everything runs fine for 15 or 20 minutes, when all of a sudden I get
the following exception:
"Unable to read data from the transport connection: A non-blocking
socket operation could not be completed immediately".
Anyone encounter this before, or have any idea where to start looking
for a solution?
If needed, I can provide some of my source code.
Thanks,
Derrick
Some further information regarding the above problem:
Exception is thrown in the ModbusTransport.cs file, line #69 (part of
the UnicastMessage function). The actual exception is a
System.IO.IOException, with the message as stated above. The inner
exception is a Socket Exception, error code 10035
(System.Net.Sockets.SocketError.WouldBlock).
I believe the error is stemming from the Write(message), line #49,
since after it breaks at the exception, the "response" variable
remains null. The "message: varialbe is:
message = {Read 10 Registers Input beginning at address 54.}
(below are all decimal values)
message.MessageFrame[0] = 1
message.MessageFrame[1] = 4
message.MessageFrame[2] = 0
message.MessageFrame[3] = 54
message.MessageFrame[4] = 0
message.MessageFrame[5] = 10
Stack trace for the main exception:
at Modbus.IO.ModbusTransport.UnicastMessage[T](IModbusMessage message)
in D:\\Projects\\DOTNET\\NModbus\\tags\\NModbus_1.\\src\\Modbus\\IO\
\ModbusTransport.cs:line 69
at Modbus.Device.ModbusMaster.ReadRegisters(Byte functionCode, Byte
slaveAddress, UInt16 startAddress, UInt16 numberOfPoints) in D:\
\Projects\\DOTNET\\NModbus\\tags\\NModbus_1.0\\src\\Modbus\\Device\
\ModbusMaster.cs:line 84
at Modbus.Device.ModbusMaster.ReadInputRegisters(Byte slaveAddress,
UInt16 startAddress, UInt16 numberOfPoints) in D:\\Projects\\DOTNET\
\NModbus\\tags\\NModbus_1.0\\src\\Modbus\\Device\\ModbusMaster.cs:line
37
*My Code Starts Below. Can post if required *
at Communications.Modbus.ModbusCommunicator.Read[DataType](Byte
slaveID, ModbusAddressType addrType, UInt16 startAddress, UInt16
numRegisters) in D:\\Projects\\DOTNET\\PowerManagement\\trunk\\src\
\Communications\\Communications\\Modbus\\ModbusCommunicator.cs:line
167
at Communications.EGCP.EGCPPoller.gatherData() in D:\\Projects\\DOTNET\
\PowerManagement\\trunk\\src\\Communications\\Communications\\EGCP\
\EGCPPoller.cs:line 141
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)\r
\n at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
Inner Exception Stack Trace:
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)
Hope that helps...
Derrick
It appears that your slave device is becoming unavailable, the master
is trying to write to it, and fails. The socket IOException code
"Resource temporarily unavailable." is described here
http://msdn2.microsoft.com/en-us/library/ms740668.aspx
If you want to more detailed logs you can enable logging shown here
http://www.nmodbus.com/Samples.aspx#Sample8 this can be very helpful
and if you send me your log files I can help you debug them.
I would suggest increasing the Transport.Retries property. This will
help you if your slave device is just temporarily unavailable. Again,
the logs will tell you the whole story.
Good luck and let me know if I can help.
Scott
> Derrick- Hide quoted text -
>
> - Show quoted text -
My trouble seems to be coming from a "Transaction ID" mismatch. I'm
getting the exception as thrown by
ModbusTcpTransport.ValidateResponse, line 119.
What could cause the transaction ID's to go out of whack? Everything
runs fine for 20 minutes to half hour, then the transaction ID's start
to incrementally get out of sync (i.e., I noticed the expected/actual
IDs were off by 23, then 24, then 25, etc.).
I'd appreciate any input you may have.
P.S: I couldn't quite get the logging working. I'm going to try
again this afternoon.
Derrick
On Apr 27, 12:55 pm, sja...@gmail.com wrote:
> Derrick,
>
> It appears that your slave device is becoming unavailable, the master
> is trying to write to it, and fails. The socket IOException code
> "Resource temporarily unavailable." is described herehttp://msdn2.microsoft.com/en-us/library/ms740668.aspx
>
> If you want to more detailed logs you can enable logging shown herehttp://www.nmodbus.com/Samples.aspx#Sample8this can be very helpful
> > - Show quoted text -- Hide quoted text -
Interesting.
Without seeing log files I'm only guessing but...
I believe it is the same problem manifesting in a different way due to
the latest code changes (support for TCP transaction ID, previously
the transaction ID was ignored). The slave is becoming temporarily
unavailable, when the master attempts to read the response it catches
an IOException. The master then retries the message (with the next
transaction ID). Eventually the slave responds to the original request
and the transactions are out of sync.
I'd really like to see some logs if you have the chance. I can help
you set up a logger if you send me your code. I should have a chance
this weekend to give this some time.
Thanks.
Scott
On May 14, 9:33 am, Derrick <derrick.sig...@gmail.com> wrote:
> Sorry for the delay;
>
> My trouble seems to be coming from a "Transaction ID" mismatch. I'm
> getting the exception as thrown by
> ModbusTcpTransport.ValidateResponse, line 119.
>
> What could cause the transaction ID's to go out of whack? Everything
> runs fine for 20 minutes to half hour, then the transaction ID's start
> to incrementally get out of sync (i.e., I noticed the expected/actual
> IDs were off by 23, then 24, then 25, etc.).
>
> I'd appreciate any input you may have.
>
> P.S: I couldn't quite get the logging working. I'm going to try
> again this afternoon.
>
> Derrick
>
> On Apr 27, 12:55 pm, sja...@gmail.com wrote:
>
> > Derrick,
>
> > It appears that your slave device is becoming unavailable, the master
> > is trying to write to it, and fails. The socket IOException code
> > "Resource temporarily unavailable." is described herehttp://msdn2.microsoft.com/en-us/library/ms740668.aspx
>
> > If you want to more detailed logs you can enable logging shown herehttp://www.nmodbus.com/Samples.aspx#Sample8thiscan be very helpful
client = new TcpClient(IPAddress, PortNumber);
master = ModbusTcpMaster.CreateTcp(client);
while(true)
{
try
{
//poll device
}
catch(IOException)
{
client.Client.Disconnect(false);
client = new TcpClient(IPAddress, PortNumber);
master = ModbusTcpMaster.CreateTcp(client);
}
}
I've gotten the log to work. Here is the good output, ending with the
exception (I've snipped some of the successfull communications):
INFO Modbus.IO.ModbusTcpTransport Write - TX: 4, 52, 0, 0, 0, 6, 1,
1, 0, 8, 0, 8
DEBUG Modbus.IO.ModbusTcpTransport ReadRequestResponse - MBAP header:
4, 52, 0, 0, 0, 4
DEBUG Modbus.IO.ModbusTcpTransport ReadRequestResponse - 4 bytes in
PDU.
DEBUG Modbus.IO.ModbusTcpTransport ReadRequestResponse - PDU: 4
INFO Modbus.IO.ModbusTcpTransport ReadRequestResponse - RX: 4, 52, 0,
0, 0, 4, 1, 1, 1, 0
INFO Modbus.IO.ModbusTcpTransport Write - TX: 4, 53, 0, 0, 0, 6, 1,
2, 0, 0, 0, 8
DEBUG Modbus.IO.ModbusTcpTransport ReadRequestResponse - MBAP header:
4, 53, 0, 0, 0, 4
DEBUG Modbus.IO.ModbusTcpTransport ReadRequestResponse - 4 bytes in
PDU.
DEBUG Modbus.IO.ModbusTcpTransport ReadRequestResponse - PDU: 4
INFO Modbus.IO.ModbusTcpTransport ReadRequestResponse - RX: 4, 53, 0,
0, 0, 4, 1, 2, 1, 223
INFO Modbus.IO.ModbusTcpTransport Write - TX: 4, 54, 0, 0, 0, 6, 1,
1, 0, 8, 0, 8
DEBUG Modbus.IO.ModbusTcpTransport ReadRequestResponse - MBAP header:
4, 54, 0, 0, 0, 4
DEBUG Modbus.IO.ModbusTcpTransport ReadRequestResponse - 4 bytes in
PDU.
DEBUG Modbus.IO.ModbusTcpTransport ReadRequestResponse - PDU: 4
INFO Modbus.IO.ModbusTcpTransport ReadRequestResponse - RX: 4, 54, 0,
0, 0, 4, 1, 1, 1, 0
INFO Modbus.IO.ModbusTcpTransport Write - TX: 4, 55, 0, 0, 0, 6, 1,
2, 0, 0, 0, 8
DEBUG Modbus.IO.ModbusTcpTransport ReadRequestResponse - MBAP header:
4, 55, 0, 0, 0, 4
DEBUG Modbus.IO.ModbusTcpTransport ReadRequestResponse - 4 bytes in
PDU.
DEBUG Modbus.IO.ModbusTcpTransport ReadRequestResponse - PDU: 4
INFO Modbus.IO.ModbusTcpTransport ReadRequestResponse - RX: 4, 55, 0,
0, 0, 4, 1, 2, 1, 223
INFO Modbus.IO.ModbusTcpTransport Write - TX: 4, 56, 0, 0, 0, 6, 1,
1, 0, 8, 0, 8
DEBUG Modbus.IO.ModbusTcpTransport ReadRequestResponse - MBAP header:
4, 56, 0, 0, 0, 4
DEBUG Modbus.IO.ModbusTcpTransport ReadRequestResponse - 4 bytes in
PDU.
DEBUG Modbus.IO.ModbusTcpTransport ReadRequestResponse - PDU: 4
INFO Modbus.IO.ModbusTcpTransport ReadRequestResponse - RX: 4, 56, 0,
0, 0, 4, 1, 1, 1, 0
INFO Modbus.IO.ModbusTcpTransport Write - TX: 4, 57, 0, 0, 0, 6, 1,
2, 0, 0, 0, 8
DEBUG Modbus.IO.ModbusTcpTransport ReadRequestResponse - MBAP header:
4, 57, 0, 0, 0, 4
DEBUG Modbus.IO.ModbusTcpTransport ReadRequestResponse - 4 bytes in
PDU.
DEBUG Modbus.IO.ModbusTcpTransport ReadRequestResponse - PDU: 4
INFO Modbus.IO.ModbusTcpTransport ReadRequestResponse - RX: 4, 57, 0,
0, 0, 4, 1, 2, 1, 223
INFO Modbus.IO.ModbusTcpTransport Write - TX: 4, 58, 0, 0, 0, 6, 1,
1, 0, 8, 0, 8
DEBUG Modbus.IO.ModbusTcpTransport ReadRequestResponse - MBAP header:
4, 58, 0, 0, 0, 4
DEBUG Modbus.IO.ModbusTcpTransport ReadRequestResponse - 4 bytes in
PDU.
DEBUG Modbus.IO.ModbusTcpTransport ReadRequestResponse - PDU: 4
INFO Modbus.IO.ModbusTcpTransport ReadRequestResponse - RX: 4, 58, 0,
0, 0, 4, 1, 1, 1, 0
INFO Modbus.IO.ModbusTcpTransport Write - TX: 4, 59, 0, 0, 0, 6, 1,
2, 0, 0, 0, 8
DEBUG Modbus.IO.ModbusTcpTransport ReadRequestResponse - MBAP header:
4, 59, 0, 0, 0, 4
DEBUG Modbus.IO.ModbusTcpTransport ReadRequestResponse - 4 bytes in
PDU.
DEBUG Modbus.IO.ModbusTcpTransport ReadRequestResponse - PDU: 4
INFO Modbus.IO.ModbusTcpTransport ReadRequestResponse - RX: 4, 59, 0,
0, 0, 4, 1, 2, 1, 223
INFO Modbus.IO.ModbusTcpTransport Write - TX: 4, 60, 0, 0, 0, 6, 1,
1, 0, 8, 0, 8
ERROR Modbus.IO.ModbusTransport UnicastMessage - IO Exception, 5
retries remaining - Unable to read data from the transport connection:
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.
INFO Modbus.IO.ModbusTcpTransport Write - TX: 4, 60, 0, 0, 0, 6, 1,
1, 0, 8, 0, 8
ERROR Modbus.IO.ModbusTransport UnicastMessage - IO Exception, 4
retries remaining - Unable to read data from the transport connection:
A non-blocking socket operation could not be completed immediately.
INFO Modbus.IO.ModbusTcpTransport Write - TX: 4, 60, 0, 0, 0, 6, 1,
1, 0, 8, 0, 8
ERROR Modbus.IO.ModbusTransport UnicastMessage - IO Exception, 3
retries remaining - Unable to read data from the transport connection:
A non-blocking socket operation could not be completed immediately.
INFO Modbus.IO.ModbusTcpTransport Write - TX: 4, 60, 0, 0, 0, 6, 1,
1, 0, 8, 0, 8
ERROR Modbus.IO.ModbusTransport UnicastMessage - IO Exception, 2
retries remaining - Unable to read data from the transport connection:
A non-blocking socket operation could not be completed immediately.
INFO Modbus.IO.ModbusTcpTransport Write - TX: 4, 60, 0, 0, 0, 6, 1,
1, 0, 8, 0, 8
ERROR Modbus.IO.ModbusTransport UnicastMessage - IO Exception, 1
retries remaining - Unable to read data from the transport connection:
A non-blocking socket operation could not be completed immediately.
INFO Modbus.IO.ModbusTcpTransport Write - TX: 4, 60, 0, 0, 0, 6, 1,
1, 0, 8, 0, 8
ERROR Modbus.IO.ModbusTransport UnicastMessage - IO Exception, 0
retries remaining - Unable to read data from the transport connection: