Link states and progress bar
When we click Connect, the KINGSTAR Subsystem starts to link to hardware. To display the link state, we use a Label and Progress Bar.
Label: link states
We use the event t_Staus_Tick to check the state of an EtherCAT link. t_Staus is a Timer variable that is enabled when the Windows Forms is initialized. Tick is the event of the Timer class. t_Staus.Interval is set to 5 (defined in Form1.Designer.vb), which means t_Staus_Tick is run every 5 milliseconds.
An EtherCAT link has four states: Init, Pre-Operational (Pre-Op), Safe-Operational (Safe-Op), and Operational (Op). When the state is Op, the connection has been created. To display these states during linking, we use GetStatus to get the state of the EtherCAT link. KSMStatus
is an instance of the SubsystemStatus class, which has a State field to receive the state of an EtherCAT link. We use If statements to check the state. The Label lbStatus
reflects the corresponding state through the Text property. If the state is ecatOP
(Op), the text of the Connect button is changed to Disconnect.
Private Sub t_Staus_Tick(sender As Object, e As EventArgs) Handles t_Staus.Tick
Dim Mindex As Integer = lbMList.SelectedIndex
Code = KS_API.GetStatus(KSMStatus, Nothing)
'Gets the state of the EtherCAT link.
If KSMStatus.State = EthercatState.ecatOffline Then
lbStatus.Text = "Offline"
End If
If KSMStatus.State = EthercatState.ecatInit Then
lbStatus.Text = "Init"
End If
If KSMStatus.State = EthercatState.ecatPreOP Then
lbStatus.Text = "PreOP"
End If
If KSMStatus.State = EthercatState.ecatSafeOP Then
lbStatus.Text = "SafeOP"
End If
If KSMStatus.State = EthercatState.ecatOP Then
lbStatus.Text = "OP"
btnConnect.Text = "Disconnect"
.........
End Sub
Progress Bar: the linking progress
A progress bar shows the status of a process that requires a long time to finish. You need to initialize some properties of the progress bar before using it. In this sample, we use the variable progressBar1 to represent the bar.
We set a timer in Connect to use in the progress bar. The variable t_ProcessBar represents the timer. When the button is clicked, the timer is called. The t_ProcessBar_Tick event is executed every 100 milliseconds. The progress block is repeatedly moving across the progress bar while the EtherCAT link between the KINGSTAR Subsystem and hardware is being created. The moving speed is 30 milliseconds, as set by the MarqueeAnimationSpeed property.
Private Sub btnConnect_Click(sender As Object, e As EventArgs) Handles btnConnect.Click
...........
progressBar1.MarqueeAnimationSpeed = 30
t_ProcessBar.Interval = 100
t_ProcessBar.Start()
...........
End Sub
We use KSMStatus to detect whether the state is Op. If it is, the link is successfully created. The progress block's moving speed is set to zero, and the timer stops.
Private Sub t_ProcessBar_Tick(sender As Object, e As EventArgs) Handles t_ProcessBar.Tick
If KSMStatus.State = EthercatState.ecatOP Then
ProgressBar1.MarqueeAnimationSpeed = 0
t_ProcessBar.Stop()
End If
End Sub