'!TITLE "KEYENCE_CVX.pcs"
'
'Syntax library to communicate with Keyence CV-X series.
'
static D_dnCvxData(30) as Double		'Received data(double) [Receive Data(double)] 
'In this sample, static variables are used for data passing.
'however it is possible to use local variables instead of static variables.

Sub InitData
	Dim iCnt as Integer
	For iCnt = 0 to 30
		D_dnCvxData(iCnt) = 0
	Next	
End Sub

Function ConnectCVX(ByVal iLineNo as Integer) as Integer
	On Error Goto ErrorProc
	If Comm.State(iLineNo) = 0 Then Comm.Open iLineNo	'Open the communication port
	Comm.Clear iLineNo	'Clear communication buffer
	ConnectCVX = -1
	Exit Function

ErrorProc:
	PrintMsg HEX(Err.Number) & " : " & Err.Description
	ConnectCVX = 0
End Function


Function DisConnectCVX(ByVal iLineNo as Integer) as Integer
	On Error Goto ErrorProc

	Comm.Close iLineNo
	DisConnectCVX = -1

	Exit Function

ErrorProc:
	PrintMsg HEX(Err.Number) & " : " & Err.Description
	DisConnectCVX = 0
End Function


Function X_R0(ByVal iLineNo as Integer) as Integer
	On Error Goto ErrorProc
	Dim strResult as String

	Comm.Output iLineNo, "R0"
	strResult = Comm.Input(iLineNo, 2000)
	If(strResult = "R0") then
		X_R0 = -1
	Else
		PrintMsg strResult
		X_R0 = 0
	End If
	Exit Function

ErrorProc:
	PrintMsg HEX(Err.Number) & " : " & Err.Description
	X_R0 = 0
End Function


Function X_S0(ByVal iLineNo as Integer) as Integer
	On Error Goto ErrorProc
	Dim strResult as String

	Comm.Output iLineNo, "S0"
	strResult = Comm.Input(iLineNo, 2000)
	If(strResult = "S0") then
		X_S0 = -1
	Else
		PrintMsg strResult
		X_S0 = 0
	End If
	Exit Function

ErrorProc:
	PrintMsg HEX(Err.Number) & " : " & Err.Description
	X_S0 = 0
End Function


Function X_PW(ByVal iLineNo as Integer, ByVal iSDNo as Integer, ByVal lProgNo as Integer) as Integer
	On Error Goto ErrorProc
	Dim strResult as String

	Comm.Output iLineNo, "PW," & iSDNo & "," & lProgNo
	strResult = Comm.Input(iLineNo, 3000)
	If(Left(strResult,2) = "PW") then
		X_PW = -1
	Else
		PrintMsg strResult
		X_PW = 0
	End If
	Exit Function

ErrorProc:
	PrintMsg HEX(Err.Number) & " : " & Err.Description
	X_PW = 0
End Function


Function X_T(ByVal iLineNo as Integer, ByVal iTriggerNo as Integer, ByRef strResultPacket as String) as Integer
	On Error Goto ErrorProc
	Dim strResult as String
	Dim vntValue as Variant
	Dim Icnt as Integer

	If iTriggerNo > 0 then
		Comm.Output iLineNo, "T" & iTriggerNo
	Else
		Comm.Output iLineNo, "TA"
	End If
	strResult = Comm.Input(iLineNo, 2000)
	If (Left(strResult,2) = "T" & iTriggerNo) or (Left(strResult,2) = "TA") then
		strResultPacket = Comm.Input(iLineNo, 3000)

'		Put the results in variables
		vntValue = Split(strResultPacket,",")			'Split the results by commas
		For Icnt = 0 to Ubound(vntValue)				'Repeat the command to the number of elements
			D_dnCvxData(Icnt) = Val(vntValue(Icnt))		'Convert characters to values,then store them.
		Next
		X_T = -1
	Else
		PrintMsg strResult
		X_T = 0
	End If
	Exit Function

ErrorProc:
	PrintMsg HEX(Err.Number) & " : " & Err.Description
	X_T = 0
End Function

