Option Strict Off
Option Explicit On 
Public Class Mode
	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 GroupBox1 As System.Windows.Forms.GroupBox
	Friend WithEvents Label10 As System.Windows.Forms.Label
	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 cboClr As System.Windows.Forms.ComboBox
	Friend WithEvents cboMul As System.Windows.Forms.ComboBox
	Friend WithEvents txtFilter As System.Windows.Forms.TextBox
	Friend WithEvents cboPhase As System.Windows.Forms.ComboBox
	Friend WithEvents cboDir As System.Windows.Forms.ComboBox
	Friend WithEvents cboSource As System.Windows.Forms.ComboBox
	Friend WithEvents cboZLogic As System.Windows.Forms.ComboBox
	Friend WithEvents cboZMode As System.Windows.Forms.ComboBox
	Friend WithEvents cboChNo As System.Windows.Forms.ComboBox
	Friend WithEvents btnCancel As System.Windows.Forms.Button
	Friend WithEvents btnSet As System.Windows.Forms.Button
	<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.btnCancel = New System.Windows.Forms.Button()
        Me.btnSet = New System.Windows.Forms.Button()
        Me.GroupBox1 = New System.Windows.Forms.GroupBox()
        Me.Label10 = New System.Windows.Forms.Label()
        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.cboClr = New System.Windows.Forms.ComboBox()
        Me.cboMul = New System.Windows.Forms.ComboBox()
        Me.txtFilter = New System.Windows.Forms.TextBox()
        Me.cboPhase = New System.Windows.Forms.ComboBox()
        Me.cboDir = New System.Windows.Forms.ComboBox()
        Me.cboSource = New System.Windows.Forms.ComboBox()
        Me.cboZLogic = New System.Windows.Forms.ComboBox()
        Me.cboZMode = New System.Windows.Forms.ComboBox()
        Me.cboChNo = New System.Windows.Forms.ComboBox()
        Me.Label2 = New System.Windows.Forms.Label()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.GroupBox1.SuspendLayout()
        Me.SuspendLayout()
        '
        'btnCancel
        '
        Me.btnCancel.Location = New System.Drawing.Point(231, 360)
        Me.btnCancel.Name = "btnCancel"
        Me.btnCancel.Size = New System.Drawing.Size(96, 25)
        Me.btnCancel.TabIndex = 5
        Me.btnCancel.Text = "Cancel"
        '
        'btnSet
        '
        Me.btnSet.Location = New System.Drawing.Point(119, 360)
        Me.btnSet.Name = "btnSet"
        Me.btnSet.Size = New System.Drawing.Size(96, 25)
        Me.btnSet.TabIndex = 4
        Me.btnSet.Text = "Set"
        '
        'GroupBox1
        '
        Me.GroupBox1.Controls.Add(Me.Label10)
        Me.GroupBox1.Controls.Add(Me.Label9)
        Me.GroupBox1.Controls.Add(Me.Label8)
        Me.GroupBox1.Controls.Add(Me.Label7)
        Me.GroupBox1.Controls.Add(Me.Label6)
        Me.GroupBox1.Controls.Add(Me.Label5)
        Me.GroupBox1.Controls.Add(Me.Label4)
        Me.GroupBox1.Controls.Add(Me.Label3)
        Me.GroupBox1.Controls.Add(Me.cboClr)
        Me.GroupBox1.Controls.Add(Me.cboMul)
        Me.GroupBox1.Controls.Add(Me.txtFilter)
        Me.GroupBox1.Controls.Add(Me.cboPhase)
        Me.GroupBox1.Controls.Add(Me.cboDir)
        Me.GroupBox1.Controls.Add(Me.cboSource)
        Me.GroupBox1.Controls.Add(Me.cboZLogic)
        Me.GroupBox1.Controls.Add(Me.cboZMode)
        Me.GroupBox1.Controls.Add(Me.cboChNo)
        Me.GroupBox1.Controls.Add(Me.Label2)
        Me.GroupBox1.Controls.Add(Me.Label1)
        Me.GroupBox1.Location = New System.Drawing.Point(15, 11)
        Me.GroupBox1.Name = "GroupBox1"
        Me.GroupBox1.Size = New System.Drawing.Size(313, 325)
        Me.GroupBox1.TabIndex = 3
        Me.GroupBox1.TabStop = False
        Me.GroupBox1.Text = "Counter Mode"
        '
        'Label10
        '
        Me.Label10.Location = New System.Drawing.Point(280, 191)
        Me.Label10.Name = "Label10"
        Me.Label10.Size = New System.Drawing.Size(26, 18)
        Me.Label10.TabIndex = 18
        Me.Label10.Text = "Hex"
        '
        'Label9
        '
        Me.Label9.Location = New System.Drawing.Point(11, 287)
        Me.Label9.Name = "Label9"
        Me.Label9.Size = New System.Drawing.Size(109, 19)
        Me.Label9.TabIndex = 17
        Me.Label9.Text = "Synchronous Clear :"
        '
        'Label8
        '
        Me.Label8.Location = New System.Drawing.Point(11, 255)
        Me.Label8.Name = "Label8"
        Me.Label8.Size = New System.Drawing.Size(85, 19)
        Me.Label8.TabIndex = 16
        Me.Label8.Text = "Multiplication :"
        '
        'Label7
        '
        Me.Label7.Location = New System.Drawing.Point(11, 223)
        Me.Label7.Name = "Label7"
        Me.Label7.Size = New System.Drawing.Size(104, 19)
        Me.Label7.TabIndex = 15
        Me.Label7.Text = "Number of Phases :"
        '
        'Label6
        '
        Me.Label6.Location = New System.Drawing.Point(11, 159)
        Me.Label6.Name = "Label6"
        Me.Label6.Size = New System.Drawing.Size(88, 19)
        Me.Label6.TabIndex = 14
        Me.Label6.Text = "Count Direction :"
        '
        'Label5
        '
        Me.Label5.Location = New System.Drawing.Point(11, 128)
        Me.Label5.Name = "Label5"
        Me.Label5.Size = New System.Drawing.Size(85, 18)
        Me.Label5.TabIndex = 13
        Me.Label5.Text = "Signal Source :"
        '
        'Label4
        '
        Me.Label4.Location = New System.Drawing.Point(11, 96)
        Me.Label4.Name = "Label4"
        Me.Label4.Size = New System.Drawing.Size(85, 19)
        Me.Label4.TabIndex = 12
        Me.Label4.Text = "Z Phase Logic :"
        '
        'Label3
        '
        Me.Label3.Location = New System.Drawing.Point(11, 65)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(85, 18)
        Me.Label3.TabIndex = 11
        Me.Label3.Text = "Z Phase Mode :"
        '
        'cboClr
        '
        Me.cboClr.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.cboClr.Location = New System.Drawing.Point(121, 283)
        Me.cboClr.Name = "cboClr"
        Me.cboClr.Size = New System.Drawing.Size(151, 21)
        Me.cboClr.TabIndex = 10
        '
        'cboMul
        '
        Me.cboMul.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.cboMul.Location = New System.Drawing.Point(121, 251)
        Me.cboMul.Name = "cboMul"
        Me.cboMul.Size = New System.Drawing.Size(151, 21)
        Me.cboMul.TabIndex = 9
        '
        'txtFilter
        '
        Me.txtFilter.Location = New System.Drawing.Point(121, 188)
        Me.txtFilter.Name = "txtFilter"
        Me.txtFilter.Size = New System.Drawing.Size(151, 20)
        Me.txtFilter.TabIndex = 8
        '
        'cboPhase
        '
        Me.cboPhase.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.cboPhase.Location = New System.Drawing.Point(121, 219)
        Me.cboPhase.Name = "cboPhase"
        Me.cboPhase.Size = New System.Drawing.Size(151, 21)
        Me.cboPhase.TabIndex = 7
        '
        'cboDir
        '
        Me.cboDir.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.cboDir.Location = New System.Drawing.Point(121, 156)
        Me.cboDir.Name = "cboDir"
        Me.cboDir.Size = New System.Drawing.Size(151, 21)
        Me.cboDir.TabIndex = 6
        '
        'cboSource
        '
        Me.cboSource.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.cboSource.Location = New System.Drawing.Point(121, 124)
        Me.cboSource.Name = "cboSource"
        Me.cboSource.Size = New System.Drawing.Size(151, 21)
        Me.cboSource.TabIndex = 5
        '
        'cboZLogic
        '
        Me.cboZLogic.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.cboZLogic.Location = New System.Drawing.Point(121, 92)
        Me.cboZLogic.Name = "cboZLogic"
        Me.cboZLogic.Size = New System.Drawing.Size(151, 21)
        Me.cboZLogic.TabIndex = 4
        '
        'cboZMode
        '
        Me.cboZMode.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.cboZMode.Location = New System.Drawing.Point(121, 60)
        Me.cboZMode.Name = "cboZMode"
        Me.cboZMode.Size = New System.Drawing.Size(151, 21)
        Me.cboZMode.TabIndex = 3
        '
        'cboChNo
        '
        Me.cboChNo.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.cboChNo.Location = New System.Drawing.Point(121, 28)
        Me.cboChNo.Name = "cboChNo"
        Me.cboChNo.Size = New System.Drawing.Size(151, 21)
        Me.cboChNo.TabIndex = 2
        '
        'Label2
        '
        Me.Label2.Location = New System.Drawing.Point(11, 191)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(85, 19)
        Me.Label2.TabIndex = 1
        Me.Label2.Text = "Digital Filter :"
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(11, 33)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(85, 19)
        Me.Label1.TabIndex = 0
        Me.Label1.Text = "Channel No. :"
        '
        'Mode
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(343, 399)
        Me.Controls.Add(Me.btnCancel)
        Me.Controls.Add(Me.btnSet)
        Me.Controls.Add(Me.GroupBox1)
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog
        Me.MaximizeBox = False
        Me.MinimizeBox = False
        Me.Name = "Mode"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = "Set Mode"
        Me.GroupBox1.ResumeLayout(False)
        Me.GroupBox1.PerformLayout()
        Me.ResumeLayout(False)

    End Sub

#End Region
    Dim Ret As Int32                                    ' Return value
	Dim szError As New System.Text.StringBuilder(256)	' Error string
	Dim szReturnCode As New String("", 256)				' The string for error
	Dim szText As New String("", 256)					' Temporary string

	Dim ChNo As Short
	Dim ZMode As Short
	Dim ZLogic As Short
	Dim SigType As Short
	Dim Dir As Short
	Dim Phase As Short
	Dim Mul As Short
	Dim SyncClr As Short
	Dim FilterValue As Short

	'---------------------------------------------------------
	' Define mode
	'---------------------------------------------------------
	Private Structure StrNum
		Public Str As String
		Public Num As Short
		Public Sub New(ByVal Str As String, ByVal Num As Short)
			Me.Str = Str
			Me.Num = Num
		End Sub
	End Structure

	Dim cboZModeL() As StrNum = { _
												New StrNum("Not Use", CNT_ZPHASE_NOT_USE), _
												New StrNum("Next Time", CNT_ZPHASE_NEXT_ONE), _
												New StrNum("Everytime", CNT_ZPHASE_EVERY_TIME), _
												New StrNum("End", 0) _
	}
	Dim cboZLogicL() As StrNum = { _
												New StrNum("Positive Logic", CNT_ZLOGIC_POSITIVE), _
												New StrNum("Negative Logic", CNT_ZLOGIC_NEGATIVE), _
												New StrNum("End", 0) _
	}
	Dim cboChannelSignalL() As StrNum = { _
												New StrNum("Photoisolator", CNT_SIGTYPE_ISOLATE), _
												New StrNum("TTL", CNT_SIGTYPE_TTL), _
												New StrNum("Differential", CNT_SIGTYPE_LINERECEIVER), _
												New StrNum("End", 0) _
	}
	Dim cboCntDirL() As StrNum = { _
												New StrNum("Down-count", CNT_DIR_DOWN), _
												New StrNum("Up-count", CNT_DIR_UP), _
												New StrNum("End", 0) _
	}
	Dim cboPhaseL() As StrNum = { _
												New StrNum("1 Phase", CNT_MODE_1PHASE), _
												New StrNum("2 Phase", CNT_MODE_2PHASE), _
												New StrNum("Gate Control", CNT_MODE_GATECONTROL), _
												New StrNum("End", 0) _
	}
	Dim cboMulL() As StrNum = { _
												New StrNum("x1", CNT_MUL_X1), _
												New StrNum("x2", CNT_MUL_X2), _
												New StrNum("x4", CNT_MUL_X4), _
												New StrNum("End", 0) _
	}
	Dim cboSyncClrL() As StrNum = { _
												New StrNum("Asynchronous Clear", CNT_CLR_ASYNC), _
												New StrNum("Synchronous Clear", CNT_CLR_SYNC), _
												New StrNum("End", 0) _
	}

	'---------------------------------------------------------
	'	Convert a number to a string and convert a string to a number
	'---------------------------------------------------------
	Private Function NumToStr(ByVal p() As StrNum, ByVal Num As Short) As String

		Dim i As Short
		Dim None As String = ""
		i = 0
		While (StrComp(p(i).Str, "End") <> 0)
			If p(i).Num = Num Then
				Return p(i).Str
			End If
			i += 1
		End While
		Return None

	End Function

	Private Function StrToNum(ByVal p() As StrNum, ByVal Str As String) As Short

		Dim i As Short
		i = 0
		While (StrComp(p(i).Str, "End") <> 0)
			If StrComp(p(i).Str, Str) = 0 Then
				Return p(i).Num
			End If
			i += 1
		End While
		Return 0

	End Function

	'---------------------------------------------------------
	'	Initialize dialog
	'---------------------------------------------------------
	Private Sub Mode_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

		'-----------------------------------
		' Initialize combobox
		'-----------------------------------
		Dim i As Short
		For i = 0 To (ChannelNum - 1)
			cboChNo.Items.Add(i)
		Next i
		cboChNo.SelectedIndex = 0
		i = 0
		While (StrComp(cboZModeL(i).Str, "End") <> 0)
			cboZMode.Items.Add(cboZModeL(i).Str)
			i += 1
		End While
		i = 0
		While (StrComp(cboZLogicL(i).Str, "End") <> 0)
			cboZLogic.Items.Add(cboZLogicL(i).Str)
			i += 1
		End While
		i = 0
		While (StrComp(cboChannelSignalL(i).Str, "End") <> 0)
			cboSource.Items.Add(cboChannelSignalL(i).Str)
			i += 1
		End While
		i = 0
		While (StrComp(cboCntDirL(i).Str, "End") <> 0)
			cboDir.Items.Add(cboCntDirL(i).Str)
			i += 1
		End While
		i = 0
		While (StrComp(cboPhaseL(i).Str, "End") <> 0)
			cboPhase.Items.Add(cboPhaseL(i).Str)
			i += 1
		End While
		i = 0
		While (StrComp(cboMulL(i).Str, "End") <> 0)
			cboMul.Items.Add(cboMulL(i).Str)
			i += 1
		End While
		i = 0
		While (StrComp(cboSyncClrL(i).Str, "End") <> 0)
			cboClr.Items.Add(cboSyncClrL(i).Str)
			i += 1
		End While
		'-----------------------------------
		' Select channel 0
		'-----------------------------------
		cboChNo_SelectionChangeCommitted(cboChNo, New System.EventArgs())

	End Sub

	'---------------------------------------------------------
	'	Process Set button
	'---------------------------------------------------------
	Private Sub btnSet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSet.Click
		'-----------------------------------
		' Get mode from screen
		'-----------------------------------
		szText = cboZMode.Text
		ZMode = StrToNum(cboZModeL, szText)
		szText = cboZLogic.Text
		ZLogic = StrToNum(cboZLogicL, szText)
		szText = cboSource.Text
		SigType = StrToNum(cboChannelSignalL, szText)
		szText = cboDir.Text
		Dir = StrToNum(cboCntDirL, szText)
		szText = cboPhase.Text
		Phase = StrToNum(cboPhaseL, szText)
		szText = cboMul.Text
		Mul = StrToNum(cboMulL, szText)
		szText = cboClr.Text
		SyncClr = StrToNum(cboSyncClrL, szText)
		szText = txtFilter.Text
		FilterValue = CLng("&H" & szText)
		'-----------------------------------
		' Set mode
		'-----------------------------------
		Ret = CntSetZMode(Id, ChNo, ZMode)
		If Ret <> CNT_ERR_SUCCESS Then
			CntGetErrorString(Ret, szError)
			szReturnCode = "CntSetZMode : " & szError.ToString()
			MsgBox(szReturnCode, MsgBoxStyle.OKOnly, "Error")
		End If
		Ret = CntSetZLogic(Id, ChNo, ZLogic)
		If Ret <> CNT_ERR_SUCCESS Then
			CntGetErrorString(Ret, szError)
			szReturnCode = "CntSetZLogic : " & szError.ToString()
			MsgBox(szReturnCode, MsgBoxStyle.OKOnly, "Error")
		End If
		Ret = CntSelectChannelSignal(Id, ChNo, SigType)
		If Ret <> CNT_ERR_SUCCESS Then
			CntGetErrorString(Ret, szError)
			szReturnCode = "CntSelectChannelSignal : " & szError.ToString()
			MsgBox(szReturnCode, MsgBoxStyle.OKOnly, "Error")
		End If
		Ret = CntSetCountDirection(Id, ChNo, Dir)
		If Ret <> CNT_ERR_SUCCESS Then
			CntGetErrorString(Ret, szError)
			szReturnCode = "CntSetCountDirection : " & szError.ToString()
			MsgBox(szReturnCode, MsgBoxStyle.OKOnly, "Error")
		End If
		Ret = CntSetOperationMode(Id, ChNo, Phase, Mul, SyncClr)
		If Ret <> CNT_ERR_SUCCESS Then
			CntGetErrorString(Ret, szError)
			szReturnCode = "CntSetOperationMode : " & szError.ToString()
			MsgBox(szReturnCode, MsgBoxStyle.OKOnly, "Error")
		End If
		Ret = CntSetDigitalFilter(Id, ChNo, FilterValue)
		If Ret <> CNT_ERR_SUCCESS Then
			CntGetErrorString(Ret, szError)
			szReturnCode = "CntSetDigitalFilter : " & szError.ToString()
			MsgBox(szReturnCode, MsgBoxStyle.OKOnly, "Error")
		End If

		Me.Close()

	End Sub

	'---------------------------------------------------------
	'	Process Cancel button
	'---------------------------------------------------------
	Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click

		Me.Close()

	End Sub

	'---------------------------------------------------------
	'	Select channel combobox
	'---------------------------------------------------------
	Private Sub cboChNo_SelectionChangeCommitted(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboChNo.SelectionChangeCommitted

		'-----------------------------------
		' Get mode
		'-----------------------------------
		ChNo = CShort(cboChNo.Text)

		CntGetZMode(Id, ChNo, ZMode)
		CntGetZLogic(Id, ChNo, ZLogic)
		CntGetChannelSignal(Id, ChNo, SigType)
		CntGetCountDirection(Id, ChNo, Dir)
		CntGetOperationMode(Id, ChNo, Phase, Mul, SyncClr)
		CntGetDigitalFilter(Id, ChNo, FilterValue)
		'-----------------------------------
		' Set mode
		'-----------------------------------
		cboZMode.Text = NumToStr(cboZModeL, ZMode)
		cboZLogic.Text = NumToStr(cboZLogicL, ZLogic)
		cboSource.Text = NumToStr(cboChannelSignalL, SigType)
		cboDir.Text = NumToStr(cboCntDirL, Dir)
		cboPhase.Text = NumToStr(cboPhaseL, Phase)
		cboMul.Text = NumToStr(cboMulL, Mul)
		cboClr.Text = NumToStr(cboSyncClrL, SyncClr)
		txtFilter.Text = Hex(FilterValue)

	End Sub
End Class
