Author Topic: Testing PLC connection without using a ping command  (Read 3091 times)

FuzyDragon

  • Newbie
  • *
  • Posts: 19
    • View Profile
Testing PLC connection without using a ping command
« on: February 09, 2016, 08:47:50 AM »
Hello all,

I am running into issues with my current project when it comes to checking the connection state of my PLC. I am currently using the ping command to ping the IP address of the PLC but that just checks if the PLC is on the network, not if the PLC is actually online and ready to send/receive data. Obviously, this causes exceptions as I currently have reads and writes happen only when the PLC is on the network. Is there a way through AdvancedHMI to check if a PLC is ready for data as opposed to if it is connected to a network? Here is a sample of what I currently use just for reference.

If My.Computer.Network.Ping(plcIP) = True Then
            outputDataReals = EthernetIPforCLXCom1.Read("HMIIO.Input[0]", 500)
            inputDataReals = EthernetIPforCLXCom1.Read("HMIIO.Output[0]", 500)
            outputDataBits = EthernetIPforCLXCom1.Read("HMIIO.InputBits[0]", 500)
            inputDataBits = EthernetIPforCLXCom1.Read("HMIIO.OutputBits[0]", 500)

            outputDataRealsBuffer = EthernetIPforCLXCom1.Read("HMIIO.Input[0]", 500)
            outputDataBitsBuffer = EthernetIPforCLXCom1.Read("HMIIO.InputBits[0]", 500)
        Else
            plcStatus = "Connection Lost"
            Label1.Visible = True
            Label1.Text = plcStatus
            Label1.BackColor = Color.Red
        End If

Thank you in advance for any help or advice,

Fuzydragon

rbelknap

  • Jr. Member
  • **
  • Posts: 68
    • View Profile
Re: Testing PLC connection without using a ping command
« Reply #1 on: February 09, 2016, 09:03:35 AM »
FuzyDragon,

It sounds like you need to implement a watchdog timer.  If you google it you'll get lot of examples/explanations on how it works.

Essentially, you would have a timer in your AHMI app, and a timer in your PLC.
The AHMI app would turn on a bit in the PLC, which the PLC would turn back off after a short delay.
When the AHMI app turns this bit on you would also start a timer.
If the timer times out before the bit turns off then you've lost comms.

You'll want to implement this after you finish debugging the rest of the program or it'll get in the way all the time.

If this doesn't make complete sense then hopefully the google search will make it clearer.

Good Luck.


Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5324
    • View Profile
    • AdvancedHMI
Re: Testing PLC connection without using a ping command
« Reply #2 on: February 09, 2016, 09:18:43 AM »
You always want to wrap your read/writes via code in a Try-Catch block so your application doesn't just dump. For example
Code: [Select]
Try
   EthernetIPforCLXCom1.Read("MyTag")
catch ex as exception
   msgbox "failed to read. " & ex.message
End Try

You can also the the 2 events to let your program know when connection is established and when it is lost:
Code: [Select]
    Private Sub EthernetIPforCLXCom1_ConnectionEstablished(sender As Object, e As EventArgs) Handles EthernetIPforCLXCom1.ConnectionEstablished
        MsgBox("Now connected")
    End Sub

    Private Sub EthernetIPforCLXCom1_ConnectionClosed(sender As Object, e As EventArgs) Handles EthernetIPforCLXCom1.ConnectionClosed
        MsgBox("Lost connection")
    End Sub
« Last Edit: February 09, 2016, 01:51:56 PM by Archie »

FuzyDragon

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Testing PLC connection without using a ping command
« Reply #3 on: February 09, 2016, 01:48:50 PM »
Thank you both for your suggestions. It seems like the try catch is more effective in preventing my program from crashing when a disconnect occurs. I'll keep in mind the watchdog timer and try to find the best way to implement it into my project. Thanks again for your help and I'll post again if I have any trouble