VERSION 5.00
Object = "{5E9E78A0-531B-11CF-91F6-C2863C385E30}#1.0#0"; "MSFLXGRD.OCX"
Begin VB.Form frmTask 
   BorderStyle     =   1  '固定(実線)
   Caption         =   "Task"
   ClientHeight    =   4605
   ClientLeft      =   45
   ClientTop       =   330
   ClientWidth     =   7290
   LockControls    =   -1  'True
   MaxButton       =   0   'False
   MinButton       =   0   'False
   ScaleHeight     =   4605
   ScaleWidth      =   7290
   StartUpPosition =   3  'Windows の既定値
   Begin VB.Timer Timer1 
      Enabled         =   0   'False
      Interval        =   150
      Left            =   1860
      Top             =   4140
   End
   Begin VB.CommandButton cmdRefresh 
      Caption         =   "Refresh"
      Height          =   375
      Left            =   120
      TabIndex        =   10
      Top             =   4170
      Width           =   1215
   End
   Begin VB.Frame Frame2 
      Caption         =   "Operation"
      Height          =   3015
      Left            =   90
      TabIndex        =   6
      Top             =   1080
      Width           =   7095
      Begin VB.ComboBox cmbRun 
         Height          =   300
         Left            =   180
         Style           =   2  'ﾄﾞﾛｯﾌﾟﾀﾞｳﾝ ﾘｽﾄ
         TabIndex        =   12
         Top             =   2610
         Width           =   2325
      End
      Begin VB.ComboBox cmbStop 
         Height          =   300
         Left            =   3900
         Style           =   2  'ﾄﾞﾛｯﾌﾟﾀﾞｳﾝ ﾘｽﾄ
         TabIndex        =   11
         Top             =   2610
         Width           =   2325
      End
      Begin MSFlexGridLib.MSFlexGrid flxTask 
         Height          =   2205
         Left            =   120
         TabIndex        =   9
         Top             =   240
         Width           =   6855
         _ExtentX        =   12091
         _ExtentY        =   3889
         _Version        =   393216
         Cols            =   5
         SelectionMode   =   1
         AllowUserResizing=   1
      End
      Begin VB.CommandButton cmdStop 
         Caption         =   "&Stop"
         Height          =   375
         Left            =   6240
         TabIndex        =   8
         Top             =   2550
         Width           =   705
      End
      Begin VB.CommandButton cmdRun 
         Caption         =   "&Run"
         Height          =   375
         Left            =   2550
         TabIndex        =   7
         Top             =   2550
         Width           =   705
      End
   End
   Begin VB.CommandButton cmdExit 
      Caption         =   "Exit"
      Height          =   405
      Left            =   5970
      TabIndex        =   4
      Top             =   4170
      Width           =   1215
   End
   Begin VB.Frame Frame1 
      Caption         =   "Connection"
      Height          =   855
      Left            =   90
      TabIndex        =   0
      Top             =   120
      Width           =   7095
      Begin VB.CommandButton cmdConnect 
         Caption         =   "&Connect"
         Height          =   375
         Left            =   4440
         TabIndex        =   2
         Top             =   300
         Width           =   1215
      End
      Begin VB.CommandButton cmdDisconnect 
         Caption         =   "&Disconnect"
         Height          =   375
         Left            =   5760
         TabIndex        =   3
         Top             =   300
         Width           =   1215
      End
      Begin VB.ComboBox cmbParameter 
         Height          =   300
         Left            =   1320
         TabIndex        =   1
         Top             =   360
         Width           =   3015
      End
      Begin VB.Label Label2 
         Alignment       =   1  '右揃え
         Caption         =   "Parameter : "
         Height          =   255
         Left            =   240
         TabIndex        =   5
         Top             =   360
         Width           =   975
      End
   End
End
Attribute VB_Name = "frmTask"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit

Private caoEng As New CaoEngine
Private caoCtrls As CaoControllers
Private caoCtrl  As CaoController
Attribute caoCtrl.VB_VarHelpID = -1

' ----------------------------------------------------
Private Sub Form_Load()
    
    With cmbParameter
        .Clear
        .AddItem "eth:192.168.0.1:4112" ' <- Change this for your controller IP setting. "eth:<controller IP>:4112
        .AddItem "com:1:38400:N:8:1" ' <- Change this for your controller RS-232C setting. "com:<COM port no.>:<speed>:N:8:1"
        .ListIndex = 0
    End With
    
    flxTask.FormatString = "<Program name |Status |LineNo |CycleTime |Priority "
    
    With cmbRun
        .Clear
        .AddItem "1 - One cycle execution": .ItemData(.NewIndex) = 1
        .AddItem "2 - Cyclic execution": .ItemData(.NewIndex) = 2
        .AddItem "3 - One step forward": .ItemData(.NewIndex) = 3
        .AddItem "5 - Resume all": .ItemData(.NewIndex) = 5
        .ListIndex = 0
    End With
    
    With cmbStop
        .Clear
        .AddItem "0 - Default stop": .ItemData(.NewIndex) = 0
        .AddItem "1 - Instant stop": .ItemData(.NewIndex) = 1
        .AddItem "2 - Step stop": .ItemData(.NewIndex) = 2
        .AddItem "3 - Cycle stop": .ItemData(.NewIndex) = 3
        .AddItem "4 - Initialized stop": .ItemData(.NewIndex) = 4
        .AddItem "5 - Suspend all": .ItemData(.NewIndex) = 5
        .ListIndex = 0
    End With

    Timer1.Enabled = False
    cmdRefresh.Enabled = False
    cmdRun.Enabled = False
    cmdStop.Enabled = False
    
    Set caoEng = New CaoEngine
    Set caoCtrls = caoEng.Workspaces(0).Controllers

End Sub

' ----------------------------------------------------
Private Sub Form_Unload(Cancel As Integer)

    cmdDisconnect_Click
    
    Set caoCtrls = Nothing
    Set caoEng = Nothing
    
End Sub

' ----------------------------------------------------
' Refresh the task list
Private Sub cmdRefresh_Click()
    
    Dim tsk As CaoTask
    Dim tsks As CaoTasks
    ' Clear all task in caoCtrl.Tasks
    Set tsks = caoCtrl.Tasks
    For Each tsk In tsks
        tsks.Remove tsk.Index
    Next
    
    ' Append all task into caoCtrl.Tasks
    Dim l As Long
    Dim names As Variant
    names = caoCtrl.TaskNames
    For l = LBound(names) To UBound(names)
        Set tsk = caoCtrl.AddTask(names(l))
        With tsk
            .AddVariable ("@STATUS")
            .AddVariable ("@LINE_NO")
            .AddVariable ("@CYCLE_TIME")
            .AddVariable ("@PRIORITY")
        End With
    Next l
    
    flxTask.Rows = caoCtrl.Tasks.Count + 1
    
    Timer1_Timer
    
End Sub

' ----------------------------------------------------
Private Sub cmdRun_Click()
On Error GoTo ErrProc
    Dim l As Long
    Dim tsk As CaoTask
    
    l = flxTask.RowData(flxTask.row) ' Get selected task index
    Set tsk = caoCtrl.Tasks.Item(l)
    
    l = cmbRun.ItemData(cmbRun.ListIndex)
    tsk.Start l

    Exit Sub
ErrProc:
    MsgBox Err.Description
End Sub

' ----------------------------------------------------
Private Sub cmdStop_Click()
On Error GoTo ErrProc
    Dim l As Long
    Dim tsk As CaoTask
    
    l = flxTask.RowData(flxTask.row) ' Get selected task index
    Set tsk = caoCtrl.Tasks.Item(l)
    
    l = cmbStop.ItemData(cmbStop.ListIndex)
    tsk.Stop l

    Exit Sub
ErrProc:
    MsgBox Err.Description
End Sub

' ----------------------------------------------------
Private Sub cmdConnect_Click()
On Error GoTo ErrProc
    
    Dim sConn As String
    
    cmdDisconnect_Click
    
    sConn = "Conn=" & cmbParameter.Text
    
    Set caoCtrl = caoCtrls.Add("", "CaoProv.DENSO.NetwoRC", "", sConn)
    
    cmdRefresh_Click

    cmdRefresh.Enabled = True
    cmdRun.Enabled = True
    cmdStop.Enabled = True
    Timer1.Enabled = True

    Exit Sub
ErrProc:
    MsgBox Err.Description
End Sub

' ----------------------------------------------------
Private Sub cmdDisconnect_Click()
    
    If Not caoCtrl Is Nothing Then
        caoCtrls.Remove caoCtrl.Index
        Set caoCtrl = Nothing
    End If
    
    flxTask.Rows = 1

    Timer1.Enabled = False
    cmdRefresh.Enabled = False
    cmdRun.Enabled = False
    cmdStop.Enabled = False
    
End Sub

' ----------------------------------------------------
Private Sub cmdExit_Click()
    Unload Me
End Sub

' ----------------------------------------------------
'Monitor the task status
Private Sub Timer1_Timer()
    Dim row As Long
    Dim tsk As CaoTask
    
    row = 1 '0 = Fixed row
    For Each tsk In caoCtrl.Tasks
        With tsk
            flxTask.RowData(row) = .Index ' Store the task index into RowData
            flxTask.TextMatrix(row, 0) = .Name
            flxTask.TextMatrix(row, 1) = .Variables("@STATUS").Value
            flxTask.TextMatrix(row, 2) = .Variables("@LINE_NO").Value
            flxTask.TextMatrix(row, 3) = .Variables("@CYCLE_TIME").Value
            flxTask.TextMatrix(row, 4) = .Variables("@PRIORITY").Value
            row = row + 1
        End With
    Next

End Sub
