﻿Imports System.Text

Public Class frmMode
    Inherits System.Windows.Forms.Form

#Region " Windows フォーム デザイナで生成されたコード "

    Public Sub New()
        MyBase.New()

        ' この呼び出しは Windows フォーム デザイナで必要です。
        InitializeComponent()

        ' InitializeComponent() 呼び出しの後に初期化を追加します。

    End Sub

    ' Form は dispose をオーバーライドしてコンポーネント一覧を消去します。
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    ' Windows フォーム デザイナで必要です。
    Private components As System.ComponentModel.IContainer

    ' メモ : 以下のプロシージャは、Windows フォーム デザイナで必要です。
    ' Windows フォーム デザイナを使って変更してください。  
    ' コード エディタは使用しないでください。
    Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents Label3 As System.Windows.Forms.Label
    Friend WithEvents Label4 As System.Windows.Forms.Label
    Friend WithEvents Label5 As System.Windows.Forms.Label
    Friend WithEvents Label6 As System.Windows.Forms.Label
    Friend WithEvents Label7 As System.Windows.Forms.Label
    Friend WithEvents Label8 As System.Windows.Forms.Label
    Friend WithEvents Label9 As System.Windows.Forms.Label
    Friend WithEvents Label10 As System.Windows.Forms.Label
    Friend WithEvents cboZPhaseMode As System.Windows.Forms.ComboBox
    Friend WithEvents cboZPhaseLogic As System.Windows.Forms.ComboBox
    Friend WithEvents cboSignalSource As System.Windows.Forms.ComboBox
    Friend WithEvents cboCountDirection As System.Windows.Forms.ComboBox
    Friend WithEvents cboFilter As System.Windows.Forms.ComboBox
    Friend WithEvents cboSyncClear As System.Windows.Forms.ComboBox
    Friend WithEvents cboMul As System.Windows.Forms.ComboBox
    Friend WithEvents cboPhase As System.Windows.Forms.ComboBox
    Friend WithEvents cboCounterMode As System.Windows.Forms.ComboBox
    Friend WithEvents btnOK As System.Windows.Forms.Button
    Friend WithEvents btnCancel As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.GroupBox1 = New System.Windows.Forms.GroupBox()
        Me.Label9 = New System.Windows.Forms.Label()
        Me.Label8 = New System.Windows.Forms.Label()
        Me.Label7 = New System.Windows.Forms.Label()
        Me.Label6 = New System.Windows.Forms.Label()
        Me.Label5 = New System.Windows.Forms.Label()
        Me.Label4 = New System.Windows.Forms.Label()
        Me.Label3 = New System.Windows.Forms.Label()
        Me.Label2 = New System.Windows.Forms.Label()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.cboCounterMode = New System.Windows.Forms.ComboBox()
        Me.cboFilter = New System.Windows.Forms.ComboBox()
        Me.cboSyncClear = New System.Windows.Forms.ComboBox()
        Me.cboMul = New System.Windows.Forms.ComboBox()
        Me.cboPhase = New System.Windows.Forms.ComboBox()
        Me.cboCountDirection = New System.Windows.Forms.ComboBox()
        Me.cboSignalSource = New System.Windows.Forms.ComboBox()
        Me.cboZPhaseLogic = New System.Windows.Forms.ComboBox()
        Me.cboZPhaseMode = New System.Windows.Forms.ComboBox()
        Me.Label10 = New System.Windows.Forms.Label()
        Me.btnOK = New System.Windows.Forms.Button()
        Me.btnCancel = New System.Windows.Forms.Button()
        Me.GroupBox1.SuspendLayout()
        Me.SuspendLayout()
        '
        'GroupBox1
        '
        Me.GroupBox1.Controls.AddRange(New System.Windows.Forms.Control() {Me.Label9, Me.Label8, Me.Label7, Me.Label6, Me.Label5, Me.Label4, Me.Label3, Me.Label2, Me.Label1, Me.cboCounterMode, Me.cboFilter, Me.cboSyncClear, Me.cboMul, Me.cboPhase, Me.cboCountDirection, Me.cboSignalSource, Me.cboZPhaseLogic, Me.cboZPhaseMode})
        Me.GroupBox1.Location = New System.Drawing.Point(8, 8)
        Me.GroupBox1.Name = "GroupBox1"
        Me.GroupBox1.Size = New System.Drawing.Size(408, 272)
        Me.GroupBox1.TabIndex = 0
        Me.GroupBox1.TabStop = False
        Me.GroupBox1.Text = "カウンタ モード"
        '
        'Label9
        '
        Me.Label9.Location = New System.Drawing.Point(16, 239)
        Me.Label9.Name = "Label9"
        Me.Label9.Size = New System.Drawing.Size(104, 20)
        Me.Label9.TabIndex = 17
        Me.Label9.Text = "カウントモード："
        '
        'Label8
        '
        Me.Label8.Location = New System.Drawing.Point(16, 212)
        Me.Label8.Name = "Label8"
        Me.Label8.Size = New System.Drawing.Size(104, 20)
        Me.Label8.TabIndex = 16
        Me.Label8.Text = "デジタルフィルタ値："
        '
        'Label7
        '
        Me.Label7.Location = New System.Drawing.Point(16, 184)
        Me.Label7.Name = "Label7"
        Me.Label7.Size = New System.Drawing.Size(104, 20)
        Me.Label7.TabIndex = 15
        Me.Label7.Text = "クリア："
        '
        'Label6
        '
        Me.Label6.Location = New System.Drawing.Point(16, 157)
        Me.Label6.Name = "Label6"
        Me.Label6.Size = New System.Drawing.Size(104, 20)
        Me.Label6.TabIndex = 14
        Me.Label6.Text = "逓倍："
        '
        'Label5
        '
        Me.Label5.Location = New System.Drawing.Point(16, 130)
        Me.Label5.Name = "Label5"
        Me.Label5.Size = New System.Drawing.Size(120, 20)
        Me.Label5.TabIndex = 13
        Me.Label5.Text = "ワンショットパルス幅："
        '
        'Label4
        '
        Me.Label4.Location = New System.Drawing.Point(16, 103)
        Me.Label4.Name = "Label4"
        Me.Label4.Size = New System.Drawing.Size(104, 20)
        Me.Label4.TabIndex = 12
        Me.Label4.Text = "カウント方向："
        '
        'Label3
        '
        Me.Label3.Location = New System.Drawing.Point(16, 76)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(104, 20)
        Me.Label3.TabIndex = 11
        Me.Label3.Text = "外部信号源："
        '
        'Label2
        '
        Me.Label2.Location = New System.Drawing.Point(16, 49)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(104, 20)
        Me.Label2.TabIndex = 10
        Me.Label2.Text = "Z相論理："
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(16, 22)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(104, 20)
        Me.Label1.TabIndex = 9
        Me.Label1.Text = "Z相モード："
        '
        'cboCounterMode
        '
        Me.cboCounterMode.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.cboCounterMode.Location = New System.Drawing.Point(160, 239)
        Me.cboCounterMode.Name = "cboCounterMode"
        Me.cboCounterMode.Size = New System.Drawing.Size(216, 20)
        Me.cboCounterMode.TabIndex = 8
        '
        'cboFilter
        '
        Me.cboFilter.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.cboFilter.Location = New System.Drawing.Point(160, 212)
        Me.cboFilter.Name = "cboFilter"
        Me.cboFilter.Size = New System.Drawing.Size(216, 20)
        Me.cboFilter.TabIndex = 7
        '
        'cboSyncClear
        '
        Me.cboSyncClear.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.cboSyncClear.Location = New System.Drawing.Point(160, 184)
        Me.cboSyncClear.Name = "cboSyncClear"
        Me.cboSyncClear.Size = New System.Drawing.Size(216, 20)
        Me.cboSyncClear.TabIndex = 6
        '
        'cboMul
        '
        Me.cboMul.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.cboMul.Location = New System.Drawing.Point(160, 157)
        Me.cboMul.Name = "cboMul"
        Me.cboMul.Size = New System.Drawing.Size(216, 20)
        Me.cboMul.TabIndex = 5
        '
        'cboPhase
        '
        Me.cboPhase.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.cboPhase.Location = New System.Drawing.Point(160, 130)
        Me.cboPhase.Name = "cboPhase"
        Me.cboPhase.Size = New System.Drawing.Size(216, 20)
        Me.cboPhase.TabIndex = 4
        '
        'cboCountDirection
        '
        Me.cboCountDirection.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.cboCountDirection.Location = New System.Drawing.Point(160, 103)
        Me.cboCountDirection.Name = "cboCountDirection"
        Me.cboCountDirection.Size = New System.Drawing.Size(216, 20)
        Me.cboCountDirection.TabIndex = 3
        '
        'cboSignalSource
        '
        Me.cboSignalSource.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.cboSignalSource.Location = New System.Drawing.Point(160, 76)
        Me.cboSignalSource.Name = "cboSignalSource"
        Me.cboSignalSource.Size = New System.Drawing.Size(216, 20)
        Me.cboSignalSource.TabIndex = 2
        '
        'cboZPhaseLogic
        '
        Me.cboZPhaseLogic.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.cboZPhaseLogic.Location = New System.Drawing.Point(160, 49)
        Me.cboZPhaseLogic.Name = "cboZPhaseLogic"
        Me.cboZPhaseLogic.Size = New System.Drawing.Size(216, 20)
        Me.cboZPhaseLogic.TabIndex = 1
        '
        'cboZPhaseMode
        '
        Me.cboZPhaseMode.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.cboZPhaseMode.ImeMode = System.Windows.Forms.ImeMode.NoControl
        Me.cboZPhaseMode.Location = New System.Drawing.Point(160, 22)
        Me.cboZPhaseMode.Name = "cboZPhaseMode"
        Me.cboZPhaseMode.Size = New System.Drawing.Size(216, 20)
        Me.cboZPhaseMode.TabIndex = 0
        '
        'Label10
        '
        Me.Label10.Location = New System.Drawing.Point(16, 288)
        Me.Label10.Name = "Label10"
        Me.Label10.Size = New System.Drawing.Size(224, 16)
        Me.Label10.TabIndex = 2
        Me.Label10.Text = "全てのチャネルが同一の設定となります。"
        '
        'btnOK
        '
        Me.btnOK.Location = New System.Drawing.Point(184, 312)
        Me.btnOK.Name = "btnOK"
        Me.btnOK.Size = New System.Drawing.Size(104, 26)
        Me.btnOK.TabIndex = 4
        Me.btnOK.Text = "OK"
        '
        'btnCancel
        '
        Me.btnCancel.Location = New System.Drawing.Point(304, 312)
        Me.btnCancel.Name = "btnCancel"
        Me.btnCancel.Size = New System.Drawing.Size(104, 26)
        Me.btnCancel.TabIndex = 5
        Me.btnCancel.Text = "ｷｬﾝｾﾙ"
        '
        'frmMode
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 12)
        Me.ClientSize = New System.Drawing.Size(424, 350)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnCancel, Me.btnOK, Me.Label10, Me.GroupBox1})
        Me.MaximizeBox = False
        Me.MinimizeBox = False
        Me.Name = "frmMode"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = "カウンタモード設定"
        Me.GroupBox1.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

#End Region

    Dim Id As Short
    '===================================================
    '	ダイアログ初期化
    '===================================================
    Private Sub frmMode_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        '===================================================
        '	コンボボックス初期化
        '===================================================
        ' Z相使用方法
        With cboZPhaseMode
            .Items.Clear()
            .Items.Insert(0, "未使用")
            .Items.Insert(1, "1回")
            .Items.Insert(2, "毎回")
        End With
        ' Z相論理
        With cboZPhaseLogic
            .Items.Clear()
            .Items.Insert(0, "正論理")
            .Items.Insert(1, "負論理")
        End With
        ' 外部信号源
        With cboSignalSource
            .Items.Clear()
            .Items.Insert(0, "フォトカプラ絶縁")
            .Items.Insert(1, "TTL")
            .Items.Insert(2, "差動")
        End With
        ' カウント方向
        With cboCountDirection
            .Items.Clear()
            .Items.Insert(0, "ダウンカウント")
            .Items.Insert(1, "アップカウント")
        End With
        ' 動作モード
        With cboPhase
            .Items.Clear()
            .Items.Insert(0, "単相")
            .Items.Insert(1, "2相")
            .Items.Insert(2, "ゲートコントロール")
        End With
        ' 逓倍
        With cboMul
            .Items.Clear()
            .Items.Insert(0, "1逓倍")
            .Items.Insert(1, "2逓倍")
            .Items.Insert(2, "4逓倍")
        End With
        ' 同期/非同期クリア
        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
        '-------------------------------
        ' 初期データのセット
        '-------------------------------
        cboZPhaseMode.SelectedIndex = 0
        cboZPhaseLogic.SelectedIndex = 1
        cboSignalSource.SelectedIndex = 2
        cboCountDirection.SelectedIndex = 1
        cboPhase.SelectedIndex = 1
        cboMul.SelectedIndex = 0
        cboSyncClear.SelectedIndex = 0
        cboFilter.SelectedIndex = 0
        cboCounterMode.SelectedIndex = 0

        Id = Infinite.Id
    End Sub

    '---------------------------------------------
    ' 設定完了処理
    '---------------------------------------------
    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 = "フォトカプラ絶縁" Then wSignalSource = CNT_SIGTYPE_ISOLATE
        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)
                MsgBox(str, MsgBoxStyle.OKOnly, "Error")
                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)
                MsgBox(str, MsgBoxStyle.OKOnly, "Error")
                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)
                MsgBox(str, MsgBoxStyle.OKOnly, "Error")
                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)
                MsgBox(str, MsgBoxStyle.OKOnly, "Error")
                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)
                MsgBox(str, MsgBoxStyle.OKOnly, "Error")
                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)
                MsgBox(str, MsgBoxStyle.OKOnly, "Error")
                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)
                MsgBox(str, MsgBoxStyle.OKOnly, "Error")
                Exit For
            End If
        Next ChNo
        Me.Hide()
    End Sub
    '---------------------------------------------
    ' キャンセル処理
    '---------------------------------------------
    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Hide()
    End Sub

    Private Sub frmMode_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        e.Cancel = True
        Me.Hide()
    End Sub
End Class
