﻿Public Class Form1
    Dim Id As Short                                          ' デバイスID
    Dim Ret As Integer                                       ' 戻り値
    Dim szError As New System.Text.StringBuilder("", 256)    ' エラー文字列
    Dim szText As New String("", 100)

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

        '-----------------------------------
        ' デバイスの検出
        '-----------------------------------
        Dim Index As Short
        Dim GroupID, UnitID, DeviceID As Short
        Dim DeviceName As New System.Text.StringBuilder("", 256)
        Dim Device As New System.Text.StringBuilder("", 256)
        Index = 0
        Do
            Ret = DioQueryDeviceName(Index, GroupID, UnitID, DeviceID, DeviceName, Device)
            If Ret <> DIO_ERR_SUCCESS Then
                Exit Do
            End If
            cboDeviceName.Items.Add(DeviceName.ToString())
            Index += 1
        Loop
        cboDeviceName.SelectedIndex = 0
        '-----------------------------------
        ' 画面アイテムの初期化
        '-----------------------------------
        txtInpPortNo.Text = "0"
        txtInpBitNo.Text = "0"
        txtOutPortNo.Text = "0"
        txtOutBitNo.Text = "0"
        txtOutPortData.Text = "55"
        txtOutBitData.Text = "1"
        txtReturnCode.Text = ""
        txtInpPortData.Text = ""
        txtInpBitData.Text = ""

    End Sub

    '---------------------------------------------------------
    ' 初期化処理
    '---------------------------------------------------------
    Private Sub btnDioInit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDioInit.Click

        Dim szDeviceName As New String("", 100)
        '-----------------------------
        ' 画面からデバイス名を取得する
        '-----------------------------
        szDeviceName = cboDeviceName.Text
        '-----------------------------
        ' 初期化処理を行ない、IDを取得する
        '-----------------------------
        Ret = DioInit(szDeviceName, Id)
        '-----------------------------
        ' エラー処理
        '-----------------------------
        DioGetErrorString(Ret, szError)
        txtReturnCode.Text = "DioInit : " & szError.ToString()

    End Sub

    '---------------------------------------------------------
    ' １ポート入力
    '---------------------------------------------------------
    Private Sub btnInpByte_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInpByte.Click

        Dim InpPortNo As Short
        Dim InpPortData As Byte
        '-----------------------------
        ' 画面からポート番号を取得する
        '-----------------------------
        InpPortNo = Val(txtInpPortNo.Text)
        '-----------------------------
        ' ポート入力
        '-----------------------------
        Ret = DioInpByte(Id, InpPortNo, InpPortData)
        '-----------------------------
        ' エラー処理
        '-----------------------------
        DioGetErrorString(Ret, szError)
        txtReturnCode.Text = "DioInpByte : " & szError.ToString()
        If Ret <> DIO_ERR_SUCCESS Then
            Exit Sub
        End If
        '-----------------------------
        ' データ表示
        '-----------------------------
        txtInpPortData.Text = String.Format("{0:X2}", InpPortData)

    End Sub

    '---------------------------------------------------------
    ' １ビット入力
    '---------------------------------------------------------
    Private Sub btnInpBit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInpBit.Click

        Dim InpBitNo As Short
        Dim InpBitData As Byte
        '-----------------------------
        ' 画面からビット番号を取得する
        '-----------------------------
        InpBitNo = Val(txtInpBitNo.Text)
        '-----------------------------
        ' ビット入力
        '-----------------------------
        Ret = DioInpBit(Id, InpBitNo, InpBitData)
        '-----------------------------
        ' エラー処理
        '-----------------------------
        DioGetErrorString(Ret, szError)
        txtReturnCode.Text = "DioInpBit : " & szError.ToString()
        If Ret <> DIO_ERR_SUCCESS Then
            Exit Sub
        End If
        '-----------------------------
        ' データ表示
        '-----------------------------
        txtInpBitData.Text = String.Format("{0:X}", InpBitData)

    End Sub

    '---------------------------------------------------------
    ' １ポート出力
    '---------------------------------------------------------
    Private Sub btnOutByte_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOutByte.Click

        Dim OutPortNo As Short
        Dim OutPortData As Byte
        '-----------------------------
        ' 画面からポート番号と出力データを取得する
        '-----------------------------
        szText = txtOutPortNo.Text
        OutPortNo = Val(szText)
        szText = txtOutPortData.Text
        OutPortData = Val("&H" & szText)
        '-----------------------------
        ' ポート出力
        '-----------------------------
        Ret = DioOutByte(Id, OutPortNo, OutPortData)
        '-----------------------------
        ' エラー処理
        '-----------------------------
        DioGetErrorString(Ret, szError)
        txtReturnCode.Text = "DioOutByte : " & szError.ToString()
        If Ret <> DIO_ERR_SUCCESS Then
            Exit Sub
        End If

    End Sub

    '---------------------------------------------------------
    ' １ビット出力
    '---------------------------------------------------------
    Private Sub btnOutBit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOutBit.Click

        Dim OutBitNo As Short
        Dim OutBitData As Byte
        '-----------------------------
        ' 画面からビット番号と出力データを取得する
        '-----------------------------
        szText = txtOutBitNo.Text
        OutBitNo = Val(szText)
        szText = txtOutBitData.Text
        OutBitData = Val("&H" & szText)
        '-----------------------------
        ' ビット出力
        '-----------------------------
        Ret = DioOutBit(Id, OutBitNo, OutBitData)
        '-----------------------------
        ' エラー処理
        '-----------------------------
        DioGetErrorString(Ret, szError)
        txtReturnCode.Text = "DioOutBit : " & szError.ToString()
        If Ret <> DIO_ERR_SUCCESS Then
            Exit Sub
        End If

    End Sub

    '---------------------------------------------------------
    ' １ポートエコーバック
    '---------------------------------------------------------
    Private Sub btnEchoBackByte_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEchoBackByte.Click

        Dim OutPortNo As Short
        Dim OutPortData As Byte
        '-----------------------------
        ' 画面からポート番号を取得する
        '-----------------------------
        OutPortNo = Val(txtOutPortNo.Text)
        '-----------------------------
        ' ポートエコーバック
        '-----------------------------
        Ret = DioEchoBackByte(Id, OutPortNo, OutPortData)
        '-----------------------------
        ' エラー処理
        '-----------------------------
        DioGetErrorString(Ret, szError)
        txtReturnCode.Text = "DioEchoBackByte : " & szError.ToString()
        If Ret <> DIO_ERR_SUCCESS Then
            Exit Sub
        End If
        '-----------------------------
        ' データ表示
        '-----------------------------
        txtOutPortData.Text = String.Format("{0:X2}", OutPortData)

    End Sub

    '---------------------------------------------------------
    ' １ビットエコーバック
    '---------------------------------------------------------
    Private Sub btnEchoBackBit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEchoBackBit.Click

        Dim OutBitNo As Short
        Dim OutBitData As Byte
        '-----------------------------
        ' 画面からビット番号を取得する
        '-----------------------------
        OutBitNo = Val(txtOutBitNo.Text)
        '-----------------------------
        ' ビットエコーバック
        '-----------------------------
        Ret = DioEchoBackBit(Id, OutBitNo, OutBitData)
        '-----------------------------
        ' エラー処理
        '-----------------------------
        DioGetErrorString(Ret, szError)
        txtReturnCode.Text = "DioEchoBackBit : " & szError.ToString()
        If Ret <> DIO_ERR_SUCCESS Then
            Exit Sub
        End If
        '-----------------------------
        ' データ表示
        '-----------------------------
        txtOutBitData.Text = String.Format("{0:X}", OutBitData)

    End Sub

    '---------------------------------------------------------
    ' 終了処理
    '---------------------------------------------------------
    Private Sub btnDioExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDioExit.Click

        ' 終了処理を行う
        '-----------------------------
        Ret = DioExit(Id)
        '-----------------------------
        ' エラー処理
        '-----------------------------
        DioGetErrorString(Ret, szError)
        txtReturnCode.Text = "DioExit : " & szError.ToString()

    End Sub

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

        '-----------------------------
        ' 終了処理を行う
        '-----------------------------
        Ret = DioExit(Id)
        '-----------------------------
        ' ダイアログを終了する
        '-----------------------------
        End

    End Sub

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

        ' 終了処理を行う
        Ret = DioExit(Id)

    End Sub
End Class
