How-to
Control PLC connect and disconnect from a button
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
- What the two components expose
- Connect: bind the address before enabling subscriptions
- Disconnect: stop polling, then let the driver time out
- Replace the address object, never edit its member
- The OPC DA component pauses, it never disconnects
- Surface a missing tag through ComError
- Test the cycle before you release it
- 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 #
- Leave
EthernetIPforCLXCom1.DisableSubscriptionsatTruefrom form load, and leavePLCAddressValueempty 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. - In the Connect branch, validate the tag box and refuse an empty string before you touch the driver.
- Read the endpoint from the form: IP address, port and poll override.
- Assign the tag as a new address object.
- Enable subscriptions last, after the address exists.
- 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 #
- Set
EthernetIPforCLXCom1.DisableSubscriptions = Truefirst, before unlocking any inputs. - Unlock the endpoint and tag boxes and flip the caption.
- Confirm nothing else communicates through the same driver instance: a chart, a second subscribed control or a DataSubscriber reading through
EthernetIPforCLXCom1keeps 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:
- Confirm the branch executed: the caption must have flipped, since the handler selects its branch by comparing
Button7.Text. - Confirm the updating control is bound to this
OpcDaCom1instance and not to a second driver component dropped on the form. - Check the designer: a control with
PLCAddressValuepopulated there subscribes independently of your textbox, which is exactly how the documented test ended up with one display updating and one not. - Look for timers or code reads outside the subscription path that refresh the control.
- 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 #
- Point the OPC DA development rig at a simulation item that ramps, click Connect, and confirm the display tracks it.
- Click Disconnect and confirm the display freezes and
OpcDaCom1.DisableSubscriptionsreadsTruein the debugger. - On the controller rig, click Disconnect and keep every other read off
EthernetIPforCLXCom1for the idle window; the transport drop follows only when traffic is actually zero. - Enter a deliberately nonexistent tag, click Connect, and confirm the
ComErrormessage box appears and subscriptions went disabled. - 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.