
'/***********************************************************************/
'/*                                                                     */
'/*     IF_50  API用サンプルプログラム                                  */
'/*                                                                     */
'/*     Copyright(c) 2014  TEXIO TECHNOLOGY CORPORATION                 */
'/***********************************************************************/
Option Strict Off
Option Explicit On 

Public Class Form1
    Inherits System.Windows.Forms.Form

    Public DeviceId As Integer
    Public res As Integer

#Region " Windows フォーム デザイナで生成されたコード "

    Public Sub New()
        MyBase.New()

        ' この呼び出しは Windows フォーム デザイナで必要です。
        InitializeComponent()

        ' InitializeComponent() 呼び出しの後に初期化を追加します。

    End Sub

    ' Form は dispose をオーバーライドしてコンポーネント一覧を消去します。
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    ' Windows フォーム デザイナで必要です。
    Private components As System.ComponentModel.IContainer

    ' メモ : 以下のプロシージャは、Windows フォーム デザイナで必要です。
    ' Windows フォーム デザイナを使って変更してください。  
    ' コード エディタは使用しないでください。
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents Label3 As System.Windows.Forms.Label
    Friend WithEvents Button2 As System.Windows.Forms.Button
    Friend WithEvents Button6 As System.Windows.Forms.Button
    Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
    Friend WithEvents GroupBox2 As System.Windows.Forms.GroupBox
    Friend WithEvents GroupBox3 As System.Windows.Forms.GroupBox
    Friend WithEvents Button3 As System.Windows.Forms.Button
    Friend WithEvents Button4 As System.Windows.Forms.Button
    Friend WithEvents Button5 As System.Windows.Forms.Button
    Friend WithEvents Button7 As System.Windows.Forms.Button
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents Label5 As System.Windows.Forms.Label
    Friend WithEvents Label8 As System.Windows.Forms.Label
    Friend WithEvents ComboBox1 As System.Windows.Forms.ComboBox
    Friend WithEvents ComboBox2 As System.Windows.Forms.ComboBox
    Friend WithEvents ComboBox3 As System.Windows.Forms.ComboBox
    Friend WithEvents Label2 As System.Windows.Forms.Label
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))
        Me.Button1 = New System.Windows.Forms.Button()
        Me.Label3 = New System.Windows.Forms.Label()
        Me.Button2 = New System.Windows.Forms.Button()
        Me.Button6 = New System.Windows.Forms.Button()
        Me.GroupBox1 = New System.Windows.Forms.GroupBox()
        Me.ComboBox3 = New System.Windows.Forms.ComboBox()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.GroupBox2 = New System.Windows.Forms.GroupBox()
        Me.GroupBox3 = New System.Windows.Forms.GroupBox()
        Me.Button5 = New System.Windows.Forms.Button()
        Me.Button4 = New System.Windows.Forms.Button()
        Me.Button3 = New System.Windows.Forms.Button()
        Me.Button7 = New System.Windows.Forms.Button()
        Me.TextBox1 = New System.Windows.Forms.TextBox()
        Me.Label5 = New System.Windows.Forms.Label()
        Me.Label8 = New System.Windows.Forms.Label()
        Me.ComboBox1 = New System.Windows.Forms.ComboBox()
        Me.ComboBox2 = New System.Windows.Forms.ComboBox()
        Me.Label2 = New System.Windows.Forms.Label()
        Me.GroupBox1.SuspendLayout()
        Me.GroupBox3.SuspendLayout()
        Me.SuspendLayout()
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(24, 32)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(56, 24)
        Me.Button1.TabIndex = 0
        Me.Button1.Text = "OPEN"
        '
        'Label3
        '
        Me.Label3.Font = New System.Drawing.Font("Arial", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label3.Location = New System.Drawing.Point(184, 16)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(64, 16)
        Me.Label3.TabIndex = 6
        Me.Label3.Text = "PC ADDR"
        '
        'Button2
        '
        Me.Button2.BackColor = System.Drawing.SystemColors.Control
        Me.Button2.Enabled = False
        Me.Button2.Location = New System.Drawing.Point(24, 88)
        Me.Button2.Name = "Button2"
        Me.Button2.Size = New System.Drawing.Size(56, 24)
        Me.Button2.TabIndex = 8
        Me.Button2.Text = "ON"
        '
        'Button6
        '
        Me.Button6.Enabled = False
        Me.Button6.Location = New System.Drawing.Point(176, 16)
        Me.Button6.Name = "Button6"
        Me.Button6.Size = New System.Drawing.Size(48, 24)
        Me.Button6.TabIndex = 12
        Me.Button6.Text = "4"
        '
        'GroupBox1
        '
        Me.GroupBox1.Controls.AddRange(New System.Windows.Forms.Control() {Me.ComboBox3})
        Me.GroupBox1.Location = New System.Drawing.Point(8, 0)
        Me.GroupBox1.Name = "GroupBox1"
        Me.GroupBox1.Size = New System.Drawing.Size(328, 64)
        Me.GroupBox1.TabIndex = 13
        Me.GroupBox1.TabStop = False
        Me.GroupBox1.Text = " Device "
        '
        'ComboBox3
        '
        Me.ComboBox3.Location = New System.Drawing.Point(248, 32)
        Me.ComboBox3.Name = "ComboBox3"
        Me.ComboBox3.Size = New System.Drawing.Size(64, 20)
        Me.ComboBox3.TabIndex = 19
        Me.ComboBox3.Text = "1"
        '
        'Label1
        '
        Me.Label1.Font = New System.Drawing.Font("Arial", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label1.Location = New System.Drawing.Point(176, 32)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(8, 16)
        Me.Label1.TabIndex = 0
        Me.Label1.Text = ":"
        '
        'GroupBox2
        '
        Me.GroupBox2.Location = New System.Drawing.Point(8, 72)
        Me.GroupBox2.Name = "GroupBox2"
        Me.GroupBox2.Size = New System.Drawing.Size(88, 48)
        Me.GroupBox2.TabIndex = 14
        Me.GroupBox2.TabStop = False
        Me.GroupBox2.Text = " Input  "
        '
        'GroupBox3
        '
        Me.GroupBox3.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button5, Me.Button4, Me.Button3, Me.Button6})
        Me.GroupBox3.Location = New System.Drawing.Point(104, 72)
        Me.GroupBox3.Name = "GroupBox3"
        Me.GroupBox3.Size = New System.Drawing.Size(232, 48)
        Me.GroupBox3.TabIndex = 15
        Me.GroupBox3.TabStop = False
        Me.GroupBox3.Text = "  Preset     "
        '
        'Button5
        '
        Me.Button5.Enabled = False
        Me.Button5.Location = New System.Drawing.Point(120, 16)
        Me.Button5.Name = "Button5"
        Me.Button5.Size = New System.Drawing.Size(48, 24)
        Me.Button5.TabIndex = 18
        Me.Button5.Text = "3"
        '
        'Button4
        '
        Me.Button4.Enabled = False
        Me.Button4.Location = New System.Drawing.Point(64, 16)
        Me.Button4.Name = "Button4"
        Me.Button4.Size = New System.Drawing.Size(48, 24)
        Me.Button4.TabIndex = 17
        Me.Button4.Text = "2"
        '
        'Button3
        '
        Me.Button3.Enabled = False
        Me.Button3.Location = New System.Drawing.Point(8, 16)
        Me.Button3.Name = "Button3"
        Me.Button3.Size = New System.Drawing.Size(48, 24)
        Me.Button3.TabIndex = 16
        Me.Button3.Text = "1"
        '
        'Button7
        '
        Me.Button7.Enabled = False
        Me.Button7.Location = New System.Drawing.Point(24, 144)
        Me.Button7.Name = "Button7"
        Me.Button7.Size = New System.Drawing.Size(56, 24)
        Me.Button7.TabIndex = 17
        Me.Button7.Text = "READ"
        '
        'TextBox1
        '
        Me.TextBox1.Location = New System.Drawing.Point(104, 136)
        Me.TextBox1.Multiline = True
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.Size = New System.Drawing.Size(232, 56)
        Me.TextBox1.TabIndex = 16
        Me.TextBox1.Text = ""
        '
        'Label5
        '
        Me.Label5.Font = New System.Drawing.Font("Arial", 11.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label5.Location = New System.Drawing.Point(248, 32)
        Me.Label5.Name = "Label5"
        Me.Label5.Size = New System.Drawing.Size(8, 16)
        Me.Label5.TabIndex = 1
        Me.Label5.Text = ":"
        '
        'Label8
        '
        Me.Label8.Font = New System.Drawing.Font("Arial", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label8.Location = New System.Drawing.Point(256, 16)
        Me.Label8.Name = "Label8"
        Me.Label8.Size = New System.Drawing.Size(72, 16)
        Me.Label8.TabIndex = 7
        Me.Label8.Text = "SYS ADDR"
        '
        'ComboBox1
        '
        Me.ComboBox1.Location = New System.Drawing.Point(96, 32)
        Me.ComboBox1.Name = "ComboBox1"
        Me.ComboBox1.Size = New System.Drawing.Size(80, 20)
        Me.ComboBox1.TabIndex = 17
        Me.ComboBox1.Text = "USB"
        '
        'ComboBox2
        '
        Me.ComboBox2.Location = New System.Drawing.Point(184, 32)
        Me.ComboBox2.Name = "ComboBox2"
        Me.ComboBox2.Size = New System.Drawing.Size(64, 20)
        Me.ComboBox2.TabIndex = 18
        Me.ComboBox2.Text = "1"
        '
        'Label2
        '
        Me.Label2.Font = New System.Drawing.Font("Arial", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label2.Location = New System.Drawing.Point(96, 16)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(72, 16)
        Me.Label2.TabIndex = 19
        Me.Label2.Text = "Interface"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 12)
        Me.ClientSize = New System.Drawing.Size(344, 197)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Label2, Me.Label8, Me.ComboBox2, Me.ComboBox1, Me.Label1, Me.Label5, Me.TextBox1, Me.GroupBox3, Me.Button2, Me.Button1, Me.GroupBox2, Me.Label3, Me.GroupBox1, Me.Button7})
        Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
        Me.MaximizeBox = False
        Me.MinimizeBox = False
        Me.Name = "Form1"
        Me.Text = "LW_Access"
        Me.GroupBox1.ResumeLayout(False)
        Me.GroupBox3.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

#End Region

'Form Load
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim index As Integer

        ComboBox1.Items.Add("USB")
        ComboBox1.Items.Add("GPIB0")
        ComboBox1.Items.Add("GPIB1")
        For index = 1 To 30
            ComboBox2.Items.Add(Str(index))
            ComboBox3.Items.Add(Str(index))
        Next index
    End Sub
    ' フォームクローズ
    Private Sub Form1_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
            res = LW_SetMainInput(DeviceId, 0)
            res = LW_SetLocal(DeviceId)
            res = LW_DeviceClose(DeviceId)
            DeviceId = 0
        End If
        eventArgs.Cancel = Cancel

    End Sub

'Open/Close
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim IF_Name, BDADDR, PCADDR, SYSADDR As Integer

        If Button1.Text = "OPEN" Then       'device OPEN
            Select Case (ComboBox1.Text)
                Case "USB"
                    IF_Name = 2
                    BDADDR = 0

                Case "GPIB0"
                    IF_Name = 1
                    BDADDR = 0

                Case "GPIB1"
                    IF_Name = 1
                    BDADDR = 1
            End Select

            PCADDR = Val(ComboBox2.Text)
            SYSADDR = Val(ComboBox3.Text)
            DeviceId = LW_DeviceOpen(IF_Name, BDADDR, PCADDR, SYSADDR)
            If DeviceId > 0 Then
                res = LW_SetMainInput(DeviceId, 0)
                res = LW_SetInputSelect(DeviceId, 1, 0)
                res = LW_SetDisplaySelect(DeviceId, 1)
                Button1.Text = "CLOSE"
                Button2.Enabled = True
                Button2.Text = "ON"
                Button3.Enabled = True
                Button4.Enabled = True
                Button5.Enabled = True
                Button6.Enabled = True
                Button7.Enabled = True
                TextBox1.Text = ""
            Else
                TextBox1.Text = " Device Open ERR"
            End If
        Else                                'device close
            Button2.Text = "ON"
            res = LW_SetMainInput(DeviceId, 0)
            Button2.BackColor = System.Drawing.SystemColors.Control
            res = LW_SetLocal(DeviceId)
            res = LW_DeviceClose(DeviceId)
            Button1.Text = "OPEN"
            Button2.Enabled = False
            Button3.Enabled = False
            Button4.Enabled = False
            Button5.Enabled = False
            Button6.Enabled = False
            Button7.Enabled = False
            TextBox1.Text = ""
            DeviceId = 0
        End If
    End Sub

'Main Input
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        If Button2.Text = "ON" Then
            Button2.Text = "OFF"
            res = LW_SetMainInput(DeviceId, 1)
            Button2.BackColor = System.Drawing.Color.Red
        Else
            Button2.Text = "ON"
            res = LW_SetMainInput(DeviceId, 0)
            Button2.BackColor = System.Drawing.SystemColors.Control
        End If
    End Sub

'Preset1
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        res = LW_SetPresetNumber(DeviceId, 1)
    End Sub

'Preset2
    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        res = LW_SetPresetNumber(DeviceId, 2)
    End Sub

'Preset3
    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        res = LW_SetPresetNumber(DeviceId, 3)
    End Sub

'Preset4
    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        res = LW_SetPresetNumber(DeviceId, 4)
    End Sub

'Readback
    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click

		Dim Monitor As MONITOR_DATA

		res = LW_GetMonitor(DeviceId, 1, Monitor)

		TextBox1.Text = "V : " + Format(Monitor.Voltage, "#00.000 V") + vbCrLf _
		 + "C : " + Format(Monitor.Current, "#00.000 A") + vbCrLf _
		 + "P : " + Format(Monitor.Wattage, "#00.000 W")

		System.Windows.Forms.Application.DoEvents()

	End Sub

    Private Sub API_test()

        Dim Data As Double
        Dim LData As Long
        Dim IData As Long
        Dim Monitor As MONITOR_DATA
        Dim ModeData As MODE_DATA
        Dim Time As TIME_DATA
        Dim Track As TRACK_DATA
        Dim Alarm As ALARM_DATA
        Dim Limit As LIMIT_DATA
        Dim ChData As CH_DATA

		res = LW_DeviceRefresh(DeviceId)
        res = LW_SetTimeOut(DeviceId, 1)
        res = LW_SetMainInput(DeviceId, XON)
        res = LW_GetMainInput(DeviceId)
        res = LW_SetInputSelect(DeviceId, Ch_A, XON)
        res = LW_GetInputSelect(DeviceId, Ch_A)
        res = LW_SetPresetNumber(DeviceId, PR_1)
        res = LW_GetPresetNumber(DeviceId)
        res = LW_SavePreset(DeviceId, PR_1, PR_2)
        res = LW_GetMonitor(DeviceId, Ch_A, Monitor)
        res = LW_SetMonitorSelect(DeviceId, WATT)
        res = LW_GetMonitorSelect(DeviceId)
        res = LW_SetDisplaySelect(DeviceId, Ch_A)
        res = LW_GetDisplaySelect(DeviceId)
        res = LW_SetLoadMode(DeviceId, PR_1, Ch_A, M_CCL, REF_IN)
        res = LW_GetLoadMode(DeviceId, PR_1, Ch_A, ModeData)
        res = LW_SetLoadValue(DeviceId, PR_1, Ch_A, 1.1)
        res = LW_GetLoadValue(DeviceId, PR_1, Ch_A, Data)

        res = LW_SetMainInput(DeviceId, XOFF)
        res = LW_SetInputSelect(DeviceId, Ch_A, XOFF)
        res = LW_SetLoadMode(DeviceId, PR_1, Ch_A, M_CRH, REF_IN)
        res = LW_SetSiemensValue(DeviceId, PR_1, Ch_A, 30000)
        res = LW_GetSiemensValue(DeviceId, PR_1, Ch_A, LData)
        res = LW_SetCurrentLimit(DeviceId, PR_1, Ch_A, 1.2)
        res = LW_GetCurrentLimit(DeviceId, PR_1, Ch_A, Data)
        res = LW_SetLoadMode(DeviceId, PR_1, Ch_A, M_CCL, REF_IN)
        res = LW_SetMainInput(DeviceId, XON)
        res = LW_SetInputSelect(DeviceId, Ch_A, XON)

        res = LW_SaveLoadValue(DeviceId, PR_1, Ch_A)
        res = LW_SaveCurrentLimit(DeviceId, PR_1, Ch_A)
        res = LW_SaveMemoryData(DeviceId, OBJ_TAB)
        res = LW_SetSwSelect(DeviceId, Ch_A, XOFF)
        res = LW_GetSwSelect(DeviceId, Ch_A)
        res = LW_SetSwFreq(DeviceId, 100)
        res = LW_GetSwFreq(DeviceId, IData)
        res = LW_SetSwDuty(DeviceId, 56)
        res = LW_GetSwDuty(DeviceId, IData)
        res = LW_SetSwTime(DeviceId, 400.1, 300.1)
        res = LW_GetSwTime(DeviceId, Time)
        res = LW_SetSwMethod(DeviceId, SW_FREQ)
        res = LW_GetSwMethod(DeviceId)
        res = LW_SetDelayFunction(DeviceId, XOFF)
        res = LW_GetDelayFunction(DeviceId)
        res = LW_SetDelayTime(DeviceId, Ch_A, 2.1)
        res = LW_GetDelayTime(DeviceId, Ch_A, Data)
        res = LW_SetTrackFunction(DeviceId, XOFF)
        res = LW_GetTrackFunction(DeviceId)
        res = LW_SetTrackDirection(DeviceId, TRC_PLUS, TRC_MINUS, TRC_NO, TRC_NO)
        res = LW_GetTrackDirection(DeviceId, Track)
        res = LW_SetTrackMode(DeviceId, TRC_PERCENT)
        res = LW_GetTrackMode(DeviceId)
        res = LW_SetTrackValue(DeviceId, 110.0)
        res = LW_GetTrackValue(DeviceId, Data)
        res = LW_ResetAlarm(DeviceId)
        res = LW_GetAlarm(DeviceId, Alarm)
        res = LW_GetLimit(DeviceId, Limit)
        res = LW_SetOperationTimeOut(DeviceId, 5)
        res = LW_GetOperationTimeOut(DeviceId)
        res = LW_SetLocalLockOut(DeviceId, LOCK_FREE)
        res = LW_GetLocalLockOut(DeviceId)
        res = LW_GetIdNumber(DeviceId)
        res = LW_GetChExist(DeviceId, ChData)

    End Sub

End Class
