Imports TUSB0214DAMSampleAppVB.TUSB0214DAMSampleApp
Imports TUSB0214DAMSampleAppVB.TUSB0214DAMSampleAppVB

Public Class Form1

    ' 動作モードenum
    Private Enum bitter As Byte
        DC
        GATE
        TRIGGER
    End Enum

    ' 装置シリアル番号文字列
    Private DevSerialStr As String

    ' 設定値類
    ' SYNC OUT
    Private SyncOutClkN As UInteger ' 現在の要求クロック周波数から生成された設定値
    Private SyncOutFreqD As Double ' SyncOutClkNから算出されたクロック周波数
    ' GROUP A
    Private Ch1Mode As SetMode = SetMode.DC ' 現在の動作モード
    Private Ch1ClkN As UInteger ' 現在の要求クロック周波数から生成された設定値
    Private Ch1FreqD As Double ' Ch1ClkNから算出されたクロック周波数
    Private WavFreqA As Decimal = 100D ' 波形生成設定の周波数を設定するNumericUpDownの値保存用
    Private WavLenA As Decimal = 100D ' 波形生成設定の波形データ数を設定するNumericUpDownの値保存用
    ' GROUP B
    Private Ch2Mode As SetMode = SetMode.DC ' 現在の動作モード
    Private Ch2ClkN As UInteger ' 現在の要求クロック周波数から生成された設定値
    Private Ch2FreqD As Double ' Ch2ClkNから算出されたクロック周波数
    Private WavFreqB As Decimal = 100D ' 波形生成設定の周波数を設定するNumericUpDownの値保存用
    Private WavLenB As Decimal = 100D ' 波形生成設定の波形データ数を設定するNumericUpDownの値保存用

    ' コンボボックスの設定値定義
    Private Const SEL_NONE As Integer = -1 ' 何も選択されていない
    ' DIO設定 index
    Private Const DIO_IN As Integer = 0
    Private Const DIO_HIGH As Integer = 1
    Private Const DIO_LOW As Integer = 2
    ' Sync Out 0 index
    Private Const SYNC0_HIGH As Integer = 0
    Private Const SYNC0_LOW As Integer = 1
    Private Const SYNC0_GATE_TRG As Integer = 2
    Private Const SYNC0_SYNCA As Integer = 3
    Private Const CYNC0_SYNCB As Integer = 4
    ' Sync Out 1 index
    Private Const SYNC1_HIGH As Integer = 0
    Private Const SYNC1_LOW As Integer = 1
    Private Const SYNC1_CLOCK As Integer = 2
    Private Const SYNC1_SYNCA As Integer = 3
    Private Const CYNC1_SYNCB As Integer = 4
    ' EXIO0 index
    Private Const EXIO0_IN As Integer = 0
    Private Const EXIO0_GATE_TRG_IN As Integer = 1
    Private Const EXIO0_HIGH As Integer = 2
    Private Const EXIO0_LOW As Integer = 3
    Private Const EXIO0_GATE_TRG_OUT As Integer = 4
    Private Const EXIO0_SYNCA As Integer = 5
    Private Const EXIO0_SYNCB As Integer = 6
    ' EXIO1 index
    Private Const EXIO1_IN As Integer = 0
    Private Const EXIO1_HIGH As Integer = 1
    Private Const EXIO1_LOW As Integer = 2
    Private Const EXIO1_CLOCK_OUT As Integer = 3
    Private Const EXIO1_SYNCA As Integer = 4
    Private Const EXIO1_SYNCB As Integer = 5

    Private isVoltageCodeSyncing As Boolean = False

    Public Sub New()
        InitializeComponent()
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        OpenBtn.Enabled = True ' オープンボタンイネーブル
        CloseBtn.Enabled = False ' クローズボタンディセーブル

        ' 起動時は機能操作をディセーブルにする
        DIO_G.Enabled = False
        SYNCOUT_G.Enabled = False
        CH1_G.Enabled = False
        CH2_G.Enabled = False
        EXIO_G.Enabled = False

        ' コンボボックスのインデックスを初期化
        DIO_SEL0.SelectedIndex = 0
        DIO_SEL1.SelectedIndex = 0
        DIO_SEL2.SelectedIndex = 0
        DIO_SEL3.SelectedIndex = 0
        DIO_SEL4.SelectedIndex = 0
        DIO_SEL5.SelectedIndex = 0
        DIO_SEL6.SelectedIndex = 0
        DIO_SEL7.SelectedIndex = 0
        Ch1RangeCh.SelectedIndex = 0
        Ch2RangeCh.SelectedIndex = 0
        SyncOutSel0.SelectedIndex = 0
        SyncOutSel1.SelectedIndex = 0
        EXIO_SEL0.SelectedIndex = 0
        EXIO_SEL1.SelectedIndex = 0

        ' ラジオボタンの初期値を設定
        SyncOutFreq_kHz.Checked = True
        Ch1ModeDC.Checked = True
        Ch1ClkInt.Checked = True
        Ch1GTInt.Checked = True
        Ch1Freq_kHz.Checked = True
        Ch1WavSin.Checked = True
        Ch2WavSin.Checked = True
        Ch1WavFreq_kHz.Checked = True
        Ch2ModeDC.Checked = True
        Ch2ClkInt.Checked = True
        Ch2GTInt.Checked = True
        Ch2Freq_kHz.Checked = True
        Ch2WavFreq_kHz.Checked = True

        ' 数値入力の初期値を設定
        SyncOutClkFreq.Value = 100D
        SyncOutClkFreq.Minimum = 0.001D
        SyncOutClkFreq.Maximum = 1000D

        Ch1TrgModeCnt.Value = 1D
        Ch1TrgModeCnt.Minimum = 1D
        Ch1TrgModeCnt.Maximum = 65535D
        Ch1ClkFreq.Value = 100D
        Ch1ClkFreq.Minimum = 0.001D
        Ch1ClkFreq.Maximum = 1000D
        Ch1StopStSpVol.Value = 0D
        Ch1StopStSpVol.Minimum = -10D
        Ch1StopStSpVol.Maximum = 9.9998D
        Ch2StopStSpVol.Value = 0D
        Ch2StopStSpVol.Minimum = -10D
        Ch2StopStSpVol.Maximum = 9.9998D
        Ch1WavAmp.Value = 1D
        Ch1WavAmp.Maximum = 9.9998D
        Ch1WavAmp.Minimum = -10D
        Ch2WavAmp.Value = 1D
        Ch2WavAmp.Maximum = 9.9998D
        Ch2WavAmp.Minimum = -10D
        Ch1WavFreq.Value = 1D
        Ch1WavFreq.Minimum = 0.001D
        Ch1WavFreq.Maximum = 1000D

        Ch2TrgModeCnt.Value = 1D
        Ch2TrgModeCnt.Minimum = 1D
        Ch2TrgModeCnt.Maximum = 65535D
        Ch2ClkFreq.Value = 100D
        Ch2ClkFreq.Minimum = 0.001D
        Ch2ClkFreq.Maximum = 1000D
        Ch2StopStSpVol.Value = 0D
        Ch2StopStSpVol.Minimum = -10D
        Ch2StopStSpVol.Maximum = 9.9998D
        Ch2WavFreq.Value = 1D
        Ch2WavFreq.Minimum = 0.001D
        Ch2WavFreq.Maximum = 1000D
    End Sub

    Private Sub OpenBtn_Click(sender As Object, e As EventArgs) Handles OpenBtn.Click
        DevSerialStr = CInt(SerialNum.Value).ToString("D4")
        Dim retcode = TUSB0214DAM.TUSB0214DAM_Connect(DevSerialStr) ' デバイスの接続処理
        If retcode = 0 Then ' 接続が出来たらコントロールの表示状態を変える
            DIO_G.Enabled = True
            SYNCOUT_G.Enabled = True
            CH1_G.Enabled = True
            CH2_G.Enabled = True
            OpenBtn.Enabled = False
            CloseBtn.Enabled = True
            SerialNum.Enabled = False
            ControlBox = False
            IOCheckTimer.Enabled = True
            Ch1CheckTimer.Enabled = True
            Ch2CheckTimer.Enabled = True
            DigitalIOGroupInit() ' Digial I/O関係の初期化
            SyncOutGroupInit() ' SyncOut関係の初期化
            CH1GroupInit() ' CH1 Control関係の初期化
            CH2GroupInit() ' CH2 Control関係の初期化
            EXIO_G.Enabled = True
        Else
            MessageBox.Show(TUSB0214DAM.GetErrMessage(retcode))
        End If
    End Sub

    Private Sub CloseBtn_Click(sender As Object, e As EventArgs) Handles CloseBtn.Click
        ' 切断時コントロールの表示状態を変える
        IOCheckTimer.Enabled = False
        Ch1CheckTimer.Enabled = False
        Ch2CheckTimer.Enabled = False

        DIO_G.Enabled = False
        SYNCOUT_G.Enabled = False
        CH1_G.Enabled = False
        CH2_G.Enabled = False
        OpenBtn.Enabled = True
        CloseBtn.Enabled = False
        SerialNum.Enabled = True
        EXIO_G.Enabled = False
        ControlBox = True
        TUSB0214DAM.TUSB0214DAM_DisConnect(DevSerialStr) ' 接続解除
    End Sub

    ' Digital I/O 関連処理
    ' ディレクションビットと出力ビットを装置から読み出してから現在の
    ' 値として表示に反映させる
    Private Sub ChkDIO_State()
        Dim retcode As UInteger
        Dim IO_Dir As Byte
        Dim IO_Out As Byte
        retcode = TUSB0214DAM.TUSB0214DAM_Dio_Dir_R(DevSerialStr, IO_Dir) ' 入出力方向の読み込み
        If retcode <> 0 Then
            MessageBox.Show(TUSB0214DAM.GetErrMessage(retcode))
            Exit Sub
        End If
        retcode = TUSB0214DAM.TUSB0214DAM_Dio_Out_R(DevSerialStr, IO_Out) ' 出力設定ビットの読み込み
        If retcode <> 0 Then
            MessageBox.Show(TUSB0214DAM.GetErrMessage(retcode))
            Exit Sub
        End If

        ' 画面コントロールに反映させる
        DIO_SEL0.SelectedIndex = If(Not IO_Dir.IsBitSet0(), DIO_IN, If(IO_Out.IsBitSet0(), DIO_HIGH, DIO_LOW))
        DIO_SEL1.SelectedIndex = If(Not IO_Dir.IsBitSet1(), DIO_IN, If(IO_Out.IsBitSet1(), DIO_HIGH, DIO_LOW))
        DIO_SEL2.SelectedIndex = If(Not IO_Dir.IsBitSet2(), DIO_IN, If(IO_Out.IsBitSet2(), DIO_HIGH, DIO_LOW))
        DIO_SEL3.SelectedIndex = If(Not IO_Dir.IsBitSet3(), DIO_IN, If(IO_Out.IsBitSet3(), DIO_HIGH, DIO_LOW))
        DIO_SEL4.SelectedIndex = If(Not IO_Dir.IsBitSet4(), DIO_IN, If(IO_Out.IsBitSet4(), DIO_HIGH, DIO_LOW))
        DIO_SEL5.SelectedIndex = If(Not IO_Dir.IsBitSet5(), DIO_IN, If(IO_Out.IsBitSet5(), DIO_HIGH, DIO_LOW))
        DIO_SEL6.SelectedIndex = If(Not IO_Dir.IsBitSet6(), DIO_IN, If(IO_Out.IsBitSet6(), DIO_HIGH, DIO_LOW))
        DIO_SEL7.SelectedIndex = If(Not IO_Dir.IsBitSet7(), DIO_IN, If(IO_Out.IsBitSet7(), DIO_HIGH, DIO_LOW))
    End Sub

    ' Digital I/O 関連処理
    ' 入力状態をチェックして表示する
    Private Sub ChkDIO_In()
        Dim IO_In As Byte
        Dim retcode As UInteger = TUSB0214DAM.TUSB0214DAM_Dio_In(DevSerialStr, IO_In) ' 入力状態を読み込む
        If retcode <> 0 Then
            MessageBox.Show(TUSB0214DAM.GetErrMessage(retcode))
            Exit Sub
        End If

        ' 画面への反映　Highなら明るい緑、Lowなら暗い緑にする
        DIO_IN0.BackColor = If(IO_In.IsBitSet0(), Color.LightGreen, Color.DarkGreen)
        DIO_IN1.BackColor = If(IO_In.IsBitSet1(), Color.LightGreen, Color.DarkGreen)
        DIO_IN2.BackColor = If(IO_In.IsBitSet2(), Color.LightGreen, Color.DarkGreen)
        DIO_IN3.BackColor = If(IO_In.IsBitSet3(), Color.LightGreen, Color.DarkGreen)
        DIO_IN4.BackColor = If(IO_In.IsBitSet4(), Color.LightGreen, Color.DarkGreen)
        DIO_IN5.BackColor = If(IO_In.IsBitSet5(), Color.LightGreen, Color.DarkGreen)
        DIO_IN6.BackColor = If(IO_In.IsBitSet6(), Color.LightGreen, Color.DarkGreen)
        DIO_IN7.BackColor = If(IO_In.IsBitSet7(), Color.LightGreen, Color.DarkGreen)

        ' EXIO の入力状態チェック、反映
        retcode = TUSB0214DAM.TUSB0214DAM_EXIO_In(DevSerialStr, IO_In) ' 入力状態を読み込む
        If retcode <> 0 Then
            MessageBox.Show(TUSB0214DAM.GetErrMessage(retcode))
            Exit Sub
        End If

        ' 画面への反映　Highなら明るい緑、Lowなら暗い緑にする
        EXIO_IN0.BackColor = If(IO_In.IsBitSet0(), Color.LightGreen, Color.DarkGreen)
        EXIO_IN1.BackColor = If(IO_In.IsBitSet1(), Color.LightGreen, Color.DarkGreen)
    End Sub

    ' Digital I/O 関連処理
    ' 現在のIO設定を出力に反映する
    Private Sub SetDIO_Out()
        If Not DIO_G.Enabled Then Exit Sub

        Dim IO_Dir As Byte = 0 ' 入出力方向設定 0が入力
        Dim IO_Out As Byte = 0 ' 出力High Low設定 0がLow

        ' 画面コントロールから読み込む
        If DIO_SEL0.SelectedIndex <> DIO_IN Then IO_Dir.BitSet0()
        If DIO_SEL1.SelectedIndex <> DIO_IN Then IO_Dir.BitSet1()
        If DIO_SEL2.SelectedIndex <> DIO_IN Then IO_Dir.BitSet2()
        If DIO_SEL3.SelectedIndex <> DIO_IN Then IO_Dir.BitSet3()
        If DIO_SEL4.SelectedIndex <> DIO_IN Then IO_Dir.BitSet4()
        If DIO_SEL5.SelectedIndex <> DIO_IN Then IO_Dir.BitSet5()
        If DIO_SEL6.SelectedIndex <> DIO_IN Then IO_Dir.BitSet6()
        If DIO_SEL7.SelectedIndex <> DIO_IN Then IO_Dir.BitSet7()

        If DIO_SEL0.SelectedIndex = DIO_HIGH Then IO_Out.BitSet0()
        If DIO_SEL1.SelectedIndex = DIO_HIGH Then IO_Out.BitSet1()
        If DIO_SEL2.SelectedIndex = DIO_HIGH Then IO_Out.BitSet2()
        If DIO_SEL3.SelectedIndex = DIO_HIGH Then IO_Out.BitSet3()
        If DIO_SEL4.SelectedIndex = DIO_HIGH Then IO_Out.BitSet4()
        If DIO_SEL5.SelectedIndex = DIO_HIGH Then IO_Out.BitSet5()
        If DIO_SEL6.SelectedIndex = DIO_HIGH Then IO_Out.BitSet6()
        If DIO_SEL7.SelectedIndex = DIO_HIGH Then IO_Out.BitSet7()

        Dim retcode As UInteger
        retcode = TUSB0214DAM.TUSB0214DAM_Dio_Dir(DevSerialStr, IO_Dir) ' 入出力を設定
        If retcode <> 0 Then
            MessageBox.Show(TUSB0214DAM.GetErrMessage(retcode))
        End If
        retcode = TUSB0214DAM.TUSB0214DAM_Dio_Out(DevSerialStr, IO_Out) ' High Low を設定
        If retcode <> 0 Then
            MessageBox.Show(TUSB0214DAM.GetErrMessage(retcode))
        End If
    End Sub

    ' Digital I/O コントロールグループの初期化をする
    Private Sub DigitalIOGroupInit()
        ChkDIO_State()
        ChkDIO_In()
    End Sub

    ' Sync Out 関連処理
    ' コントロールグループの表示状態を設定する
    Private Sub SyncOutGroupEnControls()
        If Not SYNCOUT_G.Enabled Then Exit Sub

        ' 表示のイネーブルコントロール
        SyncOutClkFreq.Enabled = (SyncOutSel1.SelectedIndex = SYNC1_CLOCK OrElse EXIO_SEL1.SelectedIndex = EXIO1_CLOCK_OUT)
        SyncOutFreq_kHz.Enabled = SyncOutClkFreq.Enabled
        SyncOutFreq_Hz.Enabled = SyncOutClkFreq.Enabled
        SYNCOUT_GTCONT_P.Enabled = (SyncOutSel0.SelectedIndex = SYNC0_GATE_TRG OrElse EXIO_SEL0.SelectedIndex = EXIO1_CLOCK_OUT)
        If SyncOutSelGOff.Checked Then
            SyncOutTrgBtn.Enabled = True
        Else
            SyncOutTrgBtn.Enabled = False
        End If

        ' クロック周波数の表示
        If Etc.GetClockSetting(CDbl(SyncOutClkFreq.Value), SyncOutFreq_kHz.Checked, SyncOutClkN) Then ' クロック設定値の算出
            SyncOutFreqD = Etc.GetActualFrequency(SyncOutClkN) ' 設定値から周波数の算出
            SyncOutActFreq.Text = Etc.GetFrequencyStr(SyncOutFreqD) ' 周波数から周波数を表す文字列の生成
            SyncOutSetBtn.Enabled = True
        Else
            SyncOutClkFreq.AlartColor(True)
            SyncOutSetBtn.Enabled = False
        End If

        ' ボタンの色
        SyncOutSetBtn.BackColor = Color.Salmon
    End Sub

    ' Sync Out 関連処理
    ' コントロールグループの初期化をする
    Private Sub SyncOutGroupInit()
        If SyncOutSel0.SelectedIndex = SEL_NONE Then SyncOutSel0.SelectedIndex = SYNC0_HIGH
        If SyncOutSel1.SelectedIndex = SEL_NONE Then SyncOutSel1.SelectedIndex = SYNC1_HIGH

        Dim GateEn As GateSt
        Dim retcode As UInteger = TUSB0214DAM.TUSB0214DAM_GateSet_R(DevSerialStr, ModuleN.Ext, GateEn) ' ゲート出力の状態を読み出す
        If retcode <> 0 Then
            MessageBox.Show(TUSB0214DAM.GetErrMessage(retcode))
        Else
            If GateEn = GateSt.GOn Then
                SyncOutSelGOn.Checked = True
            Else
                SyncOutSelGOff.Checked = True
            End If
        End If

        Dim OutSel0 As Byte
        Dim OutSel1 As Byte
        retcode = TUSB0214DAM.TUSB0214DAM_Exs_Sel_R(DevSerialStr, OutSel0, OutSel1)
        If retcode <> 0 Then
            MessageBox.Show(TUSB0214DAM.GetErrMessage(retcode))
        Else
            SyncOutSel0.SelectedIndex = OutSel0
            SyncOutSel1.SelectedIndex = OutSel1
        End If

        Dim ExIOSel0 As Byte
        Dim ExIOSel1 As Byte
        retcode = TUSB0214DAM.TUSB0214DAM_EXIO_Sel_R(DevSerialStr, ExIOSel0, ExIOSel1)
        If retcode <> 0 Then
            MessageBox.Show(TUSB0214DAM.GetErrMessage(retcode))
        Else
            EXIO_SEL0.SelectedIndex = ExIOSel0
            EXIO_SEL1.SelectedIndex = ExIOSel1
        End If

        SyncOutGroupEnControls()
        SyncOutSetBtn.AwakeColor(True)
    End Sub

    ' Group A 関連処理
    ' クロック周波数のチェックと実際に設定される周波数の表示
    Private Function FreqChkA() As Boolean
        Dim clkresult As Boolean = True
        Dim wavresult As Boolean = True

        ' クロック設定値のチェック
        If CH1_CLKGEN_G.Enabled Then
            clkresult = Etc.GetClockSetting(CDbl(Ch1ClkFreq.Value), Ch1Freq_kHz.Checked, Ch1ClkN) ' 希望周波数に一番近くなる設定値(整数)を生成
            If clkresult Then
                Ch1FreqD = Etc.GetActualFrequency(Ch1ClkN) ' 設定値で設定した時に実際に設定される周波数を取得
                Ch1ActFreq.Text = Etc.GetFrequencyStr(Ch1FreqD) ' その周波数を文字列で取得
            End If
            Ch1ClkFreq.AlartColor(Not clkresult)
        End If
        ' 波形周波数のチェック
        If CH1_IMAGE_GEN_G.Enabled AndAlso Ch1ClkInt.Checked Then
            Dim len As Integer
            wavresult = Etc.GetWaveLength(CDbl(Ch1WavFreq.Value), Ch1WavFreq_kHz.Checked, Ch1FreqD, len) ' 波形に必要なデータ長を取得
            If wavresult Then
                Ch1WavActFreq.Text = Etc.GetFrequencyStr(Ch1FreqD / len) ' 設定整数値から再度周波数に計算しなおして周波数文字列を得る
                Ch1WavFreq.AlartColor(False)
            End If
            Ch1WavFreq.AlartColor(Not wavresult)
        Else
            Ch1WavFreq.AlartColor(False)
        End If
        Return (clkresult AndAlso wavresult)
    End Function

    ' Group B 関連処理
    ' クロック周波数のチェックと実際に設定される周波数の表示
    Private Function FreqChkB() As Boolean
        Dim clkresult As Boolean = True
        Dim wavresult As Boolean = True

        ' クロック設定値のチェック
        If CH2_CLKGEN_G.Enabled Then
            clkresult = Etc.GetClockSetting(CDbl(Ch2ClkFreq.Value), Ch2Freq_kHz.Checked, Ch2ClkN) ' 希望周波数に一番近くなる設定値(整数)を生成
            If clkresult Then
                Ch2FreqD = Etc.GetActualFrequency(Ch2ClkN) ' 設定値で設定した時に実際に設定される周波数を取得
                Ch2ActFreq.Text = Etc.GetFrequencyStr(Ch2FreqD) ' その周波数を文字列で取得
            End If
            Ch2ClkFreq.AlartColor(Not clkresult)
        End If
        ' 波形周波数のチェック
        If CH2_IMAGE_GEN_G.Enabled AndAlso Ch2ClkInt.Checked Then
            Dim len As Integer
            wavresult = Etc.GetWaveLength(CDbl(Ch2WavFreq.Value), Ch2WavFreq_kHz.Checked, Ch2FreqD, len) ' 波形に必要なデータ長を取得
            If wavresult Then
                Ch2WavActFreq.Text = Etc.GetFrequencyStr(Ch2FreqD / len) ' 設定整数値から再度周波数に計算しなおして周波数文字列を得る
                Ch2WavFreq.AlartColor(False)
            End If
            Ch2WavFreq.AlartColor(Not wavresult)
        Else
            Ch2WavFreq.AlartColor(False)
        End If
        Return (clkresult AndAlso wavresult)
    End Function

    ' Group A 関連処理
    ' 電圧設定チェック
    Private Function VoltChkA() As Boolean
        Dim err As Boolean = False

        Dim RangeCh1 As RangeSel = CType(Ch1RangeCh.SelectedIndex, RangeSel) ' ch1 レンジ
        Dim DCV1 As Double = CDbl(Ch1OffsetDC.Value) ' ch1 直流オフセット電圧
        Dim AMP1 As Double = CDbl(Ch1WavAmp.Value) ' ch1 振幅電圧
        Dim STPV1 As Double = CDbl(Ch1StopStSpVol.Value) ' ch1 停止電圧

        ' 直流電圧設定値がレンジ内に入っているか確認
        err = err OrElse Ch1OffsetDC.AlartColor(Not Etc.VoltRangeChk(RangeCh1, DCV1))

        If Not Ch1ModeDC.Checked Then ' 直流出力ではない場合
            ' 波形のピーク電圧がレンジ内に入っているか確認
            err = err OrElse Ch1WavAmp.AlartColor((Not Etc.VoltRangeChk(RangeCh1, DCV1 + AMP1)) OrElse (Not Etc.VoltRangeChk(RangeCh1, DCV1 - AMP1)))

            ' 停止電圧の設定が有効ならば、停止電圧がレンジ内に入っているか確認
            If Ch1StopStSpChk.Checked Then
                err = err OrElse Ch1StopStSpVol.AlartColor(Not Etc.VoltRangeChk(RangeCh1, STPV1))
            End If
        End If

        Return Not err
    End Function

    ' Group B 関連処理
    ' 電圧設定チェック
    Private Function VoltChkB() As Boolean
        Dim err As Boolean = False

        Dim RangeCh2 As RangeSel = CType(Ch2RangeCh.SelectedIndex, RangeSel) ' ch2 レンジ
        Dim DCV2 As Double = CDbl(Ch2OffsetDC.Value) ' ch2 直流オフセット電圧
        Dim AMP2 As Double = CDbl(Ch2WavAmp.Value) ' ch2 振幅電圧
        Dim STPV2 As Double = CDbl(Ch2StopStSpVol.Value) ' ch2 停止電圧

        ' 直流電圧設定値がレンジ内に入っているか確認
        err = err OrElse Ch2OffsetDC.AlartColor(Not Etc.VoltRangeChk(RangeCh2, DCV2))

        If Not Ch2ModeDC.Checked Then ' 直流出力ではない場合
            ' 波形のピーク電圧がレンジ内に入っているか確認
            err = err OrElse Ch2WavAmp.AlartColor((Not Etc.VoltRangeChk(RangeCh2, DCV2 + AMP2)) OrElse (Not Etc.VoltRangeChk(RangeCh2, DCV2 - AMP2)))

            ' 停止電圧の設定が有効ならば、停止電圧がレンジ内に入っているか確認
            If Ch2StopStSpChk.Checked Then
                err = err OrElse Ch2StopStSpVol.AlartColor(Not Etc.VoltRangeChk(RangeCh2, STPV2))
            End If
        End If

        Return Not err
    End Function

    ' Group A 関連処理
    ' コントロールグループの表示状態を設定する
    Private Sub CH1GroupEnControls()
        If Not CH1_G.Enabled Then Exit Sub

        Dim err As Boolean = False

        ' 表示のイネーブルコントロール
        CH1_CLK_G.Enabled = Not Ch1ModeDC.Checked
        CH1_GT_G.Enabled = Not Ch1ModeDC.Checked
        CH1_GMODE_G.Enabled = Ch1ModeGate.Checked
        Ch1GModeInv.Enabled = Ch1GTExt.Checked OrElse Ch1GTExtFront.Checked
        CH1_TMODE_G.Enabled = Ch1ModeTrg.Checked
        CH1_CLKGEN_G.Enabled = Not Ch1ModeDC.Checked AndAlso Ch1ClkInt.Checked
        Ch1TrgModeCnt.Enabled = Not Ch1TrgModeInf.Checked
        CH1_STPST_G.Enabled = Not Ch1ModeDC.Checked
        Ch1StopStSpVol.Enabled = Ch1StopStSpChk.Checked
        CH1_WGEN_G.Enabled = Not Ch1ModeDC.Checked
        CH1_IMAGE_GEN_G.Enabled = Not Ch1ModeDC.Checked
        CH1_GCONT_G.Enabled = Ch1ModeGate.Checked AndAlso Ch1GTInt.Checked
        Ch1StartTrgBtn.Enabled = Ch1ModeTrg.Checked AndAlso Ch1GTInt.Checked
        Ch1StopBtn.Enabled = Ch1ModeTrg.Checked

        ' クロック周波数のチェックと表示
        err = err OrElse Not FreqChkA()

        ' 電圧レベルチェック
        err = err OrElse Not VoltChkA()

        ' ボタンの表示
        Ch1SetBtn.AwakeColor(True)
        Ch1SetBtn.Enabled = Not err
    End Sub

    ' Group B 関連処理
    ' コントロールグループの表示状態を設定する
    Private Sub CH2GroupEnControls()
        If Not CH2_G.Enabled Then Exit Sub

        Dim err As Boolean = False

        ' 表示のイネーブルコントロール
        CH2_CLK_G.Enabled = Not Ch2ModeDC.Checked
        CH2_GT_G.Enabled = Not Ch2ModeDC.Checked
        CH2_GMODE_G.Enabled = Ch2ModeGate.Checked
        Ch2GModeInv.Enabled = Ch2GTExt.Checked
        CH2_TMODE_G.Enabled = Ch2ModeTrg.Checked
        CH2_CLKGEN_G.Enabled = Not Ch2ModeDC.Checked
        Ch2TrgModeCnt.Enabled = Not Ch2TrgModeInf.Checked
        CH2_STPST_G.Enabled = Not Ch2ModeDC.Checked
        Ch2StopStSpVol.Enabled = Ch2StopStSpChk.Checked
        CH2_WGEN_G.Enabled = Not Ch2ModeDC.Checked
        CH2_IMAGE_GEN_G.Enabled = Not Ch2ModeDC.Checked
        CH2_GCONT_G.Enabled = Ch2ModeGate.Checked AndAlso Ch2GTInt.Checked
        Ch2StartTrgBtn.Enabled = Ch2ModeTrg.Checked AndAlso Ch2GTInt.Checked
        Ch2StopBtn.Enabled = Ch2ModeTrg.Checked

        ' クロック周波数のチェックと表示
        err = err OrElse Not FreqChkB()

        ' 電圧レベルチェック
        err = err OrElse Not VoltChkB()

        ' ボタンの表示
        Ch2SetBtn.AwakeColor(True)
        Ch2SetBtn.Enabled = Not err
    End Sub

    ' Group A
    ' 構造体から設定を読み出して画面表示に反映する
    Private Sub SetSettingsA(sets As stSettings)
        Ch1Mode = sets.Mode
        Select Case sets.Mode
            Case SetMode.DC
                Ch1ModeDC.Checked = True
            Case SetMode.Gate
                Ch1ModeGate.Checked = True
            Case SetMode.Trg
                Ch1ModeTrg.Checked = True
        End Select
        If sets.ClkSel = SigSrc.Internal Then Ch1ClkInt.Checked = True Else Ch1ClkExt.Checked = True
        If sets.GTSel = SigSrc.Internal Then Ch1GTInt.Checked = True Else Ch1GTExt.Checked = True
        If sets.ExGtInv = SigInv.Invert Then Ch1GModeInv.Checked = True Else Ch1GModeInv.Checked = False
        If sets.Rstc = RestartPos.Zero Then Ch1GModeStart.Checked = True Else Ch1GModeStart.Checked = False
        If sets.FStat = FinState.SpecificValue Then Ch1StopStSpChk.Checked = True Else Ch1StopStSpChk.Checked = False
        Ch1StopStSpVol.Value = CDec(Etc.DacCodeToVolt(sets.FVal, CType(Ch1RangeCh.SelectedIndex, RangeSel)))
        If sets.RepCyc = 0 Then Ch1TrgModeInf.Checked = True Else Ch1TrgModeCnt.Value = sets.RepCyc
    End Sub

    ' Group B
    ' 構造体から設定を読み出して画面表示に反映する
    Private Sub SetSettingsB(sets As stSettings)
        Ch2Mode = sets.Mode
        Select Case sets.Mode
            Case SetMode.DC
                Ch2ModeDC.Checked = True
            Case SetMode.Gate
                Ch2ModeGate.Checked = True
            Case SetMode.Trg
                Ch2ModeTrg.Checked = True
        End Select
        If sets.ClkSel = SigSrc.Internal Then Ch2ClkInt.Checked = True Else Ch2ClkExt.Checked = True
        If sets.GTSel = SigSrc.Internal Then Ch2GTInt.Checked = True Else Ch2GTExt.Checked = True
        If sets.ExGtInv = SigInv.Invert Then Ch2GModeInv.Checked = True Else Ch2GModeInv.Checked = False
        If sets.Rstc = RestartPos.Zero Then Ch2GModeStart.Checked = True Else Ch2GModeStart.Checked = False
        If sets.FStat = FinState.SpecificValue Then Ch2StopStSpChk.Checked = True Else Ch2StopStSpChk.Checked = False
        Ch2StopStSpVol.Value = CDec(Etc.DacCodeToVolt(sets.FVal, CType(Ch2RangeCh.SelectedIndex, RangeSel)))
        If sets.RepCyc = 0 Then Ch2TrgModeInf.Checked = True Else Ch2TrgModeCnt.Value = sets.RepCyc
    End Sub

    ' Group A 関連処理
    ' コントロールグループの初期化をする
    Private Sub CH1GroupInit()
        Dim retcode As UInteger

        ' 設定されているレンジを読み込んで画面に反映させる
        Dim Range As RangeSel
        retcode = TUSB0214DAM.TUSB0214DAM_RangeSet_R(DevSerialStr, ChSel.Ch1, Range)
        If retcode <> 0 Then
            MessageBox.Show(TUSB0214DAM.GetErrMessage(retcode))
        Else
            Dim r As Integer = CInt(Range)
            Ch1RangeCh.SelectedIndex = r
        End If

        ' 設定構造体を読み込む
        Dim Damset As stSettings
        retcode = TUSB0214DAM.TUSB0214DAM_DacChSet_R(DevSerialStr, ModuleN.CH1, Damset)
        If retcode <> 0 Then
            MessageBox.Show(TUSB0214DAM.GetErrMessage(retcode))
        Else
            SetSettingsA(Damset)
        End If

        CH1GroupEnControls() ' コントロールの表示設定
        Ch1SetBtn.AwakeColor(True)

        ' ゲートコントロールの設定値を読み込んで画面に反映させる
        Dim GateEn As GateSt
        retcode = TUSB0214DAM.TUSB0214DAM_GateSet_R(DevSerialStr, ModuleN.CH1, GateEn)
        If retcode <> 0 Then
            MessageBox.Show(TUSB0214DAM.GetErrMessage(retcode))
        Else
            If GateEn = GateSt.GOn Then
                Ch1GateOn.Checked = True
            Else
                Ch1GateOff.Checked = True
            End If
        End If
    End Sub

    ' Group B 関連処理
    ' コントロールグループの初期化をする
    Private Sub CH2GroupInit()
        Dim retcode As UInteger

        ' 設定されているレンジを読み込んで画面に反映させる
        Dim Range As RangeSel
        retcode = TUSB0214DAM.TUSB0214DAM_RangeSet_R(DevSerialStr, ChSel.Ch2, Range)
        If retcode <> 0 Then
            MessageBox.Show(TUSB0214DAM.GetErrMessage(retcode))
        Else
            Ch2RangeCh.SelectedIndex = CInt(Range)
        End If

        ' 設定構造体を読み込む
        Dim Damset As stSettings
        retcode = TUSB0214DAM.TUSB0214DAM_DacChSet_R(DevSerialStr, ModuleN.CH2, Damset)
        If retcode <> 0 Then
            MessageBox.Show(TUSB0214DAM.GetErrMessage(retcode))
        Else
            SetSettingsB(Damset)
        End If

        CH2GroupEnControls() ' コントロールの表示設定
        Ch2SetBtn.AwakeColor(True)

        ' ゲートコントロールの設定値を読み込んで画面に反映させる
        Dim GateEn As GateSt
        retcode = TUSB0214DAM.TUSB0214DAM_GateSet_R(DevSerialStr, ModuleN.CH2, GateEn)
        If retcode <> 0 Then
            MessageBox.Show(TUSB0214DAM.GetErrMessage(retcode))
        Else
            If GateEn = GateSt.GOn Then
                Ch1GateOn.Checked = True
            Else
                Ch1GateOff.Checked = True
            End If
        End If
    End Sub

    ' Group A Group B 関連処理
    ' 動作設定
    Private Sub SetModeSetting(moduleSel As ModuleN)
        Dim retcode As UInteger
        Dim sets As stSettings
        sets.FStat = FinState.LastValue
        sets.FVal = 0

        ' 画面から設定値を構造体要素に設定する
        If moduleSel = ModuleN.CH1 Then
            sets.Mode = If(Ch1ModeDC.Checked, SetMode.DC, If(Ch1ModeGate.Checked, SetMode.Gate, SetMode.Trg))
            sets.ClkSel = If(Ch1ClkInt.Checked, SigSrc.Internal, If(Ch1ClkExt.Checked, SigSrc.External, SigSrc.EXIO))
            sets.GTSel = If(Ch1GTInt.Checked, SigSrc.Internal, If(Ch1GTExt.Checked, SigSrc.External, SigSrc.EXIO))
            sets.ExGtInv = If(Ch1GModeInv.Checked, SigInv.Invert, SigInv.NonInvert)
            sets.Rstc = If(Ch1GModeStart.Checked, RestartPos.Zero, RestartPos.Conti)
            sets.xOption = 0
            sets.FStat = If(Ch1StopStSpChk.Checked, FinState.SpecificValue, FinState.LastValue)
            sets.FVal = Etc.DoubleToDacCode(CDbl(Ch1StopStSpVol.Value), CType(Ch1RangeCh.SelectedIndex, RangeSel))
            sets.RepCyc = If(Ch1TrgModeInf.Checked, CShort(0), Ch1TrgModeCnt.Value.ToUShort())
            Ch1Mode = sets.Mode
        Else
            sets.Mode = If(Ch2ModeDC.Checked, SetMode.DC, If(Ch2ModeGate.Checked, SetMode.Gate, SetMode.Trg))
            sets.ClkSel = If(Ch2ClkInt.Checked, SigSrc.Internal, If(Ch2ClkExt.Checked, SigSrc.External, SigSrc.EXIO))
            sets.GTSel = If(Ch2GTInt.Checked, SigSrc.Internal, If(Ch2GTExt.Checked, SigSrc.External, SigSrc.EXIO))
            sets.ExGtInv = If(Ch2GModeInv.Checked, SigInv.Invert, SigInv.NonInvert)
            sets.Rstc = If(Ch2GModeStart.Checked, RestartPos.Zero, RestartPos.Conti)
            sets.xOption = 0
            sets.FStat = If(Ch2StopStSpChk.Checked, FinState.SpecificValue, FinState.LastValue)
            sets.FVal = Etc.DoubleToDacCode(CDbl(Ch2StopStSpVol.Value), CType(Ch2RangeCh.SelectedIndex, RangeSel))
            sets.RepCyc = If(Ch2TrgModeInf.Checked, CShort(0), Ch2TrgModeCnt.Value.ToUShort())
            Ch2Mode = sets.Mode
        End If

        ' 設定を実行する
        retcode = TUSB0214DAM.TUSB0214DAM_DacChSet(DevSerialStr, moduleSel, sets)
        If retcode <> 0 Then
            MessageBox.Show(TUSB0214DAM.GetErrMessage(retcode))
        End If
    End Sub

    ' Group A Group B 関連処理
    ' クロック生成器設定
    Private Sub SetClockGen(moduleSel As ModuleN)
        Dim retcode As UInteger
        Dim DivideN As UInteger
        If moduleSel = ModuleN.CH1 Then
            DivideN = Ch1ClkN - 1 ' 除数は1小さい値を設定する
        Else
            DivideN = Ch2ClkN - 1 ' 除数は1小さい値を設定する
        End If

        retcode = TUSB0214DAM.TUSB0214DAM_Clock_Gen(DevSerialStr, moduleSel, DivideN) ' クロックを設定する
        If retcode <> 0 Then
            MessageBox.Show(TUSB0214DAM.GetErrMessage(retcode))
        End If
    End Sub

    ' Group A Group B 関連処理
    ' レンジの設定
    Private Sub SetRange(ch As ChSel)
        Dim retcode As UInteger
        Dim range As RangeSel

        Select Case ch
            Case ChSel.Ch1
                range = CType(Ch1RangeCh.SelectedIndex, RangeSel)
            Case ChSel.Ch2
                range = CType(Ch2RangeCh.SelectedIndex, RangeSel)
            Case Else
                range = RangeSel.Unipol5
        End Select
        retcode = TUSB0214DAM.TUSB0214DAM_RangeSet(DevSerialStr, ch, range) ' レンジを設定する
        If retcode <> 0 Then
            MessageBox.Show(TUSB0214DAM.GetErrMessage(retcode))
        End If
    End Sub

    ' Group A Group B 関連処理
    ' 直流電圧設定
    Private Sub SetDCVolt(ch As ChSel)
        Dim dcv As UShort
        Select Case ch
            Case ChSel.Ch1
                dcv = Etc.DoubleToDacCode(CDbl(Ch1OffsetDC.Value), CType(Ch1RangeCh.SelectedIndex, RangeSel))
            Case ChSel.Ch2
                dcv = Etc.DoubleToDacCode(CDbl(Ch2OffsetDC.Value), CType(Ch2RangeCh.SelectedIndex, RangeSel))
            Case Else
                dcv = 0
        End Select
        Dim retcode As UInteger = TUSB0214DAM.TUSB0214DAM_DacValSet(DevSerialStr, ch, dcv) ' 出力値を設定する
        If retcode <> 0 Then
            MessageBox.Show(TUSB0214DAM.GetErrMessage(retcode))
        End If
    End Sub

    ' Group A Group B 関連処理
    ' 波形設定
    Private Sub SetWavImage(moduleSel As ModuleN)
        Dim retcode As UInteger
        Dim wimage1() As UShort ' ch1(or ch3)波形イメージ配列
        Dim wimage2() As UShort ' ch2(or ch4)波形イメージ配列
        Dim len As Integer ' 波形長

        If moduleSel = ModuleN.CH1 Then
            Dim shape As WShape ' 波形種類
            Dim range As RangeSel ' 電圧レンジ
            Dim offset As Double ' オフセット電圧
            Dim amp As Double ' 振幅電圧

            If Ch1ClkInt.Checked Then
                Etc.GetWaveLength(CDbl(Ch1WavFreq.Value), Ch1WavFreq_kHz.Checked, Ch1FreqD, len) ' 波形に必要なデータ長を取得
            Else
                len = CInt(Ch1WavFreq.Value)
            End If

            ' ch1 波形イメージ生成
            shape = If(Ch1WavSin.Checked, WShape.Sin, If(Ch1WavRect.Checked, WShape.Rectangle, WShape.Triangle))
            range = CType(Ch1RangeCh.SelectedIndex, RangeSel)
            offset = CDbl(Ch1OffsetDC.Value)
            amp = CDbl(Ch1WavAmp.Value)
            ReDim wimage1(len - 1)
            Etc.WaveImageGen(shape, range, offset, amp, wimage1, len)

            ' 波形イメージの書き込み
            retcode = TUSB0214DAM.TUSB0214DAM_WavImgSet(DevSerialStr, moduleSel, wimage1, len)
            If retcode <> 0 Then
                MessageBox.Show(TUSB0214DAM.GetErrMessage(retcode))
            End If
        Else
            Dim shape As WShape ' 波形種類
            Dim range As RangeSel ' 電圧レンジ
            Dim offset As Double ' オフセット電圧
            Dim amp As Double ' 振幅電圧

            If Ch2ClkInt.Checked Then
                Etc.GetWaveLength(CDbl(Ch2WavFreq.Value), Ch2WavFreq_kHz.Checked, Ch2FreqD, len) ' 波形に必要なデータ長を取得
            Else
                len = CInt(Ch2WavFreq.Value)
            End If

            ' ch2 波形イメージ生成
            shape = If(Ch2WavSin.Checked, WShape.Sin, If(Ch2WavRect.Checked, WShape.Rectangle, WShape.Triangle))
            range = CType(Ch2RangeCh.SelectedIndex, RangeSel)
            offset = CDbl(Ch2OffsetDC.Value)
            amp = CDbl(Ch2WavAmp.Value)
            ReDim wimage1(len - 1)
            Etc.WaveImageGen(shape, range, offset, amp, wimage1, len)

            ' 波形イメージの書き込み
            retcode = TUSB0214DAM.TUSB0214DAM_WavImgSet(DevSerialStr, moduleSel, wimage1, len)
            If retcode <> 0 Then
                MessageBox.Show(TUSB0214DAM.GetErrMessage(retcode))
            End If
        End If
    End Sub

    Private Sub DigitalIOChange(sender As Object, e As EventArgs) Handles DIO_SEL0.Click, DIO_SEL1.Click, DIO_SEL2.Click, DIO_SEL3.Click, DIO_SEL4.Click, DIO_SEL5.Click, DIO_SEL6.Click, DIO_SEL7.Click
        SetDIO_Out() ' Digital I/O 出力を設定する
    End Sub

    Private Sub IOCheckTimer_Tick(sender As Object, e As EventArgs) Handles IOCheckTimer.Tick
        ChkDIO_In() ' Digital I/O 入力状態を確認する
    End Sub

    Private Sub SyncOutChange(sender As Object, e As EventArgs) Handles SyncOutSel0.Click, SyncOutSel1.Click, EXIO_SEL0.SelectedIndexChanged, EXIO_SEL1.SelectedIndexChanged, SyncOutClkFreq.ValueChanged
        SyncOutGroupEnControls() ' Sync Out 表示状態を更新する
    End Sub

    Private Sub SyncOutSelGOff_CheckedChanged(sender As Object, e As EventArgs) Handles SyncOutSelGOff.CheckedChanged, SyncOutSelGOn.CheckedChanged
        Dim retcode = TUSB0214DAM.TUSB0214DAM_GateSet(DevSerialStr, ModuleN.Ext, GateSt.GOff)
        If retcode <> 0 Then
            MessageBox.Show(TUSB0214DAM.GetErrMessage(retcode))
        End If
    End Sub

    Private Sub SyncOutSelGOn_CheckedChanged(sender As Object, e As EventArgs)
        Dim retcode = TUSB0214DAM.TUSB0214DAM_GateSet(DevSerialStr, ModuleN.Ext, GateSt.GOn)
        If retcode <> 0 Then
            MessageBox.Show(TUSB0214DAM.GetErrMessage(retcode))
        End If
    End Sub

    Private Sub SyncOutSetBtn_Click(sender As Object, e As EventArgs) Handles SyncOutSetBtn.Click
        Dim OutSel0 As Byte = SyncOutSel0.SelectedIndex ' Sync Out 0 設定
        Dim OutSel1 As Byte = SyncOutSel1.SelectedIndex ' Sync Out 1 設定
        Dim retcode = TUSB0214DAM.TUSB0214DAM_Exs_Sel(DevSerialStr, OutSel0, OutSel1)
        If retcode <> 0 Then
            MessageBox.Show(TUSB0214DAM.GetErrMessage(retcode))
        End If

        Dim EXIOSel0 As Byte = EXIO_SEL0.SelectedIndex ' EXI/O 0 設定
        Dim EXIOSel1 As Byte = EXIO_SEL1.SelectedIndex ' EXI/O 1 設定
        retcode = TUSB0214DAM.TUSB0214DAM_EXIO_Sel(DevSerialStr, EXIOSel0, EXIOSel1)
        If retcode <> 0 Then
            MessageBox.Show(TUSB0214DAM.GetErrMessage(retcode))
        End If

        If SyncOutSel1.SelectedIndex = SYNC1_CLOCK OrElse EXIO_SEL1.SelectedIndex = EXIO1_CLOCK_OUT Then
            retcode = TUSB0214DAM.TUSB0214DAM_Clock_Gen(DevSerialStr, ModuleN.Ext, SyncOutClkN - 1)
            If retcode <> 0 Then
                MessageBox.Show(TUSB0214DAM.GetErrMessage(retcode))
            End If
        End If

        SyncOutSetBtn.UseVisualStyleBackColor = True
    End Sub

    Private Sub SyncOutTrgBtn_Click(sender As Object, e As EventArgs) Handles SyncOutTrgBtn.Click
        Dim retcode = TUSB0214DAM.TUSB0214DAM_Trigger(DevSerialStr, ModuleN.Ext, TrgSel.TStart)
        If retcode <> 0 Then
            MessageBox.Show(TUSB0214DAM.GetErrMessage(retcode))
        End If
    End Sub

    Private Sub CH1Change(sender As Object, e As EventArgs) Handles Ch1ModeDC.CheckedChanged, Ch1ModeGate.CheckedChanged, Ch1ModeTrg.CheckedChanged, Ch1GTInt.CheckedChanged, Ch1GTExt.CheckedChanged, Ch1GTExtFront.CheckedChanged, Ch1GModeInv.CheckedChanged, Ch1GModeStart.CheckedChanged, Ch1TrgModeCnt.ValueChanged, Ch1TrgModeInf.CheckedChanged, Ch1ClkFreq.ValueChanged, Ch1Freq_kHz.CheckedChanged, Ch1Freq_Hz.CheckedChanged, Ch1StopStSpChk.CheckedChanged, Ch1StopStSpVol.ValueChanged, Ch1RangeCh.SelectedValueChanged, Ch1OffsetDC.ValueChanged, Ch1OffsetDCCode.ValueChanged, Ch1WavSin.CheckedChanged, Ch1WavRect.CheckedChanged, Ch1WavTriang.CheckedChanged, Ch1WavAmp.ValueChanged, Ch1WavFreq.ValueChanged, Ch1WavFreq_kHz.CheckedChanged, Ch1WavFreq_Hz.CheckedChanged
        If Ch1RangeCh.SelectedIndex < 0 Then
            Return
        End If

        If Not isVoltageCodeSyncing Then
            isVoltageCodeSyncing = True

            Dim changed = TryCast(sender, NumericUpDown)

            If changed Is Ch1OffsetDC Then
                Dim code = DoubleToDacCode(Ch1OffsetDC.Value, Ch1RangeCh.SelectedIndex)
                If 0 <= code AndAlso code <= 16383 Then
                    Ch1OffsetDCCode.Value = code
                End If
            ElseIf changed Is Ch1OffsetDCCode Then
                Ch1OffsetDC.Value = CDec(DacCodeToVolt(Ch1OffsetDCCode.Value, Ch1RangeCh.SelectedIndex))
            End If

            isVoltageCodeSyncing = False
        End If
        CH1GroupEnControls()
    End Sub

    Private Sub CH1ClkSrcChange(sender As Object, e As EventArgs) Handles Ch1ClkInt.CheckedChanged, Ch1ClkExt.CheckedChanged, Ch1ClkFront.CheckedChanged
        If Ch1ClkInt.Checked Then ' 内部クロックに変更
            Ch1WavGenFreqLbl.Text = "Freq."
            Ch1WavFreq.DecimalPlaces = 3
            Ch1WavFreq.Maximum = 1000D
            Ch1WavFreq.Minimum = 0.001D
            Ch1WavFreq.Value = WavFreqA
            Ch1WavGenFreqP.Visible = True
        Else ' 外部クロックに変更
            Ch1WavGenFreqLbl.Text = "Leng."
            Ch1WavFreq.DecimalPlaces = 0
            Ch1WavFreq.Maximum = 262144D
            Ch1WavFreq.Minimum = 2D
            Ch1WavFreq.Value = WavLenA
            Ch1WavGenFreqP.Visible = False
        End If
        CH1GroupEnControls()
    End Sub

    Private Sub Ch1SetBtn_Click(sender As Object, e As EventArgs) Handles Ch1SetBtn.Click
        If Ch1ClkInt.Checked Then ' 内部クロック
            WavFreqA = Ch1WavFreq.Value
        Else
            WavLenA = Ch1WavFreq.Value
        End If

        SetModeSetting(ModuleN.CH1)

        If CH1_CLKGEN_G.Enabled Then
            SetClockGen(ModuleN.CH1)
        End If

        SetRange(ChSel.Ch1)

        If Ch1ModeDC.Checked Then ' 直流動作
            SetDCVolt(ChSel.Ch1)
        Else
            SetWavImage(ModuleN.CH1)
        End If

        Ch1SetBtn.AwakeColor(False)
    End Sub

    Private Sub Ch1GateChange(sender As Object, e As EventArgs) Handles Ch1GateOff.CheckedChanged, Ch1GateOn.CheckedChanged
        Dim GateEn = If(Ch1GateOn.Checked, GateSt.GOn, GateSt.GOff)
        Dim retcode = TUSB0214DAM.TUSB0214DAM_GateSet(DevSerialStr, ModuleN.CH1, GateEn)
        If retcode <> 0 Then
            MessageBox.Show(TUSB0214DAM.GetErrMessage(retcode))
        End If
    End Sub

    Private Sub Ch1StartTrgBtn_Click(sender As Object, e As EventArgs) Handles Ch1StartTrgBtn.Click
        Dim retcode = TUSB0214DAM.TUSB0214DAM_Trigger(DevSerialStr, ModuleN.CH1, TrgSel.TStart)
        If retcode <> 0 Then
            MessageBox.Show(TUSB0214DAM.GetErrMessage(retcode))
        End If
    End Sub

    Private Sub Ch1StopBtn_Click(sender As Object, e As EventArgs) Handles Ch1StopBtn.Click
        Dim retcode = TUSB0214DAM.TUSB0214DAM_Trigger(DevSerialStr, ModuleN.CH1, TrgSel.TStop)
        If retcode <> 0 Then
            MessageBox.Show(TUSB0214DAM.GetErrMessage(retcode))
        End If
    End Sub

    Private Sub Ch1CheckTimer_Tick(sender As Object, e As EventArgs) Handles Ch1CheckTimer.Tick
        Dim retcode As UInteger
        Select Case Ch1Mode
            Case SetMode.DC
                Ch1WavGenInd.BackColor = SystemColors.Control
            Case SetMode.Gate
                Dim GateEn As GateSt
                retcode = TUSB0214DAM.TUSB0214DAM_GateSet_R(DevSerialStr, ModuleN.CH1, GateEn)
                If retcode <> 0 Then
                    MessageBox.Show(TUSB0214DAM.GetErrMessage(retcode))
                ElseIf GateEn = GateSt.GOn Then
                    Ch1WavGenInd.BackColor = Color.LightBlue
                Else
                    Ch1WavGenInd.BackColor = SystemColors.Control
                End If
            Case SetMode.Trg
                Dim stat As TrgStat
                retcode = TUSB0214DAM.TUSB0214DAM_Trigger_Stat(DevSerialStr, ModuleN.CH1, stat)
                If retcode <> 0 Then
                    MessageBox.Show(TUSB0214DAM.GetErrMessage(retcode))
                ElseIf stat = TrgStat.TRun Then
                    Ch1WavGenInd.BackColor = Color.LightBlue
                Else
                    Ch1WavGenInd.BackColor = SystemColors.Control
                End If
        End Select
    End Sub

    Private Sub CH2Change(sender As Object, e As EventArgs) Handles Ch2ModeDC.CheckedChanged, Ch2ModeGate.CheckedChanged, Ch2ModeTrg.CheckedChanged, Ch2GTInt.CheckedChanged, Ch2GTExt.CheckedChanged, Ch2GTExtFront.CheckedChanged, Ch2GModeInv.CheckedChanged, Ch2GModeStart.CheckedChanged, Ch2TrgModeCnt.ValueChanged, Ch2ClkFreq.ValueChanged, Ch2Freq_kHz.CheckedChanged, Ch2Freq_Hz.CheckedChanged, Ch2TrgModeInf.CheckedChanged, Ch2StopStSpChk.CheckedChanged, Ch2StopStSpVol.ValueChanged, Ch2RangeCh.SelectedValueChanged, Ch2OffsetDC.ValueChanged, Ch2OffsetDCCode.ValueChanged, Ch2WavSin.CheckedChanged, Ch2WavRect.CheckedChanged, Ch2WavTriang.CheckedChanged, Ch2WavAmp.ValueChanged, Ch2WavFreq.ValueChanged, Ch2WavFreq_kHz.CheckedChanged, Ch2WavFreq_Hz.CheckedChanged
        If Ch2RangeCh.SelectedIndex < 0 Then
            Return
        End If
        If Not isVoltageCodeSyncing Then
            isVoltageCodeSyncing = True

            Dim changed = TryCast(sender, NumericUpDown)

            If changed Is Ch2OffsetDC Then
                Dim code = DoubleToDacCode(Ch2OffsetDC.Value, Ch2RangeCh.SelectedIndex)
                If 0 <= code AndAlso code <= 16383 Then
                    Ch2OffsetDCCode.Value = code
                End If
            ElseIf changed Is Ch2OffsetDCCode Then
                Ch2OffsetDC.Value = CDec(DacCodeToVolt(Ch2OffsetDCCode.Value, Ch2RangeCh.SelectedIndex))
            End If

            isVoltageCodeSyncing = False
        End If

        CH2GroupEnControls()
    End Sub

    Private Sub CH2ClkSrcChange(sender As Object, e As EventArgs) Handles Ch2ClkInt.CheckedChanged, Ch2ClkExt.CheckedChanged, Ch2ClkExtFront.CheckedChanged
        If Ch2ClkInt.Checked Then ' 内部クロックに変更
            Ch2WavGenFreqLbl.Text = "Freq."
            Ch2WavFreq.DecimalPlaces = 3
            Ch2WavFreq.Maximum = 1000D
            Ch2WavFreq.Minimum = 0.001D
            Ch2WavFreq.Value = WavFreqB
            Ch2WavGenFreqP.Visible = True
        Else
            Ch2WavGenFreqLbl.Text = "Leng."
            Ch2WavFreq.DecimalPlaces = 0
            Ch2WavFreq.Maximum = 262144D
            Ch2WavFreq.Minimum = 2D
            Ch2WavFreq.Value = WavLenB
            Ch2WavGenFreqP.Visible = False
        End If
        CH2GroupEnControls()
    End Sub

    Private Sub Ch2SetBtn_Click(sender As Object, e As EventArgs) Handles Ch2SetBtn.Click
        If Ch2ClkInt.Checked Then ' 内部クロック
            WavFreqB = Ch2WavFreq.Value
        Else
            WavLenB = Ch2WavFreq.Value
        End If

        SetModeSetting(ModuleN.CH2)

        If CH2_CLKGEN_G.Enabled Then
            SetClockGen(ModuleN.CH2)
        End If

        SetRange(ChSel.Ch2)

        If Ch2ModeDC.Checked Then ' 直流動作
            SetDCVolt(ChSel.Ch2)
        Else
            SetWavImage(ModuleN.CH2)
        End If

        Ch2SetBtn.AwakeColor(False)
    End Sub

    Private Sub Ch2GateChange(sender As Object, e As EventArgs) Handles Ch2GateOff.CheckedChanged, Ch2GateOn.CheckedChanged
        Dim GateEn = If(Ch2GateOn.Checked, GateSt.GOn, GateSt.GOff)
        Dim retcode = TUSB0214DAM.TUSB0214DAM_GateSet(DevSerialStr, ModuleN.CH2, GateEn)
        If retcode <> 0 Then
            MessageBox.Show(TUSB0214DAM.GetErrMessage(retcode))
        End If
    End Sub

    Private Sub Ch2StartTrgBtn_Click(sender As Object, e As EventArgs) Handles Ch2StartTrgBtn.Click
        Dim retcode = TUSB0214DAM.TUSB0214DAM_Trigger(DevSerialStr, ModuleN.CH2, TrgSel.TStart)
        If retcode <> 0 Then
            MessageBox.Show(TUSB0214DAM.GetErrMessage(retcode))
        End If
    End Sub

    Private Sub Ch2StopBtn_Click(sender As Object, e As EventArgs) Handles Ch2StopBtn.Click
        Dim retcode = TUSB0214DAM.TUSB0214DAM_Trigger(DevSerialStr, ModuleN.CH2, TrgSel.TStop)
        If retcode <> 0 Then
            MessageBox.Show(TUSB0214DAM.GetErrMessage(retcode))
        End If
    End Sub

    Private Sub Ch2CheckTimer_Tick(sender As Object, e As EventArgs) Handles Ch2CheckTimer.Tick
        Dim retcode As UInteger
        Select Case Ch2Mode
            Case SetMode.DC
                Ch2WavGenInd.BackColor = SystemColors.Control
            Case SetMode.Gate
                Dim GateEn As GateSt
                retcode = TUSB0214DAM.TUSB0214DAM_GateSet_R(DevSerialStr, ModuleN.CH2, GateEn)
                If retcode <> 0 Then
                    MessageBox.Show(TUSB0214DAM.GetErrMessage(retcode))
                ElseIf GateEn = GateSt.GOn Then
                    Ch2WavGenInd.BackColor = Color.LightBlue
                Else
                    Ch2WavGenInd.BackColor = SystemColors.Control
                End If
            Case SetMode.Trg
                Dim stat As TrgStat
                retcode = TUSB0214DAM.TUSB0214DAM_Trigger_Stat(DevSerialStr, ModuleN.CH2, stat)
                If retcode <> 0 Then
                    MessageBox.Show(TUSB0214DAM.GetErrMessage(retcode))
                ElseIf stat = TrgStat.TRun Then
                    Ch2WavGenInd.BackColor = Color.LightBlue
                Else
                    Ch2WavGenInd.BackColor = SystemColors.Control
                End If
        End Select
    End Sub

    Private Sub ExIOSetBtn_Click(sender As Object, e As EventArgs)
        Dim ExIOSel0 As Byte = EXIO_SEL0.SelectedIndex ' Sync Out 0 設定
        Dim ExIOSel1 As Byte = EXIO_SEL1.SelectedIndex ' Sync Out 1 設定
        Dim retcode = TUSB0214DAM.TUSB0214DAM_Exs_Sel(DevSerialStr, ExIOSel0, ExIOSel1)
        If retcode <> 0 Then
            MessageBox.Show(TUSB0214DAM.GetErrMessage(retcode))
        End If

        SyncOutSetBtn.UseVisualStyleBackColor = True
    End Sub

    Private Sub op(sender As Object, e As EventArgs)

    End Sub

    Private Sub d(sender As Object, e As EventArgs)

    End Sub
End Class
