Author Topic: Detect PLC connection lost in VB  (Read 2849 times)

ASF

  • Newbie
  • *
  • Posts: 26
    • View Profile
Detect PLC connection lost in VB
« on: April 21, 2017, 01:19:17 AM »
I have a PC running AdvancedHMI, connected to my PLC via USB-Ethernet adaptor, which is turning out to be quite flaky. I'm going to replace it, but while I have a more or less constant stream of comms failures, I'd like to find a way to programatically detect that comms failure, and take appropriate action.

For the most part, the action I want to take is to prevent certain subs from running until the connection to the PLC is restored again, and all of the tag data has been updated.

Is there any way I can address the comms driver directly, to detect a change in the connection status and take action accordingly?

I'm using the Modbus TCP driver.

ShriramPendse

  • Newbie
  • *
  • Posts: 24
  • " AUTOMATION " simplified .
    • View Profile
Re: Detect PLC connection lost in VB
« Reply #1 on: April 21, 2017, 01:45:18 AM »
Hello ,

Read an autoincrementing integer from PLC . In AdvHMI keep checking its value , if it is unchanged for certain time , you know comm is in trouble .

Comm driver too has error status which you can monitor .

ASF

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: Detect PLC connection lost in VB
« Reply #2 on: April 21, 2017, 02:53:07 AM »
I'm already monitoring the RTC, which changes every second - the problem is, how do I trigger a sub to check if it has been updated?

The error status in the comms driver sounds promising...I'll have a look into that. Anyone got an example of how they've implemented it?

ASF

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: Detect PLC connection lost in VB
« Reply #3 on: April 21, 2017, 03:11:48 AM »
Perhaps I'd be better to ask some more specific questions...

I've had a look through the different items I can get from the comms driver:

- Com Error
- Connection Closed
- Connection Established
- Data Received
- Disposed
- Subscription Data Received

...and here's my guess as to when each of them trigger. Can anyone correct my mistakes, and fill in the blanks? Just trying to get an overall picture so I can describe how best to construct my code.

- Com Error: will execute once only when communications to the PLC are lost. Will not re-trigger until communications are restored, and then lost again (question - if I were to add a button linked to a tag that doesn't exist in the PLC, and click that button, would the Com Error code execute? What if one object on the display momentarily loses it's value, and then comes back? Or is it only if I lose the entire link?)
- Connection Closed: will execute once when AHMI closes the connection to the PLC (question: what causes this? Does a com error trigger the connection to close? Or is it only on application shutdown?)
- Connection Established: will execute once when a connection to the configured PLC is established. Will not execute again until the connection is lost (or closed???) and restored (question: does "connection established" mean data transfer has started, or just that the PLC has been contacted and is ready to exchange data?)
- Data Received: will execute every time any data is received from the PLC (or is it just once, after the connection is established, when the data exchange actually starts?)
- Disposed: ???
- Subscription Data Received: ???

Phrog30

  • Guest
Re: Detect PLC connection lost in VB
« Reply #4 on: April 21, 2017, 11:34:14 AM »
Here's what I have, you can use this to display a message, or just use the status to prevent other code from executing.  It seems to work for what I have, so hopefully you can adapt to your project.

Code: [Select]
#Region "PLC Comms"
    Public CommsOK As Boolean
    Private Sub PLC_1_ConnectionEstablished(sender As Object, e As EventArgs) Handles PLC_1.ConnectionEstablished

        CommsOK = True
        Comms()

    End Sub

    Private Sub PLC_1_ConnectionClosed(sender As Object, e As EventArgs) Handles PLC_1.ConnectionClosed

        CommsOK = False
        Comms()

    End Sub

    Private Sub PLC_1_ComError(sender As Object, e As EventArgs) Handles PLC_1.ComError

        CommsOK = False

    End Sub

    Private Sub PLC_1_DataReceived(sender As Object, e As EventArgs) Handles PLC_1.DataReceived

        CommsOK = True

    End Sub

    Private Sub Comms()

        'Do something, or nothing...

    End Sub

#End Region

DougLyons

  • Jr. Member
  • **
  • Posts: 68
    • View Profile
Detect PLC connection lost in VB
« Reply #5 on: April 22, 2017, 12:06:43 AM »