﻿Public Class Form1
    Private Const DIO_MAX_ACCS_PORTS As Short = 8       ' Maxinum number of access ports in this sample
    Private Const DIO_MAX_ACCS_BITS As Short = 8        ' Maxinum number of access bits in this sample

    Dim Id As Short                                     ' Device ID
    Dim Ret As Integer                                  ' Return value
    Dim InpPortNum As Short                             ' Number of input ports
    Dim OutPortNum As Short                             ' Number of output ports
    Dim InpBitNum As Short                              ' Number of input bits
    Dim OutBitNum As Short                              ' Number of output bits
    Dim szError As New System.Text.StringBuilder(256)   ' Error string
    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

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

        '-----------------------------------
        ' Detect device
        '-----------------------------------
        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
        '-----------------------------------
        ' Initialize screen items
        '-----------------------------------
        ' Port input
        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
        ' Bit input
        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
        ' Port output
        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
        ' Bit output
        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
        ' Return value
        txtReturnCode.Text = ""

    End Sub

    '---------------------------------------------------------
    ' Initialization
    '---------------------------------------------------------
    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
        '-----------------------------
        ' Get device name from screen
        '-----------------------------
        szDeviceName = cboDeviceName.Text
        '-----------------------------
        ' Initialize and get ID
        '-----------------------------
        Ret = DioInit(szDeviceName, Id)
        '-----------------------------
        ' Process error
        '-----------------------------
        DioGetErrorString(Ret, szError)
        txtReturnCode.Text = "DioInit : " & szError.ToString()
        If Ret <> DIO_ERR_SUCCESS Then
            Exit Sub
        End If
        '-----------------------------
        ' Get maximum number of ports
        '-----------------------------
        Ret = DioGetMaxPorts(Id, InpPortNum, OutPortNum)
        If Ret <> DIO_ERR_SUCCESS Then
            DioGetErrorString(Ret, szError)
            txtReturnCode.Text = "DioGetMaxPorts : " & szError.ToString()
            Exit Sub
        End If
        ' Up to 8 ports in this sample
        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
        '-----------------------------
        ' Get maximum number of bits
        '-----------------------------
        Ret = DioGetMaxBits(Id, InpBitNum, OutBitNum)
        If Ret <> DIO_ERR_SUCCESS Then
            DioGetErrorString(Ret, szError)
            txtReturnCode.Text = "DioGetMaxBits : " & szError.ToString()
            Exit Sub
        End If
        ' Up to 8 bits in this sample
        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
        '-----------------------------
        ' Mask screen
        '-----------------------------
        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

    '---------------------------------------------------------
    ' MultiPort input
    '---------------------------------------------------------
    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
        '-----------------------------
        ' Set port No.
        '-----------------------------
        For i = 0 To (InpPortNum - 1)
            InpPortNo(i) = i
        Next i
        '-----------------------------
        ' Port input
        '-----------------------------
        Ret = DioInpMultiByte(Id, InpPortNo, InpPortNum, InpPortData)
        '-----------------------------
        ' Process error
        '-----------------------------
        DioGetErrorString(Ret, szError)
        txtReturnCode.Text = "DioInpMultiByte : " & szError.ToString()
        If Ret <> DIO_ERR_SUCCESS Then
            Exit Sub
        End If
        '-----------------------------
        ' Display data
        '-----------------------------
        For i = 0 To (InpPortNum - 1)
            txtInpPort(i).Text = String.Format("{0:X2}", InpPortData(i))
        Next

    End Sub

    '---------------------------------------------------------
    ' MultiBit input
    '---------------------------------------------------------
    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
        '-----------------------------
        ' Set bit No.
        '-----------------------------
        For i = 0 To (InpBitNum - 1)
            InpBitNo(i) = i
        Next i
        '-----------------------------
        ' Bit input
        '-----------------------------
        Ret = DioInpMultiBit(Id, InpBitNo, InpBitNum, InpBitData)
        '-----------------------------
        ' Process error
        '-----------------------------
        DioGetErrorString(Ret, szError)
        txtReturnCode.Text = "DioInpMultiBit : " & szError.ToString()
        If Ret <> DIO_ERR_SUCCESS Then
            Exit Sub
        End If
        '-----------------------------
        ' Display data
        '-----------------------------
        For i = 0 To (InpBitNum - 1)
            txtInpBit(i).Text = String.Format("{0:X}", InpBitData(i))
        Next i

    End Sub

    '---------------------------------------------------------
    ' MultiPort output
    '---------------------------------------------------------
    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
        '-----------------------------
        ' Set port No. and port data
        '-----------------------------
        For i = 0 To (OutPortNum - 1)
            OutPortNo(i) = i
            szText = txtOutPort(i).Text
            OutPortData(i) = Val("&H" & szText)
        Next i
        '-----------------------------
        ' Port output
        '-----------------------------
        Ret = DioOutMultiByte(Id, OutPortNo, OutPortNum, OutPortData)
        '-----------------------------
        ' Process error
        '-----------------------------
        DioGetErrorString(Ret, szError)
        txtReturnCode.Text = "DioOutMultiByte : " & szError.ToString()
        If Ret <> DIO_ERR_SUCCESS Then
            Exit Sub
        End If

    End Sub

    '---------------------------------------------------------
    ' MultiBit output
    '---------------------------------------------------------
    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
        '-----------------------------
        ' Set bit No. and bit data
        '-----------------------------
        For i = 0 To (OutBitNum - 1)
            OutBitNo(i) = i
            szText = txtOutBit(i).Text
            OutBitData(i) = Val("&H" & szText)
        Next i
        '-----------------------------
        ' Bit output
        '-----------------------------
        Ret = DioOutMultiBit(Id, OutBitNo, OutBitNum, OutBitData)
        '-----------------------------
        ' Process error
        '-----------------------------
        DioGetErrorString(Ret, szError)
        txtReturnCode.Text = "DioOutMultiBit : " & szError.ToString()
        If Ret <> DIO_ERR_SUCCESS Then
            Exit Sub
        End If

    End Sub

    '---------------------------------------------------------
    ' Termination
    '---------------------------------------------------------
    Private Sub btnDioExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDioExit.Click

        '-----------------------------
        ' Termination
        '-----------------------------
        Ret = DioExit(Id)
        '-----------------------------
        ' Process error
        '-----------------------------
        DioGetErrorString(Ret, szError)
        txtReturnCode.Text = "DioExit : " & szError.ToString()

    End Sub

    '---------------------------------------------------------
    ' Exit dialog
    '---------------------------------------------------------
    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click

        '-----------------------------
        ' Termination
        '-----------------------------
        Ret = DioExit(Id)
        '-----------------------------
        ' Exit dialog
        '-----------------------------
        End

    End Sub

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

        ' Termination
        Ret = DioExit(Id)

    End Sub
End Class
