VERSION 5.00
Begin VB.Form Form1 
   Caption         =   "Robot"
   ClientHeight    =   4305
   ClientLeft      =   60
   ClientTop       =   450
   ClientWidth     =   4050
   LinkTopic       =   "Form1"
   ScaleHeight     =   4305
   ScaleWidth      =   4050
   StartUpPosition =   3  'Windows の既定値
   Begin VB.Frame Frm2 
      BackColor       =   &H00FFCC99&
      BorderStyle     =   0  'なし
      Height          =   1575
      Left            =   360
      TabIndex        =   4
      Top             =   2520
      Width           =   3255
      Begin VB.CommandButton cmdTaskStop 
         Caption         =   "TaskStop"
         Enabled         =   0   'False
         Height          =   495
         Left            =   480
         TabIndex        =   6
         Top             =   840
         Width           =   2295
      End
      Begin VB.CommandButton cmdTaskStart 
         Caption         =   "TaskStart"
         Enabled         =   0   'False
         Height          =   495
         Left            =   480
         TabIndex        =   5
         Top             =   240
         Width           =   2295
      End
   End
   Begin VB.Frame Frm1 
      BackColor       =   &H00CCFFCC&
      BorderStyle     =   0  'なし
      Height          =   2175
      Left            =   360
      TabIndex        =   0
      Top             =   120
      Width           =   3255
      Begin VB.CommandButton cmdDisconnectRobot 
         Caption         =   "Disconnect Robot"
         Enabled         =   0   'False
         Height          =   495
         Left            =   480
         TabIndex        =   3
         Top             =   1440
         Width           =   2295
      End
      Begin VB.CommandButton cmdConnectRobot 
         Caption         =   "Connect Robot"
         Height          =   495
         Left            =   480
         TabIndex        =   2
         Top             =   840
         Width           =   2295
      End
      Begin VB.CommandButton cmdInit 
         Caption         =   "Init"
         Height          =   495
         Left            =   480
         TabIndex        =   1
         Top             =   240
         Width           =   2295
      End
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Dim caoEng As CaoEngine
Dim caoWS As CaoWorkspace
' Robot objects
Dim caoCtrl As CaoController ' Robot Controller
Dim caoTask As caoTask      ' for Task
Dim caoTaskST As CaoVariable    ' for Task Variable

' -----------------------------Initialization----------------------------
Private Sub cmdInit_Click()

    ' Disconnect Robot
    Call cmdDisconnectRobot_Click

    ' Create Engine
    Set caoEng = Nothing
    Set caoEng = New CaoEngine
    Set caoWS = caoEng.Workspaces(0)

End Sub

' ---------------------------------------------------------------
' Connect with the Robot
' ---------------------------------------------------------------
Private Sub cmdConnectRobot_Click()

    On Error GoTo ErrorProc
    
    ' Create object and connect with RC8 controller
    Set caoCtrl = caoWS.AddController("Robot", "caoProv.DENSO.RC8", "", "Server=192.168.0.1")
    ' Task control with RC8 controller
    Set caoTask = caoCtrl.AddTask("Pro1")
    ' Task class system variable
    Set caoTaskST = caoTask.AddVariable("@STATUS")
       
    ' Enable command buttons
    cmdConnectRobot.Enabled = False
    cmdDisconnectRobot.Enabled = True
    cmdTaskStart.Enabled = True
    cmdTaskStop.Enabled = True
    
    Exit Sub
    
ErrorProc:
    cmdDisconnectRobot_Click
    MsgBox Err.Description & " : " & Hex(Err.Number)

End Sub

' ---------------------------------------------------------------
' disconnect from the Robot
' ---------------------------------------------------------------
Private Sub cmdDisconnectRobot_Click()

    On Error Resume Next
    
    ' Disconnect from the Task Variable
    If Not (caoTaskST Is Nothing) Then
        If Not (caoTask Is Nothing) Then
            caoTask.Variables.Remove caoTaskST.Name
            Set caoTaskST = Nothing
        End If
    End If
    
    ' Disconnect from the Task
    If Not (caoTask Is Nothing) Then
        If Not (caoCtrl Is Nothing) Then
            caoCtrl.Tasks.Remove caoTask.Name
            Set caoTask = Nothing
        End If
    End If
    
    ' Disconnect from the controller
    If Not (caoCtrl Is Nothing) Then
        If Not (caoEng Is Nothing) Then
            caoEng.Workspaces(0).Controllers.Remove caoCtrl.Name
            Set caoCtrl = Nothing
        End If
    End If
    
    ' Disable command buttons
    cmdConnectRobot.Enabled = True
    cmdDisconnectRobot.Enabled = False
    cmdTaskStart.Enabled = False
    cmdTaskStop.Enabled = False

End Sub

Private Sub cmdTaskStart_Click()

    On Error GoTo ErrorProc
    
    'caoTaskST [0:Task not yet generated (NON_EXISTENT), 1:Hold-stopped, 2:Stopped, 3:Running, 4:Step-stopped]
    If caoTaskST.Value <> 3 Then
        'Start [1:One cycle execution, 2:Continuous execution, 3:Step forward]
        caoTask.Start 2
    End If
    
    Exit Sub
    
ErrorProc:
    MsgBox Err.Description & " : " & Hex(Err.Number)

End Sub

Private Sub cmdTaskStop_Click()

    On Error GoTo ErrorProc
         
    'caoTaskST [0:Task not yet generated (NON_EXISTENT), 1:Hold-stopped, 2:Stopped, 3:Running, 4:Step-stopped]
    If caoTaskST.Value = 3 Then
        'Stop [0:Default stop, 1:Instant stop, 2:Step stop, 3:Cycle stop, 4:Initialized stop]
        caoTask.Stop 4
    End If
    
    Exit Sub
    
ErrorProc:
    MsgBox Err.Description & " : " & Hex(Err.Number)

End Sub

Private Sub Form_Unload(Cancel As Integer)
    
    ' Delete CaoEngine
    If Not (caoTaskST Is Nothing) Then
        Set caoTaskST = Nothing
    End If
    If Not (caoTask Is Nothing) Then
        Set caoTask = Nothing
    End If
    If Not (caoCtrl Is Nothing) Then
        Set caoCtrl = Nothing
    End If
    If Not (caoWS Is Nothing) Then
        Set caoWS = Nothing
    End If
    If Not (caoEng Is Nothing) Then
        Set caoEng = Nothing
    End If
        
End Sub

