﻿Option Strict Off
Option Explicit On

Public Class Form1
    Dim m_Id As Short    'ID

    ' ダイアログの初期化
    Private Sub Pti4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        ' 登録されているデバイス一覧を取得
        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
                cboDevice.SelectedIndex = 0
            End If
        Loop While (ret = 0)

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

    End Sub

    ' [初期化処理]ボタンクリック
    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
        ' デバイスの決定
        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

        ' 初期化処理
        ret = PtInit(devicename.ToString(), m_Id)
        If ret <> 0 Then
            ErrorHandling("PtInit", ret)
            Exit Sub
        End If

        txtRet.Text = "初期化処理：正常終了"
        ShowRemoteStatus()

    End Sub

    ' [計測設定]ボタンクリック
    Private Sub btnMSet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMSet.Click

        Dim measureDlg As New measure()

        ' 計測設定ダイアログを起動
        measureDlg.SetId(m_Id)
        measureDlg.ShowDialog()
        measureDlg.Dispose()

    End Sub

    ' [アラーム設定]ボタンクリック
    Private Sub btnASet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnASet.Click

        Dim alarmDlg As New alarm()

        ' アラーム設定ダイアログを起動
        alarmDlg.SetId(m_Id)
        alarmDlg.ShowDialog()
        alarmDlg.Dispose()

    End Sub

    ' [起動]ボタンクリック
    Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click

        '-----------------------------------
        ' 起動
        '-----------------------------------
        Dim ret As Integer
        ret = PtStartIO(m_Id, &H1)
        '-----------------------------------
        ' エラー処理
        '-----------------------------------
        If ret <> 0 Then
            ErrorHandling("PtStartIO", ret)
            Exit Sub
        End If
        txtRet.Text = "起動：正常終了"
        ShowRemoteStatus()

    End Sub

    ' [停止]ボタンクリック
    Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click

        '-----------------------------------
        ' 停止
        '-----------------------------------
        Dim ret As Integer
        ret = PtStartIO(m_Id, &H0)
        '-----------------------------------
        ' エラー処理
        '-----------------------------------
        If ret <> 0 Then
            ErrorHandling("PtStartIO", ret)
            Exit Sub
        End If
        txtRet.Text = "停止：正常終了"
        ShowRemoteStatus()

    End Sub

    ' [入力値]ボタンクリック
    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

        ' 4チャネル分の温度または抵抗値を入力
        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 = "入力処理：正常終了"

    End Sub

    ' [ステータス]ボタンクリック
    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)

        ' 断線状態を取得
        ret = PtGetDisconnectStatus(m_Id, disconnectstatus)
        If ret <> 0 Then
            ErrorHandling("PtGetDisconnectStatus", ret)
            Exit Sub
        End If

        ' アラーム状態を取得
        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 = "ステータス取得：正常終了"

    End Sub

    ' [終了処理]ボタンクリック
    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click

        Dim ret As Integer

        ' 終了処理
        ret = PtExit(m_Id)
        If ret <> 0 Then
            ErrorHandling("PtExit", ret)
            Exit Sub
        End If

        txtRet.Text = "終了処理：正常終了"

    End Sub

    ' ダイアログ終了
    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click

        End

    End Sub

    ' エラー処理
    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

    ' リモートステータスの表示
    Private Sub ShowRemoteStatus()

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

        ' リモートステータス入力
        ret = PtGetRemoteStatus(m_Id, Start, Err)
        ' エラー処理
        If ret <> 0 Then
            ErrorHandling("PtGetRemoteStatus", ret)
            Exit Sub
        End If
        ' リモートステータス表示
        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

        ' 終了処理
        ret = PtExit(m_Id)

    End Sub
End Class
