﻿Imports System.Text

Public Class Form1
    '----------------------------------------
    ' Declaration of global variables
    '----------------------------------------
    Dim g_id As Short    ' ID for device access

    Private Sub DigitalIO_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        '----------------------------------------
        ' Set the default button
        '----------------------------------------
        AcceptButton = Button_Close
        '----------------------------------------
        ' Update the initial display of device name
        '----------------------------------------
        TextBox_DeviceName.Text = "AIO000"
        '----------------------------------------
        ' Update the initial display of I/O direction of digital I/O ports
        '----------------------------------------
        TextBox_IoDirection.Text = "0"
        '----------------------------------------
        ' Update the initial display of the specified digital input port No.
        '----------------------------------------
        TextBox_InputByteNo.Text = "0"
        '----------------------------------------
        ' Update the initial display of the specified digital input port data
        '----------------------------------------
        TextBox_InputByteData.Text = "00"
        '----------------------------------------
        ' Update the initial display of the specified digital input bit No.
        '----------------------------------------
        TextBox_InputBitNo.Text = "0"
        '----------------------------------------
        ' Update the initial display of the specified digital input bit data
        '----------------------------------------
        TextBox_InputBitData.Text = "0"
        '----------------------------------------
        ' Update the initial display of the specified digital output port No.
        '----------------------------------------
        TextBox_OutputByteNo.Text = "0"
        '----------------------------------------
        ' Update the initial display of the specified digital output port data
        '----------------------------------------
        TextBox_OutputByteData.Text = "00"
        '----------------------------------------
        ' Update the initial display of the specified digital output bit No.
        '----------------------------------------
        TextBox_OutputBitNo.Text = "0"
        '----------------------------------------
        ' Update the initial display of the specified digital output bit data
        '----------------------------------------
        TextBox_OutputBitData.Text = "0"
        '----------------------------------------
        ' Initialize global variables
        '----------------------------------------
        g_id = 0
    End Sub

    '================================================================================
    ' It is the process when the AioInit button is clicked
    ' Initialize the device
    '================================================================================
    Private Sub Button_AioInit_Click(sender As Object, e As EventArgs) Handles Button_AioInit.Click
        Dim device_name As String                         ' Device name
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim ret As Integer                                ' For return code

        '----------------------------------------
        ' Get device name from textbox
        '----------------------------------------
        device_name = TextBox_DeviceName.Text
        '----------------------------------------
        ' Device initialization
        ' Caution for modify this function (if modified, the application may not work)
        '----------------------------------------
        ret = AioInit(device_name, g_id)
        '----------------------------------------
        ' Get error string
        '----------------------------------------
        AioGetErrorString(ret, error_string)
        '----------------------------------------
        ' Display message
        '----------------------------------------
        TextBox_ReturnCode.Text = "AioInit = " & ret & " : " & error_string.ToString()
    End Sub

    '================================================================================
    ' It is the process when the AioResetDevice button is clicked
    ' Reset the device
    '================================================================================
    Private Sub Button_AioResetDevice_Click(sender As Object, e As EventArgs) Handles Button_AioResetDevice.Click
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim ret As Integer                                ' For return code

        '----------------------------------------
        ' Reset device
        '----------------------------------------
        ret = AioResetDevice(g_id)
        '----------------------------------------
        ' Get error string
        '----------------------------------------
        AioGetErrorString(ret, error_string)
        '----------------------------------------
        ' Display message
        '----------------------------------------
        TextBox_ReturnCode.Text = "AioResetDevice = " & ret & " : " & error_string.ToString()
    End Sub

    '================================================================================
    ' It is the process when the AioSetDioDirection button is clicked
    ' Set I/O direction of digital I/O ports
    '================================================================================
    Private Sub Button_SetDioDirection_Click(sender As Object, e As EventArgs) Handles Button_SetDioDirection.Click
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim ret As Integer                                ' For return code
        Dim io_direction As Integer                       ' I/O direction

        '----------------------------------------
        ' Get I/O direction from textbox
        '----------------------------------------
        If (Integer.TryParse(TextBox_IoDirection.Text, io_direction) = False) Then
            io_direction = 0
        End If
        '----------------------------------------
        ' Set I/O direction of digital I/O ports
        '----------------------------------------
        ret = AioSetDioDirection(g_id, io_direction)
        '----------------------------------------
        ' Get error string
        '----------------------------------------
        AioGetErrorString(ret, error_string)
        '----------------------------------------
        ' Display message
        '----------------------------------------
        TextBox_ReturnCode.Text = "AioSetDioDirection = " & ret & " : " & error_string.ToString()
    End Sub

    '================================================================================
    ' It is the process when the AioInputDiByte button is clicked
    ' Input data in byte from the specified digital input port
    '================================================================================
    Private Sub Button_InputDiByte_Click(sender As Object, e As EventArgs) Handles Button_InputDiByte.Click
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim text_string As String                         ' For updating the textbox
        Dim ret As Integer                                ' For return code
        Dim di_port As Short                              ' Digital input port No.
        Dim di_data As Short                              ' Input data

        '----------------------------------------
        ' Get port No. from textbox
        '----------------------------------------
        If (Short.TryParse(TextBox_InputByteNo.Text, di_port) = False) Then
            di_port = 0
        End If
        '----------------------------------------
        ' Get specified port data
        '----------------------------------------
        ret = AioInputDiByte(g_id, di_port, di_data)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        '--------------------
        ' Get error string
        '--------------------
        AioGetErrorString(ret, error_string)
        '--------------------
        ' Display message
        '--------------------
        TextBox_ReturnCode.Text = "AioInputDiByte = " & ret & " : " & error_string.ToString()
        '--------------------
        ' Exit the process if not completed successfully
        '--------------------
        If (ret <> 0) Then
            Exit Sub
        End If
        '----------------------------------------
        ' Display specified port data
        '----------------------------------------
        text_string = String.Format("{0:x2}", di_data)
        '--------------------
        ' Update the display of conversion data
        '--------------------
        TextBox_InputByteData.Text = text_string
    End Sub

    '================================================================================
    ' It is the process when the AioInputDiBit button is clicked
    ' Input data from the specified digital input bit
    '================================================================================
    Private Sub Button_InputDiBit_Click(sender As Object, e As EventArgs) Handles Button_InputDiBit.Click
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim text_string As String                         ' For updating the textbox
        Dim ret As Integer                                ' For return code
        Dim di_bit As Short                               ' Digital input bit No.
        Dim di_data As Short                              ' Input data

        '----------------------------------------
        ' Get bit No. from textbox
        '----------------------------------------
        If (Short.TryParse(TextBox_InputBitNo.Text, di_bit) = False) Then
            di_bit = 0
        End If
        '----------------------------------------
        ' Get specified bit data
        '----------------------------------------
        ret = AioInputDiBit(g_id, di_bit, di_data)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        '--------------------
        ' Get error string
        '--------------------
        AioGetErrorString(ret, error_string)
        '--------------------
        ' Display message
        '--------------------
        TextBox_ReturnCode.Text = "AioInputDiBit = " & ret & " : " & error_string.ToString()
        '--------------------
        ' Exit the process if not completed successfully
        '--------------------
        If (ret <> 0) Then
            Exit Sub
        End If
        '----------------------------------------
        ' Display specified bit data
        '----------------------------------------
        text_string = String.Format("{0:d}", di_data)
        '--------------------
        ' Update the display of conversion data
        '--------------------
        TextBox_InputBitData.Text = text_string
    End Sub

    '================================================================================
    ' It is the process when the AioOutputDoByte button is clicked
    ' Output data to specified digital output byte
    '================================================================================
    Private Sub Button_OutputDoByte_Click(sender As Object, e As EventArgs) Handles Button_OutputDoByte.Click
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim ret As Integer                                ' For return code
        Dim do_port As Short                              ' Digital output port No.
        Dim do_data As Short                              ' Output data

        '----------------------------------------
        ' Get port No. from textbox
        '----------------------------------------
        If (Short.TryParse(TextBox_OutputByteNo.Text, do_port) = False) Then
            do_port = 0
        End If
        '----------------------------------------
        ' Get output data from textbox
        '----------------------------------------
        Try
            do_data = Convert.ToInt16(TextBox_OutputByteData.Text, 16)
        Catch
            do_data = 0
        End Try
        '----------------------------------------
        ' Output data to specified digital output byte
        '----------------------------------------
        ret = AioOutputDoByte(g_id, do_port, do_data)
        '----------------------------------------
        ' Get error string
        '----------------------------------------
        AioGetErrorString(ret, error_string)
        '----------------------------------------
        ' Display message
        '----------------------------------------
        TextBox_ReturnCode.Text = "AioOutputDoByte = " & ret & " : " & error_string.ToString()
    End Sub

    '================================================================================
    ' It is the process when the AioOutputDoBit button is clicked
    ' Output data to specified digital output bit
    '================================================================================
    Private Sub Button_OutputDoBit_Click(sender As Object, e As EventArgs) Handles Button_OutputDoBit.Click
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim ret As Integer                                ' For return code
        Dim do_bit As Short                               ' Digital output bit No.
        Dim do_data As Short                              ' Output data

        '----------------------------------------
        ' Get bit No. from textbox
        '----------------------------------------
        If (Short.TryParse(TextBox_OutputBitNo.Text, do_bit) = False) Then
            do_bit = 0
        End If
        '----------------------------------------
        ' Get output data from textbox
        '----------------------------------------
        If (Short.TryParse(TextBox_OutputBitData.Text, do_data) = False) Then
            do_data = 0
        End If
        '----------------------------------------
        ' Output data to specified digital output bit
        '----------------------------------------
        ret = AioOutputDoBit(g_id, do_bit, do_data)
        '----------------------------------------
        ' Get error string
        '----------------------------------------
        AioGetErrorString(ret, error_string)
        '----------------------------------------
        ' Display message
        '----------------------------------------
        TextBox_ReturnCode.Text = "AioOutputDoBit = " & ret & " : " & error_string.ToString()
    End Sub

    '================================================================================
    ' It is the process when the AioExit button is clicked
    ' Perform the exit process of device
    '================================================================================
    Private Sub Button_AioExit_Click(sender As Object, e As EventArgs) Handles Button_AioExit.Click
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim ret As Integer                                ' For return code

        '----------------------------------------
        ' Exit process
        '----------------------------------------
        ret = AioExit(g_id)
        '----------------------------------------
        ' Get error string
        '----------------------------------------
        AioGetErrorString(ret, error_string)
        '----------------------------------------
        ' Display message
        '----------------------------------------
        TextBox_ReturnCode.Text = "AioExit = " & ret & " : " & error_string.ToString()
    End Sub

    '================================================================================
    ' It is the process when the Close button is clicked
    ' Perform the exit process of device and terminate the application
    '================================================================================
    Private Sub Button_Close_Click(sender As Object, e As EventArgs) Handles Button_Close.Click
        '----------------------------------------
        ' Exit process
        '----------------------------------------
        AioExit(g_id)
        '----------------------------------------
        ' Close dialog
        '----------------------------------------
        Me.Close()
    End Sub
End Class
