Option Strict Off
Option Explicit On

Friend Class PWA_Access
    Inherits System.Windows.Forms.Form
    '/***********************************************************************/
    '/*                                                                     */
    '/*     IF-60　API_Test  for Visual Basic .net 2005                     */
    '/*       Interface   National Instruments GP-IB / USB / RS-232C        */
    '/*                                                                     */
    '/*     Copyright(c) 2012  TEXIO TECHNOLOGY CORPORATION                 */
    '/***********************************************************************/


    Public res As Integer
    Public DeviceId As Integer 'Device Handle
    Public IF_Sel As Short 'IF_select 0:close,1:USB,2：DEV0，3:COM
    Public SYSAddr As String 'local Addr Name
    Public PcAddr As Integer 'PC Addr

    ' Open / Close
    Private Sub Command1_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Command1.Click 'device OPEN/CLOSE

        If Command1.Text = "OPEN" Then 'device OPEN

			Dim sModel As String = ""
			' 使用するインターフェースを選択
            Select Case (Combo1.Text)
                Case "USB"
					sModel = "USB:"
					IF_Sel = IF_USB
				Case "GPIB0"
					sModel = "DEV0:"
					IF_Sel = IF_GPIB0
				Case "COM"
					sModel = "COM1:"
					IF_Sel = IF_COM1
			End Select

			sModel = sModel & Trim(Combo2.Text) & ":" & Trim(Combo3.Text)

            PcAddr = Val(Combo2.Text)
            SYSAddr = ":ADDR " & Trim(Combo3.Text)

            ' デバイスオープン
			DeviceId = TMI_HandleOpen("PW-A", sModel)

            If DeviceId > 0 Then

				' OutputをOFF
				TMI_MainOutput(DeviceId, 0)

				' 出力CHAのアウトプットセレクトをONに設定
				TMI_OutputSel(DeviceId, 1, 1)

				' 出力CHAをLEDディスプレイに表示
				TMI_Display(DeviceId, 1)

                ' 画面表示
                Command1.Text = "CLOSE"
                Command2.Enabled = True
                Command2.Text = "ON"
                Command3.Enabled = True
                Command4.Enabled = True
                Command5.Enabled = True
                Command6.Enabled = True
                Command7.Enabled = True
                Text1.Text = ""
            Else
                Text1.Text = " Device Open ERR"
            End If
        Else 'device close

			' OutputをOFFに設定する
			TMI_MainOutput(DeviceId, 0)

			' リモート状態をローカルに戻す
			TMI_RemoteLocal(DeviceId)

			' Deviceクローズ
			TMI_HandleClose(DeviceId)

			DeviceId = 0

            ' 画面表示
            Command2.Text = "ON"
            Command2.BackColor = System.Drawing.SystemColors.Control
            Command1.Text = "OPEN"
            Command2.Enabled = False
            Command3.Enabled = False
            Command4.Enabled = False
            Command5.Enabled = False
            Command6.Enabled = False
            Command7.Enabled = False
            Text1.Text = ""
            IF_Sel = 0
        End If
    End Sub

    ' Output On/Off
    Private Sub Command2_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Command2.Click 'OUTPUT On/Off
        If Command2.Text = "ON" Then

            ' OutputをONに設定
			TMI_MainOutput(DeviceId, 1)

            ' 表示の更新
            Command2.Text = "OFF"
            Command2.BackColor = System.Drawing.Color.Red
        Else

            ' OutputをOFFに設定
			TMI_MainOutput(DeviceId, 0)

            ' 表示の更新
            Command2.Text = "ON"
            Command2.BackColor = System.Drawing.SystemColors.Control
        End If
    End Sub

    ' Preset 1
    Private Sub Command3_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Command3.Click 'Preset 1 call
        ' Preset 1を選択
		TMI_Preset(DeviceId, 1)
	End Sub

    ' Preset 2
    Private Sub Command4_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Command4.Click 'Preset 2 call
        ' Preset 2を選択
		TMI_Preset(DeviceId, 2)
	End Sub

    ' Preset 3
    Private Sub Command5_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Command5.Click 'Preset 3 call
        ' Preset 3を選択
		TMI_Preset(DeviceId, 3)
	End Sub

    ' Preset 0
    Private Sub Command6_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Command6.Click 'Preset Off
		' Preset 4を選択
        TMI_Preset(DeviceId, 4)
        API_test()
	End Sub

    ' Read
    Private Sub Command7_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Command7.Click 'Request Monitoring Value

		Dim bbuf As Byte
		Dim dblVoltage, dblCurrent As Double

		' モニタ値をリード
		TMI_MoniDataQ(DeviceId, 1, dblVoltage, dblCurrent, bbuf)

		' モニタ値はカンマ区切りで電圧、電流、電力、ステータス
		Text1.Text = Str(dblVoltage) & "V" & vbCrLf & Str(dblCurrent) & "A" & vbCrLf
		If (bbuf = 0) Then
			Text1.Text += "CVCC : CV"
		Else
			Text1.Text += "CVCC : CC"
		End If
		System.Windows.Forms.Application.DoEvents()

	End Sub

    ' フォームロード
    Private Sub PWA_Access_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load 'Form Load
        Dim index As Short

        ' 使用するインターフェースを選択できるようにする。
        Combo1.Items.Add(("USB"))
		Combo1.Items.Add(("GPIB0"))
		Combo1.Items.Add(("COM"))

        For index = 1 To 30
            Combo2.Items.Add((Str(index)))
            Combo3.Items.Add((Str(index)))
        Next index
    End Sub

    ' フォームクローズ
    Private Sub PWA_Access_FormClosing(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

		Dim Cancel As Boolean = eventArgs.Cancel
        Dim UnloadMode As System.Windows.Forms.CloseReason = eventArgs.CloseReason 'Form Close(x Button)

		If (DeviceId <> 0) Then
			' OutputをOFFに設定する。
			TMI_MainOutput(DeviceId, 0)

			' リモート状態をローカルに戻す
			TMI_RemoteLocal(DeviceId)

			' Deviceクローズ
			TMI_HandleClose(DeviceId)

			DeviceId = 0
		End If

		eventArgs.Cancel = Cancel

	End Sub
	Private Sub API_test()

		Dim iRet As Integer
		Dim Ch_A As Byte
		Dim Pre_1 As Byte
		Dim Voltage As Double
		Dim Current As Double
		Dim cv_cc As Byte
		Dim Addr As Byte
		Dim Output As Byte
		Dim DelayOn As Byte
		Dim DelayValue As Double
		Dim TrackOn As Byte
		Dim v_Set As Byte
		Dim TrackGroup As Byte
		Dim TrackValue As Double
		Dim PName As String
		Dim strPresetData As String
		Dim PresetData(31) As Double
		Dim Que As String

		Voltage = 3.124
		Current = 1.235
		Ch_A = 1
		Pre_1 = 1
		Output = 1
		PName = Space(256)
		Que = Space(256)
		strPresetData = Space(256)

		iRet = TMI_ModelNameQ(DeviceId, PName)
		iRet = TMI_TimeOut(DeviceId, 10)

		iRet = TMI_AllPresetQ(DeviceId, PresetData(0))
		iRet = TMI_AllPresetQS(DeviceId, strPresetData)
		iRet = TMI_DataBackUp(DeviceId)
		iRet = TMI_SRQEnable(DeviceId, 1)

		iRet = TMI_Refresh(DeviceId)
		iRet = TMI_Voltage(DeviceId, Ch_A, Pre_1, Voltage)
		iRet = TMI_VoltageQ(DeviceId, Ch_A, Pre_1, Voltage)
		iRet = TMI_Current(DeviceId, Ch_A, Pre_1, Current)
		iRet = TMI_CurrentQ(DeviceId, Ch_A, Pre_1, Current)
		iRet = TMI_MainOutput(DeviceId, Output)
		iRet = TMI_MainOutputQ(DeviceId, Output)
		iRet = TMI_OutputSel(DeviceId, Ch_A, Output)
		iRet = TMI_OutputSelQ(DeviceId, Ch_A, Output)

		iRet = TMI_Display(DeviceId, Ch_A)
		iRet = TMI_DisplayQ(DeviceId, Ch_A)
		iRet = TMI_Preset(DeviceId, Pre_1)
		iRet = TMI_PresetQ(DeviceId, Pre_1)
		iRet = TMI_MoniDataQ(DeviceId, Ch_A, Voltage, Current, cv_cc)

		iRet = TMI_TrackingOnOff(DeviceId, TrackOn)
		iRet = TMI_TrackingOnOffQ(DeviceId, TrackOn)
		iRet = TMI_TrackingMode(DeviceId, TrackOn)
		iRet = TMI_TrackingModeQ(DeviceId, TrackOn)
		iRet = TMI_TrackingGroup(DeviceId, Ch_A, TrackGroup)
		iRet = TMI_TrackingGroupQ(DeviceId, Ch_A, TrackGroup)
		iRet = TMI_TrackingData(DeviceId, Ch_A, v_Set, TrackValue)
		iRet = TMI_TrackingDataQ(DeviceId, Ch_A, v_Set, TrackValue)

		iRet = TMI_Delay(DeviceId, DelayOn)
		iRet = TMI_DelayQ(DeviceId, DelayOn)
		iRet = TMI_DelayTime(DeviceId, Ch_A, DelayValue)
		iRet = TMI_DelayTimeQ(DeviceId, Ch_A, DelayValue)

		iRet = TMI_AdrQ(DeviceId, Addr)
		iRet = TMI_RemoteLocal(DeviceId)

		iRet = TMI_Out(DeviceId, "ST3")
		iRet = TMI_In(DeviceId, Que)

		iRet = TMI_LocalLockOut(DeviceId)
	End Sub

End Class