Author Topic: Modbus TCP - Check for Connectivity on Launch  (Read 2025 times)

iamjer76

  • Newbie
  • *
  • Posts: 21
    • View Profile
Modbus TCP - Check for Connectivity on Launch
« 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.

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: Modbus TCP - Check for Connectivity on Launch
« Reply #1 on: August 07, 2015, 10:26:55 AM »
Are you reading via code or are the visual controls throwing exceptions?

iamjer76

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Modbus TCP - Check for Connectivity on Launch
« Reply #2 on: August 07, 2015, 11:46:02 AM »
The visual controls

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: Modbus TCP - Check for Connectivity on Launch
« Reply #3 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.

Noe

  • Full Member
  • ***
  • Posts: 205
    • View Profile
Re: Modbus TCP - Check for Connectivity on Launch
« Reply #4 on: August 07, 2015, 01:08:29 PM »
What version of AdvancedHMI are you using?

iamjer76

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Modbus TCP - Check for Connectivity on Launch
« Reply #5 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?

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: Modbus TCP - Check for Connectivity on Launch
« Reply #6 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

iamjer76

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Modbus TCP - Check for Connectivity on Launch
« Reply #7 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.

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: Modbus TCP - Check for Connectivity on Launch
« Reply #8 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

iamjer76

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Modbus TCP - Check for Connectivity on Launch
« Reply #9 on: August 07, 2015, 03:24:32 PM »
Perfect! Thanks Archie!