﻿Option Strict Off
Option Explicit On

Public Class Form1
    Private Const DIO_MAX_ACCS_PORTS As Short = 8            ' このサンプルの最大アクセスポート数
    Private Const DIO_MAX_ACCS_BITS As Short = 8             ' このサンプルの最大アクセスビット数

    Dim Id As Short                                          ' デバイスID
    Dim Ret As Integer                                       ' 戻り値
    Dim InpPortNum As Short                                  ' 入力ポート数
    Dim OutPortNum As Short                                  ' 出力ポート数
    Dim InpBitNum As Short                                   ' 入力ビット数
    Dim OutBitNum As Short                                   ' 出力ビット数
    Dim szError As New System.Text.StringBuilder("", 256)    ' エラー文字列
    Dim szText As New String("", 100)
    Dim txtInpPort(7) As TextBox
    Dim txtInpBit(7) As TextBox
    Dim txtOutPort(7) As TextBox
    Dim txtOutBit(7) As TextBox

    '---------------------------------------------------------
    ' ダイアログ初期化
    '---------------------------------------------------------
    Private Sub MultiIO_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
        '-----------------------------------
        ' 画面アイテムの初期化
        '-----------------------------------
        ' ポート入力
        txtInpPortData0.Text = ""
        txtInpPortData1.Text = ""
        txtInpPortData2.Text = ""
        txtInpPortData3.Text = ""
        txtInpPortData4.Text = ""
        txtInpPortData5.Text = ""
        txtInpPortData6.Text = ""
        txtInpPortData7.Text = ""
        txtInpPort(0) = txtInpPortData0
        txtInpPort(1) = txtInpPortData1
        txtInpPort(2) = txtInpPortData2
        txtInpPort(3) = txtInpPortData3
        txtInpPort(4) = txtInpPortData4
        txtInpPort(5) = txtInpPortData5
        txtInpPort(6) = txtInpPortData6
        txtInpPort(7) = txtInpPortData7
        ' ビット入力
        txtInpBitData0.Text = ""
        txtInpBitData1.Text = ""
        txtInpBitData2.Text = ""
        txtInpBitData3.Text = ""
        txtInpBitData4.Text = ""
        txtInpBitData5.Text = ""
        txtInpBitData6.Text = ""
        txtInpBitData7.Text = ""
        txtInpBit(0) = txtInpBitData0
        txtInpBit(1) = txtInpBitData1
        txtInpBit(2) = txtInpBitData2
        txtInpBit(3) = txtInpBitData3
        txtInpBit(4) = txtInpBitData4
        txtInpBit(5) = txtInpBitData5
        txtInpBit(6) = txtInpBitData6
        txtInpBit(7) = txtInpBitData7
        ' ポート出力
        txtOutPortData0.Text = "11"
        txtOutPortData1.Text = "22"
        txtOutPortData2.Text = "33"
        txtOutPortData3.Text = "44"
        txtOutPortData4.Text = "55"
        txtOutPortData5.Text = "66"
        txtOutPortData6.Text = "77"
        txtOutPortData7.Text = "88"
        txtOutPort(0) = txtOutPortData0
        txtOutPort(1) = txtOutPortData1
        txtOutPort(2) = txtOutPortData2
        txtOutPort(3) = txtOutPortData3
        txtOutPort(4) = txtOutPortData4
        txtOutPort(5) = txtOutPortData5
        txtOutPort(6) = txtOutPortData6
        txtOutPort(7) = txtOutPortData7
        ' ビット出力
        txtOutBitData0.Text = "1"
        txtOutBitData1.Text = "0"
        txtOutBitData2.Text = "1"
        txtOutBitData3.Text = "0"
        txtOutBitData4.Text = "1"
        txtOutBitData5.Text = "0"
        txtOutBitData6.Text = "1"
        txtOutBitData7.Text = "0"
        txtOutBit(0) = txtOutBitData0
        txtOutBit(1) = txtOutBitData1
        txtOutBit(2) = txtOutBitData2
        txtOutBit(3) = txtOutBitData3
        txtOutBit(4) = txtOutBitData4
        txtOutBit(5) = txtOutBitData5
        txtOutBit(6) = txtOutBitData6
        txtOutBit(7) = txtOutBitData7
        ' 戻り値
        txtReturnCode.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)
        Dim i As Short
        '-----------------------------
        ' 画面からデバイス名を取得する
        '-----------------------------
        szDeviceName = cboDeviceName.Text
        '-----------------------------
        ' 初期化処理を行ない、IDを取得する
        '-----------------------------
        Ret = DioInit(szDeviceName, Id)
        '-----------------------------
        ' エラー処理
        '-----------------------------
        DioGetErrorString(Ret, szError)
        txtReturnCode.Text = "DioInit : " & szError.ToString()
        If Ret <> DIO_ERR_SUCCESS Then
            Exit Sub
        End If
        '-----------------------------
        ' 全ポート数の取得
        '-----------------------------
        Ret = DioGetMaxPorts(Id, InpPortNum, OutPortNum)
        If Ret <> DIO_ERR_SUCCESS Then
            DioGetErrorString(Ret, szError)
            txtReturnCode.Text = "DioGetMaxPorts : " & szError.ToString()
            Exit Sub
        End If
        ' このサンプルは最大8ポートまで
        If InpPortNum > DIO_MAX_ACCS_PORTS Then
            InpPortNum = DIO_MAX_ACCS_PORTS
        End If
        If OutPortNum > DIO_MAX_ACCS_PORTS Then
            OutPortNum = DIO_MAX_ACCS_PORTS
        End If
        '-----------------------------
        ' 全ビット数の取得
        '-----------------------------
        Ret = DioGetMaxBits(Id, InpBitNum, OutBitNum)
        If Ret <> DIO_ERR_SUCCESS Then
            DioGetErrorString(Ret, szError)
            txtReturnCode.Text = "DioGetMaxBits : " & szError.ToString()
            Exit Sub
        End If
        ' このサンプルは最大8ビットまで
        If InpBitNum > DIO_MAX_ACCS_BITS Then
            InpBitNum = DIO_MAX_ACCS_BITS
        End If
        If OutBitNum > DIO_MAX_ACCS_BITS Then
            OutBitNum = DIO_MAX_ACCS_BITS
        End If
        '-----------------------------
        ' 画面をマスク
        '-----------------------------
        For i = 0 To (DIO_MAX_ACCS_PORTS - 1)
            If i < InpPortNum Then
                txtInpPort(i).Enabled = True
            Else
                txtInpPort(i).Enabled = False
            End If
        Next i
        For i = 0 To (DIO_MAX_ACCS_BITS - 1)
            If i < InpBitNum Then
                txtInpBit(i).Enabled = True
            Else
                txtInpBit(i).Enabled = False
            End If
        Next i
        For i = 0 To (DIO_MAX_ACCS_PORTS - 1)
            If i < OutPortNum Then
                txtOutPort(i).Enabled = True
            Else
                txtOutPort(i).Enabled = False
            End If
        Next i
        For i = 0 To (DIO_MAX_ACCS_BITS - 1)
            If i < OutBitNum Then
                txtOutBit(i).Enabled = True
            Else
                txtOutBit(i).Enabled = False
            End If
        Next i

    End Sub

    '---------------------------------------------------------
    ' 複数ポート入力
    '---------------------------------------------------------
    Private Sub btnInpMultiByte_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInpMultiByte.Click

        Dim InpPortNo(DIO_MAX_ACCS_PORTS - 1) As Short
        Dim InpPortData(DIO_MAX_ACCS_PORTS - 1) As Byte
        Dim i As Short
        '-----------------------------
        ' ポート番号をセットする
        '-----------------------------
        For i = 0 To (InpPortNum - 1)
            InpPortNo(i) = i
        Next i
        '-----------------------------
        ' ポート入力
        '-----------------------------
        Ret = DioInpMultiByte(Id, InpPortNo, InpPortNum, InpPortData)
        '-----------------------------
        ' エラー処理
        '-----------------------------
        DioGetErrorString(Ret, szError)
        txtReturnCode.Text = "DioInpMultiByte : " & szError.ToString()
        If Ret <> DIO_ERR_SUCCESS Then
            Exit Sub
        End If
        '-----------------------------
        ' データ表示
        '-----------------------------
        For i = 0 To (InpPortNum - 1)
            txtInpPort(i).Text = String.Format("{0:X2}", InpPortData(i))
        Next

    End Sub

    '---------------------------------------------------------
    ' 複数ビット入力
    '---------------------------------------------------------
    Private Sub btnInpMultiBit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInpMultiBit.Click

        Dim InpBitNo(DIO_MAX_ACCS_BITS - 1) As Short
        Dim InpBitData(DIO_MAX_ACCS_BITS - 1) As Byte
        Dim i As Short
        '-----------------------------
        ' ビット番号をセットする
        '-----------------------------
        For i = 0 To (InpBitNum - 1)
            InpBitNo(i) = i
        Next i
        '-----------------------------
        ' ビット入力
        '-----------------------------
        Ret = DioInpMultiBit(Id, InpBitNo, InpBitNum, InpBitData)
        '-----------------------------
        ' エラー処理
        '-----------------------------
        DioGetErrorString(Ret, szError)
        txtReturnCode.Text = "DioInpMultiBit : " & szError.ToString()
        If Ret <> DIO_ERR_SUCCESS Then
            Exit Sub
        End If
        '-----------------------------
        ' データ表示
        '-----------------------------
        For i = 0 To (InpBitNum - 1)
            txtInpBit(i).Text = String.Format("{0:X}", InpBitData(i))
        Next i

    End Sub

    '---------------------------------------------------------
    ' 複数ポート出力
    '---------------------------------------------------------
    Private Sub btnOutMultiByte_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOutMultiByte.Click

        Dim OutPortNo(DIO_MAX_ACCS_PORTS - 1) As Short
        Dim OutPortData(DIO_MAX_ACCS_PORTS - 1) As Byte
        Dim i As Short
        '-----------------------------
        ' ポート番号、ポートデータをセットする
        '-----------------------------
        For i = 0 To (OutPortNum - 1)
            OutPortNo(i) = i
            szText = txtOutPort(i).Text
            OutPortData(i) = Val("&H" & szText)
        Next i
        '-----------------------------
        ' ポート出力
        '-----------------------------
        Ret = DioOutMultiByte(Id, OutPortNo, OutPortNum, OutPortData)
        '-----------------------------
        ' エラー処理
        '-----------------------------
        DioGetErrorString(Ret, szError)
        txtReturnCode.Text = "DioOutMultiByte : " & szError.ToString()
        If Ret <> DIO_ERR_SUCCESS Then
            Exit Sub
        End If

    End Sub

    '---------------------------------------------------------
    ' 複数ビット出力
    '---------------------------------------------------------
    Private Sub btnOutMultiBit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOutMultiBit.Click

        Dim OutBitNo(DIO_MAX_ACCS_BITS - 1) As Short
        Dim OutBitData(DIO_MAX_ACCS_BITS - 1) As Byte
        Dim i As Short
        '-----------------------------
        ' ビット番号、ビットデータをセットする
        '-----------------------------
        For i = 0 To (OutBitNum - 1)
            OutBitNo(i) = i
            szText = txtOutBit(i).Text
            OutBitData(i) = Val("&H" & szText)
        Next i
        '-----------------------------
        ' ビット出力
        '-----------------------------
        Ret = DioOutMultiBit(Id, OutBitNo, OutBitNum, OutBitData)
        '-----------------------------
        ' エラー処理
        '-----------------------------
        DioGetErrorString(Ret, szError)
        txtReturnCode.Text = "DioOutMultiBit : " & szError.ToString()
        If Ret <> DIO_ERR_SUCCESS Then
            Exit Sub
        End If

    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 MultiIO_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed

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

    End Sub
End Class
