﻿Imports System.Text

Public Class Form1
    '----------------------------------------
    ' マクロ定義
    '----------------------------------------
    Public Const MAX_AI_RANGE_NUM As Integer = 32    ' アナログ入力レンジ配列最大要素数

    '----------------------------------------
    ' グローバル変数の宣言
    '----------------------------------------
    Dim g_id As Short                                                         ' デバイスアクセス用ID
    Dim g_ai_range_index As Long                                              ' レンジのインデックス
    Dim g_ai_range() As Short = New Short(MAX_AI_RANGE_NUM - 1) {PM10,        ' ±10V
                                                                 PM5,         ' ±5V
                                                                 PM25,        ' ±2.5V
                                                                 PM125,       ' ±1.25V
                                                                 PM1,         ' ±1V
                                                                 PM0625,      ' ±0.625V
                                                                 PM05,        ' ±0.5V
                                                                 PM03125,     ' ±0.3125V
                                                                 PM025,       ' ±0.25V
                                                                 PM0125,      ' ±0.125V
                                                                 PM01,        ' ±0.1V
                                                                 PM005,       ' ±0.05V
                                                                 PM0025,      ' ±0.025V
                                                                 PM00125,     ' ±0.0125V
                                                                 PM001,       ' ±0.01V
                                                                 P10,         ' 0～10V
                                                                 P5,          ' 0～5V
                                                                 P4095,       ' 0～4.095V
                                                                 P25,         ' 0～2.5V
                                                                 P125,        ' 0～1.25V
                                                                 P1,          ' 0～1V
                                                                 P05,         ' 0～0.5V
                                                                 P025,        ' 0～0.25V
                                                                 P01,         ' 0～0.1V
                                                                 P005,        ' 0～0.05V
                                                                 P0025,       ' 0～0.025V
                                                                 P00125,      ' 0～0.0125V
                                                                 P001,        ' 0～0.01V
                                                                 P20MA,       ' 0～20mA
                                                                 P4TO20MA,    ' 4～20mA
                                                                 PM20MA,      ' ±20mA
                                                                 P1TO5}       ' 1～5V

    Private Sub AiSimple_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        '----------------------------------------
        ' デフォルトボタンを設定
        '----------------------------------------
        AcceptButton = Button_Close
        '----------------------------------------
        ' デバイス名の初期表示を更新
        '----------------------------------------
        TextBox_DeviceName.Text = "AIO000"
        '----------------------------------------
        ' 指定チャネル入力のチャネル番号の初期表示を更新
        '----------------------------------------
        TextBox_ChNo.Text = "0"
        '----------------------------------------
        ' 複数チャネル入力のチャネル数の初期表示を更新
        '----------------------------------------
        TextBox_ChNum.Text = "1"
        '----------------------------------------
        ' アナログ入力レンジ用コンボボックスの作成
        '----------------------------------------
        ComboBox_RangeValue.Items.Add("-10 - +10V")
        ComboBox_RangeValue.Items.Add("-5 - +5V")
        ComboBox_RangeValue.Items.Add("-2.5 - +2.5V")
        ComboBox_RangeValue.Items.Add("-1.25 - +1.25V")
        ComboBox_RangeValue.Items.Add("-1 - +1V")
        ComboBox_RangeValue.Items.Add("-0.625 - +0.625V")
        ComboBox_RangeValue.Items.Add("-0.5 - +0.5V")
        ComboBox_RangeValue.Items.Add("-0.3125 - +0.3125V")
        ComboBox_RangeValue.Items.Add("-0.25 - +0.25V")
        ComboBox_RangeValue.Items.Add("-0.125 - +0.125V")
        ComboBox_RangeValue.Items.Add("-0.1 - +0.1V")
        ComboBox_RangeValue.Items.Add("-0.05 - +0.05V")
        ComboBox_RangeValue.Items.Add("-0.025 - +0.025V")
        ComboBox_RangeValue.Items.Add("-0.0125 - +0.0125V")
        ComboBox_RangeValue.Items.Add("-0.01 - +0.01V")
        ComboBox_RangeValue.Items.Add("0 - +10V")
        ComboBox_RangeValue.Items.Add("0 - +5V")
        ComboBox_RangeValue.Items.Add("0 - +4.095V")
        ComboBox_RangeValue.Items.Add("0 - +2.5V")
        ComboBox_RangeValue.Items.Add("0 - +1.25V")
        ComboBox_RangeValue.Items.Add("0 - +1V")
        ComboBox_RangeValue.Items.Add("0 - +0.5V")
        ComboBox_RangeValue.Items.Add("0 - +0.25V")
        ComboBox_RangeValue.Items.Add("0 - +0.1V")
        ComboBox_RangeValue.Items.Add("0 - +0.05V")
        ComboBox_RangeValue.Items.Add("0 - +0.025V")
        ComboBox_RangeValue.Items.Add("0 - +0.0125V")
        ComboBox_RangeValue.Items.Add("0 - +0.01V")
        ComboBox_RangeValue.Items.Add("0 - +20mA")
        ComboBox_RangeValue.Items.Add("4 - +20mA")
        ComboBox_RangeValue.Items.Add("-20 - +20mA")
        ComboBox_RangeValue.Items.Add("1 - +5V")
        '----------------------------------------
        ' アナログ入力レンジ用コンボボックスの選択を0番に変更
        '----------------------------------------
        ComboBox_RangeValue.SelectedIndex = 0
        '----------------------------------------
        ' アナログ入力方式用コンボボックスの作成
        '----------------------------------------
        ComboBox_InpMethod.Items.Add("シングルエンド入力")
        ComboBox_InpMethod.Items.Add("差動入力")
        '----------------------------------------
        ' アナログ入力方式用コンボボックスの選択を0番に変更
        '----------------------------------------
        ComboBox_InpMethod.SelectedIndex = 0
        '----------------------------------------
        ' グローバル変数の初期化
        '----------------------------------------
        g_id = 0
        g_ai_range_index = 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

    '================================================================================
    ' AioSetAiRangeAllボタンをクリックした際の処理になります
    ' アナログ入力の入力レンジの設定を行います
    '================================================================================
    Private Sub Button_SetAiRangeAll_Click(sender As Object, e As EventArgs) Handles Button_SetAiRangeAll.Click
        Dim error_string As New StringBuilder("", 256)    ' エラーコード文字列
        Dim ret As Integer                                ' 戻り値取得用

        '----------------------------------------
        ' GUIの情報を更新
        '----------------------------------------
        Me.Refresh()
        '----------------------------------------
        ' コンボボックスからレンジ設定値のインデックスを取得
        '----------------------------------------
        g_ai_range_index = ComboBox_RangeValue.SelectedIndex
        '----------------------------------------
        ' アナログ入力レンジの設定(全チャネル)
        '----------------------------------------
        ret = AioSetAiRangeAll(g_id, g_ai_range(g_ai_range_index))
        '----------------------------------------
        ' エラー文字列取得
        '----------------------------------------
        AioGetErrorString(ret, error_string)
        '----------------------------------------
        ' メッセージ表示
        '----------------------------------------
        TextBox_ReturnCode.Text = "AioSetAiRangeAll = " & ret & "：" & error_string.ToString()
    End Sub

    '================================================================================
    ' AioSetAiInputMethodボタンをクリックした際の処理になります
    ' アナログ入力の入力方式の設定を行います
    '================================================================================
    Private Sub Button_SetAiInpMethod_Click(sender As Object, e As EventArgs) Handles Button_SetAiInpMethod.Click
        Dim error_string As New StringBuilder("", 256)    ' エラーコード文字列
        Dim ret As Integer                                ' 戻り値取得用
        Dim input_method As Integer                       ' 入力方式

        '----------------------------------------
        ' GUIの情報を更新
        '----------------------------------------
        Me.Refresh()
        '----------------------------------------
        ' コンボボックスから入力方式設定値を取得
        '----------------------------------------
        input_method = ComboBox_InpMethod.SelectedIndex
        '----------------------------------------
        ' アナログ入力方式の設定
        '----------------------------------------
        ret = AioSetAiInputMethod(g_id, input_method)
        '----------------------------------------
        ' エラー文字列取得
        '----------------------------------------
        AioGetErrorString(ret, error_string)
        '----------------------------------------
        ' メッセージ表示
        '----------------------------------------
        TextBox_ReturnCode.Text = "AioSetAiInputMethod = " & ret & "：" & error_string.ToString()
    End Sub

    '================================================================================
    ' AioSingleAiExボタンをクリックした際の処理になります
    ' 入力データ(電圧または電流)を指定チャネルから1回入力します
    '================================================================================
    Private Sub Button_SingleAiEx_Click(sender As Object, e As EventArgs) Handles Button_SingleAiEx.Click
        Dim error_string As New StringBuilder("", 256)    ' エラーコード文字列
        Dim text_string As String                         ' テキストボックス更新用
        Dim ret As Integer                                ' 戻り値取得用
        Dim ai_data As Single                             ' 入力データ
        Dim ai_channel As Short                           ' チャネル番号

        '----------------------------------------
        ' テキストボックスからチャネル番号を取得
        '----------------------------------------
        If (Short.TryParse(TextBox_ChNo.Text, ai_channel) = False) Then
            ai_channel = 0
        End If
        '----------------------------------------
        ' 指定チャネルのAD変換データを取得
        '----------------------------------------
        ret = AioSingleAiEx(g_id, ai_channel, ai_data)
        '----------------------------------------
        ' エラー処理
        '----------------------------------------
        '--------------------
        ' エラー文字列取得
        '--------------------
        AioGetErrorString(ret, error_string)
        '--------------------
        ' メッセージ表示
        '--------------------
        TextBox_ReturnCode.Text = "AioSingleAiEx = " & ret & "：" & error_string.ToString()
        '--------------------
        ' 正常終了でなければ処理を抜ける
        '--------------------
        If (ret <> 0) Then
            Exit Sub
        End If
        '----------------------------------------
        ' 指定チャネルのAD変換データを表示
        '----------------------------------------
        If ((g_ai_range(g_ai_range_index) = P20MA) Or (g_ai_range(g_ai_range_index) = P4TO20MA) Or (g_ai_range(g_ai_range_index) = PM20MA)) Then
            text_string = String.Format("{0:F5}mA", ai_data)
        Else
            text_string = String.Format("{0:F5}V", ai_data)
        End If
        '--------------------
        ' 変換データ表示を更新
        '--------------------
        TextBox_SingleAiData.Text = text_string
    End Sub

    '================================================================================
    ' AioMultiAiExボタンをクリックした際の処理になります
    ' 入力データ(電圧または電流)を複数チャネルから1回入力します
    '================================================================================
    Private Sub Button_MultiAiEx_Click(sender As Object, e As EventArgs) Handles Button_MultiAiEx.Click
        Dim error_string As New StringBuilder("", 256)    ' エラーコード文字列
        Dim text_string As String                         ' テキストボックス更新用
        Dim ret As Integer                                ' 戻り値取得用
        Dim ai_data() As Single                           ' 入力データ
        Dim i As Short                                    ' 入力データ表示ループ用
        Dim ai_channels As Short                          ' チャネル数

        '----------------------------------------
        ' テキストボックスからチャネル数を取得
        '----------------------------------------
        If (Short.TryParse(TextBox_ChNum.Text, ai_channels) = False) Then
            ai_channels = 1
        End If
        '----------------------------------------
        ' AD変換データ格納配列の確保
        '----------------------------------------
        Try
            ReDim ai_data(ai_channels)
        '----------------------------------------
        ' AD変換データ格納配列の確保に失敗した場合
        '----------------------------------------
        Catch ex As Exception
            TextBox_ReturnCode.Text = "AD変換データ格納配列の確保に失敗しました"
            Exit Sub
        End Try
        '----------------------------------------
        ' 複数チャネルのAD変換データを取得
        '----------------------------------------
        ret = AioMultiAiEx(g_id, ai_channels, ai_data)
        '----------------------------------------
        ' エラー処理
        '----------------------------------------
        '--------------------
        ' エラー文字列取得
        '--------------------
        AioGetErrorString(ret, error_string)
        '--------------------
        ' メッセージ表示
        '--------------------
        TextBox_ReturnCode.Text = "AioMultiAiEx = " & ret & "：" & error_string.ToString()
        '--------------------
        ' 正常終了でなければ処理を抜ける
        '--------------------
        If (ret <> 0) Then
            '--------------------
            ' AD変換データ格納配列の解放
            '--------------------
            Erase ai_data
            Exit Sub
        End If
        '----------------------------------------
        ' AD変換データを表示
        '----------------------------------------
        '--------------------
        ' AD変換データ表示用ListBoxをクリア
        '--------------------
        ListBox_MultiInpData.Items.Clear()
        '--------------------
        ' 設定チャネル数まで処理を繰り返す
        '--------------------
        For i = 0 To ai_channels - 1
            '--------------------
            ' 表示データの生成
            '--------------------
            If ((g_ai_range(g_ai_range_index) = P20MA) Or (g_ai_range(g_ai_range_index) = P4TO20MA) Or (g_ai_range(g_ai_range_index) = PM20MA)) Then
                text_string = String.Format("{0,3}              {1:F5}mA", i, ai_data(i))
            Else
                text_string = String.Format("{0,3}              {1:F5}V", i, ai_data(i))
            End If
            '--------------------
            ' 変換データ表示を更新
            '--------------------
            ListBox_MultiInpData.Items.Add(text_string)
        Next i
        '----------------------------------------
        ' AD変換データ格納配列の解放
        '----------------------------------------
        Erase ai_data
    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
