option explicit

' --------------------------------------------------------------------------
' ※ Server側のCaoSQLConfig
'
' Server側でのCaoSQLConfigの設定でActionメニュー→Setting→RACの設定を行う．
' 追加 - 「Conn=eth::5006,PacketOpt=4:0:0:0,EtherOpt=1:5,Timeout=500」
'
' ※ 注意！
'
' さらにServer側でのCaoSQLConfigの設定で登録するアイテムにRequestTypeを「VT_VARIANT」にする！
' 配列を扱う場合，VBSなどではバリアント型の配列を扱うため．
' --------------------------------------------------------------------------


'------ 使用変数の宣言

  dim CaoEng							' CAO Engine
  dim CaoWS							' CAO WS

  dim CaoCtrl							' CAO Controller
  dim CaoVarPos							' CAO Position Variable

'--------------------------------------------

  ' オブジェクト生成
  initCao

  ' 現在値取得してMsg表示
  CurrentPos  
	
  ' 解放
  ReleaseCao


'--------------------------------------------
' Cao生成
sub initCao()
	on error resume next

	dim result

	result = True

	'----- CaoEngine生成
	set CaoEng = createobject("CAO.CaoEngine")

	if not (CaoEng is Nothing) then

		' DefaultWorkspaceを取得
		set CaoWS = CaoEng.Workspaces(0)

		if Not (CaoWS is nothing) then
	
			'---- 接続
			set CaoCtrl = CaoWS.AddController("Sample","CaoProv.RAC","","Conn=eth:192.168.0.1:5006")    ' パラメータは環境に合わせる
			
			if (CaoCtrl is nothing) then
				result = False
			end if

		end if

	else
		result = False
	end if

	if (result = False) then
		' Caoオブジェクト生成失敗
		msgbox "Fail to create CAO.",vbExclamation
	end if

end sub



'--------------------------------------------
' 現在位置をORiNのRAC経由で取得してメッセージで表示
sub CurrentPos()

	dim i
	dim pos
	dim strMsg

	if not (CaoCtrl is Nothing) then

		' Variableの取得
		set CaoVarPos = CaoCtrl.AddVariable("RC5::CurJnt")		' ※Server側にCaoSQLConfigで設定されているアイテムを指定する

		if not (CaoVarPos is Nothing) then

			pos = CaoVarPos.Value			' 値取得

			for i = 0 to Ubound(pos)

				if Not (IsEmpty(pos)) then
					
					' J1=○ J2=○ ‥ Jx=○
					strMsg = strMsg & "J" & cstr(i+1) & " = " & pos(i) & vbcrlf
					
				end if

			next
			
			' --- 現在位置表示
			msgbox strMsg

		else
			msgbox "Can't access to Current Position.",vbExclamation
		end if
	
	else
		msgbox "Controller is Nothing.",vbExclamation
	end if

end sub



' --------------------------------------------
' ReleaseCao
sub ReleaseCao()

	' --------- Delete CaoVariable
	if not (CaoVarPos is Nothing) then
		set CaoVarPos = Nothing
	end if

	' --------- Delete CaoController
	if not (CaoCtrl is Nothing) then
		set CaoCtrl = Nothing
	end if

	' --------- DeleteCaoWS
	if not (CaoWS is Nothing) then
		set CaoWS = Nothing
	end if

	' --------- Delete CaoEngine
	if not (CaoEng is Nothing) then
		set CaoEng = Nothing
	end if

end sub

