﻿Imports CWaveformGenerator
Public Class Form1
    Dim c_fgen As CCWaveformGeneratorClass

    '----------------------------------------
    ' マクロ定義
    '----------------------------------------
    Public AO_CHANNEL As Integer = 0                        ' アナログ出力チャネル番号	: 0
    Public CWAVEFORMGENERATOR_CHANNELS As Integer = 16      ' 最大チャネル数
    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
        '----------------------------------------
        ' チャネル番号コンボボックスの作成
        '----------------------------------------
        Dim i As Integer
        For i = 0 To CWAVEFORMGENERATOR_CHANNELS
            comboBox_AoChannel.Items.Add(i.ToString())
        Next i
        '----------------------------------------
        ' チャネル番号コンボボックスの選択を0番に変更
        '----------------------------------------
        comboBox_AoChannel.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("任意")
        '----------------------------------------
        ' 波形タイプコンボボックスの選択を0番に変更
        '----------------------------------------
        comboBox_WaveType.SelectedIndex = 0
        '----------------------------------------
        ' データ保存コンボボックスの作成
        '----------------------------------------
        comboBox_Param.Items.Add("単チャネル")
        comboBox_Param.Items.Add("複数チャネル")
        '----------------------------------------
        ' データ保存コンボボックスの選択を0番に変更
        '----------------------------------------
        comboBox_Param.SelectedIndex = 0
    End Sub

    '================================================================================
    ' OpenApplicationボタンをクリックした際の処理になります
    ' アプリの初期化処理を行います
    '================================================================================
    Private Sub button_OpenApplication_Click(sender As Object, e As EventArgs) Handles button_OpenApplication.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

    '================================================================================
    ' SetDeviceNameボタンをクリックした際の処理になります
    ' デバイス名の設定を行います
    '================================================================================
    Private Sub button_SetDeviceName_Click(sender As Object, e As EventArgs) Handles button_SetDeviceName.Click
        Dim device_name As String         ' デバイス名
        Dim ret As Long                   ' 戻り値取得用

        '----------------------------------------
        ' テキストボックスから設定ファイル名を取得
        '----------------------------------------
        device_name = textBox_DeviceName.Text
        '----------------------------------------
        ' デバイス名の設定
        '----------------------------------------
        Try
            ret = c_fgen.SetDeviceName(device_name)
        Catch
            MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "エラー", MessageBoxButtons.OK)
            Return
        End Try
        '----------------------------------------
        ' エラーコードを表示
        '----------------------------------------
        textBox_ReturnNum.Text = String.Format("SetDeviceName = {0} ", ret)
    End Sub

    '================================================================================
    ' ...ボタンをクリックした際の処理になります  (設定ファイル)
    ' iniファイルを選択するダイアログを表示します
    '================================================================================
    Private Sub button_SettingSelectFile_Click(sender As Object, e As EventArgs) Handles button_SettingSelectFile.Click
        '----------------------------------------
        ' ファイル名が存在するかどうかをチェックしない
        '----------------------------------------
        openFileDialog1.CheckFileExists = False
        '----------------------------------------
        ' ファイルのパスが存在する場合は、ファイルのパスをダイアログの初期パスに設定します
        '----------------------------------------
        If textBox_SettingFile.Text.Length > 0 Then
            openFileDialog1.InitialDirectory = textBox_SettingFile.Text
            openFileDialog1.FileName = openFileDialog1.SafeFileName
            '----------------------------------------
            ' ファイルのパスが存在しない場合は、MyDocumentsをダイアログの初期パスに設定します
            '----------------------------------------
        Else
            openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
            openFileDialog1.FileName = ""
        End If
        '----------------------------------------
        ' 選択したパスを表示
        '----------------------------------------
        If openFileDialog1.ShowDialog() = DialogResult.OK Then
            textBox_SettingFile.Text = System.IO.Path.GetFullPath(openFileDialog1.FileName)
        End If
    End Sub

    '================================================================================
    ' ExportSettingFileボタンをクリックした際の処理になります
    ' 設定をiniファイルに保存します
    '================================================================================
    Private Sub button_ExportSettingFile_Click(sender As Object, e As EventArgs) Handles button_ExportSettingFile.Click
        Dim filepath As String           ' ファイルパス
        Dim arg1 As Short                ' 予約
        Dim ret As Long                  ' 戻り値取得用

        '----------------------------------------
        ' テキストボックスから設定ファイルパスを取得
        '----------------------------------------
        filepath = textBox_SettingFile.Text
        '----------------------------------------
        ' arg1（予約）
        '----------------------------------------
        arg1 = 0
        '----------------------------------------
        ' 設定をiniファイルに保存
        '----------------------------------------
        Try
            ret = c_fgen.ExportSettingFile(filepath, arg1)
        Catch
            MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "エラー", MessageBoxButtons.OK)
            Return
        End Try
        '----------------------------------------
        ' エラーコードを表示
        '----------------------------------------
        textBox_ReturnNum.Text = String.Format("ExportSettingFile = {0} ", ret)
    End Sub

    '================================================================================
    ' ImportSettingFileボタンをクリックした際の処理になります
    ' iniファイルの設定を読み込みます。
    '================================================================================
    Private Sub button_ImportSettingFile_Click(sender As Object, e As EventArgs) Handles button_ImportSettingFile.Click
        Dim filepath As String            ' ファイルパス
        Dim arg1 As Short                 ' 予約
        Dim ret As Long                   ' 戻り値取得用

        '----------------------------------------
        ' テキストボックスから設定ファイルパスを取得
        '----------------------------------------
        filepath = textBox_SettingFile.Text
        '----------------------------------------
        ' arg1（予約）
        '----------------------------------------
        arg1 = 0
        '----------------------------------------
        ' iniファイルの設定読み出しを行う
        '----------------------------------------
        Try
            ret = c_fgen.ImportSettingFile(filepath, arg1)
        Catch
            MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "エラー", MessageBoxButtons.OK)
            Return
        End Try
        '----------------------------------------
        ' エラーコードを表示
        '----------------------------------------
        textBox_ReturnNum.Text = String.Format("ImportSettingFile = {0} ", ret)
    End Sub

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

        '----------------------------------------
        ' コンボボックスから波形タイプを取得
        '----------------------------------------
        wavetype = comboBox_WaveType.SelectedIndex
        '----------------------------------------
        ' コンボボックスからチャネル番号を取得
        '----------------------------------------
        aochannel = comboBox_AoChannel.SelectedIndex
        '----------------------------------------
        ' arg1（予約）
        '----------------------------------------
        arg1 = 0
        '----------------------------------------
        ' 波形タイプの設定を行う
        '----------------------------------------
        Try
            ret = c_fgen.SetWaveType(aochannel, wavetype, 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

    '================================================================================
    ' ...ボタンをクリックした際の処理になります (データファイル)
    ' csvファイルを選択するダイアログを表示します
    '================================================================================
    Private Sub button_ArSelectFile_Click(sender As Object, e As EventArgs) Handles button_ArSelectFile.Click
        '----------------------------------------
        ' ファイル名が存在するかどうかをチェックしない
        '----------------------------------------
        openFileDialog1.CheckFileExists = False
        '----------------------------------------
        ' パスが存在する場合は、現在のパスをダイアログの初期パスに設定します
        '----------------------------------------
        If textBox_ArbitraryDataFile.Text.Length > 0 Then
            openFileDialog1.InitialDirectory = textBox_ArbitraryDataFile.Text
            openFileDialog1.FileName = openFileDialog1.SafeFileName
            '----------------------------------------
            ' パスが存在しない場合は、MyDocumentsをダイアログの初期パスに設定します
            '----------------------------------------
        Else
            openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
            openFileDialog1.FileName = ""
        End If
        '----------------------------------------
        ' 選択したパスを表示
        '----------------------------------------
        If openFileDialog1.ShowDialog() = DialogResult.OK Then
            textBox_ArbitraryDataFile.Text = System.IO.Path.GetFullPath(openFileDialog1.FileName)
        End If
    End Sub

    '================================================================================
    ' SetArbitraryDataFileボタンをクリックした際の処理になります
    ' データファイル（csvファイル）パスを設定します
    '================================================================================
    Private Sub button_SetArbitraryDataFile_Click(sender As Object, e As EventArgs) Handles button_SetArbitraryDataFile.Click
        Dim filepath As String            ' ファイルパス
        Dim aochannel As Short            ' チャネル番号
        Dim arg1 As Short                 ' 予約
        Dim ret As Long                   ' 戻り値取得用

        '----------------------------------------
        ' テキストボックスからデータファイルパスを取得
        '----------------------------------------
        filepath = textBox_ArbitraryDataFile.Text
        '----------------------------------------
        ' コンボボックスからチャネル番号を取得
        '----------------------------------------
        aochannel = comboBox_AoChannel.SelectedIndex
        '----------------------------------------
        ' arg1（予約）
        '----------------------------------------
        arg1 = 0
        '----------------------------------------
        ' データファイルパスの設定
        '----------------------------------------
        Try
            ret = c_fgen.SetArbitraryDataFile(aochannel, filepath, arg1)
        Catch
            MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "エラー", MessageBoxButtons.OK)
            Return
        End Try
        '----------------------------------------
        ' エラーコードを表示
        '----------------------------------------
        textBox_ReturnNum.Text = String.Format("SetArbitraryDataFile = {0} ", ret)
    End Sub

    '================================================================================
    ' GetArbitraryDataFileボタンをクリックした際の処理になります
    ' 設定されているデータファイル（csvファイル）パスを取得します
    '================================================================================
    Private Sub button_GetArbitraryDataFile_Click(sender As Object, e As EventArgs) Handles button_GetArbitraryDataFile.Click
        Dim filepath As String              ' ファイルパス
        Dim aochannel As Short              ' チャネル番号
        Dim arg1 As Short                   ' 予約
        Dim ret As Long                     ' 戻り値取得用

        arg1 = 0
        filepath = vbNullString
        '----------------------------------------
        ' 設定されているチャネルを取得
        '----------------------------------------
        aochannel = comboBox_AoChannel.SelectedIndex
        '----------------------------------------
        ' データファイルパスの取得
        '----------------------------------------
        Try
            ret = c_fgen.GetArbitraryDataFile(aochannel, filepath, arg1)
        Catch
            MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "エラー", MessageBoxButtons.OK)
            Return
        End Try
        '----------------------------------------
        ' エラーコードを表示
        '----------------------------------------
        textBox_ReturnNum.Text = String.Format("GetArbitraryDataFile = {0} ", ret)
        '----------------------------------------
        ' データファイルパスを表示
        '----------------------------------------
        textBox_ArbitraryDataFile.Text = filepath
    End Sub

    '================================================================================
    ' SaveDataFileボタンをクリックした際の処理になります
    ' 指定したチャネルのデータをcsvファイルに保存します
    '================================================================================
    Private Sub button_SaveDataFile_Click(sender As Object, e As EventArgs) Handles button_SaveDataFile.Click
        Dim filepath As String            ' ファイルパス
        Dim aochannel As Short            ' チャネル番号
        Dim param As Short                ' 単数or複数
        Dim ret As Long                   ' 戻り値取得用

        '----------------------------------------
        ' テキストボックスからデータファイル名を取得
        '----------------------------------------
        filepath = textBox_ArbitraryDataFile.Text
        '----------------------------------------
        ' コンボボックスからチャネルのパラメータ（単数or複数）を取得
        '----------------------------------------
        param = comboBox_Param.SelectedIndex
        '----------------------------------------
        ' コンボボックスからチャネル番号を取得
        '----------------------------------------
        aochannel = comboBox_AoChannel.SelectedIndex
        '----------------------------------------
        ' データファイルの設定
        '----------------------------------------
        '-------------------
        ' 単数チャネルの場合、第3引数をチャネル番号にする
        '-------------------
        If param = 0 Then
            Try
                ret = c_fgen.SaveDataFile(filepath, param, aochannel)
            Catch
                MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "エラー", MessageBoxButtons.OK)
                Return
            End Try
            '-------------------
            ' 複数チャネルの場合、第3引数を0にする
            '-------------------
        Else
            Try
                ret = c_fgen.SaveDataFile(filepath, param, 0)
            Catch
                MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "エラー", MessageBoxButtons.OK)
                Return
            End Try
        End If
        '----------------------------------------
        ' エラーコードを表示
        '----------------------------------------
        textBox_ReturnNum.Text = String.Format("SaveDataFile = {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 = String.Format("{0} ", status)
        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
