ModbusIpMaster implementation scope

40 views
Skip to first unread message

bill m

unread,
Nov 20, 2009, 11:54:10 PM11/20/09
to NModbus
This is a best practice type of question:

I have a winform application where I communicate to a Modicon PLC in
various functions via various supporting object functions through TCP/
IP - through a client based approach with handshakes.

Is it best practice to create one instance in the winform of TcpClient
and ModbusIpMaster objects and pass the ModbusIpMaster instance to
these object functions? Holding the TcpClient connection open?

or should I follow the sample examples that are on the NModbus website
and only create these when communication is necessary. Create the
TcpClient and ModbusIpMaster objects in 'using' statements to ensure
their scope.

I am currently following the 'using' approach and occasionally receive
socket errors, and am wondering if there is a better way to do this,
understand the errors, or at least recover from the exceptions?

--------------------------

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
at System.Net.Sockets.TcpClient..ctor(String hostname, Int32 port)

A transport-level error has occurred when sending the request to the
server. (provider: TCP Provider, error: 0 - An existing connection was
forcibly closed by the remote host.)




Scott Alexander

unread,
Nov 23, 2009, 12:43:38 PM11/23/09
to nmodbus...@googlegroups.com
It really depends on how frequently you'll be using the connection. If
you're using it frequently then you should pass the ModbusIpMaster
around so as to not waste time opening/closing the connection. Else,
the other approach may be appropriate.

Disposing of the ModbusIpMaster will dispose the TcpClient.

Not sure about your SocketException. You could always retry creating
the TcpClient.

Scott
> --
>
> You received this message because you are subscribed to the Google Groups "NModbus" group.
> To post to this group, send email to nmodbus...@googlegroups.com.
> To unsubscribe from this group, send email to nmodbus-discu...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/nmodbus-discuss?hl=.
>
>
>

Elco Grobler

unread,
Nov 23, 2009, 3:53:56 PM11/23/09
to nmodbus...@googlegroups.com
Hi Scott

Thanks for your reply. I really needed a modbus driver urgently and could
not solve the problem in time and therefore had bite the bullet and bought
one from www.automatedsolutions.com. I am quite impressed with the driver
and also their support.

Thanks again for a the free source code it did get me started.

Regards
Elco
Reply all
Reply to author
Forward
0 new messages