﻿Imports System.Text

Public Class Form1
    '----------------------------------------
    ' Macro definition
    '----------------------------------------
    Public Const MAX_AO_RANGE_NUM As Integer = 31    ' Maximum number of elements in analog output range array

    '----------------------------------------
    ' Declaration of global variables
    '----------------------------------------
    Dim g_id As Short                                                         ' ID for device access
    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
        '----------------------------------------
        ' 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 output
        '----------------------------------------
        TextBox_ChNo.Text = "0"
        '----------------------------------------
        ' Update initial display of data for specified channel output
        '----------------------------------------
        TextBox_SingleAoData.Text = "0.0"
        '----------------------------------------
        ' Updated initial display of number of channels for multi-channel output
        '----------------------------------------
        TextBox_ChNum.Text = "1"
        '----------------------------------------
        ' Update initial display of data for multi-channel output
        '----------------------------------------
        TextBox_MultiAoDataCh0.Text = "0.0"
        TextBox_MultiAoDataCh1.Text = "0.0"
        TextBox_MultiAoDataCh2.Text = "0.0"
        TextBox_MultiAoDataCh3.Text = "0.0"
        '----------------------------------------
        ' Create the combobox for analog output 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("1 - +5V")
        '----------------------------------------
        ' Change the selection of analog output range combobox to 0
        '----------------------------------------
        ComboBox_RangeValue.SelectedIndex = 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 AioSetAoRangeAll button is clicked
    ' Set the output range of analog output
    '================================================================================
    Private Sub Button_SetAoRangeAll_Click(sender As Object, e As EventArgs) Handles Button_SetAoRangeAll.Click
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim ret As Integer                                ' For return code
        Dim index As Integer                              ' Index of combobox

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

    '================================================================================
    ' It is the process when the AioSingleAoEx button is clicked
    ' Outputs the output data (voltage or current) once from the specified channel
    '================================================================================
    Private Sub Button_SingleAoEx_Click(sender As Object, e As EventArgs) Handles Button_SingleAoEx.Click
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim ret As Integer                                ' For return code
        Dim ao_data As Single                             ' Output data
        Dim ao_channel As Short                           ' Channel No.

        '----------------------------------------
        ' Get the channel No. from textbox
        '----------------------------------------
        If (Short.TryParse(TextBox_ChNo.Text, ao_channel) = False) Then
            ao_channel = 0
        End If
        '----------------------------------------
        ' Get the output data from textbox
        '----------------------------------------
        If (Single.TryParse(TextBox_SingleAoData.Text, ao_data) = False) Then
            ao_data = 0.0F
        End If
        '----------------------------------------
        ' Output the output data from the specified channel
        '----------------------------------------
        ret = AioSingleAoEx(g_id, ao_channel, ao_data)
        '----------------------------------------
        ' Get error string
        '----------------------------------------
        AioGetErrorString(ret, error_string)
        '----------------------------------------
        ' Display message
        '----------------------------------------
        TextBox_ReturnCode.Text = "AioSingleAoEx = " & ret & " : " & error_string.ToString()
    End Sub

    '================================================================================
    ' It is the process when the AioMultiAoEx button is clicked
    ' Outputs the output data (voltage or current) once from the multi-channels
    '================================================================================
    Private Sub Button_MultiAoEx_Click(sender As Object, e As EventArgs) Handles Button_MultiAoEx.Click
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim ret As Integer                                ' For return code
        Dim ao_data() As Single                           ' Output data
        Dim ao_channels As Short                          ' Number of channels

        '----------------------------------------
        ' Get the number of channels from textbox
        '----------------------------------------
        If (Short.TryParse(TextBox_ChNum.Text, ao_channels) = False) Then
            ao_channels = 1
        End If
        '----------------------------------------
        ' Allocate an array for storing DA conversion data
        '----------------------------------------
        Try
            ReDim ao_data(ao_channels - 1)
        '----------------------------------------
        ' When failing to allocate the DA conversion data storage array
        '----------------------------------------
        Catch ex As Exception
            TextBox_ReturnCode.Text = "Failed to allocate DA conversion data storage array"
            Exit Sub
        End Try
        '----------------------------------------
        ' Initialize DA conversion data
        '----------------------------------------
        Array.Clear(ao_data, 0, ao_channels)
        '----------------------------------------
        ' Get DA conversion data from textbox
        '----------------------------------------
        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
        '----------------------------------------
        ' Output the output data from the multi-channels
        '----------------------------------------
        ret = AioMultiAoEx(g_id, ao_channels, ao_data)
        '----------------------------------------
        ' Get error string
        '----------------------------------------
        AioGetErrorString(ret, error_string)
        '----------------------------------------
        ' Display message
        '----------------------------------------
        TextBox_ReturnCode.Text = "AioMultiAoEx = " & ret & " : " & error_string.ToString()
        '----------------------------------------
        ' Release the array for storing DA conversion data
        '----------------------------------------
        Erase ao_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
