Option Strict Off
Option Explicit On

Friend Class PSF_Access
    Inherits System.Windows.Forms.Form
    '/***********************************************************************/
    '/*                                                                     */
    '/*     IF-60　API_Test  for Visual Basic .net 2005                     */
    '/*       Interface    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

            ' 使用するインターフェースを選択
            Select Case (Combo1.Text)
                Case "USB"
                    IF_Sel = IF_USB
                Case "COM"
                    IF_Sel = IF_COM1
            End Select

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

            ' デバイスオープン
            DeviceId = PSF_DeviceOpen(PcAddr)

            If DeviceId > 0 Then

                ' アドレス設定
                res = PSF_Send(PcAddr, SYSAddr)

                ' OUTPUT OFF
                res = PSF_Send(PcAddr, ":OUTP 0")

                ' 表示切り替え CHA-電圧-電流
                res = PSF_Send(PcAddr, ":Conf:disp1")

                ' 画面表示
                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
            res = PSF_Send(PcAddr, ":outp 0")

            ' ローカル状態
            res = PSF_Send(PcAddr, ":Remote0")

            ' アドレス設定 マスタ
            res = PSF_Send(PcAddr, ":ADDR1")

            ' ローカル設定
            res = PSF_Send(PcAddr, ":Remote0")

            ' クローズ
            res = PSF_DeviceClose(PcAddr)

            ' 画面表示
            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に設定
            res = PSF_Send(PcAddr, ":outp1")

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

            ' OutputをOFFに設定
            res = PSF_Send(PcAddr, ":outp0")

            ' 表示の更新
            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を選択
        res = PSF_Send(PcAddr, ":pres:call 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を選択
        res = PSF_Send(PcAddr, ":pres:call 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を選択
        res = PSF_Send(PcAddr, ":pres:call 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 0を選択
        res = PSF_Send(PcAddr, ":pres:call 0")
    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 String
        Dim meas_value As Object

        bbuf = Space(64)

        ' モニタ要求
        res = PSF_Send(PcAddr, ":meas?")

        Sleep(200)

        ' モニタ値受信
        res = PSF_Recive(PcAddr, 64, bbuf)

        ' モニタ値はカンマ区切りで電圧、電流、電力、ステータス
        meas_value = Split(bbuf, ",")
        If UBound(meas_value) > 2 Then
            Text1.Text = Str(meas_value(0)) & "V" & vbCrLf & Str(meas_value(1)) & "A" & vbCrLf & Str(meas_value(2)) & "W" & vbCrLf & Str(meas_value(3)) & "  0:CV/1:CC/2:CP"
        End If
        System.Windows.Forms.Application.DoEvents()
    End Sub

    ' フォームロード
    Private Sub PSF_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(("COM"))

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

    ' フォームクローズ
    Private Sub PSF_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 IF_Sel <> 0 Then
            ' OUTPUT OFF
            res = PSF_Send(PcAddr, ":outp 0")

            ' ローカル状態
            res = PSF_Send(PcAddr, ":Remote0")

            ' アドレス設定　マスタ
            res = PSF_Send(PcAddr, ":ADDR1")

            ' ローカル状態
            res = PSF_Send(PcAddr, ":Remote0")

            ' クローズ
            res = PSF_DeviceClose(PcAddr)
        End If
        eventArgs.Cancel = Cancel
    End Sub



    '/***********************************************************************/
    '/*     IF-60　Interface Select                                         */
    '/***********************************************************************/

    Private Function PSF_DeviceOpen(ByRef addr As Integer) As Integer
        Try
            Select Case (IF_Sel)
                Case IF_USB
                    PSF_DeviceOpen = USB488_DeviceOpen(addr)

                Case IF_COM1
                    SerialPort1.PortName = "COM" + addr.ToString()
                    SerialPort1.BaudRate = 57600
                    SerialPort1.DataBits = 8
                    SerialPort1.Parity = System.IO.Ports.Parity.None
                    SerialPort1.StopBits = System.IO.Ports.StopBits.One
                    SerialPort1.Handshake = System.IO.Ports.Handshake.RequestToSend
                    SerialPort1.RtsEnable = True
                    SerialPort1.ReadTimeout = 3000
                    SerialPort1.WriteTimeout = 500
                    SerialPort1.Open()
                    SerialPort1.DiscardInBuffer()
                    PSF_DeviceOpen = 1

            End Select
        Catch caught As Exception
            MessageBox.Show(caught.Message, "Open Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            PSF_DeviceOpen = -1
        End Try
    End Function

    Private Function PSF_DeviceClose(ByRef addr As Integer) As Integer

        Select Case (IF_Sel)
            Case IF_USB
                PSF_DeviceClose = USB488_DeviceClose(addr)

            Case IF_COM1
                SerialPort1.Close()
                PSF_DeviceClose = 0

        End Select
    End Function

    Private Function PSF_SetRemote(ByRef addr As Integer, ByRef rMode As Integer) As Integer

        Select Case (IF_Sel)
            Case IF_USB
                PSF_SetRemote = USB488_SetRemote(addr, rMode)

            Case IF_GPIB0
                PSF_SetRemote = 0

            Case IF_COM1
                PSF_SetRemote = 0
        End Select
    End Function

    Private Function PSF_Send(ByRef addr As Integer, ByRef strBuf As String) As Integer
        Try
            Select Case (IF_Sel)
                Case IF_USB
                    PSF_Send = USB488_Send(addr, strBuf)

                Case IF_COM1
                    SerialPort1.WriteLine(strBuf)
                    PSF_Send = strBuf.Length
            End Select
        Catch caught As Exception
            MessageBox.Show(caught.Message, "Send Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            PSF_Send = -1
        End Try
    End Function

    Private Function PSF_Recive(ByRef addr As Integer, ByRef Count As Integer, ByRef strBuf As String) As Integer
        strBuf = Space(Count + 1)

        Try
            Select Case (IF_Sel)
                Case IF_USB
                    PSF_Recive = USB488_Receive(addr, Count, strBuf)
                    If PSF_Recive > 0 Then
                        strBuf = Strings.Left(strBuf, PSF_Recive)
                    End If

                Case IF_COM1
                    strBuf = SerialPort1.ReadLine
                    PSF_Recive = strBuf.Length
            End Select
        Catch caught As Exception
            MessageBox.Show(caught.Message, "Read Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            strBuf = ""
            PSF_Recive = -1
        End Try

    End Function


End Class