AdvancedHMI Software

General Category => Support Questions => Topic started by: iamjer76 on August 07, 2015, 10:22:28 AM

Title: Modbus TCP - Check for Connectivity on Launch
Post by: iamjer76 on August 07, 2015, 10:22:28 AM
I have an application that is connecting via Modbus TCP. If the PLC is not on the network, which happens frequently, the application throws an exception as it can't read from the PLC. I'm looking for a way to check for connectivity before it loads everything else so that I can send them a message and close the application gracefully rather than the standard exception message.
Title: Re: Modbus TCP - Check for Connectivity on Launch
Post by: Archie on August 07, 2015, 10:26:55 AM
Are you reading via code or are the visual controls throwing exceptions?
Title: Re: Modbus TCP - Check for Connectivity on Launch
Post by: iamjer76 on August 07, 2015, 11:46:02 AM
The visual controls
Title: Re: Modbus TCP - Check for Connectivity on Launch
Post by: Archie on August 07, 2015, 12:35:49 PM
Which controls are you using? All of the built in controls should catch all exceptions and display messages on the controls (unless you set SuppressErrorDisplay to True). If an exception is being thrown, then I need to fix the control.
Title: Re: Modbus TCP - Check for Connectivity on Launch
Post by: Noe on August 07, 2015, 01:08:29 PM
What version of AdvancedHMI are you using?
Title: Re: Modbus TCP - Check for Connectivity on Launch
Post by: iamjer76 on August 07, 2015, 02:03:11 PM
Ok, I stand corrected. I thought it was the visual controls that were throwing the exception, but that is not the case. I have a couple of timers in my code that are running and they are reading from the PLC. So, I guess a better question to ask is "What do I need to add to my code to check if the connection is there before it tries to read and errors out?
Title: Re: Modbus TCP - Check for Connectivity on Launch
Post by: Archie on August 07, 2015, 02:07:26 PM
When reading/writing in code, you want to always wrap your code in a Try-Catch. For example:

Try
   ModbusTCPCom1.Read("40001")
Catch ex as exception
'* Do something here when there is a com problem
End Try
Title: Re: Modbus TCP - Check for Connectivity on Launch
Post by: iamjer76 on August 07, 2015, 02:38:08 PM
I'll give that a shot. Is there not a way to just check for connectivity? There may be many reasons why it couldn't read, but what I'm looking for specifically is to check for connectivity before it tries to read.
Title: Re: Modbus TCP - Check for Connectivity on Launch
Post by: Archie on August 07, 2015, 02:56:19 PM
You can do a ping test like this:

        Dim pingSender As New System.Net.NetworkInformation.Ping
        Dim reply As System.Net.NetworkInformation.PingReply
        Try
            reply = pingSender.Send("aFiller9")

            If reply.Status <> Net.NetworkInformation.IPStatus.Success Then
                System.Windows.Forms.MessageBox.Show("Failed network test")
            Else
                System.Windows.Forms.MessageBox.Show("succeeded")
            End If
        Catch ex As Exception
            System.Windows.Forms.MessageBox.Show("Failed network test")
        End Try
Title: Re: Modbus TCP - Check for Connectivity on Launch
Post by: iamjer76 on August 07, 2015, 03:24:32 PM
Perfect! Thanks Archie!