Reputation: 4700
I experienced some strange thing in someone else's application that I am making myself familiar with: A button that on a click starts or stops a plc (TwinCat from Beckhoff) like this:
(Just a remark: the hbool1
variable is just an integer handle so that the plc knows which internal variable I want to change: here: switch on/off)
Private Sub Button1_MouseDown(sender As Object, e As MouseEventArgs) Handles Button1.MouseDown
'Switch off
adsClient.WriteAny(hbool1, False)
End Sub
Private Sub Button1_MouseUp(sender As Object, e As MouseEventArgs) Handles Button1.MouseUp
'Switch on
adsClient.WriteAny(hbool1, True)
End Sub
I wasn't familiar with MouseUp/MouseDowns on a button and expected just a Click. So out of curiosity I tried it with the Click Event:
Dim state_isOn As Bool
Private Sub Button1_Click(sender As Object, e As MouseEventArgs) Handles Button1.Click
If state_isOn Then
'Switch off
adsClient.WriteAny(hbool1, False)
Else
'Switch on
adsClient.WriteAny(hbool1, True)
End If
End Sub
The variable state_isOn
is set to true or false when the plc responds with an answer.
I do not understand 2 things:
Why does the 1st code snippet work? On the first click it switches on, on the next click it switches off. As far as I see, both events are called in a row: first mouseDown, afterwards mouseUp. So the Plc should be switched on always, but I can switch it off as well.
Why does my own idea not work? Isn't it more logical?!
What I did not try yet is to check the MouseClick Event. Then perhaps my own idea would work, but still there is the question, why the 1st code does work at all.
EDIT:
I figured out another thing: Consider the first snippet, how it was done initially (working): I put 2 message boxes in the code and noticed a different behaviour: With the message boxes, it does not work any more. When I click the button, I only get to the point showing "Switch off", and no following "Switch on". While this might be related to the interrupting nature of a message box, still it is interesting.
Private Sub Button1_MouseDown(sender As Object, e As MouseEventArgs) Handles Button1.MouseDown
'Switch off
adsClient.WriteAny(hbool1, False)
MsgBox("Switch off")
End Sub
Private Sub Button1_MouseUp(sender As Object, e As MouseEventArgs) Handles Button1.MouseUp
'Switch on
adsClient.WriteAny(hbool1, True)
MsgBox("Switch on")
End Sub
Another note: With the MouseClick
-event it does not work, either.
I have the feeling there is something wrong in the code of the plc and the former author figured out this mousebuttonup/down hack somehow to make it work, instead of fixing the plc. Is this possible?
Upvotes: 0
Views: 1119
Reputation: 2430
To really start or stop the plc with the TcAdsClient you should use the WriteControl Methods available like:
tcClient.WriteControl(newStateInfo(AdsState.Run,tcClient.ReadState().DeviceState));
tcClient.WriteControl(newStateInfo(AdsState.Stop,tcClient.ReadState().DeviceState));
You are probably not really stopping the PLC. Instead you are just writing to a BOOL variable on your plc, which happens to change the control flow of your PLC program. It is often normal programming style on a PLC that you will trigger an action whith a rising or falling edge on a BOOL variable. To create this edge it makes perfect sense to use the MouseUp/MouseDown event. But there is the possibility that when you click too fast that the rising edge is not recognized. This can happen if the variable gets updated to FALSE and then TRUE in the same PLC cycle. You should probably use just the ClickEvent to set to FALSE and trigger a timer to set the variable to TRUE again so that you ensure that you are not in the same PLC cycle.
Upvotes: 0