The link status and progress bar

When we click Connect, the KINGSTAR Subsystem starts to link to the hardware. To display the link status, we use Text Controls and a Progress Bar Control.

Text Controls: the link status and error message

To monitor the link status, a timer is set in the OnInitDialog class for the Text Control to get the status of EtherCAT link regularly. The timer's ID is 60. It is triggered every 5 milliseconds.

SetTimer(60, 5, NULL);

When the timer is called, we check the status of EtherCAT link. The EtherCAT link has four states: Init, Pre-Operational (Pre-Op), Safe-Operational (Safe-Op), Operational (Op). When the state is Op, the connection has been created. m_MotorList.GetCurSel retrieves the currently selected axis in the List Box. To display these states during the link, we use GetStatus to get the state of the EtherCAT link, and then we use if statements to identify the state. The Text Control reflects the corresponding state through SetWindowText.

void CMFC_GUIDlg::OnTimer(UINT_PTR nIDEvent)
{
   ...........
   if (nIDEvent == 60)   //update timer ID
   {
      int Mindex = m_MotorList.GetCurSel();

      //Gets the state of the EtherCAT link.
      ::GetStatus(&KSMStatus, NULL);

      if (KSMStatus.State == ecatOffline)
      {
         GetDlgItem(IDC_NET_STATUS)->SetWindowText(L"Offline");
      }
      if (KSMStatus.State == ecatInit)
      {
         GetDlgItem(IDC_NET_STATUS)->SetWindowText(L"Init");
      }
      if (KSMStatus.State == ecatPreOP)
      {
         GetDlgItem(IDC_NET_STATUS)->SetWindowText(L"PreOP");
      }
      if (KSMStatus.State == ecatSafeOP)
      {
         GetDlgItem(IDC_NET_STATUS)->SetWindowText(L"SafeOP");
      }
      if (KSMStatus.State == ecatOP)
      {
         GetDlgItem(IDC_NET_STATUS)->SetWindowText(L"OP");
         GetDlgItem(IDC_BTN_CONNECT)->SetWindowText(L"Disconnect");
         ...........
      }
   }
}

If a function doesn't work as expected, an error message will be displayed above the progress bar. The error message varies between functions. The following code shows the error message of the SetCycleTime failure.

Str_Error.Format(_T("Failed to set SetCycleTime: %d\n"), Command.ErrorId);
GetDlgItem(IDC_ERROR_RETURN)->SetWindowText(Str_Error);

Progress Bar Control: the linking progress

A Progress Bar Control is a progress bar that shows a process that requires long time to finish. You need to initialize some properties of the progress bar before using it. In this sample, we use the variable m_progressbar to represent the bar. We set the state and range of the progress bar in OnInitDialog.

BOOL CMFC_GUIDlg::OnInitDialog()
{
   CDialogEx::OnInitDialog();
   m_progressbar.SetState(PBST_NORMAL);
   m_progressbar.SetRange(0, 100);
   ...........
}

We set a timer in Connect to use in the progress bar. The timer's ID is 55. It is triggered every 100 milliseconds. When the button is clicked, the timer is called. The bar is redrawn according to the position we passed. In this sample we pass the variable TimerCount, which keeps increasing by one. When it reaches 100, TimerCount is reset and starts increasing again. The bar's position keeps moving from zero to 100 repeatedly while the EtherCAT link between the KINGSTAR Subsystem and the hardware is being created.

SetTimer(55, 100, NULL);

We use KSMStatus to detect whether the state is Op. If it is, the link is successfully created. The progress bar's position is set to zero, and the timer is deleted.

void CMFC_GUIDlg::OnTimer(UINT_PTR nIDEvent)
{
   if (nIDEvent == 55)   //progress bar
   {
      m_progressbar.SetPos(TimerCount++);
      if (TimerCount >= 100)
      {
         TimerCount = 0;	
      }

      if (KSMStatus.State == ecatOP)
      {
         m_progressbar.SetPos(0);
         KillTimer(55);
      }
   }
   ...........
}