﻿Imports System.Text

Public Class frmCntMode
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    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

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents Label10 As System.Windows.Forms.Label
    Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
    Friend WithEvents Label9 As System.Windows.Forms.Label
    Friend WithEvents Label8 As System.Windows.Forms.Label
    Friend WithEvents Label7 As System.Windows.Forms.Label
    Friend WithEvents Label6 As System.Windows.Forms.Label
    Friend WithEvents Label5 As System.Windows.Forms.Label
    Friend WithEvents Label4 As System.Windows.Forms.Label
    Friend WithEvents Label3 As System.Windows.Forms.Label
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents cboCounterMode 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 cboCountDirection As System.Windows.Forms.ComboBox
    Friend WithEvents cboSignalSource As System.Windows.Forms.ComboBox
    Friend WithEvents cboZPhaseLogic As System.Windows.Forms.ComboBox
    Friend WithEvents cboZPhaseMode As System.Windows.Forms.ComboBox
    Friend WithEvents btnCancel As System.Windows.Forms.Button
    Friend WithEvents btnOK As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Label10 = New System.Windows.Forms.Label()
        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.btnCancel = New System.Windows.Forms.Button()
        Me.btnOK = New System.Windows.Forms.Button()
        Me.GroupBox1.SuspendLayout()
        Me.SuspendLayout()
        '
        'Label10
        '
        Me.Label10.Location = New System.Drawing.Point(20, 338)
        Me.Label10.Name = "Label10"
        Me.Label10.Size = New System.Drawing.Size(224, 19)
        Me.Label10.TabIndex = 11
        Me.Label10.Text = "All channels use the same setting."
        '
        '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(12, 12)
        Me.GroupBox1.Name = "GroupBox1"
        Me.GroupBox1.Size = New System.Drawing.Size(376, 317)
        Me.GroupBox1.TabIndex = 10
        Me.GroupBox1.TabStop = False
        Me.GroupBox1.Text = "Counter Mode"
        '
        'Label9
        '
        Me.Label9.Location = New System.Drawing.Point(16, 278)
        Me.Label9.Name = "Label9"
        Me.Label9.Size = New System.Drawing.Size(104, 23)
        Me.Label9.TabIndex = 17
        Me.Label9.Text = "Count Mode:"
        '
        'Label8
        '
        Me.Label8.Location = New System.Drawing.Point(16, 247)
        Me.Label8.Name = "Label8"
        Me.Label8.Size = New System.Drawing.Size(104, 23)
        Me.Label8.TabIndex = 16
        Me.Label8.Text = "Digital Filter:"
        '
        'Label7
        '
        Me.Label7.Location = New System.Drawing.Point(16, 214)
        Me.Label7.Name = "Label7"
        Me.Label7.Size = New System.Drawing.Size(104, 23)
        Me.Label7.TabIndex = 15
        Me.Label7.Text = "Clear:"
        '
        'Label6
        '
        Me.Label6.Location = New System.Drawing.Point(16, 183)
        Me.Label6.Name = "Label6"
        Me.Label6.Size = New System.Drawing.Size(104, 23)
        Me.Label6.TabIndex = 14
        Me.Label6.Text = "Multiple:"
        '
        'Label5
        '
        Me.Label5.Location = New System.Drawing.Point(16, 151)
        Me.Label5.Name = "Label5"
        Me.Label5.Size = New System.Drawing.Size(120, 24)
        Me.Label5.TabIndex = 13
        Me.Label5.Text = "Phase:"
        '
        'Label4
        '
        Me.Label4.Location = New System.Drawing.Point(16, 120)
        Me.Label4.Name = "Label4"
        Me.Label4.Size = New System.Drawing.Size(104, 23)
        Me.Label4.TabIndex = 12
        Me.Label4.Text = "Count Direction:"
        '
        'Label3
        '
        Me.Label3.Location = New System.Drawing.Point(16, 88)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(104, 24)
        Me.Label3.TabIndex = 11
        Me.Label3.Text = "Signal Type:"
        '
        'Label2
        '
        Me.Label2.Location = New System.Drawing.Point(16, 57)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(104, 23)
        Me.Label2.TabIndex = 10
        Me.Label2.Text = "Z Phase Logic:"
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(16, 26)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(104, 23)
        Me.Label1.TabIndex = 9
        Me.Label1.Text = "Z Phase Mode:"
        '
        'cboCounterMode
        '
        Me.cboCounterMode.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.cboCounterMode.Location = New System.Drawing.Point(144, 279)
        Me.cboCounterMode.Name = "cboCounterMode"
        Me.cboCounterMode.Size = New System.Drawing.Size(216, 21)
        Me.cboCounterMode.TabIndex = 8
        '
        'cboFilter
        '
        Me.cboFilter.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.cboFilter.Location = New System.Drawing.Point(144, 247)
        Me.cboFilter.Name = "cboFilter"
        Me.cboFilter.Size = New System.Drawing.Size(216, 21)
        Me.cboFilter.TabIndex = 7
        '
        'cboSyncClear
        '
        Me.cboSyncClear.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.cboSyncClear.Location = New System.Drawing.Point(144, 214)
        Me.cboSyncClear.Name = "cboSyncClear"
        Me.cboSyncClear.Size = New System.Drawing.Size(216, 21)
        Me.cboSyncClear.TabIndex = 6
        '
        'cboMul
        '
        Me.cboMul.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.cboMul.Location = New System.Drawing.Point(144, 181)
        Me.cboMul.Name = "cboMul"
        Me.cboMul.Size = New System.Drawing.Size(216, 21)
        Me.cboMul.TabIndex = 5
        '
        'cboPhase
        '
        Me.cboPhase.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.cboPhase.Location = New System.Drawing.Point(144, 149)
        Me.cboPhase.Name = "cboPhase"
        Me.cboPhase.Size = New System.Drawing.Size(216, 21)
        Me.cboPhase.TabIndex = 4
        '
        'cboCountDirection
        '
        Me.cboCountDirection.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.cboCountDirection.Location = New System.Drawing.Point(144, 116)
        Me.cboCountDirection.Name = "cboCountDirection"
        Me.cboCountDirection.Size = New System.Drawing.Size(216, 21)
        Me.cboCountDirection.TabIndex = 3
        '
        'cboSignalSource
        '
        Me.cboSignalSource.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.cboSignalSource.Location = New System.Drawing.Point(144, 84)
        Me.cboSignalSource.Name = "cboSignalSource"
        Me.cboSignalSource.Size = New System.Drawing.Size(216, 21)
        Me.cboSignalSource.TabIndex = 2
        '
        'cboZPhaseLogic
        '
        Me.cboZPhaseLogic.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.cboZPhaseLogic.Location = New System.Drawing.Point(144, 51)
        Me.cboZPhaseLogic.Name = "cboZPhaseLogic"
        Me.cboZPhaseLogic.Size = New System.Drawing.Size(216, 21)
        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(144, 19)
        Me.cboZPhaseMode.Name = "cboZPhaseMode"
        Me.cboZPhaseMode.Size = New System.Drawing.Size(216, 21)
        Me.cboZPhaseMode.TabIndex = 0
        '
        'btnCancel
        '
        Me.btnCancel.Location = New System.Drawing.Point(276, 366)
        Me.btnCancel.Name = "btnCancel"
        Me.btnCancel.Size = New System.Drawing.Size(112, 28)
        Me.btnCancel.TabIndex = 13
        Me.btnCancel.Text = "Cancel"
        '
        'btnOK
        '
        Me.btnOK.Location = New System.Drawing.Point(148, 366)
        Me.btnOK.Name = "btnOK"
        Me.btnOK.Size = New System.Drawing.Size(112, 28)
        Me.btnOK.TabIndex = 12
        Me.btnOK.Text = "OK"
        '
        'frmCntMode
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(400, 407)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Label10, Me.GroupBox1, Me.btnCancel, Me.btnOK})
        Me.MaximizeBox = False
        Me.MinimizeBox = False
        Me.Name = "frmCntMode"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = "Counter Mode"
        Me.GroupBox1.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

#End Region

    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, "Not Use")
            .Items.Insert(1, "Next One")
            .Items.Insert(2, "Every Time")
        End With
        ' Z Phase Logic
        With cboZPhaseLogic
            .Items.Clear()
            .Items.Insert(0, "Positive")
            .Items.Insert(1, "Negative")
        End With
        ' Signal Source
        With cboSignalSource
            .Items.Clear()
            .Items.Insert(0, "Line Receiver")
            .Items.Insert(1, "TTL")
        End With
        ' Count Direction
        With cboCountDirection
            .Items.Clear()
            .Items.Insert(0, "Down count")
            .Items.Insert(1, "Up count")
        End With
        ' 1Phase/2Phase
        With cboPhase
            .Items.Clear()
            .Items.Insert(0, "1 Phase")
            .Items.Insert(1, "2 Phase")
            .Items.Insert(2, "Gate Control")
        End With
        ' Mul
        With cboMul
            .Items.Clear()
            .Items.Insert(0, "X1")
            .Items.Insert(1, "X2")
            .Items.Insert(2, "X4")
        End With
        ' Sync Clear
        With cboSyncClear
            .Items.Clear()
            .Items.Insert(0, "Async")
            .Items.Insert(1, "Sync")
        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
        ' Counter Mode
        With cboCounterMode
            .Items.Clear()
            .Items.Insert(0, "Normal")
            .Items.Insert(1, "Differencial")
        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

        '-------------------------------
        ' Get the set information
        '-------------------------------
        '--------------------------------
        ' Z Phase Mode
        '--------------------------------
        wZPhaseMode = 0
        If cboZPhaseMode.Text = "Not Use" Then wZPhaseMode = CNT_ZPHASE_NOT_USE
        If cboZPhaseMode.Text = "Next One" Then wZPhaseMode = CNT_ZPHASE_NEXT_ONE
        If cboZPhaseMode.Text = "Every Time" Then wZPhaseMode = CNT_ZPHASE_EVERY_TIME
        '--------------------------------
        ' Z Phase Logic
        '--------------------------------
        wZPhaseLogic = 0
        If cboZPhaseLogic.Text = "Positive" Then wZPhaseLogic = CNT_ZLOGIC_POSITIVE
        If cboZPhaseLogic.Text = "Negative" Then wZPhaseLogic = CNT_ZLOGIC_NEGATIVE
        '--------------------------------
        ' External Signal Source
        '--------------------------------
        wSignalSource = 0
        If cboSignalSource.Text = "TTL" Then wSignalSource = CNT_SIGTYPE_TTL
        If cboSignalSource.Text = "Line Receiver" Then wSignalSource = CNT_SIGTYPE_LINERECEIVER
        '--------------------------------
        ' Count Direction
        '--------------------------------
        wCountDirection = 0
        If cboCountDirection.Text = "Down count" Then wCountDirection = CNT_DIR_DOWN
        If cboCountDirection.Text = "Up count" Then wCountDirection = CNT_DIR_UP
        '--------------------------------
        ' Operation mode
        '--------------------------------
        wPhase = 0
        If cboPhase.Text = "1 Phase" Then wPhase = CNT_MODE_1PHASE
        If cboPhase.Text = "2 Phase" Then wPhase = CNT_MODE_2PHASE
        If cboPhase.Text = "Gate Control" Then wPhase = CNT_MODE_GATECONTROL
        '--------------------------------
        ' Multiple
        '--------------------------------
        wMul = 0
        If cboMul.Text = "X1" Then wMul = CNT_MUL_X1
        If cboMul.Text = "X2" Then wMul = CNT_MUL_X2
        If cboMul.Text = "X4" Then wMul = CNT_MUL_X4
        '--------------------------------
        ' Synd/Async clear
        '--------------------------------
        wSyncClear = 0
        If cboSyncClear.Text = "Async" Then wSyncClear = CNT_CLR_ASYNC
        If cboSyncClear.Text = "Sync" Then wSyncClear = CNT_CLR_SYNC
        '--------------------------------
        ' Digital filter value
        '--------------------------------
        wFilter = cboFilter.SelectedIndex
        '--------------------------------
        ' Counter Mode
        '--------------------------------
        wCounterMode = 0
        If cboCounterMode.Text = "Normal" Then wCounterMode = CNTS_MODE_NORMAL
        If cboCounterMode.Text = "Differencial" Then wCounterMode = CNTS_MODE_DIFF
        '-------------------------------
        ' Set the set information to device
        '-------------------------------
        For ChNo = 0 To CHANNEL_NUM - 1
            '---------------------------------------------
            ' Set Z phase mode
            '---------------------------------------------
            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
            '---------------------------------------------
            ' Set Z phase logic
            '---------------------------------------------
            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
            '---------------------------------------------
            ' Set external signal source
            '---------------------------------------------
            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
            '---------------------------------------------
            ' Set count direction
            '---------------------------------------------
            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
            '---------------------------------------------
            ' Set operation mode
            '---------------------------------------------
            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
            '---------------------------------------------
            ' Set digital filter value
            '---------------------------------------------
            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
            '---------------------------------------------
            ' Set counter mode
            '---------------------------------------------
            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_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        e.Cancel = True
        Me.Hide()
    End Sub
End Class
