VERSION 5.00
Begin VB.Form frmVariable 
   BorderStyle     =   1  '固定(実線)
   Caption         =   "VarMaster"
   ClientHeight    =   3405
   ClientLeft      =   45
   ClientTop       =   330
   ClientWidth     =   7320
   MaxButton       =   0   'False
   MinButton       =   0   'False
   ScaleHeight     =   3405
   ScaleWidth      =   7320
   StartUpPosition =   3  'Windows の既定値
   Begin VB.Frame Frame2 
      Caption         =   "Operation"
      Height          =   1725
      Left            =   90
      TabIndex        =   6
      Top             =   1080
      Width           =   7095
      Begin VB.ComboBox cmbVarType 
         Height          =   300
         Left            =   120
         Style           =   2  'ﾄﾞﾛｯﾌﾟﾀﾞｳﾝ ﾘｽﾄ
         TabIndex        =   11
         Top             =   600
         Width           =   1335
      End
      Begin VB.TextBox txtVarIndex 
         Alignment       =   2  '中央揃え
         Height          =   285
         Left            =   1500
         TabIndex        =   10
         Text            =   "100"
         Top             =   600
         Width           =   915
      End
      Begin VB.TextBox txtVarValue 
         Height          =   345
         Left            =   120
         TabIndex        =   9
         Text            =   "0,1,2,3,4,5,"
         Top             =   1020
         Width           =   6855
      End
      Begin VB.CommandButton cmdPut 
         Caption         =   "&Put"
         Height          =   375
         Left            =   5730
         TabIndex        =   8
         Top             =   540
         Width           =   1215
      End
      Begin VB.CommandButton cmdGet 
         Caption         =   "&Get"
         Height          =   375
         Left            =   4410
         TabIndex        =   7
         Top             =   540
         Width           =   1215
      End
      Begin VB.Label Label3 
         Caption         =   "="
         Height          =   345
         Left            =   2550
         TabIndex        =   13
         Top             =   660
         Width           =   495
      End
      Begin VB.Label Label1 
         Caption         =   "Variable Type:    Index:"
         Height          =   225
         Left            =   180
         TabIndex        =   12
         Top             =   390
         Width           =   2415
      End
   End
   Begin VB.CommandButton cmdExit 
      Caption         =   "Exit"
      Height          =   405
      Left            =   5970
      TabIndex        =   4
      Top             =   2910
      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 = "frmVariable"
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 "192.168.0.1" ' <- Change this for your controller IP setting. "eth:<controller IP>:4112
        .ListIndex = 0
    End With
    
    With cmbVarType
        .Clear
        .AddItem "I"
        .AddItem "F"
        .AddItem "D"
        .AddItem "V"
        .AddItem "P"
        .AddItem "J"
        .AddItem "T"
        .AddItem "S"
        .AddItem "IO" 'I/O
        .AddItem "IOB"
        .AddItem "IOW"
        .AddItem "IOD"
        .AddItem "IOF"
        .ListIndex = 5 'J type
    End With

    txtVarIndex.Text = "10"
    
    cmdGet.Enabled = False
    cmdPut.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

' ----------------------------------------------------
Private Sub cmdGet_Click()

    Dim caoVar  As CaoVariable
    Set caoVar = caoCtrl.AddVariable(cmbVarType.List(cmbVarType.ListIndex) + txtVarIndex)
    
    Dim vVal As Variant
    vVal = caoVar.Value
    If (VarType(vVal) And vbArray) Then 'If the variable is array
        Dim v As Variant
        Dim sVal As String
        
        sVal = ""
        For Each v In vVal
            sVal = sVal + LStr(v) + ", "
        Next
        
        txtVarValue.Text = sVal
    Else
        txtVarValue.Text = LStr(vVal)
    End If
    
    caoCtrl.Variables.Remove (caoVar.Index)
    Set caoVar = Nothing

End Sub

' ----------------------------------------------------
Private Sub cmdPut_Click()

    Dim caoVar  As CaoVariable
    Set caoVar = caoCtrl.AddVariable(cmbVarType.List(cmbVarType.ListIndex) + txtVarIndex)

    Dim vVal As Variant
    Dim sVal As String
    
    sVal = txtVarValue.Text
    Select Case cmbVarType.List(cmbVarType.ListIndex)
    Case "I": vVal = LInt(sVal)
    Case "F": vVal = LSng(sVal)
    Case "D": vVal = LDbl(sVal)
    Case "V": vVal = StringToArray(sVal, 3)
    Case "P": vVal = StringToArray(sVal, 7)
    Case "J": vVal = StringToArray(sVal, 8)
    Case "T": vVal = StringToArray(sVal, 10)
    Case "S": vVal = sVal
    Case "IO": vVal = LBool(sVal)
    Case "TOOL": vVal = StringToArray(sVal, 6)
    Case "WORK": vVal = StringToArray(sVal, 7)
    Case "AREA": vVal = StringToArray(sVal, 34)
    Case Else 'CNF
        vVal = LLng(sVal)
    End Select
    caoVar.Value = vVal
    
    caoCtrl.Variables.Remove (caoVar.Index)
    Set caoVar = Nothing

End Sub

' Convert string to array
' ex. "0, 1, 2, 3," -> Array(0,1,2,3)
Private Function StringToArray(ByVal str As String, ByVal nElements As Integer) As Variant
    
    Dim i As Integer
    Dim v As Variant
    Dim vVal As Variant
    Dim fV() As Single
    
    ReDim fV(0 To nElements - 1)
    
    vVal = Split(str, ",")
    i = 0
    For Each v In vVal
        v = Trim(v)
        If v <> "" Then
            fV(i) = LSng(v)
            i = i + 1
        End If
    Next
    StringToArray = fV()
    
End Function

' ----------------------------------------------------
Private Sub cmdConnect_Click()
    
    On Error GoTo ErrProc
    
    Dim sConn As String
    
    cmdDisconnect_Click
    
    sConn = "Server=" & cmbParameter.Text
    
    Set caoCtrl = caoCtrls.Add("", "CaoProv.DENSO.RC8", "", sConn)
    
    cmdGet.Enabled = True
    cmdPut.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
    
    cmdGet.Enabled = False
    cmdPut.Enabled = False
    
End Sub

' ----------------------------------------------------
Private Sub cmdExit_Click()

    Unload Me

End Sub


'-----------------------------------------------------------
Public Function LLng(ByRef expression) As Long
    
    LLng = CLng(Val(expression))

End Function

'-----------------------------------------------------------------------------
Public Function LInt(ByRef expression) As Integer
    
    LInt = CInt(Val(expression))

End Function

'-----------------------------------------------------------------------------
Public Function LByte(ByRef expression) As Byte
    
    LByte = CByte(Val(expression))

End Function

'-----------------------------------------------------------------------------
Public Function LDbl(ByRef expression) As Double
    
    LDbl = CDbl(Val(expression))

End Function

'-----------------------------------------------------------------------------
Public Function LSng(ByRef expression) As Single
    
    LSng = CSng(Val(expression))

End Function

'-----------------------------------------------------------------------------
Public Function LBool(ByRef expression) As Boolean
    
    LBool = CBool(Val(expression))

End Function

'-----------------------------------------------------------------------------
Public Function LVar(ByRef expression) As Variant
    
    LVar = CVar(Val(expression))

End Function

'-----------------------------------------------------------
Public Function LStr(ByRef expression) As String
    
    On Error Resume Next
    
    LStr = expression
    LStr = LTrim$(str(expression))

End Function



