﻿Imports System.Text

Public Class Form1
    '----------------------------------------
    ' グローバル変数の宣言
    '----------------------------------------
    Dim g_id As Short    ' デバイスアクセス用ID

    Private Sub DigitalIO_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        '----------------------------------------
        ' デフォルトボタンを設定
        '----------------------------------------
        AcceptButton = Button_Close
        '----------------------------------------
        ' デバイス名の初期表示を更新
        '----------------------------------------
        TextBox_DeviceName.Text = "AIO000"
        '----------------------------------------
        ' デジタルI/Oポートの入出力方向の初期表示を更新
        '----------------------------------------
        TextBox_IoDirection.Text = "0"
        '----------------------------------------
        ' 指定デジタル入力ポート番号の初期表示を更新
        '----------------------------------------
        TextBox_InputByteNo.Text = "0"
        '----------------------------------------
        ' 指定デジタル入力ポートデータの初期表示を更新
        '----------------------------------------
        TextBox_InputByteData.Text = "00"
        '----------------------------------------
        ' 指定デジタル入力ビット番号の初期表示を更新
        '----------------------------------------
        TextBox_InputBitNo.Text = "0"
        '----------------------------------------
        ' 指定デジタル入力ビットデータの初期表示を更新
        '----------------------------------------
        TextBox_InputBitData.Text = "0"
        '----------------------------------------
        ' 指定デジタル出力ポート番号の初期表示を更新
        '----------------------------------------
        TextBox_OutputByteNo.Text = "0"
        '----------------------------------------
        ' 指定デジタル出力ポートデータの初期表示を更新
        '----------------------------------------
        TextBox_OutputByteData.Text = "00"
        '----------------------------------------
        ' 指定デジタル出力ビット番号の初期表示を更新
        '----------------------------------------
        TextBox_OutputBitNo.Text = "0"
        '----------------------------------------
        ' 指定デジタル出力ビットデータの初期表示を更新
        '----------------------------------------
        TextBox_OutputBitData.Text = "0"
        '----------------------------------------
        ' グローバル変数の初期化
        '----------------------------------------
        g_id = 0
    End Sub

    '================================================================================
    ' AioInitボタンをクリックした際の処理になります
    ' デバイスの初期化処理を行います
    '================================================================================
    Private Sub Button_AioInit_Click(sender As Object, e As EventArgs) Handles Button_AioInit.Click
        Dim device_name As String                         ' デバイス名
        Dim error_string As New StringBuilder("", 256)    ' エラーコード文字列
        Dim ret As Integer                                ' 戻り値取得用

        '----------------------------------------
        ' デバイス名をテキストボックスから取得
        '----------------------------------------
        device_name = TextBox_DeviceName.Text
        '----------------------------------------
        ' デバイス初期化
        ' 変更注意関数(変更するとアプリが動作しなくなる可能性があります)
        '----------------------------------------
        ret = AioInit(device_name, g_id)
        '----------------------------------------
        ' エラー文字列取得
        '----------------------------------------
        AioGetErrorString(ret, error_string)
        '----------------------------------------
        ' メッセージ表示
        '----------------------------------------
        TextBox_ReturnCode.Text = "AioInit = " & ret & "：" & error_string.ToString()
    End Sub

    '================================================================================
    ' AioResetDeviceボタンをクリックした際の処理になります
    ' デバイスのリセット、ドライバの初期化を行います
    '================================================================================
    Private Sub Button_AioResetDevice_Click(sender As Object, e As EventArgs) Handles Button_AioResetDevice.Click
        Dim error_string As New StringBuilder("", 256)    ' エラーコード文字列
        Dim ret As Integer                                ' 戻り値取得用

        '----------------------------------------
        ' デバイスのリセット、ドライバの初期化
        '----------------------------------------
        ret = AioResetDevice(g_id)
        '----------------------------------------
        ' エラー文字列取得
        '----------------------------------------
        AioGetErrorString(ret, error_string)
        '----------------------------------------
        ' メッセージ表示
        '----------------------------------------
        TextBox_ReturnCode.Text = "AioResetDevice = " & ret & "：" & error_string.ToString()
    End Sub

    '================================================================================
    ' AioSetDioDirectionボタンをクリックした際の処理になります
    ' デジタルI/Oポートの入出力方向を設定します
    '================================================================================
    Private Sub Button_SetDioDirection_Click(sender As Object, e As EventArgs) Handles Button_SetDioDirection.Click
        Dim error_string As New StringBuilder("", 256)    ' エラーコード文字列
        Dim ret As Integer                                ' 戻り値取得用
        Dim io_direction As Integer                       ' 入出力方向

        '----------------------------------------
        ' テキストボックスから入出力方向を取得
        '----------------------------------------
        If (Integer.TryParse(TextBox_IoDirection.Text, io_direction) = False) Then
            io_direction = 0
        End If
        '----------------------------------------
        ' デジタルI/Oポートの入出力方向を設定
        '----------------------------------------
        ret = AioSetDioDirection(g_id, io_direction)
        '----------------------------------------
        ' エラー文字列取得
        '----------------------------------------
        AioGetErrorString(ret, error_string)
        '----------------------------------------
        ' メッセージ表示
        '----------------------------------------
        TextBox_ReturnCode.Text = "AioSetDioDirection = " & ret & "：" & error_string.ToString()
    End Sub

    '================================================================================
    ' AioInputDiByteボタンをクリックした際の処理になります
    ' 指定デジタル入力ポートからバイト単位でデータを入力します
    '================================================================================
    Private Sub Button_InputDiByte_Click(sender As Object, e As EventArgs) Handles Button_InputDiByte.Click
        Dim error_string As New StringBuilder("", 256)    ' エラーコード文字列
        Dim text_string As String                         ' テキストボックス更新用
        Dim ret As Integer                                ' 戻り値取得用
        Dim di_port As Short                              ' デジタル入力ポート番号
        Dim di_data As Short                              ' 入力データ

        '----------------------------------------
        ' テキストボックスからポート番号を取得
        '----------------------------------------
        If (Short.TryParse(TextBox_InputByteNo.Text, di_port) = False) Then
            di_port = 0
        End If
        '----------------------------------------
        ' 指定ポートデータを取得
        '----------------------------------------
        ret = AioInputDiByte(g_id, di_port, di_data)
        '----------------------------------------
        ' エラー処理
        '----------------------------------------
        '--------------------
        ' エラー文字列取得
        '--------------------
        AioGetErrorString(ret, error_string)
        '--------------------
        ' メッセージ表示
        '--------------------
        TextBox_ReturnCode.Text = "AioInputDiByte = " & ret & "：" & error_string.ToString()
        '--------------------
        ' 正常終了でなければ処理を抜ける
        '--------------------
        If (ret <> 0) Then
            Exit Sub
        End If
        '----------------------------------------
        ' 指定ポートデータを表示
        '----------------------------------------
        text_string = String.Format("{0:x2}", di_data)
        '--------------------
        ' 変換データ表示を更新
        '--------------------
        TextBox_InputByteData.Text = text_string
    End Sub

    '================================================================================
    ' AioInputDiBitボタンをクリックした際の処理になります
    ' 指定デジタル入力ビットからデータを入力します
    '================================================================================
    Private Sub Button_InputDiBit_Click(sender As Object, e As EventArgs) Handles Button_InputDiBit.Click
        Dim error_string As New StringBuilder("", 256)    ' エラーコード文字列
        Dim text_string As String                         ' テキストボックス更新用
        Dim ret As Integer                                ' 戻り値取得用
        Dim di_bit As Short                               ' デジタル入力ビット番号
        Dim di_data As Short                              ' 入力データ

        '----------------------------------------
        ' テキストボックスからビット番号を取得
        '----------------------------------------
        If (Short.TryParse(TextBox_InputBitNo.Text, di_bit) = False) Then
            di_bit = 0
        End If
        '----------------------------------------
        ' 指定ビットデータを取得
        '----------------------------------------
        ret = AioInputDiBit(g_id, di_bit, di_data)
        '----------------------------------------
        ' エラー処理
        '----------------------------------------
        '--------------------
        ' エラー文字列取得
        '--------------------
        AioGetErrorString(ret, error_string)
        '--------------------
        ' メッセージ表示
        '--------------------
        TextBox_ReturnCode.Text = "AioInputDiBit = " & ret & "：" & error_string.ToString()
        '--------------------
        ' 正常終了でなければ処理を抜ける
        '--------------------
        If (ret <> 0) Then
            Exit Sub
        End If
        '----------------------------------------
        ' 指定ビットデータを表示
        '----------------------------------------
        text_string = String.Format("{0:d}", di_data)
        '--------------------
        ' 変換データ表示を更新
        '--------------------
        TextBox_InputBitData.Text = text_string
    End Sub

    '================================================================================
    ' AioOutputDoByteボタンをクリックした際の処理になります
    ' 指定デジタル出力バイトにデータを出力します
    '================================================================================
    Private Sub Button_OutputDoByte_Click(sender As Object, e As EventArgs) Handles Button_OutputDoByte.Click
        Dim error_string As New StringBuilder("", 256)    ' エラーコード文字列
        Dim ret As Integer                                ' 戻り値取得用
        Dim do_port As Short                              ' デジタル出力ポート番号
        Dim do_data As Short                              ' 出力データ

        '----------------------------------------
        ' テキストボックスからポート番号を取得
        '----------------------------------------
        If (Short.TryParse(TextBox_OutputByteNo.Text, do_port) = False) Then
            do_port = 0
        End If
        '----------------------------------------
        ' テキストボックスから出力データを取得
        '----------------------------------------
        Try
            do_data = Convert.ToInt16(TextBox_OutputByteData.Text, 16)
        Catch
            do_data = 0
        End Try
        '----------------------------------------
        ' 指定デジタル出力バイトにデータを出力
        '----------------------------------------
        ret = AioOutputDoByte(g_id, do_port, do_data)
        '----------------------------------------
        ' エラー文字列取得
        '----------------------------------------
        AioGetErrorString(ret, error_string)
        '----------------------------------------
        ' メッセージ表示
        '----------------------------------------
        TextBox_ReturnCode.Text = "AioOutputDoByte = " & ret & "：" & error_string.ToString()
    End Sub

    '================================================================================
    ' AioOutputDoBitボタンをクリックした際の処理になります
    ' 指定デジタル出力ビットにデータを出力します
    '================================================================================
    Private Sub Button_OutputDoBit_Click(sender As Object, e As EventArgs) Handles Button_OutputDoBit.Click
        Dim error_string As New StringBuilder("", 256)    ' エラーコード文字列
        Dim ret As Integer                                ' 戻り値取得用
        Dim do_bit As Short                               ' デジタル出力ビット番号
        Dim do_data As Short                              ' 出力データ

        '----------------------------------------
        ' テキストボックスからビット番号を取得
        '----------------------------------------
        If (Short.TryParse(TextBox_OutputBitNo.Text, do_bit) = False) Then
            do_bit = 0
        End If
        '----------------------------------------
        ' テキストボックスから出力データを取得
        '----------------------------------------
        If (Short.TryParse(TextBox_OutputBitData.Text, do_data) = False) Then
            do_data = 0
        End If
        '----------------------------------------
        ' 指定デジタル出力ビットにデータを出力
        '----------------------------------------
        ret = AioOutputDoBit(g_id, do_bit, do_data)
        '----------------------------------------
        ' エラー文字列取得
        '----------------------------------------
        AioGetErrorString(ret, error_string)
        '----------------------------------------
        ' メッセージ表示
        '----------------------------------------
        TextBox_ReturnCode.Text = "AioOutputDoBit = " & ret & "：" & error_string.ToString()
    End Sub

    '================================================================================
    ' AioExitボタンをクリックした際の処理になります
    ' デバイスの終了処理を行います
    '================================================================================
    Private Sub Button_AioExit_Click(sender As Object, e As EventArgs) Handles Button_AioExit.Click
        Dim error_string As New StringBuilder("", 256)    ' エラーコード文字列
        Dim ret As Integer                                ' 戻り値取得用

        '----------------------------------------
        ' 終了処理の実行
        '----------------------------------------
        ret = AioExit(g_id)
        '----------------------------------------
        ' エラー文字列取得
        '----------------------------------------
        AioGetErrorString(ret, error_string)
        '----------------------------------------
        ' メッセージ表示
        '----------------------------------------
        TextBox_ReturnCode.Text = "AioExit = " & ret & "：" & error_string.ToString()
    End Sub

    '================================================================================
    ' 閉じるボタンをクリックした際の処理になります
    ' デバイスの終了処理を行い、アプリケーションを終了します
    '================================================================================
    Private Sub Button_Close_Click(sender As Object, e As EventArgs) Handles Button_Close.Click
        '----------------------------------------
        ' 終了処理の実行
        '----------------------------------------
        AioExit(g_id)
        '----------------------------------------
        ' ダイアログを終了する
        '----------------------------------------
        Me.Close()
    End Sub
End Class
