IMDHMIControls LibraryShop main store

How-to

Control PLC connect and disconnect from a button

SCADA and data logging Published 6 min read

Short answer

Run the whole connect and disconnect cycle from one button with DisableSubscriptions: True while the operator edits settings, False only after the address is bound as a new Drivers.PLCAddressItem. EthernetIPforCLXCom drops its connection about 5 s after requests stop; OpcDaCom exposes no disconnect method, only paused subscriptions.

On this page
  1. What the two components expose
  2. Connect: bind the address before enabling subscriptions
  3. Disconnect: stop polling, then let the driver time out
  4. Replace the address object, never edit its member
  5. The OPC DA component pauses, it never disconnects
  6. Surface a missing tag through ComError
  7. Test the cycle before you release it
  8. Questions asked

One button can own the whole cycle once you separate what these components treat as three independent things: whether subscriptions run, which tag a control is bound to, and whether the transport is connected. Keep DisableSubscriptions at True while the operator edits the endpoint and the tag, bind the address as a new Drivers.PLCAddressItem, then set DisableSubscriptions to False — that is the Connect command. Set it back to True for Disconnect: EthernetIPforCLXCom stops polling immediately and drops the connection about 5 s after the last request, while OpcDaCom exposes no disconnect method at all, only paused subscriptions.

What the two components expose #

Component Subscription control Connection behavior
EthernetIPforCLXCom DisableSubscriptions = True stops subscription polling Disconnects on its own about 5 s after communications stop
OpcDaCom DisableSubscriptions = True pauses subscription updates No disconnect method is exposed

DisableSubscriptions is a subscription switch, not an offline-mode switch: it stops subscription polling, while the component keeps handling connection attempts and tag errors underneath. The connect and disconnect behavior you want therefore lives in your button convention, not in the component. These are the AdvancedHMI driver components — the EtherNet/IP side is the EthernetIPforCLXCom.vb class that links an EtherNet/IP driver to the visual controls, used here against Allen-Bradley controllers — and the OPC DA side predates explicit session control. Contrast starting an OPC UA client connection from PLC code, where the session is an object the code starts and stops on purpose.

Connect: bind the address before enabling subscriptions #

  1. Leave EthernetIPforCLXCom1.DisableSubscriptions at True from form load, and leave PLCAddressValue empty in the designer for any control you intend to bind at runtime. A designer-populated address subscribes on its own as soon as subscriptions are enabled, which is why one display in the documented test updated on Connect while its textbox stayed empty.
  2. In the Connect branch, validate the tag box and refuse an empty string before you touch the driver.
  3. Read the endpoint from the form: IP address, port and poll override.
  4. Assign the tag as a new address object.
  5. Enable subscriptions last, after the address exists.
  6. Lock the input controls and flip the caption so the next click runs the other branch.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    If Button1.Text = "Connect" Then
        Dim tagAddress As String = TextBox2.Text.Trim()
        If tagAddress = "" Then
            MsgBox("Enter a tag address before connecting.")
            Exit Sub
        End If

        EthernetIPforCLXCom1.IPAddress = TextBox1.Text
        EthernetIPforCLXCom1.Port = NumericUpDown1.Value
        EthernetIPforCLXCom1.PollRateOverride = NumericUpDown2.Value
        SevenSegment1.PLCAddressValue = New Drivers.PLCAddressItem(tagAddress)
        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

tagAddress is a local variable of this handler; everything else is a component, control or property name from the demonstrated form. The order matters: the address object is in place before subscriptions switch on, so the first poll after Connect already carries the operator's tag.

Disconnect: stop polling, then let the driver time out #

  1. Set EthernetIPforCLXCom1.DisableSubscriptions = True first, before unlocking any inputs.
  2. Unlock the endpoint and tag boxes and flip the caption.
  3. Confirm nothing else communicates through the same driver instance: a chart, a second subscribed control or a DataSubscriber reading through EthernetIPforCLXCom1 keeps traffic alive, and the idle timeout never starts.

Worked example, one subscribed SevenSegment and PollRateOverride at 500 ms: the click stops further polls, the in-flight request finishes within 500 ms × 1 s / 1000 ms = 0.5 s, and the driver then counts about 5 s of silence before disconnecting. Worst case from click to transport disconnect is therefore 0.5 s + 5 s = 5.5 s. Raise the override to 1000 ms and the bound becomes 1 s + 5 s = 6 s; add one more read through the same instance and the bound disappears, because the silence the driver needs never begins.

A display frozen by DisableSubscriptions still shows the last value it read; treat every number on it as stale, and never use it to judge whether the machine is safe to work on.

Replace the address object, never edit its member #

' Fails: nothing to edit when the designer left PLCAddressValue empty,
' and a member edit is not recognized as an address change
SevenSegment1.PLCAddressValue.PLCAddress = TextBox3.Text

' Works: assign a whole new address object
SevenSegment1.PLCAddressValue = New Drivers.PLCAddressItem(TextBox3.Text)

PLCAddressValue holds a PLCAddressItem object. When the designer never populated it there is no object whose PLCAddress member you can edit, so the line throws; when an object does exist, the control does not treat a member edit as an address replacement, so the old tag keeps feeding the display. Assigning a new instance is the demonstrated fix.

Clearing is not the mirror image of assigning. Two attempts were documented against a live tag — setting PLCAddressValue = Nothing, and setting it to New Drivers.PLCAddressItem(Nothing) — and the display kept updating from the previous tag after both. That is why the Connect branch above refuses the empty string instead of assigning it: an empty box means stay disconnected, and the operator gets a message rather than a silent stale value.

The OPC DA component pauses, it never disconnects #

Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
    If Button7.Text = "Connect" Then
        Dim itemAddress As String = TextBox3.Text.Trim()
        If itemAddress = "" Then
            MsgBox("Enter an item address before connecting.")
            Exit Sub
        End If

        OpcDaCom1.OPCServer = TextBox1.Text
        OpcDaCom1.OPCServerPath = TextBox2.Text
        SevenSegment1.PLCAddressValue = New Drivers.PLCAddressItem(itemAddress)
        OpcDaCom1.DisableSubscriptions = False
        Button7.Text = "Disconnect"
    ElseIf Button7.Text = "Disconnect" Then
        OpcDaCom1.DisableSubscriptions = True
        Button7.Text = "Connect"
    End If
End Sub

OpcDaCom does not expose a disconnect method; pausing subscription updates with DisableSubscriptions = True is everything the Disconnect branch can do. Note what is absent from that branch: OPCServer and OPCServerPath appear only under Connect, because they configure which OPC DA server to talk to — assigning them again on Disconnect reconfigures an endpoint, it does not close one.

If values keep changing after the Disconnect branch runs, work this list in order:

  1. Confirm the branch executed: the caption must have flipped, since the handler selects its branch by comparing Button7.Text.
  2. Confirm the updating control is bound to this OpcDaCom1 instance and not to a second driver component dropped on the form.
  3. Check the designer: a control with PLCAddressValue populated there subscribes independently of your textbox, which is exactly how the documented test ended up with one display updating and one not.
  4. Look for timers or code reads outside the subscription path that refresh the control.
  5. Test against a simulation item that visibly ramps. A constant tag cannot distinguish a paused subscription from a working one receiving unchanged data.

One more behavior is worth a check on your build: a reported case had subscriptions disabled on first display resume after the form was hidden and shown again, so if your operator flow re-shows forms, read DisableSubscriptions back after each re-display. And if every client of that OPC DA server stalls together, look upstream of the button — fixing CODESYS OPC DA memory growth stopping SCADA reads covers one server-side failure that looks like a client problem.

Surface a missing tag through ComError #

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 driver raises ComError with the failure text in e.ErrorMessage, so a message box there tells the operator the tag does not exist instead of leaving a dead display. Setting DisableSubscriptions = True inside the handler stops the retry loop on the bad address; this exact handler is the demonstrated fix for the missing-tag case. Set the caption back to Connect in the handler as well, because subscriptions are now off and the next click should run the Connect branch.

Test the cycle before you release it #

  1. Point the OPC DA development rig at a simulation item that ramps, click Connect, and confirm the display tracks it.
  2. Click Disconnect and confirm the display freezes and OpcDaCom1.DisableSubscriptions reads True in the debugger.
  3. On the controller rig, click Disconnect and keep every other read off EthernetIPforCLXCom1 for the idle window; the transport drop follows only when traffic is actually zero.
  4. Enter a deliberately nonexistent tag, click Connect, and confirm the ComError message box appears and subscriptions went disabled.
  5. Clear the tag box, click Connect, and confirm you get the refusal message rather than a display still carrying the previous tag.

Questions asked

Does setting DisableSubscriptions to True close the connection to the PLC?

Not immediately, and for the CLX driver only. DisableSubscriptions stops subscription polling; EthernetIPforCLXCom then disconnects about 5 s after the last communication, so with a 500 ms poll the socket can stay open up to roughly 5.5 s after the click. Any other read through the same driver instance, such as a chart or a second subscribed control, keeps traffic flowing and the connection never drops. OpcDaCom exposes no disconnect method at all, only the pause.

Why does editing SevenSegment1.PLCAddressValue.PLCAddress throw or do nothing?

PLCAddressValue holds a PLCAddressItem object, and the control only recognizes a whole new instance as an address change. Assign New Drivers.PLCAddressItem with the tag string instead of editing a member of the existing object. The same rule applies to the DataSubscriber2 address collection PLCAddresValueItems, which also takes new PLCAddressItem instances.

How do I tell the operator that a tag does not exist?

Handle the driver's ComError event. The event arguments are of type Drivers.Common.PlcComEventArgs, and e.ErrorMessage carries the driver's error text, so a MsgBox on that field reports the missing tag. Set DisableSubscriptions back to True inside the handler so the driver stops hammering a bad address, and flip the caption so the next click runs the Connect branch. That handler is the demonstrated fix for this case.

Why does my display keep updating after I clear the tag textbox?

Two documented causes. A control whose PLCAddressValue was populated in the designer subscribes on its own as soon as subscriptions are enabled, ignoring your textbox. And clearing did not work either: assigning PLCAddressValue = Nothing and New Drivers.PLCAddressItem(Nothing) both left the previous tag updating. Decide it in code instead: leave designer addresses empty on runtime-bound controls, and refuse an empty string in the Connect branch before any assignment.

Should the OPC DA disconnect branch reassign OPCServer and OPCServerPath?

No. OPCServer and OPCServerPath tell the component which OPC DA server to use; assigning them configures an endpoint, it does not tear one down. Keep both assignments in the Connect branch so one click changes one behavior. The Disconnect branch should do exactly two things: set OpcDaCom1.DisableSubscriptions = True and flip the caption, because pausing subscriptions is the only off switch this component exposes.

Keep reading

  • How-to

    Getting CompactLogix PLC data into Excel over EtherNet/IP

    Excel has no EtherNet/IP client, so a CompactLogix reaches a workbook only through a driver layer: an RSLinx edition that documents OPC or DDE support, or a driver library used within its license — RSLinx Classic Lite does neither. Put the PC and controller on one subnet, prove a single controller-scoped tag read, map that one cell, and add writes only against a dedicated test tag the logic validates.

    SCADA and data logging

  • How-to

    How to learn PLC, SCADA and DAQ from scratch

    Learn the three as one project, not three courses: pick one route, wire one real input, program a three-state sequence, exchange one Modbus register, bind SCADA tags and store a timestamped value. Pass the five checkpoint tests on the way; simulation alone never exercises terminals, polarity or reconnection.

    SCADA and data logging

  • Selection guide

    Selecting SCADA software for mixed PLCs and SQL logging

    Shortlist the candidates, then pass or fail each one on nine commissioned paths: a live read from every PLC family in the mix, one parsed serial ASCII transaction, a browser client session on the target touch-panel PC, and one logged record in SQL Server or MySQL verified across a database outage. Accept a package only when every path passes all its checks; a failed path keeps a candidate alive only with a named gateway or OPC route.

    SCADA and data logging