﻿Imports System.Text

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

    '----------------------------------------
    ' グローバル変数の宣言
    '----------------------------------------
    Dim g_id As Short                                                         ' デバイスアクセス用ID
    Dim g_ao_range() As Short = New Short(MAX_AO_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
                                                                 P1TO5}       ' 1～5V

    Private Sub AoSimple_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        '----------------------------------------
        ' デフォルトボタンを設定
        '----------------------------------------
        AcceptButton = Button_Close
        '----------------------------------------
        ' デバイス名の初期表示を更新
        '----------------------------------------
        TextBox_DeviceName.Text = "AIO000"
        '----------------------------------------
        ' 指定チャネル出力のチャネル番号の初期表示を更新
        '----------------------------------------
        TextBox_ChNo.Text = "0"
        '----------------------------------------
        ' 指定チャネル出力のデータの初期表示を更新
        '----------------------------------------
        TextBox_SingleAoData.Text = "0.0"
        '----------------------------------------
        ' 複数チャネル出力のチャネル数の初期表示を更新
        '----------------------------------------
        TextBox_ChNum.Text = "1"
        '----------------------------------------
        ' 複数チャネル出力のデータの初期表示を更新
        '----------------------------------------
        TextBox_MultiAoDataCh0.Text = "0.0"
        TextBox_MultiAoDataCh1.Text = "0.0"
        TextBox_MultiAoDataCh2.Text = "0.0"
        TextBox_MultiAoDataCh3.Text = "0.0"
        '----------------------------------------
        ' アナログ出力レンジ用コンボボックスの作成
        '----------------------------------------
        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("1 - +5V")
        '----------------------------------------
        ' アナログ出力レンジ用コンボボックスの選択を0番に変更
        '----------------------------------------
        ComboBox_RangeValue.SelectedIndex = 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

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

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

    '================================================================================
    ' AioSingleAoExボタンをクリックした際の処理になります
    ' 出力データ(電圧または電流)を指定チャネルから1回出力します
    '================================================================================
    Private Sub Button_SingleAoEx_Click(sender As Object, e As EventArgs) Handles Button_SingleAoEx.Click
        Dim error_string As New StringBuilder("", 256)    ' エラーコード文字列
        Dim ret As Integer                                ' 戻り値取得用
        Dim ao_data As Single                             ' 出力データ
        Dim ao_channel As Short                           ' チャネル番号

        '----------------------------------------
        ' テキストボックスからチャネル番号を取得
        '----------------------------------------
        If (Short.TryParse(TextBox_ChNo.Text, ao_channel) = False) Then
            ao_channel = 0
        End If
        '----------------------------------------
        ' テキストボックスから出力データを取得
        '----------------------------------------
        If (Single.TryParse(TextBox_SingleAoData.Text, ao_data) = False) Then
            ao_data = 0.0F
        End If
        '----------------------------------------
        ' 指定チャネルのDA変換データを出力
        '----------------------------------------
        ret = AioSingleAoEx(g_id, ao_channel, ao_data)
        '----------------------------------------
        ' エラー文字列取得
        '----------------------------------------
        AioGetErrorString(ret, error_string)
        '----------------------------------------
        ' メッセージ表示
        '----------------------------------------
        TextBox_ReturnCode.Text = "AioSingleAoEx = " & ret & "：" & error_string.ToString()
    End Sub

    '================================================================================
    ' AioMultiAoExボタンをクリックした際の処理になります
    ' 出力データ(電圧または電流)を複数チャネルから1回出力します
    '================================================================================
    Private Sub Button_MultiAoEx_Click(sender As Object, e As EventArgs) Handles Button_MultiAoEx.Click
        Dim error_string As New StringBuilder("", 256)    ' エラーコード文字列
        Dim ret As Integer                                ' 戻り値取得用
        Dim ao_data() As Single                           ' 出力データ
        Dim ao_channels As Short                          ' チャネル数

        '----------------------------------------
        ' テキストボックスからチャネル数を取得
        '----------------------------------------
        If (Short.TryParse(TextBox_ChNum.Text, ao_channels) = False) Then
            ao_channels = 1
        End If
        '----------------------------------------
        ' DA変換データ格納配列の確保
        '----------------------------------------
        Try
            ReDim ao_data(ao_channels - 1)
        '----------------------------------------
        ' DA変換データ格納配列の確保に失敗した場合
        '----------------------------------------
        Catch ex As Exception
            TextBox_ReturnCode.Text = "DA変換データ格納配列の確保に失敗しました"
            Exit Sub
        End Try
        '----------------------------------------
        ' DA変換データを初期化
        '----------------------------------------
        Array.Clear(ao_data, 0, ao_channels)
        '----------------------------------------
        ' テキストボックスからDA変換データを取得
        '----------------------------------------
        If (0 < ao_channels) Then
            If (Single.TryParse(TextBox_MultiAoDataCh0.Text, ao_data(0)) = False) Then
                ao_data(0) = 0.0F
            End If
        End If
        If (1 < ao_channels) Then
            If (Single.TryParse(TextBox_MultiAoDataCh1.Text, ao_data(1)) = False) Then
                ao_data(1) = 0.0F
            End If
        End If
        If (2 < ao_channels) Then
            If (Single.TryParse(TextBox_MultiAoDataCh2.Text, ao_data(2)) = False) Then
                ao_data(2) = 0.0F
            End If
        End If
        If (3 < ao_channels) Then
            If (Single.TryParse(TextBox_MultiAoDataCh3.Text, ao_data(3)) = False) Then
                ao_data(3) = 0.0F
            End If
        End If
        '----------------------------------------
        ' 指定チャネルのDA変換データを出力
        '----------------------------------------
        ret = AioMultiAoEx(g_id, ao_channels, ao_data)
        '----------------------------------------
        ' エラー文字列取得
        '----------------------------------------
        AioGetErrorString(ret, error_string)
        '----------------------------------------
        ' メッセージ表示
        '----------------------------------------
        TextBox_ReturnCode.Text = "AioMultiAoEx = " & ret & "：" & error_string.ToString()
        '----------------------------------------
        ' DA変換データ格納配列の解放
        '----------------------------------------
        Erase ao_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
