Author Topic: OPCDaCom and EthernetIPForCLXCom Connect / Disconnect Using Buttons  (Read 1200 times)

Conor_Hyland

  • Newbie
  • *
  • Posts: 12
    • View Profile
Hi Folks,

First of all, thanks for a great HMI application.
I'm new to VB.net development and am learning programming as a hobby.

I'm developing an application that will allow the user to connect to a PLC, enter PLC tags / topics in textboxes and plot the variables in a chart, then it will save the chart as a .gif image at a set time interval.
For the final version I'll be using EthernetIPForCLXCom driver to connect to the PLC.
PLCs are Allen Bradley.

I'm currently developing with OpcDaCom driver as I'm testing with KepServerEX6 with simulation tags.
I can get it to work perfectly when the form loads / unloads, but:

1) How can I connect when the user clicks a connect button, instead of when the form loads?
2) How can i disconnect when the user clicks a disconnect button, instead of when the form closes?
3) How can i achieve this with an example for each of the driver components above?

Idea is that the user can disconnect using button, enter new tag address / topic in textbox, click connect, then it starts plotting again, without closing / opening the form.
The plotting is the easy bit, the hard part for me is wondering what code to update in the driver code.

Note:
OpcDaCom driver is for development only, reading KepServerEx 6 simulation tags.
EthernetIPForCLXCom driver will be used to connect to Allen Bradley PLC when project is complete.

Any advice would be greatly appreciated, as i am struggling to figure out the connect / disconnect using buttons part.

Thanks in advance,
Regards,
Conor

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
There is a property of the driver named DisableSubscriptions. This will stop the updates. If using the CLX driver, after about 5 seconds of no communications, it will disconnect.

Add a button to the form.
Double Click the button to get back to the code
Enter this:

EthernetIPforCLXCom1.DisableSubscriptions=True

Conor_Hyland

  • Newbie
  • *
  • Posts: 12
    • View Profile
Hi Archie,

Thank you for your quick response!  :D

My code is below:

Code: [Select]
Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
        If Button7.Text = "Connect" Then
            OpcDaCom1.OPCServer = TextBox1.Text
            OpcDaCom1.OPCServerPath = TextBox2.Text
            'SevenSegment1.PLCAddressValue.PLCAddress = TextBox3.Text
            OpcDaCom1.DisableSubscriptions = False
            Button7.Text = "Disconnect"
        ElseIf Button7.Text = "Disconnect" Then
            OpcDaCom1.OPCServer = TextBox1.Text
            OpcDaCom1.OPCServerPath = TextBox2.Text
            OpcDaCom1.DisableSubscriptions = True
            Button7.Text = "Connect"
        End If
    End Sub

SevenSegment1 PLCAddressValue is not populated in the properties window.
SevenSegment2 PLCAddressValue is populated in the properties window.

First Issue:
When I click connect, SevenSegment2 will start updating, but when i click disconnect, it does not stop updating.

Second Issue:
If i un-comment the line 'SevenSegment1.PLCAddressValue.PLCAddress = TextBox3.Text, it gives a system null reference exception of object reference not set to an instance of an object.

Any help is much appreciated!

Thanks,
Conor

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
The PLCAddressValue property of the SevenSegment2 is a PLCAddressItem object, so just changing the PCLAddress property will not make the SevenSegment recognize it as an address change. You need to create a new object

        SevenSegment21.PLCAddressValue = New Drivers.PLCAddressItem("MyTag")

Conor_Hyland

  • Newbie
  • *
  • Posts: 12
    • View Profile
Hi Archie,

Again thank you for this quick support.
SevenSegment1.PLCAddressValue = New Drivers.PLCAddressItem("MyTag") worked perfect!

Any advice on why it is not disconnecting correctly when i set the OpcDaCom1.DisableSubscriptions = True?

Regards,
Conor

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
The OpcDaCom driver does not have a disconnect method exposed. You can only pause the subscription updates.

Conor_Hyland

  • Newbie
  • *
  • Posts: 12
    • View Profile
Hi Archie,

OK thanks for this info.
I'll have a go at connecting / disconnecting to the PLC via the EthernetIPForCLXCom driver tomorrow and see how i get on.
If there is a better method you recommend connecting to an Allen Bradley PLC, then let me know!

Regards,
Conor

Conor_Hyland

  • Newbie
  • *
  • Posts: 12
    • View Profile
Hi Archie,

I tried connecting to the Allen Bradley PLC with the CLX driver and it is working well, simplified code below:

Code: [Select]
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If Button1.Text = "Connect" Then
            EthernetIPforCLXCom1.IPAddress = TextBox1.Text
            EthernetIPforCLXCom1.Port = NumericUpDown1.Value
            EthernetIPforCLXCom1.PollRateOverride = NumericUpDown2.Value
            SevenSegment1.PLCAddressValue = New Drivers.PLCAddressItem(TextBox2.Text)
            EthernetIPforCLXCom1.DisableSubscriptions = False

            TextBox1.Enabled = False
            NumericUpDown1.Enabled = False
            NumericUpDown2.Enabled = False
            TextBox2.Enabled = False         
            Button1.Text = "Disconnect"
        ElseIf Button1.Text = "Disconnect" Then
            EthernetIPforCLXCom1.DisableSubscriptions = True

            TextBox1.Enabled = True
            NumericUpDown1.Enabled = True
            NumericUpDown2.Enabled = True
            TextBox2.Enabled = True
            Button1.Text = "Connect"
        End If
    End Sub

I have a few questions that i would kindly like you to help me with:

1) If i enter a tag address that doesn't exist and click connect, the sevensegment obviously doesn't update.
How can i report back to the user that the tag doesn't exist?

2) After connecting to an existing tag, then if i click disconnect and clear the tag address textbox, then click connect again, the seven segment keeps reporting back the previous tag address value.
How can i set the sevensegment to 0 for example and not have it keep updating from the previous tag value?

Thanks Archie,
Regards,
Conor

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
1) If i enter a tag address that doesn't exist and click connect, the sevensegment obviously doesn't update.
How can i report back to the user that the tag doesn't exist?

2) After connecting to an existing tag, then if i click disconnect and clear the tag address textbox, then click connect again, the seven segment keeps reporting back the previous tag address value.
How can i set the sevensegment to 0 for example and not have it keep updating from the previous tag value?
You will need an error handler to catch driver errors.

    Private Sub EthernetIPforCLXCom1_ComError_1(sender As Object, e As Drivers.Common.PlcComEventArgs) Handles EthernetIPforCLXCom1.ComError
        MsgBox(e.ErrorMessage)
    End Sub

If the tag is cleared, you will need to set PLCAddressValue to Nothing

If String.IsNullorEmpty( TextBox2.Text) then
  SevenSegment1.PLCAddressValue =Nothing
Else
.
.
End if


Conor_Hyland

  • Newbie
  • *
  • Posts: 12
    • View Profile
Hi Archie,

Thank you for your reply.

Code: [Select]
Private Sub EthernetIPforCLXCom1_ComError_1(sender As Object, e As Drivers.Common.PlcComEventArgs) Handles EthernetIPforCLXCom1.ComError
        EthernetIPforCLXCom1.DisableSubscriptions = True
        MsgBox(e.ErrorMessage)
    End Sub

The above worked great!

Neither of the following worked - the value of the SevenSegment1 keeps updating with TextBox2 cleared:

Code: [Select]
If String.IsNullOrEmpty(TextBox2.Text) Then
                SevenSegment1.PLCAddressValue = Nothing
            Else
                SevenSegment1.PLCAddressValue = New Drivers.PLCAddressItem(TextBox2.Text)
            End If

Nor:

Code: [Select]
If String.IsNullOrEmpty(TextBox2.Text) Then
                SevenSegment1.PLCAddressValue = New Drivers.PLCAddressItem(Nothing)
            Else
                SevenSegment1.PLCAddressValue = New Drivers.PLCAddressItem(TextBox2.Text)
            End If

The SevenSegment1 value keeps updating in both cases.
Any help appreciated as always.

Regards,
Conor
« Last Edit: May 14, 2019, 10:43:17 AM by Conor_Hyland »