EasyModbusTCP.NET icon indicating copy to clipboard operation
EasyModbusTCP.NET copied to clipboard

Network Stream

Open harjitsinghhpk opened this issue 3 years ago • 10 comments

If connected with TCP/IP, the connection aborted if there is no poll for sometime with server. It end up with below exception:

At this line: stream.Write(data, 0, data.Length-2);

System.Net.Sockets.SocketException : Unable to read data from the transport connection: An established connection was aborted by the software in your host machine.

Is there anyway to keep the connection live even there is no poll until disconnected from slave.

harjitsinghhpk avatar Jan 25 '22 11:01 harjitsinghhpk

You can poll any register every n seconds, where n < disconnection timeout Or you can check if connected before attempting reading or writing.

Padanian avatar Jan 25 '22 14:01 Padanian

Polling register is a hack & i do check the connection before trying write. The connection status is true. To tackle it more efficiently, can it be done by re-connecting to same tcpclient incase the exception occur?

harjitsinghhpk avatar Jan 25 '22 16:01 harjitsinghhpk

Polling a register is not a hack. Anyway, the right thing to do is to set the tcp timeout on your server to a sensible value, and/or prevent the timeout to expire.

Padanian avatar Jan 25 '22 16:01 Padanian

Or Can we add a system.timer in the master library, which will keep the connection live by writing and reading on stream after 10 seconds. The write and read can be blocked if there is any request on the function codes. If there is not request received to the library for 10 seconds it will use the above steam read/write to keep the connection alive.

harjitsinghhpk avatar Jan 25 '22 18:01 harjitsinghhpk

Polling a register is not a hack. Anyway, the right thing to do is to set the tcp timeout on your server to a sensible value, and/or prevent the timeout to expire.

: Issue with polling register is, user have to define the register which is available in the slave to poll always.

harjitsinghhpk avatar Jan 25 '22 18:01 harjitsinghhpk

Well, if you plan to connect via modbus to a slave, you should at least be aware of one or more register addresses actually available on that slave. Don't you think?

Padanian avatar Jan 25 '22 19:01 Padanian

Or Can we add a system.timer in the master library, which will keep the connection live by writing and reading on stream after 10 seconds. The write and read can be blocked if there is any request on the function codes. If there is not request received to the library for 10 seconds it will use the above steam read/write to keep the connection alive.

Terrible idea in the class definition. You can do it though in your implementation.

Padanian avatar Jan 25 '22 19:01 Padanian

Well, if you plan to connect via modbus to a slave, you should at least be aware of one or more register addresses actually available on that slave. Don't you think?

Hi, Definitely there will be some registers in the slave, but it will be uncertain extra work on the master interface who so ever is implementing the library in their project. In case where a user just want to get the data when a request is sent.

To elaborate further, say i have prepared a modbus master tool using the library. I can connect it to multiple slave for testing/data collection. Each slave may not have the same address as other, in this case a additional lookup for defining the keep alive registers need to be taken care. I was just looking a way out to avoid this.

harjitsinghhpk avatar Jan 26 '22 04:01 harjitsinghhpk

The issue here, IMHO, is that you have a timing-out slave problem, that you'd like to solve on the master. If so, why don't you just close the connection after polling and reopen the connection before polling, so that the slave will never time out.

Padanian avatar Jan 26 '22 10:01 Padanian

The issue here, IMHO, is that you have a timing-out slave problem, that you'd like to solve on the master.

If so, why don't you just close the connection after polling and reopen the connection before polling, so that the slave will never time out.

Probably the best to do i guess. 👌🏻

harjitsinghhpk avatar Jan 26 '22 10:01 harjitsinghhpk