﻿Imports System.IO
Imports System.Text

Public Class Form1
    '----------------------------------------
    ' Macro definition
    '----------------------------------------
    Public Const MAX_AI_RANGE_NUM As Integer = 32    ' Maximum number of elements in analog input range array

    '----------------------------------------
    ' Declaration of global variables
    '----------------------------------------
    Dim g_id As Short                                                         ' ID for device access
    Dim g_ai_range_index As Long                                              ' Index of range
    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
        '----------------------------------------
        ' Set the default button
        '----------------------------------------
        AcceptButton = Button_Close
        '----------------------------------------
        ' Update the initial display of device name
        '----------------------------------------
        TextBox_DeviceName.Text = "AIO000"
        '----------------------------------------
        ' Update the initial display of the channel No. for specified channel input
        '----------------------------------------
        TextBox_ChNo.Text = "0"
        '----------------------------------------
        ' Update the initial display of number of channels for multi-channel input
        '----------------------------------------
        TextBox_ChNum.Text = "1"
        '----------------------------------------
        ' Create the combobox for analog input range
        '----------------------------------------
        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")
        '----------------------------------------
        ' Change the selection of analog input range combobox to 0
        '----------------------------------------
        ComboBox_RangeValue.SelectedIndex = 0
        '----------------------------------------
        ' Create the combobox for analog input method
        '----------------------------------------
        ComboBox_InpMethod.Items.Add("Single-end input")
        ComboBox_InpMethod.Items.Add("Differential input")
        '----------------------------------------
        ' Change the selection of analog input method combobox to 0
        '----------------------------------------
        ComboBox_InpMethod.SelectedIndex = 0
        '----------------------------------------
        ' Initialize global variables
        '----------------------------------------
        g_id = 0
        g_ai_range_index = 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 AioSetAiRangeAll button is clicked
    ' Set the input range of analog input
    '================================================================================
    Private Sub Button_SetAiRangeAll_Click(sender As Object, e As EventArgs) Handles Button_SetAiRangeAll.Click
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim ret As Integer                                ' For return code

        '----------------------------------------
        ' Update GUI information
        '----------------------------------------
        Me.Refresh()
        '----------------------------------------
        ' Get the index of range setting value from combobox
        '----------------------------------------
        g_ai_range_index = ComboBox_RangeValue.SelectedIndex
        '----------------------------------------
        ' Set the analog input range (all channels)
        '----------------------------------------
        ret = AioSetAiRangeAll(g_id, g_ai_range(g_ai_range_index))
        '----------------------------------------
        ' Get error string
        '----------------------------------------
        AioGetErrorString(ret, error_string)
        '----------------------------------------
        ' Display message
        '----------------------------------------
        TextBox_ReturnCode.Text = "AioSetAiRangeAll = " & ret & " : " & error_string.ToString()
    End Sub

    '================================================================================
    ' It is the process when the AioSetAiInputMethod button is clicked
    ' Set the input method of analog input
    '================================================================================
    Private Sub Button_SetAiInpMethod_Click(sender As Object, e As EventArgs) Handles Button_SetAiInpMethod.Click
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim ret As Integer                                ' For return code
        Dim input_method As Integer                       ' Input method

        '----------------------------------------
        ' Update GUI information
        '----------------------------------------
        Me.Refresh()
        '----------------------------------------
        ' Get the index of input method setting value from combobox
        '----------------------------------------
        input_method = ComboBox_InpMethod.SelectedIndex
        '----------------------------------------
        ' Set the analog input method
        '----------------------------------------
        ret = AioSetAiInputMethod(g_id, input_method)
        '----------------------------------------
        ' Get error string
        '----------------------------------------
        AioGetErrorString(ret, error_string)
        '----------------------------------------
        ' Display message
        '----------------------------------------
        TextBox_ReturnCode.Text = "AioSetAiInputMethod = " & ret & " : " & error_string.ToString()
    End Sub

    '================================================================================
    ' It is the process when the AioSingleAiEx button is clicked
    ' Inputs the input data (voltage or current) once from the specified channel
    '================================================================================
    Private Sub Button_SingleAiEx_Click(sender As Object, e As EventArgs) Handles Button_SingleAiEx.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 ai_data As Single                             ' Input data
        Dim ai_channel As Short                           ' Channel No.

        '----------------------------------------
        ' Get the channel No. from textbox
        '----------------------------------------
        If (Short.TryParse(TextBox_ChNo.Text, ai_channel) = False) Then
            ai_channel = 0
        End If
        '----------------------------------------
        ' Get the AD conversion data of specified channel
        '----------------------------------------
        ret = AioSingleAiEx(g_id, ai_channel, ai_data)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        '--------------------
        ' Get error string
        '--------------------
        AioGetErrorString(ret, error_string)
        '--------------------
        ' Display message
        '--------------------
        TextBox_ReturnCode.Text = "AioSingleAiEx = " & ret & " : " & error_string.ToString()
        '--------------------
        ' Exit the process if not completed successfully
        '--------------------
        If (ret <> 0) Then
            Exit Sub
        End If
        '----------------------------------------
        ' Display the AD conversion data of specified channel
        '----------------------------------------
        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
        '--------------------
        ' Update conversion data display
        '--------------------
        TextBox_SingleAiData.Text = text_string
    End Sub

    '================================================================================
    ' It is the process when the AioMultiAiEx button is clicked
    ' Inputs the input data (voltage or current) once from the multi-channels
    '================================================================================
    Private Sub Button_MultiAiEx_Click(sender As Object, e As EventArgs) Handles Button_MultiAiEx.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 ai_data() As Single                           ' Input data
        Dim i As Short                                    ' For looping the input data display
        Dim ai_channels As Short                          ' Number of channels

        '----------------------------------------
        ' Get the number of channels from textbox
        '----------------------------------------
        If (Short.TryParse(TextBox_ChNum.Text, ai_channels) = False) Then
            ai_channels = 1
        End If
        '----------------------------------------
        ' Allocate an array for storing AD conversion data
        '----------------------------------------
        Try
            ReDim ai_data(ai_channels)
        '----------------------------------------
        ' When failing to allocate the AD conversion data storage array
        '----------------------------------------
        Catch ex As Exception
            TextBox_ReturnCode.Text = "Failed to allocate AD conversion data storage array"
            Exit Sub
        End Try
        '----------------------------------------
        ' Get the AD conversion data of multi-channels
        '----------------------------------------
        ret = AioMultiAiEx(g_id, ai_channels, ai_data)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        '--------------------
        ' Get error string
        '--------------------
        AioGetErrorString(ret, error_string)
        '--------------------
        ' Display message
        '--------------------
        TextBox_ReturnCode.Text = "AioMultiAiEx = " & ret & " : " & error_string.ToString()
        '--------------------
        ' Exit the process if not completed successfully
        '--------------------
        If (ret <> 0) Then
            '--------------------
            ' Release the array for storing AD conversion data
            '--------------------
            Erase ai_data
            Exit Sub
        End If
        '----------------------------------------
        ' Display AD conversion data
        '----------------------------------------
        '--------------------
        ' Clear the listbox for AD conversion data display
        '--------------------
        ListBox_MultiInpData.Items.Clear()
        '--------------------
        ' Repeat the process up to the set number of channels
        '--------------------
        For i = 0 To ai_channels - 1
            '--------------------
            ' Generate display data
            '--------------------
            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
            '--------------------
            ' Update conversion data display
            '--------------------
            ListBox_MultiInpData.Items.Add(text_string)
        Next i
        '----------------------------------------
        ' Release the array for storing AD conversion data
        '----------------------------------------
        Erase ai_data
    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
