Option Strict Off
Option Explicit On 
Public Class Comp
	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 GroupBox2 As System.Windows.Forms.GroupBox
	Friend WithEvents Label2 As System.Windows.Forms.Label
	Friend WithEvents Label1 As System.Windows.Forms.Label
	Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
	Friend WithEvents Label11 As System.Windows.Forms.Label
	Friend WithEvents Label7 As System.Windows.Forms.Label
	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 Label6 As System.Windows.Forms.Label
	Friend WithEvents Label5 As System.Windows.Forms.Label
	Friend WithEvents Label4 As System.Windows.Forms.Label
	Friend WithEvents rdoComp1 As System.Windows.Forms.RadioButton
	Friend WithEvents rdoComp0 As System.Windows.Forms.RadioButton
	Friend WithEvents btnCancel As System.Windows.Forms.Button
	Friend WithEvents btnOK As System.Windows.Forms.Button
	Friend WithEvents txtPulse As System.Windows.Forms.TextBox
	Friend WithEvents txtComp7 As System.Windows.Forms.TextBox
	Friend WithEvents txtComp6 As System.Windows.Forms.TextBox
	Friend WithEvents txtComp5 As System.Windows.Forms.TextBox
	Friend WithEvents txtComp4 As System.Windows.Forms.TextBox
	Friend WithEvents txtComp3 As System.Windows.Forms.TextBox
	Friend WithEvents txtComp2 As System.Windows.Forms.TextBox
	Friend WithEvents txtComp1 As System.Windows.Forms.TextBox
	Friend WithEvents txtComp0 As System.Windows.Forms.TextBox
	<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
		Me.btnCancel = New System.Windows.Forms.Button()
		Me.btnOK = New System.Windows.Forms.Button()
		Me.GroupBox2 = New System.Windows.Forms.GroupBox()
		Me.Label2 = New System.Windows.Forms.Label()
		Me.txtPulse = New System.Windows.Forms.TextBox()
		Me.Label1 = New System.Windows.Forms.Label()
		Me.GroupBox1 = New System.Windows.Forms.GroupBox()
		Me.txtComp7 = New System.Windows.Forms.TextBox()
		Me.txtComp6 = New System.Windows.Forms.TextBox()
		Me.txtComp5 = New System.Windows.Forms.TextBox()
		Me.txtComp4 = New System.Windows.Forms.TextBox()
		Me.txtComp3 = New System.Windows.Forms.TextBox()
		Me.txtComp2 = New System.Windows.Forms.TextBox()
		Me.txtComp1 = New System.Windows.Forms.TextBox()
		Me.txtComp0 = New System.Windows.Forms.TextBox()
		Me.Label11 = New System.Windows.Forms.Label()
		Me.Label7 = New System.Windows.Forms.Label()
		Me.Label10 = New System.Windows.Forms.Label()
		Me.Label9 = New System.Windows.Forms.Label()
		Me.Label8 = 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.rdoComp1 = New System.Windows.Forms.RadioButton()
		Me.rdoComp0 = New System.Windows.Forms.RadioButton()
		Me.GroupBox2.SuspendLayout()
		Me.GroupBox1.SuspendLayout()
		Me.SuspendLayout()
		'
		'btnCancel
		'
		Me.btnCancel.Location = New System.Drawing.Point(136, 402)
		Me.btnCancel.Name = "btnCancel"
		Me.btnCancel.Size = New System.Drawing.Size(96, 25)
		Me.btnCancel.TabIndex = 7
		Me.btnCancel.Text = "Cancel"
		'
		'btnOK
		'
		Me.btnOK.Location = New System.Drawing.Point(22, 402)
		Me.btnOK.Name = "btnOK"
		Me.btnOK.Size = New System.Drawing.Size(96, 25)
		Me.btnOK.TabIndex = 6
		Me.btnOK.Text = "OK"
		'
		'GroupBox2
		'
		Me.GroupBox2.Controls.AddRange(New System.Windows.Forms.Control() {Me.Label2, Me.txtPulse, Me.Label1})
		Me.GroupBox2.Location = New System.Drawing.Point(22, 330)
		Me.GroupBox2.Name = "GroupBox2"
		Me.GroupBox2.Size = New System.Drawing.Size(210, 61)
		Me.GroupBox2.TabIndex = 5
		Me.GroupBox2.TabStop = False
		Me.GroupBox2.Text = "Set Comparison Register"
		'
		'Label2
		'
		Me.Label2.Location = New System.Drawing.Point(177, 31)
		Me.Label2.Name = "Label2"
		Me.Label2.Size = New System.Drawing.Size(24, 19)
		Me.Label2.TabIndex = 2
		Me.Label2.Text = "Hex"
		'
		'txtPulse
		'
		Me.txtPulse.Location = New System.Drawing.Point(84, 27)
		Me.txtPulse.Name = "txtPulse"
		Me.txtPulse.Size = New System.Drawing.Size(85, 20)
		Me.txtPulse.TabIndex = 1
		Me.txtPulse.Text = ""
		'
		'Label1
		'
		Me.Label1.Location = New System.Drawing.Point(17, 28)
		Me.Label1.Name = "Label1"
		Me.Label1.Size = New System.Drawing.Size(72, 19)
		Me.Label1.TabIndex = 0
		Me.Label1.Text = "Pulse Width :"
		'
		'GroupBox1
		'
		Me.GroupBox1.Controls.AddRange(New System.Windows.Forms.Control() {Me.txtComp7, Me.txtComp6, Me.txtComp5, Me.txtComp4, Me.txtComp3, Me.txtComp2, Me.txtComp1, Me.txtComp0, Me.Label11, Me.Label7, Me.Label10, Me.Label9, Me.Label8, Me.Label6, Me.Label5, Me.Label4, Me.rdoComp1, Me.rdoComp0})
		Me.GroupBox1.Location = New System.Drawing.Point(22, 11)
		Me.GroupBox1.Name = "GroupBox1"
		Me.GroupBox1.Size = New System.Drawing.Size(210, 312)
		Me.GroupBox1.TabIndex = 4
		Me.GroupBox1.TabStop = False
		Me.GroupBox1.Text = "Set Comparison"
		'
		'txtComp7
		'
		Me.txtComp7.Location = New System.Drawing.Point(84, 270)
		Me.txtComp7.Name = "txtComp7"
		Me.txtComp7.TabIndex = 61
		Me.txtComp7.Text = ""
		'
		'txtComp6
		'
		Me.txtComp6.Location = New System.Drawing.Point(84, 242)
		Me.txtComp6.Name = "txtComp6"
		Me.txtComp6.TabIndex = 60
		Me.txtComp6.Text = ""
		'
		'txtComp5
		'
		Me.txtComp5.Location = New System.Drawing.Point(84, 214)
		Me.txtComp5.Name = "txtComp5"
		Me.txtComp5.TabIndex = 59
		Me.txtComp5.Text = ""
		'
		'txtComp4
		'
		Me.txtComp4.Location = New System.Drawing.Point(84, 186)
		Me.txtComp4.Name = "txtComp4"
		Me.txtComp4.TabIndex = 58
		Me.txtComp4.Text = ""
		'
		'txtComp3
		'
		Me.txtComp3.Location = New System.Drawing.Point(84, 158)
		Me.txtComp3.Name = "txtComp3"
		Me.txtComp3.TabIndex = 57
		Me.txtComp3.Text = ""
		'
		'txtComp2
		'
		Me.txtComp2.Location = New System.Drawing.Point(84, 130)
		Me.txtComp2.Name = "txtComp2"
		Me.txtComp2.TabIndex = 56
		Me.txtComp2.Text = ""
		'
		'txtComp1
		'
		Me.txtComp1.Location = New System.Drawing.Point(84, 102)
		Me.txtComp1.Name = "txtComp1"
		Me.txtComp1.TabIndex = 55
		Me.txtComp1.Text = ""
		'
		'txtComp0
		'
		Me.txtComp0.Location = New System.Drawing.Point(84, 74)
		Me.txtComp0.Name = "txtComp0"
		Me.txtComp0.TabIndex = 54
		Me.txtComp0.Text = ""
		'
		'Label11
		'
		Me.Label11.Location = New System.Drawing.Point(17, 271)
		Me.Label11.Name = "Label11"
		Me.Label11.Size = New System.Drawing.Size(56, 19)
		Me.Label11.TabIndex = 53
		Me.Label11.Text = "Channel 7"
		'
		'Label7
		'
		Me.Label7.Location = New System.Drawing.Point(17, 160)
		Me.Label7.Name = "Label7"
		Me.Label7.Size = New System.Drawing.Size(56, 18)
		Me.Label7.TabIndex = 49
		Me.Label7.Text = "Channel 3"
		'
		'Label10
		'
		Me.Label10.Location = New System.Drawing.Point(17, 243)
		Me.Label10.Name = "Label10"
		Me.Label10.Size = New System.Drawing.Size(56, 19)
		Me.Label10.TabIndex = 52
		Me.Label10.Text = "Channel 6"
		'
		'Label9
		'
		Me.Label9.Location = New System.Drawing.Point(17, 215)
		Me.Label9.Name = "Label9"
		Me.Label9.Size = New System.Drawing.Size(56, 19)
		Me.Label9.TabIndex = 51
		Me.Label9.Text = "Channel 5"
		'
		'Label8
		'
		Me.Label8.Location = New System.Drawing.Point(17, 187)
		Me.Label8.Name = "Label8"
		Me.Label8.Size = New System.Drawing.Size(56, 19)
		Me.Label8.TabIndex = 50
		Me.Label8.Text = "Channel 4"
		'
		'Label6
		'
		Me.Label6.Location = New System.Drawing.Point(17, 133)
		Me.Label6.Name = "Label6"
		Me.Label6.Size = New System.Drawing.Size(56, 18)
		Me.Label6.TabIndex = 48
		Me.Label6.Text = "Channel 2"
		'
		'Label5
		'
		Me.Label5.Location = New System.Drawing.Point(17, 106)
		Me.Label5.Name = "Label5"
		Me.Label5.Size = New System.Drawing.Size(56, 18)
		Me.Label5.TabIndex = 47
		Me.Label5.Text = "Channel 1"
		'
		'Label4
		'
		Me.Label4.Location = New System.Drawing.Point(17, 79)
		Me.Label4.Name = "Label4"
		Me.Label4.Size = New System.Drawing.Size(56, 18)
		Me.Label4.TabIndex = 46
		Me.Label4.Text = "Channel 0"
		'
		'rdoComp1
		'
		Me.rdoComp1.Location = New System.Drawing.Point(84, 51)
		Me.rdoComp1.Name = "rdoComp1"
		Me.rdoComp1.Size = New System.Drawing.Size(64, 19)
		Me.rdoComp1.TabIndex = 1
		Me.rdoComp1.Text = "Hex"
		'
		'rdoComp0
		'
		Me.rdoComp0.Location = New System.Drawing.Point(84, 28)
		Me.rdoComp0.Name = "rdoComp0"
		Me.rdoComp0.Size = New System.Drawing.Size(64, 19)
		Me.rdoComp0.TabIndex = 0
		Me.rdoComp0.Text = "Dec"
		'
		'Comp
		'
		Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
		Me.ClientSize = New System.Drawing.Size(254, 441)
		Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnCancel, Me.btnOK, Me.GroupBox2, Me.GroupBox1})
		Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog
		Me.MaximizeBox = False
		Me.MinimizeBox = False
		Me.Name = "Comp"
		Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
		Me.Text = "Set Comparison"
		Me.GroupBox2.ResumeLayout(False)
		Me.GroupBox1.ResumeLayout(False)
		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("", 100)					' Temporary string
	Dim m_Comp(7) As TextBox							' Controls of comparison value

    Dim CompValue(7) As Int32                           ' Comparison value
    Dim PulseWidth As Int32                         ' Pulse width

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

		'-----------------------------------
		' Initialize screen items
		'-----------------------------------
		Dim i As Short
		rdoComp0.Checked = True
		m_Comp(0) = txtComp0
		m_Comp(1) = txtComp1
		m_Comp(2) = txtComp2
		m_Comp(3) = txtComp3
		m_Comp(4) = txtComp4
		m_Comp(5) = txtComp5
		m_Comp(6) = txtComp6
		m_Comp(7) = txtComp7
		For i = 0 To 7
			If i >= ChannelNum Then
				m_Comp(i).Enabled = False
			End If
		Next i
		'-----------------------------------
		' Get and display the count-match pulse
		'-----------------------------------
		Dim PlsWidth As Integer
		Ret = CntGetPulseWidth(Id, 0, PlsWidth)
		If Ret <> CNT_ERR_SUCCESS Then
			PlsWidth = 0
		End If
		txtPulse.Text = Hex(PlsWidth)
		'-----------------------------------
		' Get and display count comparison value
		'-----------------------------------
		For i = 0 To (ChannelNum - 1)
			CompValue(i) = 0
			Ret = CntGetCountMatchReg(Id, i, 0, CompValue(i))
			m_Comp(i).Text = String.Format("{0:d}", CompValue(i))
		Next i

	End Sub

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

		Dim i As Short
		'-----------------------------------
		' Get setting value from screen
		'-----------------------------------
        For i = 0 To (ChannelNum - 1)
            szText = m_Comp(i).Text
            Try
                If rdoComp0.Checked = True Then
                    CompValue(i) = Integer.Parse(szText)
                Else
                    CompValue(i) = Integer.Parse(szText, System.Globalization.NumberStyles.HexNumber)
                End If
            Catch ex As Exception
                CompValue(i) = 0
            End Try
        Next i
		szText = txtPulse.Text
        PulseWidth = Integer.Parse(szText, System.Globalization.NumberStyles.HexNumber)
		'-----------------------------------
		' Each channel
		'-----------------------------------
		For i = 0 To (ChannelNum - 1)
			'-----------------------------------
			' Set comparison value
			'-----------------------------------
			Ret = CntSetCountMatchEvent(Id, i, 0, CompValue(i), 1)
			'-----------------------------
			' Process error
			'-----------------------------
			If Ret <> CNT_ERR_SUCCESS Then
				CntGetErrorString(Ret, szError)
				szReturnCode = "CntSetCountMatchEvent : " & szError.ToString()
				MsgBox(szReturnCode, MsgBoxStyle.OKOnly, "Error")
				Exit Sub
			End If
			'-----------------------------------
			' Set pulse width
			'-----------------------------------
			Ret = CntSetPulseWidth(Id, i, PulseWidth)
			'-----------------------------
			' Process error
			'-----------------------------
			If Ret <> CNT_ERR_SUCCESS Then
				CntGetErrorString(Ret, szError)
				szReturnCode = "CntSetPulseWidth : " & szError.ToString()
				MsgBox(szReturnCode, MsgBoxStyle.OKOnly, "Error")
				Exit Sub
			End If
		Next i
		'-----------------------------------
		' Set pulse width
		'-----------------------------------
		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

	'---------------------------------------------------------
	'	Change the display of the comparison value
	'---------------------------------------------------------
	'	Dec
	Private Sub rdoComp0_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles rdoComp0.MouseDown

		'-----------------------------------
		' Refresh screen information
		'-----------------------------------
		Dim i As Short
		For i = 0 To 7
			szText = String.Format("{0:d}", CompValue(i))
			m_Comp(i).Text = szText
		Next i

	End Sub

	'	Hex
	Private Sub rdoComp1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles rdoComp1.MouseDown

		'-----------------------------------
		' Refresh screen information
		'-----------------------------------
		Dim i As Short
		For i = 0 To 7
			szText = String.Format("{0:X8}", CompValue(i))
			m_Comp(i).Text = szText
		Next i

	End Sub
End Class

