﻿Public Class Form1
    Dim m_Id As Short    'ID

    ' Initialize dialog
    Private Sub Pti4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        ' Get the list of devices registered
        Dim ret As Integer
        Dim device As New System.Text.StringBuilder("", 50)
        Dim devicename As New System.Text.StringBuilder("", 50)
        Dim str As New String("", 100)
        Dim index As Short
        Dim group_id, unit_id, device_id As Short
        index = 0
        Do
            ret = PtQueryDeviceName(index, group_id, unit_id, device_id, devicename, device)
            If ret = 0 Then
                str = String.Format("{0} [{1}]", devicename, device)
                cboDevice.Items.Add(str)
                index += 1
            End If
        Loop While (ret = 0)
        cboDevice.SelectedIndex = 0

        txtCh0.Text = "0"
        txtCh1.Text = "0"
        txtCh2.Text = "0"
        txtCh3.Text = "0"

    End Sub

    ' Click the [Initialization] button
    Private Sub btnInit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInit.Click

        Dim ret As Integer
        Dim devicename As New System.Text.StringBuilder("", 50)
        Dim tmp As New System.Text.StringBuilder("", 50)
        Dim index As Short
        Dim group_id, unit_id, device_id As Short
        ' Detect device
        index = cboDevice.SelectedIndex
        ret = PtQueryDeviceName(index, group_id, unit_id, device_id, devicename, tmp)
        If ret <> 0 Then
            ErrorHandling("PtQueryDeviceName", ret)
            Exit Sub
        End If

        ' Initialization
        ret = PtInit(devicename.ToString(), m_Id)
        If ret <> 0 Then
            ErrorHandling("PtInit", ret)
            Exit Sub
        End If

        txtRet.Text = "Initialization : Successed"
        ShowRemoteStatus()

    End Sub

    ' Click the [Measurement Setting] button
    Private Sub btnMSet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMSet.Click

        Dim measureDlg As New measure()

        ' Invoke the Measurement Setting dialog
        measureDlg.SetId(m_Id)
        measureDlg.ShowDialog()
        measureDlg.Dispose()

    End Sub

    ' Click the [Alarm Setting] button
    Private Sub btnASet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnASet.Click

        Dim alarmDlg As New alarm()

        ' Invoke the Alarm Setting dialog
        alarmDlg.SetId(m_Id)
        alarmDlg.ShowDialog()
        alarmDlg.Dispose()

    End Sub

    ' Click the [Start] button
    Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click

        '-----------------------------------
        ' Start
        '-----------------------------------
        Dim ret As Integer
        ret = PtStartIO(m_Id, &H1)
        '-----------------------------------
        ' Process error
        '-----------------------------------
        If ret <> 0 Then
            ErrorHandling("PtStartIO", ret)
            Exit Sub
        End If
        txtRet.Text = "Start : Successed"
        ShowRemoteStatus()

    End Sub

    ' Click the [Stop] button
    Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click

        '-----------------------------------
        ' Stop
        '-----------------------------------
        Dim ret As Integer
        ret = PtStartIO(m_Id, &H0)
        '-----------------------------------
        ' Process error
        '-----------------------------------
        If ret <> 0 Then
            ErrorHandling("PtStartIO", ret)
            Exit Sub
        End If
        txtRet.Text = "Stop : Successed"
        ShowRemoteStatus()

    End Sub

    ' Click the [Input Value] button
    Private Sub btnInput_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInput.Click

        Dim ret As Integer
        Dim data(3) As Single

        ' Input the temperature or capacity value for 4 channels
        ret = PtMultiInputEx(m_Id, 4, data)
        If ret <> 0 Then
            ErrorHandling("PtMultiInputEx", ret)
            If ret <> 20102 Then
                Exit Sub
            End If
        End If

        txtCh0.Text = data(0)
        txtCh1.Text = data(1)
        txtCh2.Text = data(2)
        txtCh3.Text = data(3)

        txtRet.Text = "Input Value : Successed"

    End Sub

    ' Click the [Status] button
    Private Sub btnStatus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStatus.Click

        Dim ret As Integer
        Dim disconnectstatus, upperstatus, lowerstatus As Integer
        Dim str As New String("", 32)

        ' Get the disconnect status
        ret = PtGetDisconnectStatus(m_Id, disconnectstatus)
        If ret <> 0 Then
            ErrorHandling("PtGetDisconnectStatus", ret)
            Exit Sub
        End If

        ' Get the alarm status
        ret = PtGetCurrentAlarmStatus(m_Id, upperstatus, lowerstatus)
        If ret <> 0 Then
            ErrorHandling("PtGetCurrentAlarmStatus", ret)
            Exit Sub
        End If

        str = String.Format("{0:x}", disconnectstatus)
        txtStsDc.Text = str
        str = String.Format("{0:x}", upperstatus)
        txtStsUAlarm.Text = str
        str = String.Format("{0:x}", lowerstatus)
        txtStsLAlarm.Text = str

        txtRet.Text = "Getting Status : Successed"

    End Sub

    ' Click the [Termination] button
    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click

        Dim ret As Integer

        ' Termination
        ret = PtExit(m_Id)
        If ret <> 0 Then
            ErrorHandling("PtExit", ret)
            Exit Sub
        End If

        txtRet.Text = "Termination : Successed"

    End Sub

    ' Exit dialog
    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click

        End

    End Sub

    ' Process error
    Private Sub ErrorHandling(ByVal Dfunction As String, ByVal ret As Integer)

        Dim errstr As New System.Text.StringBuilder("", 256)
        Dim str As New String("", 256)
        PtGetErrorString(ret, errstr)
        str = String.Format("[{0}] {1}", Dfunction, errstr)
        txtRet.Text = str

    End Sub

    ' Display remote status
    Private Sub ShowRemoteStatus()

        Dim Start, Err As Byte
        Dim szText As New String("", 100)
        Dim ret As Integer

        ' Input remote status
        ret = PtGetRemoteStatus(m_Id, Start, Err)
        ' Process error
        If ret <> 0 Then
            ErrorHandling("PtGetRemoteStatus", ret)
            Exit Sub
        End If
        ' Display remote status
        szText = String.Format("{0:X2}", Start)
        lblStart.Text = szText
        szText = String.Format("{0:X2}", Err)
        lblErr.Text = szText

    End Sub

    Private Sub Pti4_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed

        Dim ret As Integer

        ' Termination
        ret = PtExit(m_Id)

    End Sub
End Class
