Option Strict Off
Option Explicit On 
Imports System.Text

Public Class Interval
    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 Button_TimInit As System.Windows.Forms.Button
    Friend WithEvents Button_TimStartTimer As System.Windows.Forms.Button
    Friend WithEvents Button_TimStopTimer As System.Windows.Forms.Button
    Friend WithEvents Button_TimExit As System.Windows.Forms.Button
    Friend WithEvents Button_Exit As System.Windows.Forms.Button
    Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
    Friend WithEvents GroupBox2 As System.Windows.Forms.GroupBox
    Friend WithEvents GroupBox3 As System.Windows.Forms.GroupBox
    Friend WithEvents GroupBox4 As System.Windows.Forms.GroupBox
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents Text_DeviceName As System.Windows.Forms.TextBox
    Friend WithEvents Label_ProductName As System.Windows.Forms.Label
    Friend WithEvents Text_Interval As System.Windows.Forms.TextBox
    Friend WithEvents Label4 As System.Windows.Forms.Label
    Friend WithEvents Label_Msg_Count As System.Windows.Forms.Label
    Friend WithEvents Text_ReturnCode As System.Windows.Forms.TextBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Button_TimInit = New System.Windows.Forms.Button
        Me.Button_TimStartTimer = New System.Windows.Forms.Button
        Me.Button_TimStopTimer = New System.Windows.Forms.Button
        Me.Button_TimExit = New System.Windows.Forms.Button
        Me.Button_Exit = New System.Windows.Forms.Button
        Me.GroupBox1 = New System.Windows.Forms.GroupBox
        Me.Label_ProductName = New System.Windows.Forms.Label
        Me.Text_DeviceName = New System.Windows.Forms.TextBox
        Me.Label2 = New System.Windows.Forms.Label
        Me.Label1 = New System.Windows.Forms.Label
        Me.GroupBox2 = New System.Windows.Forms.GroupBox
        Me.Label4 = New System.Windows.Forms.Label
        Me.Text_Interval = New System.Windows.Forms.TextBox
        Me.GroupBox3 = New System.Windows.Forms.GroupBox
        Me.Label_Msg_Count = New System.Windows.Forms.Label
        Me.GroupBox4 = New System.Windows.Forms.GroupBox
        Me.Text_ReturnCode = New System.Windows.Forms.TextBox
        Me.GroupBox1.SuspendLayout()
        Me.GroupBox2.SuspendLayout()
        Me.GroupBox3.SuspendLayout()
        Me.GroupBox4.SuspendLayout()
        Me.SuspendLayout()
        '
        'Button_TimInit
        '
        Me.Button_TimInit.Location = New System.Drawing.Point(256, 8)
        Me.Button_TimInit.Name = "Button_TimInit"
        Me.Button_TimInit.Size = New System.Drawing.Size(88, 24)
        Me.Button_TimInit.TabIndex = 0
        Me.Button_TimInit.Text = "TimInit"
        '
        'Button_TimStartTimer
        '
        Me.Button_TimStartTimer.Location = New System.Drawing.Point(256, 40)
        Me.Button_TimStartTimer.Name = "Button_TimStartTimer"
        Me.Button_TimStartTimer.Size = New System.Drawing.Size(88, 24)
        Me.Button_TimStartTimer.TabIndex = 1
        Me.Button_TimStartTimer.Text = "TimStartTimer"
        '
        'Button_TimStopTimer
        '
        Me.Button_TimStopTimer.Location = New System.Drawing.Point(256, 72)
        Me.Button_TimStopTimer.Name = "Button_TimStopTimer"
        Me.Button_TimStopTimer.Size = New System.Drawing.Size(88, 24)
        Me.Button_TimStopTimer.TabIndex = 2
        Me.Button_TimStopTimer.Text = "TimStopTimer"
        '
        'Button_TimExit
        '
        Me.Button_TimExit.Location = New System.Drawing.Point(256, 104)
        Me.Button_TimExit.Name = "Button_TimExit"
        Me.Button_TimExit.Size = New System.Drawing.Size(88, 24)
        Me.Button_TimExit.TabIndex = 3
        Me.Button_TimExit.Text = "TimExit"
        '
        'Button_Exit
        '
        Me.Button_Exit.Location = New System.Drawing.Point(256, 136)
        Me.Button_Exit.Name = "Button_Exit"
        Me.Button_Exit.Size = New System.Drawing.Size(88, 24)
        Me.Button_Exit.TabIndex = 4
        Me.Button_Exit.Text = "Exit"
        '
        'GroupBox1
        '
        Me.GroupBox1.Controls.Add(Me.Label_ProductName)
        Me.GroupBox1.Controls.Add(Me.Text_DeviceName)
        Me.GroupBox1.Controls.Add(Me.Label2)
        Me.GroupBox1.Controls.Add(Me.Label1)
        Me.GroupBox1.Location = New System.Drawing.Point(8, 8)
        Me.GroupBox1.Name = "GroupBox1"
        Me.GroupBox1.Size = New System.Drawing.Size(240, 56)
        Me.GroupBox1.TabIndex = 5
        Me.GroupBox1.TabStop = False
        Me.GroupBox1.Text = "Initialization"
        '
        'Label_ProductName
        '
        Me.Label_ProductName.Location = New System.Drawing.Point(88, 36)
        Me.Label_ProductName.Name = "Label_ProductName"
        Me.Label_ProductName.Size = New System.Drawing.Size(144, 16)
        Me.Label_ProductName.TabIndex = 3
        Me.Label_ProductName.Text = "Not Initialization"
        '
        'Text_DeviceName
        '
        Me.Text_DeviceName.Location = New System.Drawing.Point(88, 12)
        Me.Text_DeviceName.Name = "Text_DeviceName"
        Me.Text_DeviceName.Size = New System.Drawing.Size(120, 20)
        Me.Text_DeviceName.TabIndex = 2
        '
        'Label2
        '
        Me.Label2.Location = New System.Drawing.Point(8, 36)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(80, 16)
        Me.Label2.TabIndex = 1
        Me.Label2.Text = "Product Name:"
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(8, 16)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(80, 16)
        Me.Label1.TabIndex = 0
        Me.Label1.Text = "Device Name:"
        '
        'GroupBox2
        '
        Me.GroupBox2.Controls.Add(Me.Label4)
        Me.GroupBox2.Controls.Add(Me.Text_Interval)
        Me.GroupBox2.Location = New System.Drawing.Point(8, 72)
        Me.GroupBox2.Name = "GroupBox2"
        Me.GroupBox2.Size = New System.Drawing.Size(240, 40)
        Me.GroupBox2.TabIndex = 6
        Me.GroupBox2.TabStop = False
        Me.GroupBox2.Text = "Interval"
        '
        'Label4
        '
        Me.Label4.Location = New System.Drawing.Point(160, 18)
        Me.Label4.Name = "Label4"
        Me.Label4.Size = New System.Drawing.Size(40, 16)
        Me.Label4.TabIndex = 1
        Me.Label4.Text = "[us]"
        '
        'Text_Interval
        '
        Me.Text_Interval.Location = New System.Drawing.Point(16, 16)
        Me.Text_Interval.Name = "Text_Interval"
        Me.Text_Interval.Size = New System.Drawing.Size(136, 20)
        Me.Text_Interval.TabIndex = 0
        Me.Text_Interval.Text = "1000"
        Me.Text_Interval.TextAlign = System.Windows.Forms.HorizontalAlignment.Right
        '
        'GroupBox3
        '
        Me.GroupBox3.Controls.Add(Me.Label_Msg_Count)
        Me.GroupBox3.Location = New System.Drawing.Point(8, 120)
        Me.GroupBox3.Name = "GroupBox3"
        Me.GroupBox3.Size = New System.Drawing.Size(240, 40)
        Me.GroupBox3.TabIndex = 7
        Me.GroupBox3.TabStop = False
        Me.GroupBox3.Text = "Count"
        '
        'Label_Msg_Count
        '
        Me.Label_Msg_Count.Location = New System.Drawing.Point(16, 16)
        Me.Label_Msg_Count.Name = "Label_Msg_Count"
        Me.Label_Msg_Count.Size = New System.Drawing.Size(136, 16)
        Me.Label_Msg_Count.TabIndex = 0
        Me.Label_Msg_Count.Text = "0"
        Me.Label_Msg_Count.TextAlign = System.Drawing.ContentAlignment.TopRight
        '
        'GroupBox4
        '
        Me.GroupBox4.Controls.Add(Me.Text_ReturnCode)
        Me.GroupBox4.Location = New System.Drawing.Point(8, 168)
        Me.GroupBox4.Name = "GroupBox4"
        Me.GroupBox4.Size = New System.Drawing.Size(336, 48)
        Me.GroupBox4.TabIndex = 8
        Me.GroupBox4.TabStop = False
        Me.GroupBox4.Text = "Ret"
        '
        'Text_ReturnCode
        '
        Me.Text_ReturnCode.Location = New System.Drawing.Point(8, 16)
        Me.Text_ReturnCode.Name = "Text_ReturnCode"
        Me.Text_ReturnCode.Size = New System.Drawing.Size(304, 20)
        Me.Text_ReturnCode.TabIndex = 0
        '
        'Interval
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(352, 222)
        Me.Controls.Add(Me.GroupBox4)
        Me.Controls.Add(Me.GroupBox3)
        Me.Controls.Add(Me.GroupBox2)
        Me.Controls.Add(Me.GroupBox1)
        Me.Controls.Add(Me.Button_Exit)
        Me.Controls.Add(Me.Button_TimExit)
        Me.Controls.Add(Me.Button_TimStopTimer)
        Me.Controls.Add(Me.Button_TimStartTimer)
        Me.Controls.Add(Me.Button_TimInit)
        Me.MaximizeBox = False
        Me.MinimizeBox = False
        Me.Name = "Interval"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = "Interval Timer"
        Me.GroupBox1.ResumeLayout(False)
        Me.GroupBox1.PerformLayout()
        Me.GroupBox2.ResumeLayout(False)
        Me.GroupBox2.PerformLayout()
        Me.GroupBox3.ResumeLayout(False)
        Me.GroupBox4.ResumeLayout(False)
        Me.GroupBox4.PerformLayout()
        Me.ResumeLayout(False)

    End Sub

#End Region

    Dim TimerId As Short
    Dim IntervalCount As Integer

    '==============================================================================
    ' TimInit
    '==============================================================================
    Private Sub Button_TimInit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_TimInit.Click
        Dim DeviceName As New String("", 256)
        Dim ProductName As New String("", 256)
        Dim ErrorString As New StringBuilder("", 256)
        Dim Ret As Integer

        '-----------------------------------
        ' Get Device Name
        '-----------------------------------
        DeviceName = Text_DeviceName.Text.PadRight(256, ControlChars.NullChar)
        '-----------------------------------
        ' Initialization
        '-----------------------------------
        Ret = TimInit(DeviceName, ProductName)
        '-----------------------------------
        ' Error handle
        '-----------------------------------
        Call TimGetErrorString(Ret, ErrorString)
        Text_ReturnCode.Text = "TimInit ret = " & Ret & " " & ErrorString.ToString()
        If Ret = TIM_ERR_SUCCESS Then
            Text_DeviceName.Text = DeviceName
            Label_ProductName.Text = ProductName
        End If
    End Sub

    '==============================================================================
    ' TimStartTimer
    '==============================================================================
    Private Sub Button_TimStartTimer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_TimStartTimer.Click
        Dim Microseconds As Integer
        Dim ErrorString As New StringBuilder("", 256)
        Dim Ret As Integer

        '-----------------------------------
        ' Interval Count Initialization
        '-----------------------------------
        IntervalCount = 0
        '-----------------------------------
        ' Get the specified value of Interval
        '-----------------------------------
        Microseconds = Text_Interval.Text
        '-----------------------------------
        ' Start Interval Timer
        '-----------------------------------
        Ret = TimStartTimer(TimerId, Microseconds, Handle.ToInt32)
        '-----------------------------------
        ' Error handle
        '-----------------------------------
        Call TimGetErrorString(Ret, ErrorString)
        Text_ReturnCode.Text = "TimStartTimer = " & Ret & " " & ErrorString.ToString()
        If Ret = TIM_ERR_SUCCESS Then
            '-----------------------------------
            ' Enable the TimStopTimer Button
            '-----------------------------------
            Button_TimStopTimer.Enabled = True
            '-----------------------------------
            ' Disable the TimStartTimer Button
            '-----------------------------------
            Button_TimStartTimer.Enabled = False
        End If
    End Sub

    '==============================================================================
    ' TimStopTimer
    '==============================================================================
    Private Sub Button_TimStopTimer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_TimStopTimer.Click
        Dim ErrorString As New StringBuilder("", 256)
        Dim Ret As Integer

        '-----------------------------------
        ' Stop Interval Timer
        '-----------------------------------
        Ret = TimStopTimer(TimerId)
        '-----------------------------------
        ' Error handle
        '-----------------------------------
        Call TimGetErrorString(Ret, ErrorString)
        Text_ReturnCode.Text = "TimStopTimer = " & Ret & " " & ErrorString.ToString()
        If Ret = TIM_ERR_SUCCESS Then
            '-----------------------------------
            ' Disable the TimStopTimer button
            '-----------------------------------
            Button_TimStopTimer.Enabled = False
            '-----------------------------------
            ' Enable the TimStartTimer button
            '-----------------------------------
            Button_TimStartTimer.Enabled = True
        End If
    End Sub

    '==============================================================================
    ' TimExit
    '==============================================================================
    Private Sub Button_TimExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_TimExit.Click
        Dim ErrorString As New StringBuilder("", 256)
        Dim Ret As Integer

        '-----------------------------------
        ' Exit handle
        '-----------------------------------
        Ret = TimExit()
        '-----------------------------------
        ' Error handle
        '-----------------------------------
        Call TimGetErrorString(Ret, ErrorString)
        Text_ReturnCode.Text = "TimExit = " & Ret & " " & ErrorString.ToString()
        If Ret = TIM_ERR_SUCCESS Then
            '-----------------------------------
            ' Disable the TimStopTimer button
            '-----------------------------------
            Button_TimStopTimer.Enabled = False
            '-----------------------------------
            ' Enable the TimStartTimer button
            '-----------------------------------
            Button_TimStartTimer.Enabled = True
        End If
    End Sub

    '==============================================================================
    ' Exit
    '==============================================================================
    Private Sub Button_Exit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Exit.Click
        '-----------------------------------
        ' Exit handle
        '-----------------------------------
        Call TimExit()
        '-----------------------------------
        ' Exit
        '-----------------------------------
        End
    End Sub

    '===================================================
    ' Event notification handle
    '===================================================
    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        If m.Msg = TIMM_INTERVAL Then
            '-----------------------------------
            ' Interval Timer message handle
            '-----------------------------------
            IntervalCount = IntervalCount + 1
            Label_Msg_Count.Text = IntervalCount
        End If
        MyBase.WndProc(m)
    End Sub

End Class
