﻿Imports System.Text

Public Class frmCntMode
    Public Shared Id As Short
    '---------------------------------------------------
    ' InitDialog
    '---------------------------------------------------
    Private Sub frmCntMode_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        '-------------------------------
        ' Initialize ComboBox
        '-------------------------------
        ' Z Phase Mode
        With cboZPhaseMode
            .Items.Clear()
            .Items.Insert(0, "未使用")
            .Items.Insert(1, "1回")
            .Items.Insert(2, "毎回")
        End With
        ' Z Phase Logic
        With cboZPhaseLogic
            .Items.Clear()
            .Items.Insert(0, "正論理")
            .Items.Insert(1, "負論理")
        End With
        ' Signal Source
        With cboSignalSource
            .Items.Clear()
            .Items.Insert(0, "差動")
            .Items.Insert(1, "TTL")
        End With
        ' Count Direction
        With cboCountDirection
            .Items.Clear()
            .Items.Insert(0, "ダウンカウント")
            .Items.Insert(1, "アップカウント")
        End With
        ' 1Phase/2Phase
        With cboPhase
            .Items.Clear()
            .Items.Insert(0, "単相")
            .Items.Insert(1, "2相")
            .Items.Insert(2, "ゲートコントロール")
        End With
        ' Mul
        With cboMul
            .Items.Clear()
            .Items.Insert(0, "1逓倍")
            .Items.Insert(1, "2逓倍")
            .Items.Insert(2, "4逓倍")
        End With
        ' Sync Clear
        With cboSyncClear
            .Items.Clear()
            .Items.Insert(0, "非同期クリア")
            .Items.Insert(1, "同期クリア")
        End With
        With cboFilter
            .Items.Clear()
            .Items.Insert(&H0S, "0")
            .Items.Insert(&H1S, "1")
            .Items.Insert(&H2S, "2")
            .Items.Insert(&H3S, "3")
            .Items.Insert(&H4S, "4")
            .Items.Insert(&H5S, "5")
            .Items.Insert(&H6S, "6")
            .Items.Insert(&H7S, "7")
            .Items.Insert(&H8S, "8")
            .Items.Insert(&H9S, "9")
            .Items.Insert(&HAS, "A")
            .Items.Insert(&HBS, "B")
            .Items.Insert(&HCS, "C")
            .Items.Insert(&HDS, "D")
            .Items.Insert(&HES, "E")
            .Items.Insert(&HFS, "F")
        End With
        With cboCounterMode
            .Items.Clear()
            .Items.Insert(0, "積算")
            .Items.Insert(1, "差分")
        End With
        '-------------------------------
        ' Initialize ComboBox Data
        '-------------------------------
        cboZPhaseMode.SelectedIndex = 0
        cboZPhaseLogic.SelectedIndex = 1
        cboSignalSource.SelectedIndex = 0
        cboCountDirection.SelectedIndex = 1
        cboPhase.SelectedIndex = 1
        cboMul.SelectedIndex = 0
        cboSyncClear.SelectedIndex = 0
        cboFilter.SelectedIndex = 0
        cboCounterMode.SelectedIndex = 0

    End Sub

    '---------------------------------------------------
    ' OK Button
    '---------------------------------------------------
    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click

        Dim ChNo As Short
        Dim ret As Integer
        Dim str As String
        Dim error_string As New StringBuilder("", 256)

        Dim wZPhaseMode As Short
        Dim wZPhaseLogic As Short
        Dim wSignalSource As Short
        Dim wCountDirection As Short
        Dim wPhase As Short
        Dim wMul As Short
        Dim wSyncClear As Short
        Dim wFilter As Short
        Dim wCounterMode As Short

        '-------------------------------
        ' 設定情報の取得
        '-------------------------------
        '--------------------------------
        ' Z相使用方法
        '--------------------------------
        wZPhaseMode = 0
        If cboZPhaseMode.Text = "未使用" Then wZPhaseMode = CNT_ZPHASE_NOT_USE
        If cboZPhaseMode.Text = "1回" Then wZPhaseMode = CNT_ZPHASE_NEXT_ONE
        If cboZPhaseMode.Text = "毎回" Then wZPhaseMode = CNT_ZPHASE_EVERY_TIME
        '--------------------------------
        ' Z相使用方法
        '--------------------------------
        wZPhaseLogic = 0
        If cboZPhaseLogic.Text = "正論理" Then wZPhaseLogic = CNT_ZLOGIC_POSITIVE
        If cboZPhaseLogic.Text = "負論理" Then wZPhaseLogic = CNT_ZLOGIC_NEGATIVE
        '--------------------------------
        ' 外部信号源
        '--------------------------------
        wSignalSource = 0
        If cboSignalSource.Text = "TTL" Then wSignalSource = CNT_SIGTYPE_TTL
        If cboSignalSource.Text = "差動" Then wSignalSource = CNT_SIGTYPE_LINERECEIVER
        '--------------------------------
        ' カウント方向
        '--------------------------------
        wCountDirection = 0
        If cboCountDirection.Text = "ダウンカウント" Then wCountDirection = CNT_DIR_DOWN
        If cboCountDirection.Text = "アップカウント" Then wCountDirection = CNT_DIR_UP
        '--------------------------------
        ' 動作モード
        '--------------------------------
        wPhase = 0
        If cboPhase.Text = "単相" Then wPhase = CNT_MODE_1PHASE
        If cboPhase.Text = "2相" Then wPhase = CNT_MODE_2PHASE
        If cboPhase.Text = "ゲートコントロール" Then wPhase = CNT_MODE_GATECONTROL
        '--------------------------------
        ' 逓倍
        '--------------------------------
        wMul = 0
        If cboMul.Text = "1逓倍" Then wMul = CNT_MUL_X1
        If cboMul.Text = "2逓倍" Then wMul = CNT_MUL_X2
        If cboMul.Text = "4逓倍" Then wMul = CNT_MUL_X4
        '--------------------------------
        ' 同期/非同期クリア
        '--------------------------------
        wSyncClear = 0
        If cboSyncClear.Text = "非同期クリア" Then wSyncClear = CNT_CLR_ASYNC
        If cboSyncClear.Text = "同期クリア" Then wSyncClear = CNT_CLR_SYNC
        '--------------------------------
        ' デジタルフィルタ値
        '--------------------------------
        wFilter = cboFilter.SelectedIndex
        '--------------------------------
        ' カウンタモード
        '--------------------------------
        wCounterMode = 0
        If cboCounterMode.Text = "積算" Then wCounterMode = CNTS_MODE_NORMAL
        If cboCounterMode.Text = "差分" Then wCounterMode = CNTS_MODE_DIFF


        '-------------------------------
        ' 設定情報をデバイスへセット
        '-------------------------------
        For ChNo = 0 To CHANNEL_NUM - 1
            '---------------------------------------------
            ' Z相使用方法 設定
            '---------------------------------------------
            ret = CntSetZMode(Id, ChNo, wZPhaseMode)
            If ret <> 0 Then
                CntGetErrorString(ret, error_string)
                str = String.Format("CntSetZMode : ret = {0:X} h" & vbCrLf & error_string.ToString(), ret)
                MessageBox.Show(str, "Error", MessageBoxButtons.OK)
                Exit For
            End If
            '---------------------------------------------
            ' Z相論理 設定
            '---------------------------------------------
            ret = CntSetZLogic(Id, ChNo, wZPhaseLogic)
            If ret <> 0 Then
                CntGetErrorString(ret, error_string)
                str = String.Format("CntSetZLogic : ret = {0:X} h" & vbCrLf & error_string.ToString(), ret)
                MessageBox.Show(str, "Error", MessageBoxButtons.OK)
                Exit For
            End If
            '---------------------------------------------
            ' 外部信号源 設定
            '---------------------------------------------
            ret = CntSelectChannelSignal(Id, ChNo, wSignalSource)
            If ret <> 0 Then
                CntGetErrorString(ret, error_string)
                str = String.Format("CntSelectChannelSignal : ret = {0:X} h" & vbCrLf & error_string.ToString(), ret)
                MessageBox.Show(str, "Error", MessageBoxButtons.OK)
                Exit For
            End If
            '---------------------------------------------
            ' カウント方向 設定
            '---------------------------------------------
            ret = CntSetCountDirection(Id, ChNo, wCountDirection)
            If ret <> 0 Then
                CntGetErrorString(ret, error_string)
                str = String.Format("CntSetCountDirection : ret = {0:X} h" & vbCrLf & error_string.ToString(), ret)
                MessageBox.Show(str, "Error", MessageBoxButtons.OK)
                Exit For
            End If
            '---------------------------------------------
            ' 動作モード 設定
            '---------------------------------------------
            ret = CntSetOperationMode(Id, ChNo, wPhase, wMul, wSyncClear)
            If ret <> 0 Then
                CntGetErrorString(ret, error_string)
                str = String.Format("CntSetOperationMode : ret = {0:X} h" & vbCrLf & error_string.ToString(), ret)
                MessageBox.Show(str, "Error", MessageBoxButtons.OK)
                Exit For
            End If
            '---------------------------------------------
            ' デジタルフィルタ値 設定
            '---------------------------------------------
            ret = CntSetDigitalFilter(Id, ChNo, wFilter)
            If ret <> 0 Then
                CntGetErrorString(ret, error_string)
                str = String.Format("CntSetOperationMode : ret = {0:X} h" & vbCrLf & error_string.ToString(), ret)
                MessageBox.Show(str, "Error", MessageBoxButtons.OK)
                Exit For
            End If
            '---------------------------------------------
            ' カウンタモード値 設定
            '---------------------------------------------
            ret = CntSetCounterMode(Id, ChNo, wCounterMode)
            If ret <> 0 Then
                CntGetErrorString(ret, error_string)
                str = String.Format("CntSetCounterMode : ret = {0:X} h" & vbCrLf & error_string.ToString(), ret)
                MessageBox.Show(str, "Error", MessageBoxButtons.OK)
                Exit For
            End If
        Next ChNo
        Me.Hide()
    End Sub

    '---------------------------------------------------
    ' Cancel Button
    '---------------------------------------------------
    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Hide()
    End Sub

    Private Sub frmCntMode_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
        e.Cancel = True
        Me.Hide()
    End Sub
End Class