Option Strict Off
Option Explicit On
Module SubFunc
	
	'///// Win32Api /////////////////////////////////////////////////////////////////////////
	Public Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Integer) As Short
	
	'///// SubFunc.basŎgpO[oϐ //////////////////////////////////////////
	Public CmdNum As Integer						'R}h
	Public Mode As Integer							'}X^(=0)X[u(=1)̔
	Public MyAddr As Integer						'}CAhX(VVer)
	Public YrAddr As Integer						'@AhX(VVer)
	Public Cmd(30) As Integer						'bZ[W(R}h)
	Public Cnt As Integer							'JԂ񐔂Ȃ
	Public Delim As Integer							'f~^
	Public Eoi As Integer							'EOI
	Public Timeout As Integer						'^CAEg
	Public Dmamode As Integer						'DMA[h
	Public DmaPos As Integer						'DMA`l
	Public Ret As Integer							'߂l
	Public Srbuf As New String("", 10000)			'̃obt@
	Public Srlen As Integer							'̒
	Public ErrText As String						'G[
	Public RetTmp As Integer						'ꎞIȖ߂l
	Public pCmd As IntPtr							'CmdzpIntPtr
	
	'///// [ CheckRet ֐ ] ////////////////////////////////////////////////////////////////
	Public Function CheckRet(ByRef FuncStr As String, ByRef RetCode As Integer, ByRef Text As String) As Integer
		Dim RetSts As Integer
		Text = FuncStr & " : ɏI܂"
		CheckRet = RetCode And &HFFs
		RetSts = 0
		If (CheckRet >= 3) Then				 'CheckRetRȏŃG[
			RetSts = 1						  'ăR[hɑ΂G[𕶎ɂ܂B
			If (CheckRet = 80) Then Text = FuncStr & " : I/OAhXG[łA܂GpInisG[łB" : GoTo CheckStatus
			If (CheckRet = 82) Then Text = FuncStr & " : WXgݒ̃G[łB" : GoTo CheckStatus
			If (CheckRet = 128) Then Text = FuncStr & " : f[^MI[o[(M)[SRQ]͏オĂ܂(|[O)" : GoTo CheckStatus
			If (CheckRet = 140) Then Text = FuncStr & " : 񓯊֐̎sł" : GoTo CheckStatus
			If (CheckRet = 141) Then Text = FuncStr & " : 񓯊֐I܂" : GoTo CheckStatus
			If (CheckRet = 190) Then Text = FuncStr & " : CxgIuWFNg쐬ł܂" : GoTo CheckStatus
			If (CheckRet = 200) Then Text = FuncStr & " : Xbh쐬ł܂B" : GoTo CheckStatus
			If (CheckRet = 201) Then Text = FuncStr & " : ̃Cxg֐słB" : GoTo CheckStatus
			If (CheckRet = 210) Then Text = FuncStr & " : DMAݒł܂łB" : GoTo CheckStatus
			If (CheckRet = 240) Then Text = FuncStr & " : [Esc]L[܂B" : GoTo CheckStatus
			If (CheckRet = 241) Then Text = FuncStr & " : Fileo̓G[łB" : GoTo CheckStatus
			If (CheckRet = 242) Then Text = FuncStr & " : AhXw肪ԈĂ܂B" : GoTo CheckStatus
			If (CheckRet = 243) Then Text = FuncStr & " : obt@wG[łB" : GoTo CheckStatus
			If (CheckRet = 244) Then Text = FuncStr & " : zTCYG[łB" : GoTo CheckStatus
			If (CheckRet = 245) Then Text = FuncStr & " : obt@܂B" : GoTo CheckStatus
			If (CheckRet = 246) Then Text = FuncStr & " : sȃIuWFNgłB" : GoTo CheckStatus
			If (CheckRet = 247) Then Text = FuncStr & " : foCX̉̃`FbNłB" : GoTo CheckStatus
			If (CheckRet = 248) Then Text = FuncStr & " : sȃf[^^łB" : GoTo CheckStatus
			If (CheckRet = 249) Then Text = FuncStr & " : ȏfoCXǉł܂B" : GoTo CheckStatus
			If (CheckRet = 250) Then Text = FuncStr & " : foCX܂B" : GoTo CheckStatus
			If (CheckRet = 251) Then Text = FuncStr & " : f~^foCXԂňĂ܂B" : GoTo CheckStatus
			If (CheckRet = 252) Then Text = FuncStr & " : GP-IBG[łBmFĂB" : GoTo CheckStatus
			If (CheckRet = 253) Then Text = FuncStr & " : f~^݂̂M܂B" : GoTo CheckStatus
			If (CheckRet = 254) Then Text = FuncStr & " : ^CAEg܂B" : GoTo CheckStatus
			If (CheckRet = 255) Then Text = FuncStr & " : p[^G[A܂͕sĂяołB" : GoTo CheckStatus
		End If
CheckStatus:
		'----- IFC & SRQ Me[^X̃bZ[W ------
		CheckRet = RetCode And &HFF00S
		If (CheckRet = &H100S) Then Text = Text & " -SRQM[Xe[^X]" : GoTo CheckEnd
		If (CheckRet = &H200S) Then Text = Text & " -IFCM[Xe[^X]" : GoTo CheckEnd
		If (CheckRet = &H300S) Then Text = Text & " -SRQIFCM[Xe[^X]"
CheckEnd:
		CheckRet = RetSts
		
	End Function
	
	'///// [ AddCommand ֐ ] //////////////////////////////////////////////////////////////
	Public Function AddCommand(ByRef CommandType As Integer, ByRef CommandNum As Short) As Boolean
		Dim ErrMsg As String							'G[bZ[Wp
		
		If (CmdNum > 30) Then							'bZ[W30ȏ̎
			ErrMsg = "ȏݒł܂B"
			MsgBox(ErrMsg)
			AddCommand = False
			Exit Function
		End If
		If (CommandNum = 2) Then						'bZ[Wɑ@AhX
			Cmd(CmdNum) = (YrAddr Or &H20S)
			CmdNum = CmdNum + 1
		End If
		Cmd(CmdNum) = CommandType						'ǂ̃bZ[WlœnB
		CmdNum = CmdNum + 1
		AddCommand = True
		
	End Function
	
	'///// [ DispCommandList ֐ ] /////////////////////////////////////////////////////////
	Public Function DispCommandList() As String
		Dim TxtRet As Integer							'16ibZ[Wp
		Dim TmpString, Txt As String					'TmpString = 쐬MSGText = SĂMSG
		
		Txt = ""
		For Cnt = 1 To CmdNum - 1						'SĂMSG̍Ōɍ쐬MSGǉ
			TxtRet = Cmd(Cnt)							'ԖڂCmdz
			TmpString = "[" & Hex(TxtRet) & "h] "		'\p
			Txt = Txt & TmpString						'VKMSGǉ
		Next

		DispCommandList = Txt
		
	End Function
	
	'///// [ GpinInit ֐ ] ////////////////////////////////////////////////////////////////
	Public Function GpibInit(ByRef TextInit As String) As Integer
		
		'Delim = 1: Eoi = 1								'Delim = 0:gpȂ/1:CR+LF/2:CR/3:LF Eoi = 0:gpȂ/1:gp
		Timeout = 10000									'Timeout = ms(1000ms -> 1s)
		GpibInit = 0										'ݒ
		
		Ret = GpExit()									'ďɂG[h܂
		Ret = GpIni()									'GP-IB{[h̏,֐gp̊Jn錾s܂
		If CheckRet("GpIni", Ret, TextInit) = 1 Then GoTo Err_Renamed 'GpInił̃G[`FbN܂
		'GpBoardsts()ł̓WXg擾邱Ƃł܂
		'PԖڂ̈̓wvɑΏƕ\܂2Ԗڂ͑ϐł
		Ret = GpBoardsts(&HAS, Mode)					'Master(=0)Slave(=1)擾܂
		Ret = GpBoardsts(&H8S, MyAddr)					'}CAhX擾܂
		Ret = GpBoardsts(&HCS, Dmamode)					'DMA [h
		Ret = GpBoardsts(&HDS, DmaPos)					'DMA `l
		
		If Mode = 0 Then								'}X^̎GpIni()AGpRen()gp܂B
			Ret = GpIfc(1)								'}X^ł邱Ƃ`܂B(1̏ꍇ100usɂȂ܂)
			If CheckRet("GpIfc", Ret, TextInit) = 1 Then GoTo Err_Renamed 'GpRetł̃G[`FbN܂

			Ret = GpRen()								'@[gԂɂ܂B
			If CheckRet("GpRen", Ret, TextInit) = 1 Then GoTo Err_Renamed 'GpRenł̃G[`FbN܂
		End If
		'sȂꍇ̏lCR+LF+EOIɂȂĂ܂B
		'Ret = GpDelim(Delim, Eoi)						'f~^R[h(EOI)o̎w܂B(w肵ȂĂł)
		'If CheckRet("GpDelim", Ret, TextInit) = 1 Then GoTo Err	'GpDelimł̃G[`FbN܂

		'sȂꍇ̏l10000ms -> 10sɂȂĂ܂B
		Ret = GpTimeout(Timeout)						'^CAEgԂw肵܂B(w肵ȂĂł)
		If CheckRet("GpTimeout", Ret, TextInit) = 1 Then GoTo Err_Renamed 'Timeoutł̃G[`FbN܂

		TextInit = "I܂B"				'CheckSĂO(I)̏ꍇ
		Exit Function
Err_Renamed:
		GpibInit = 1
		
	End Function
	
	'///// [ GpibExit ֐ ] ////////////////////////////////////////////////////////////////
	Public Function GpibExit() As Integer
		
		Ret = GpBoardsts(&HAS, Mode)					'Master(=0)Slave(=1)擾܂
		If Mode = 0 Then
			Cmd(0) = 2									'R}h
			Cmd(1) = &H3FS								'AX^UNL
			Cmd(2) = &H5FS								'Ag[N^UNT
			Ret = GpComand(pCmd)
			Ret = GpResetren()							'@̃[gԂ܂B
		End If
		Ret = GpExit()									'GP-IB{[h̃Zbg֐gp̏I錾܂B
		
	End Function
	
	'///// [ GpibPrint ֐ ] ///////////////////////////////////////////////////////////////
	Public Function GpibPrint(ByRef DeviceAddr As Integer, ByRef Str_Renamed As String) As Integer

		Ret = GpBoardsts(&H8S, MyAddr)
		Cmd(0) = 2										'őM
		Cmd(1) = MyAddr									'}CAhX(PC)
		Cmd(2) = DeviceAddr								'PIA3200

		Srbuf = Str_Renamed								'Mf[^
		Srlen = Len(Str_Renamed)						'f[^
		Ret = GpTalk(pCmd, Srlen, Srbuf)				'ۂ̑M
		If Ret <> 0 Then								'G[`FbN
			GpibPrint = 1								'G[
			RetTmp = CheckRet("GpTalk", Ret, ErrText)
			Ret = MsgBox("AhX [ " & DeviceAddr & " ] <- [ " & Str_Renamed & " ] ̑MɎs܂B", MsgBoxStyle.YesNo, "p܂H")
			If Ret = MsgBoxResult.No Then GpibPrint = 1 '[]̏ꍇ͏I
			If Ret = MsgBoxResult.Yes Then GpibPrint = 0 'G[Ȃ
		End If

	End Function

	'///// [ GpibInput ֐ ] ///////////////////////////////////////////////////////////////
	Public Function GpibInput(ByRef DeviceAddr As Integer, ByRef Str_Renamed As String) As Integer

		Str_Renamed = Space(10000)						'Xy[Xŏ܂B
		Ret = GpBoardsts(&H8S, MyAddr)
		Srlen = 10000									'őM
		Cmd(0) = 2										'z(R}h)
		Cmd(1) = DeviceAddr								'PIA3200̃AhX
		Cmd(2) = MyAddr									'}CAhX(PC)

		Ret = GpListen(pCmd, Srlen, Srbuf)

		If Ret > 3 Then									'G[`FbN
			RetTmp = CheckRet("GpListen", Ret, ErrText)
			Ret = MsgBox("AhX [ " & DeviceAddr & " ] ̎MɎs܂B", MsgBoxStyle.YesNo, "p܂H")
			If Ret = MsgBoxResult.No Then GpibInput = 1
			If Ret = MsgBoxResult.Yes Then
				GpibInput = 0							'G[Ȃ
			End If
		End If
		Str_Renamed = Mid(Srbuf, 1, Srlen)

	End Function

	'Tektronix/TDS3000oCif[^Mp ////////////////////////////////////////////////
	Public Function GpibListenB(ByRef Dev As Integer, ByVal pByteData As IntPtr, ByRef Srlen As Integer) As Integer
		Dim DataLen As New String("", 10)

		Ret = GpBoardsts(&H8S, MyAddr)					'}CAhX擾
		Srlen = 2										'őM
		Cmd(0) = 2										'z(R}h)
		Cmd(1) = Dev									'PIA3200̃AhX
		Cmd(2) = MyAddr									'}CAhX(Board)
		Ret = GpDelim(0, 1)								'TDS3000̃f~^ݒł͢LF݂̂Ȃ̂Ńf~^̕ύXKv
		Ret = GpListen(pCmd, Srlen, DataLen)
		If (Ret >= 3) Then
			If (Ret <> 128) Then						'킴ƃf[^؂Ă邽߂ Ret=128 ƂȂ܂B
				RetTmp = CheckRet("GpListen", Ret, ErrText)
				Ret = MsgBox("AhX [ " & Dev & " ] ̎MɎs܂B", MsgBoxStyle.YesNo, "p܂H")
				If Ret = MsgBoxResult.No Then
					GpibListenB = 1						'[]̏ꍇ͏I
					Exit Function
				End If
			End If
			If Ret = MsgBoxResult.Yes Then GpibListenB = 0 'G[Ȃ
		End If
		Cmd(0) = 0										'R}hoGpListen܂B
		Srlen = Val(Mid(DataLen, 2, 1))
		Ret = GpListen(pCmd, Srlen, DataLen)

		Srlen = Val(Mid(DataLen, 1, Srlen))
		Ret = GpListenBinary(pCmd, Srlen, pByteData)
		Ret = GpDelim(3, 1)								'f~^߂܂B

	End Function

	'///// [ GpibInputB ֐ ] //////////////////////////////////////////////////////////////
	Public Function GpibInputB(ByRef Dev As Integer, ByVal pByteData As IntPtr) As Integer

		Ret = GpBoardsts(&H8S, MyAddr)
		Srlen = 10000									'őM
		Cmd(0) = 2										'z(R}h)
		Cmd(1) = Dev									'PIA3200̃AhX
		Cmd(2) = MyAddr									'}CAhX(PC)

		Ret = GpListenBinary(pCmd, Srlen, pByteData)
		If Ret > 3 Then									'G[`FbN
			RetTmp = CheckRet("GpListenBinary", Ret, ErrText)
			Ret = MsgBox(ErrText & " p܂H", MsgBoxStyle.YesNo, "G[")
			If Ret = MsgBoxResult.No Then
				GpibInputB = 1
				Exit Function
			End If
		Else
			GpibInputB = 0								'G[Ȃ
		End If

	End Function

	'///// [ GpibCmd ֐ ] /////////////////////////////////////////////////////////////////
	Public Function GpibCmd() As Integer

		Cmd(0) = 2										'R}h
		Cmd(1) = &H3FS									'AX  ^UNL
		Cmd(2) = &H5FS									'Ag[N^UNT

		Ret = GpComand(pCmd)							'R}hM
		If Ret <> 0 Then								'G[`FbN
			GpibCmd = 1									'G[
			RetTmp = CheckRet("GpComand", Ret, ErrText)
			Ret = MsgBox(ErrText, MsgBoxStyle.OKOnly, "G[")
			End											'vOI(G[ŏIȂꍇ͏ĉB)
		End If
		GpibCmd = 0										'I

	End Function

	' ///// [ OPC̃`FbN ] ///////////////////////////////////////////////////////////////
	Public Sub WaitOPC(ByRef Dev As Integer)
		Dim RdData As String = ""

		Ret = GpibPrint(Dev, "*OPC?")					'HƂĂ邩
		Ret = GpibInput(Dev, RdData)

	End Sub

    '///// [ESC]L[̐ //////////////////////////////////////////////////////////////////
    Public Function EscCheck() As Integer
        Dim vKey As Integer

        vKey = &H1BS         'ESC̃AXL[R[h
        If (GetAsyncKeyState(vKey) <> 0) Then
            EscCheck = 1        'ESCL[o(߂l)
        Else
            EscCheck = 0        'ESCL[Ȃ(߂l)
        End If

    End Function
End Module