﻿Option Strict Off
Option Explicit On

Public Class Form1
    Dim Id As Short                                     ' Device ID
    Dim Ret As Integer                                  ' Return value
    Dim szError As New System.Text.StringBuilder(256)   ' Error string
    Dim szText As New String("", 100)

    '---------------------------------------------------------
    ' Initialize dialog
    '---------------------------------------------------------
    Private Sub SimpleIO_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
        '-----------------------------------
        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

    '---------------------------------------------------------
    ' Initialization
    '---------------------------------------------------------
    Private Sub btnDioInit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDioInit.Click

        Dim szDeviceName As New String("", 100)
        '-----------------------------
        ' 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()

    End Sub

    '---------------------------------------------------------
    ' 1 port input
    '---------------------------------------------------------
    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
        '-----------------------------
        ' Get port No. from screen
        '-----------------------------
        InpPortNo = Val(txtInpPortNo.Text)
        '-----------------------------
        ' Port input
        '-----------------------------
        Ret = DioInpByte(Id, InpPortNo, InpPortData)
        '-----------------------------
        ' Process error
        '-----------------------------
        DioGetErrorString(Ret, szError)
        txtReturnCode.Text = "DioInpByte : " & szError.ToString()
        If Ret <> DIO_ERR_SUCCESS Then
            Exit Sub
        End If
        '-----------------------------
        ' Display data
        '-----------------------------
        txtInpPortData.Text = String.Format("{0:X2}", InpPortData)

    End Sub

    '---------------------------------------------------------
    ' 1 bit input
    '---------------------------------------------------------
    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
        '-----------------------------
        ' Get bit No. from screen
        '-----------------------------
        InpBitNo = Val(txtInpBitNo.Text)
        '-----------------------------
        ' Bit input
        '-----------------------------
        Ret = DioInpBit(Id, InpBitNo, InpBitData)
        '-----------------------------
        ' Process error
        '-----------------------------
        DioGetErrorString(Ret, szError)
        txtReturnCode.Text = "DioInpBit : " & szError.ToString()
        If Ret <> DIO_ERR_SUCCESS Then
            Exit Sub
        End If
        '-----------------------------
        ' Display data
        '-----------------------------
        txtInpBitData.Text = String.Format("{0:X}", InpBitData)

    End Sub

    '---------------------------------------------------------
    ' 1 port output
    '---------------------------------------------------------
    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
        '-----------------------------
        ' Get the port No. and the output data from screen
        '-----------------------------
        szText = txtOutPortNo.Text
        OutPortNo = Val(szText)
        szText = txtOutPortData.Text
        OutPortData = Val("&H" & szText)
        '-----------------------------
        ' Port output
        '-----------------------------
        Ret = DioOutByte(Id, OutPortNo, OutPortData)
        '-----------------------------
        ' Process error
        '-----------------------------
        DioGetErrorString(Ret, szError)
        txtReturnCode.Text = "DioOutByte : " & szError.ToString()
        If Ret <> DIO_ERR_SUCCESS Then
            Exit Sub
        End If

    End Sub

    '---------------------------------------------------------
    ' 1 bit output
    '---------------------------------------------------------
    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
        '-----------------------------
        ' Get the bit No. and the output data from screen
        '-----------------------------
        szText = txtOutBitNo.Text
        OutBitNo = Val(szText)
        szText = txtOutBitData.Text
        OutBitData = Val("&H" & szText)
        '-----------------------------
        ' Bit output
        '-----------------------------
        Ret = DioOutBit(Id, OutBitNo, OutBitData)
        '-----------------------------
        ' Process error
        '-----------------------------
        DioGetErrorString(Ret, szError)
        txtReturnCode.Text = "DioOutBit : " & szError.ToString()
        If Ret <> DIO_ERR_SUCCESS Then
            Exit Sub
        End If

    End Sub

    '---------------------------------------------------------
    ' 1 port EchoBack
    '---------------------------------------------------------
    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
        '-----------------------------
        ' Get port No. from screen
        '-----------------------------
        OutPortNo = Val(txtOutPortNo.Text)
        '-----------------------------
        ' Port EchoBack
        '-----------------------------
        Ret = DioEchoBackByte(Id, OutPortNo, OutPortData)
        '-----------------------------
        ' Process error
        '-----------------------------
        DioGetErrorString(Ret, szError)
        txtReturnCode.Text = "DioEchoBackByte : " & szError.ToString()
        If Ret <> DIO_ERR_SUCCESS Then
            Exit Sub
        End If
        '-----------------------------
        ' Display data
        '-----------------------------
        txtOutPortData.Text = String.Format("{0:X2}", OutPortData)

    End Sub

    '---------------------------------------------------------
    ' 1 Bit EchoBack
    '---------------------------------------------------------
    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
        '-----------------------------
        ' Get bit No. from screen
        '-----------------------------
        OutBitNo = Val(txtOutBitNo.Text)
        '-----------------------------
        ' Bit EchoBack
        '-----------------------------
        Ret = DioEchoBackBit(Id, OutBitNo, OutBitData)
        '-----------------------------
        ' Process error
        '-----------------------------
        DioGetErrorString(Ret, szError)
        txtReturnCode.Text = "DioEchoBackBit : " & szError.ToString()
        If Ret <> DIO_ERR_SUCCESS Then
            Exit Sub
        End If
        '-----------------------------
        ' Display data
        '-----------------------------
        txtOutBitData.Text = String.Format("{0:X}", OutBitData)

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

        ' Termination
        Ret = DioExit(Id)

    End Sub
End Class
