﻿Imports CWaveformGenerator

Public Class Form1
    Dim c_fgen As CCWaveformGeneratorClass

    '----------------------------------------
    ' マクロ定義
    '----------------------------------------
    Public Const CWAVEFORMGENERATOR_CHANNELS As Integer = 16    ' CWaveformGeneratorの上限チャネル数


    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        '----------------------------------------
        ' C-WaveFormGeneratorをSDKで呼び出すための初期処理
        '----------------------------------------
        Try
            '----------------------------------------
            ' C-WaveformGeneratorのクラスを作成
            '----------------------------------------
            c_fgen = New CCWaveformGeneratorClass()
            '----------------------------------------
            ' エラーの時、エラーメッセージを表示
            '----------------------------------------
        Catch
            MessageBox.Show("Unanticipated exception creating CCWaveformGeneratorClass.", "エラー", MessageBoxButtons.OK)
            Me.Close()
            Return
        End Try
        '----------------------------------------
        ' デバイス名の初期表示を更新
        '----------------------------------------
        textBox_DeviceName.Text = "AIO000"
        '----------------------------------------
        ' 画面サイズコンボボックスの作成
        '----------------------------------------
        comboBox_OpenMode.Items.Add("最小化")
        comboBox_OpenMode.Items.Add("通常")
        comboBox_OpenMode.Items.Add("最大化")
        '----------------------------------------
        ' 画面サイズ用コンボボックスの選択を0番に変更
        '----------------------------------------
        comboBox_OpenMode.SelectedIndex = 0
        '----------------------------------------
        ' 波形タイプコンボボックスの作成
        '----------------------------------------
        comboBox_WaveType.Items.Add("正弦")
        comboBox_WaveType.Items.Add("固定")
        comboBox_WaveType.Items.Add("三角")
        comboBox_WaveType.Items.Add("のこぎり")
        comboBox_WaveType.Items.Add("矩形")
        comboBox_WaveType.Items.Add("ノイズ")
        comboBox_WaveType.Items.Add("任意")       'データファイルの読み方のサンプルはfileサンプルへ。
        '----------------------------------------
        ' 波形タイプコンボボックスの選択を0番に変更
        '----------------------------------------
        comboBox_WaveType.SelectedIndex = 0
        '----------------------------------------
        ' チャネル番号コンボボックスの作成
        ' C-WaveformGeneratorの上限チャネル数
        '----------------------------------------
        For i = 0 To (CWAVEFORMGENERATOR_CHANNELS - 1)
            comboBox_AoChannel.Items.Add(i.ToString())
        Next i
        '----------------------------------------
        ' チャネル番号コンボボックスの選択を0番に変更
        '----------------------------------------
        comboBox_AoChannel.SelectedIndex = 0
    End Sub

    '================================================================================
    ' OpenApplicationボタンをクリックした際の処理になります
    ' アプリの初期化処理を行います
    '================================================================================
    Private Sub button_OpenApplicaion_Click(sender As Object, e As EventArgs) Handles button_OpenApplicaion.Click
        Dim device_name As String   ' デバイス名
        Dim arg1 As String          ' 予約
        Dim openmode As Short       ' 画面サイズ
        Dim ret As Long             ' 戻り値取得用

        '----------------------------------------
        ' テキストボックスからデバイス名を取得
        '----------------------------------------
        device_name = textBox_DeviceName.Text
        '----------------------------------------
        ' コンボボックスから画面サイズを取得
        '----------------------------------------
        openmode = comboBox_OpenMode.SelectedIndex
        '----------------------------------------
        ' arg1（予約）
        '----------------------------------------
        arg1 = ""
        '----------------------------------------
        ' アプリの初期化処理を行う
        '----------------------------------------
        Try
            ret = c_fgen.OpenApplication(openmode, device_name, arg1)
        Catch
            MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "エラー", MessageBoxButtons.OK)
            Return
        End Try
        '----------------------------------------
        ' エラーコードを表示
        '----------------------------------------
        textBox_ReturnNum.Text = String.Format("OpenApplication = {0} ", ret)
    End Sub

    '================================================================================
    ' DeviceSettingボタンをクリックした際の処理になります
    ' デバイス設定のダイアログを開きます
    '================================================================================
    Private Sub button_DeviceSetting_Click(sender As Object, e As EventArgs) Handles button_DeviceSetting.Click
        Dim deviceinfodlg As New DeviceSetting
        Dim aochannels As Short = 1         ' チャネル数
        Dim frequency As Single = 1         ' 周波数
        Dim unit_window As Short = 0        ' 周波数単位コンボボックス
        Dim frequency_type As Short = 0     ' 周波数or周期
        Dim frequency_unit As Short = 0     ' Hz(sec) Or kHz(msec)
        Dim oscilation_type As Short = 0    ' 発振タイプ
        Dim oscilation_start As Short = 1   ' 発振波数
        Dim oscilation_stop As Single = 1   ' 発振停止波数or発振停止時間
        Dim arg1 As Short = 0               ' 予約
        Dim ret As Long                     ' 戻り値取得用

        '----------------------------------------
        ' 戻り値を空白にしておく
        '----------------------------------------
        textBox_ReturnNum.Text = ""
        '----------------------------------------
        ' デバイス情報の取得を行う
        '----------------------------------------
        Try
            ret = c_fgen.GetDeviceSetting(aochannels, frequency, frequency_type, frequency_unit, arg1)
        Catch
            MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "エラー", MessageBoxButtons.OK)
            Return
        End Try
        unit_window = ((frequency_type << 1) + frequency_unit)
        '----------------------------------------
        ' 発振の取得を行う
        '----------------------------------------
        Try
            ret = c_fgen.GetOscilation(oscilation_type, oscilation_start, oscilation_stop, arg1)
        Catch
            MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "エラー", MessageBoxButtons.OK)
            Return
        End Try
        '----------------------------------------
        ' デバイス設定のダイアログへ
        '----------------------------------------
        deviceinfodlg.devinfo_data_set(aochannels, frequency, unit_window, oscilation_type, oscilation_start, oscilation_stop)
        '----------------------------------------
        ' OKを押された場合、処理を行う
        '----------------------------------------
        If (deviceinfodlg.ShowDialog() = DialogResult.OK) Then
            '----------------------------------------
            ' デバイス設定のダイアログから
            '----------------------------------------
            deviceinfodlg.devinfo_data_get(aochannels, frequency, unit_window, oscilation_type, oscilation_start, oscilation_stop)
            '----------------------------------------
            ' 周波数単位から、タイプと単位に変更
            '----------------------------------------
            frequency_type = ((unit_window And &H2S) >> 1)
            frequency_unit = (unit_window And &H1S)
            '----------------------------------------
            ' arg1（予約）
            '----------------------------------------
            arg1 = 0
            '----------------------------------------
            ' デバイス情報の設定を行う
            '----------------------------------------
            Try
                ret = c_fgen.SetDeviceSetting(aochannels, frequency, frequency_type, frequency_unit, arg1)
            Catch
                MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "エラー", MessageBoxButtons.OK)
                Return
            End Try
            '----------------------------------------
            ' 正常終了ではない場合、エラーコードを表示
            '----------------------------------------
            If (ret <> 0) Then
                textBox_ReturnNum.Text = String.Format("SetDeviceSetting = {0} ", ret)
                Return
            End If
            '----------------------------------------
            ' 発振の設定を行う
            '----------------------------------------
            Try
                ret = c_fgen.SetOscilation(oscilation_type, oscilation_start, oscilation_stop, arg1)
            Catch
                MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "エラー", MessageBoxButtons.OK)
                Return
            End Try
            '----------------------------------------
            ' 正常終了ではない場合、エラーコードを表示
            '----------------------------------------
            If (ret <> 0) Then
                textBox_ReturnNum.Text = String.Format("SetOscilation = {0} ", ret)
                Return
            End If
            '----------------------------------------
            ' 条件設定完了のメッセージ表示
            '----------------------------------------
            textBox_ReturnNum.Text = "デバイス設定：正常終了"
        End If
        deviceinfodlg.Dispose()
    End Sub

    '================================================================================
    ' ChannelSettingボタンをクリックした際の処理になります
    ' チャネル設定のダイアログを開きます
    '================================================================================
    Private Sub button_ChannelSetting_Click(sender As Object, e As EventArgs) Handles button_ChannelSetting.Click
        Dim channelinfodlg As New ChannelSetting
        Dim aochannel As Short = 0          ' チャネル番号
        Dim amplitude As Single = 0         ' 振幅
        Dim offset As Single = 0            ' オフセット
        Dim amplitude_type As Short = 0     ' 振幅タイプ
        Dim amplitude_unit As Short = 0     ' 振幅単位
        Dim duty As Single = 0              ' Duty
        Dim phase As Single = 0             ' 位相
        Dim sweep_type As Short = 0         ' 簡易スイープ
        Dim oscilation_time As Short = 1    ' スイープ時間
        Dim oscilation_start As Single = 0  ' スイープ変化前
        Dim oscilation_stop As Single = 0   ' スイープ変化後
        Dim arg1 As Short = 0               ' 予約
        Dim ret As Long                     ' 戻り値取得用
        Dim wave_type As Short = 0          ' 波形タイプ

        '----------------------------------------
        ' 戻り値を空白にしておく
        '----------------------------------------
        textBox_ReturnNum.Text = ""
        '----------------------------------------
        ' コンボボックスからチャネル番号を取得
        '----------------------------------------
        aochannel = comboBox_AoChannel.SelectedIndex
        '----------------------------------------
        ' 波形タイプを取得を行う
        '----------------------------------------
        Try
            ret = c_fgen.GetWaveType(aochannel, wave_type, arg1)
        Catch
            MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "エラー", MessageBoxButtons.OK)
            Return
        End Try
        '----------------------------------------
        ' チャネル情報の取得を行う
        '----------------------------------------
        Try
            ret = c_fgen.GetChannelSetting(aochannel, amplitude, offset, amplitude_type, amplitude_unit, duty, phase, arg1)
        Catch
            MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "エラー", MessageBoxButtons.OK)
            Return
        End Try
        '----------------------------------------
        ' 簡易スイープの取得を行う
        '----------------------------------------
        Try
            ret = c_fgen.GetAoSweep(aochannel, sweep_type, oscilation_time, oscilation_start, oscilation_stop, arg1)
        Catch
            MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "エラー", MessageBoxButtons.OK)
            Return
        End Try
        '----------------------------------------
        ' チャネル設定のダイアログへ
        '----------------------------------------
        channelinfodlg.chinfo_data_set(aochannel, amplitude, offset, amplitude_type,
                                       amplitude_unit, duty, phase, sweep_type,
                                       oscilation_time, oscilation_start, oscilation_stop, wave_type)
        '----------------------------------------
        ' OKを押された場合、処理を行う
        '----------------------------------------
        If (channelinfodlg.ShowDialog() = DialogResult.OK) Then
            '----------------------------------------
            ' チャネル設定のダイアログから
            '----------------------------------------
            channelinfodlg.chinfo_data_get(aochannel, amplitude, offset, amplitude_type,
                                           amplitude_unit, duty, phase, sweep_type,
                                           oscilation_time, oscilation_start, oscilation_stop)
            '----------------------------------------
            ' arg1（予約）
            '----------------------------------------
            arg1 = 0
            '----------------------------------------
            ' チャネル情報の設定を行う
            '----------------------------------------
            Try
                ret = c_fgen.SetChannelSetting(aochannel, amplitude, offset, amplitude_type, amplitude_unit, duty, phase, arg1)
            Catch
                MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "エラー", MessageBoxButtons.OK)
                Return
            End Try
            '----------------------------------------
            ' 正常終了ではない場合、エラーコードを表示
            '----------------------------------------
            If (ret <> 0) Then
                textBox_ReturnNum.Text = String.Format("SetChannelSetting = {0} ", ret)
                Return
            End If
            '----------------------------------------
            ' 簡易スイープの設定を行う
            '----------------------------------------
            Try
                ret = c_fgen.SetAoSweep(aochannel, sweep_type, oscilation_time, oscilation_start, oscilation_stop, arg1)
            Catch
                MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "エラー", MessageBoxButtons.OK)
                Return
            End Try
            '----------------------------------------
            ' 正常終了ではない場合、エラーコードを表示
            '----------------------------------------
            If (ret <> 0) Then
                textBox_ReturnNum.Text = String.Format("SetAoSweep = {0} ", ret)
                Return
            End If
            '----------------------------------------
            ' 条件設定完了のメッセージ表示
            '----------------------------------------
            textBox_ReturnNum.Text = "チャネル設定：正常終了"
        End If
        channelinfodlg.Dispose()
    End Sub

    '================================================================================
    ' SetWaveTypeボタンをクリックした際の処理になります
    ' 波形タイプの設定を行います
    '================================================================================
    Private Sub button_SetWaveType_Click(sender As Object, e As EventArgs) Handles button_SetWaveType.Click
        Dim wave_type As Short  ' 波形タイプ
        Dim aochannel As Short  ' チャネル番号
        Dim arg1 As Short       ' 予約
        Dim ret As Long         ' 戻り値取得用

        '----------------------------------------
        ' コンボボックスから波形タイプを取得
        '----------------------------------------
        wave_type = comboBox_WaveType.SelectedIndex
        '----------------------------------------
        ' コンボボックスからチャネル番号を取得
        '----------------------------------------
        aochannel = comboBox_AoChannel.SelectedIndex
        '----------------------------------------
        ' arg1（予約）
        '----------------------------------------
        arg1 = 0
        '----------------------------------------
        ' 波形タイプの設定を行う
        '----------------------------------------
        Try
            ret = c_fgen.SetWaveType(aochannel, wave_type, arg1)
        Catch
            MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "エラー", MessageBoxButtons.OK)
            Return
        End Try
        '----------------------------------------
        ' エラーコードを表示
        '----------------------------------------
        textBox_ReturnNum.Text = String.Format("SetWaveType = {0} ", ret)
    End Sub

    '================================================================================
    ' StartOperationボタンをクリックした際の処理になります
    ' アプリの出力開始を行います
    '================================================================================
    Private Sub button_StartOperation_Click(sender As Object, e As EventArgs) Handles button_StartOperation.Click
        Dim arg1 As Short       ' 予約
        Dim ret As Long         ' 戻り値取得用

        '----------------------------------------
        ' arg1（予約）
        '----------------------------------------
        arg1 = 0
        '----------------------------------------
        ' アプリの出力開始を行う
        '----------------------------------------
        Try
            ret = c_fgen.StartOperation(arg1)
        Catch
            MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "エラー", MessageBoxButtons.OK)
            Return
        End Try
        '----------------------------------------
        ' エラーコードを表示
        '----------------------------------------
        textBox_ReturnNum.Text = String.Format("StartOperation = {0} ", ret)
    End Sub

    '================================================================================
    ' GetApplicationStatusボタンをクリックした際の処理になります
    ' 動作状況の取得を行います
    '================================================================================
    Private Sub button_GetApplicationStatus_Click(sender As Object, e As EventArgs) Handles button_GetApplicationStatus.Click
        Dim status As Integer = -1     ' ステータス
        Dim ret As Long                ' 戻り値取得用

        '----------------------------------------
        ' 動作状況の取得を行う
        '----------------------------------------
        Try
            ret = c_fgen.GetApplicationStatus(status)
        Catch
            MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "エラー", MessageBoxButtons.OK)
            Return
        End Try
        '----------------------------------------
        ' エラーコードを表示
        '----------------------------------------
        textBox_ReturnNum.Text = String.Format("GetApplicationStatus = {0} ", ret)
        '----------------------------------------
        ' ステータスを表示
        '----------------------------------------
        If (status = 0) Then
            textBox_Status.Text = "00H(停止中)"
        ElseIf (status = 1) Then
            textBox_Status.Text = "01H(動作中)"
        ElseIf (status = 2) Then
            textBox_Status.Text = "02H(開始トリガ待ち)"
        ElseIf (status = 3) Then
            textBox_Status.Text = "03H(動作中、開始トリガ待ち)"
        Else
            textBox_Status.Text = status.ToString()
        End If
    End Sub

    '================================================================================
    ' StopOperationボタンをクリックした際の処理になります
    ' アプリの出力停止を行います
    '================================================================================
    Private Sub button_StopOperation_Click(sender As Object, e As EventArgs) Handles button_StopOperation.Click
        Dim arg1 As Short       ' 予約
        Dim ret As Long         ' 戻り値取得用

        '----------------------------------------
        ' arg1（予約）
        '----------------------------------------
        arg1 = 0
        '----------------------------------------
        ' アプリの出力停止を行う
        '----------------------------------------
        Try
            ret = c_fgen.StopOperation(arg1)
        Catch
            MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "エラー", MessageBoxButtons.OK)
            Return
        End Try
        '----------------------------------------
        ' エラーコードを表示
        '----------------------------------------
        textBox_ReturnNum.Text = String.Format("StopOperation = {0} ", ret)
    End Sub

    '================================================================================
    ' CloseApplicationボタンをクリックした際の処理になります
    ' アプリの終了処理を行います
    '================================================================================
    Private Sub button_CloseApplicaion_Click(sender As Object, e As EventArgs) Handles button_CloseApplicaion.Click
        Dim arg1 As Short       ' 予約
        Dim ret As Long         ' 戻り値取得用

        '----------------------------------------
        ' arg1（予約）
        '----------------------------------------
        arg1 = 0
        '----------------------------------------
        ' アプリの終了処理を行う
        '----------------------------------------
        Try
            ret = c_fgen.CloseApplication(arg1)
        Catch
            MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "エラー", MessageBoxButtons.OK)
            Return
        End Try
        '----------------------------------------
        ' エラーコードを表示
        '----------------------------------------
        textBox_ReturnNum.Text = String.Format("CloseApplication = {0} ", ret)
    End Sub

    '================================================================================
    ' 閉じるボタンをクリックした際の処理になります
    ' アプリケーションの終了処理を行います
    '================================================================================
    Private Sub button_Exit_Click(sender As Object, e As EventArgs) Handles button_Exit.Click
        Dim arg1 As Short                ' 予約

        '----------------------------------------
        ' arg1（予約）
        '----------------------------------------
        arg1 = 0
        '----------------------------------------
        ' デバイスの終了処理を行う
        '----------------------------------------
        Try
            c_fgen.CloseApplication(arg1)
        Catch
        End Try
        '----------------------------------------
        ' ダイアログを終了する
        '----------------------------------------
        Me.Close()
    End Sub
End Class
