﻿' Usual Counter Sample
Imports VB = Microsoft.VisualBasic
Imports System.Text

Public Class Form1
    Dim Ret As Integer ' Return code
    Dim szError As New StringBuilder("", 256) ' Error code
    Dim szText As New String("", 100) ' Temporary
    Dim Edit_Ch(7) As TextBox
    Dim Edit_Status(7) As TextBox
    Dim Edit_Preset(7) As TextBox

    '---------------------------------------------------------
    ' Dialog initialization
    '---------------------------------------------------------
    Private Sub Counter_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load

        Dim i As Short
        Dim PresetData As Integer

        Me.Left = (Screen.PrimaryScreen.Bounds.Width - Me.Width) / 2
        Me.Top = (Screen.PrimaryScreen.Bounds.Height - Me.Height) / 2

        Edit_Ch(0) = _Edit_Ch_0
        Edit_Ch(1) = _Edit_Ch_1
        Edit_Ch(2) = _Edit_Ch_2
        Edit_Ch(3) = _Edit_Ch_3
        Edit_Ch(4) = _Edit_Ch_4
        Edit_Ch(5) = _Edit_Ch_5
        Edit_Ch(6) = _Edit_Ch_6
        Edit_Ch(7) = _Edit_Ch_7

        Edit_Status(0) = _Edit_Status_0
        Edit_Status(1) = _Edit_Status_1
        Edit_Status(2) = _Edit_Status_2
        Edit_Status(3) = _Edit_Status_3
        Edit_Status(4) = _Edit_Status_4
        Edit_Status(5) = _Edit_Status_5
        Edit_Status(6) = _Edit_Status_6
        Edit_Status(7) = _Edit_Status_7

        Edit_Preset(0) = _Edit_Preset_0
        Edit_Preset(1) = _Edit_Preset_1
        Edit_Preset(2) = _Edit_Preset_2
        Edit_Preset(3) = _Edit_Preset_3
        Edit_Preset(4) = _Edit_Preset_4
        Edit_Preset(5) = _Edit_Preset_5
        Edit_Preset(6) = _Edit_Preset_6
        Edit_Preset(7) = _Edit_Preset_7

        'Screen item initialize
        Edit_DeviceName.Text = "CNT000"
        Edit_CompResult.Text = ""
        _Radio_Ch_0.Checked = True
        _Radio_Preset_0.Checked = True

        'Count value
        For i = 0 To 7
            Edit_Ch(i).Text = ""
        Next i

        'Status
        For i = 0 To 7
            Edit_Status(i).Text = ""
        Next i

        'Preset value
        For i = 0 To 7
            PresetData = i * 100
            ' In order to display the preset value to unsigned, convert it once through hexadecimal characters
            Edit_Preset(i).Text = CStr(Convert.ToUInt32(Hex(PresetData), 16))
        Next i
        Edit_ReturnCode.Text = ""

    End Sub

    '---------------------------------------------------------
    ' Initialization process
    '---------------------------------------------------------
    Private Sub Button_CntInit_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Button_CntInit.Click

        Dim szDeviceName As String
        Dim i As Short

        ' Get device name from screen
        szDeviceName = Edit_DeviceName.Text

        ' Initializtion function execute, get ID
        Ret = CntInit(szDeviceName, Id)

        ' Error process
        Call CntGetErrorString(Ret, szError)
        Edit_ReturnCode.Text = "Ret = " & Ret & ":" & szError.ToString()
        If (Ret <> CNT_ERR_SUCCESS) Then
            Exit Sub
        End If

        ' Get all channel number
        Call CntGetMaxChannels(Id, ChannelNum)

        ' Counter�AStatus�APreset
        ' Exprocess invalidate
        For i = ChannelNum To 7
            Edit_Ch(i).Enabled = False
            Edit_Status(i).Enabled = False
            Edit_Preset(i).Enabled = False
        Next i

    End Sub

    '---------------------------------------------------------
    ' Exit process
    '---------------------------------------------------------
    Private Sub Button_CntExit_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Button_CntExit.Click

        ' Timer stop
        CntTimer.Enabled = False

        ' Exit function execute
        Ret = CntExit(Id)

        ' Error process
        Call CntGetErrorString(Ret, szError)
        Edit_ReturnCode.Text = "Ret = " & Ret & ":" & szError.ToString()

    End Sub

    '---------------------------------------------------------
    ' Dialog exit
    '---------------------------------------------------------
    Private Sub Button_Exit_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Button_Exit.Click

        ' Timer stop
        CntTimer.Enabled = False

        ' Exit function execute
        Ret = CntExit(Id)

        ' Dialog exit
        End

    End Sub

    '---------------------------------------------------------
    ' Mode dialog express
    '---------------------------------------------------------
    Private Sub Button_CntMode_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Button_CntMode.Click

        ' Dialog express in modal
        Mode.ShowDialog()

    End Sub

    '---------------------------------------------------------
    ' Comparison dialog express
    '---------------------------------------------------------
    Private Sub Button_CntCompare_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Button_CntCompare.Click

        ' Dialog express in modal
        Comp.ShowDialog()

    End Sub

    '---------------------------------------------------------
    ' Counter start
    '---------------------------------------------------------
    Private Sub Button_CntStart_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Button_CntStart.Click

        Dim ChNo(8) As Short
        Dim i As Short

        ' Set parameter
        For i = 0 To ChannelNum - 1
            ChNo(i) = i
        Next i

        ' Counter start
        Ret = CntStartCount(Id, ChNo, ChannelNum)

        ' Error process
        Call CntGetErrorString(Ret, szError)
        Edit_ReturnCode.Text = "Ret = " & Ret & ":" & szError.ToString()
        If (Ret <> CNT_ERR_SUCCESS) Then
            Exit Sub
        End If

        ' Timer start
        CntTimer.Enabled = True

    End Sub

    '---------------------------------------------------------
    ' Counter stop
    '---------------------------------------------------------
    Private Sub Button_CntStop_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Button_CntStop.Click

        Dim ChNo(8) As Short
        Dim i As Short

        ' Set parament
        For i = 0 To ChannelNum - 1
            ChNo(i) = i
        Next i

        ' Counter stop
        Ret = CntStopCount(Id, ChNo, ChannelNum)

        ' Error process
        Call CntGetErrorString(Ret, szError)
        Edit_ReturnCode.Text = "Ret = " & Ret & ":" & szError.ToString()

        ' Timer stop
        CntTimer.Enabled = False

    End Sub

    '---------------------------------------------------------
    ' Preset
    '---------------------------------------------------------
    Private Sub Button_CntPreset_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Button_CntPreset.Click

        Dim ChNo(8) As Short
        Dim PresetData(8) As Integer
        Dim i As Short

        ' Get data from screen and set parament
        For i = 0 To ChannelNum - 1
            ChNo(i) = i
            szText = Edit_Preset(i).Text
            If _Radio_Preset_0.Checked = True Then
                ' In order to set the preset value to signed, convert it once through hexadecimal characters
                PresetData(i) = Convert.ToInt32(Hex(Val(szText)), 16)
            Else
                PresetData(i) = Val("&H" & szText)
            End If
        Next i

        ' Preset
        Ret = CntPreset(Id, ChNo, ChannelNum, PresetData)

        ' Error process
        Call CntGetErrorString(Ret, szError)
        Edit_ReturnCode.Text = "Ret = " & Ret & ":" & szError.ToString()

    End Sub


    '---------------------------------------------------------
    ' Dec shift precess(Counter value)
    '---------------------------------------------------------
    Private Sub _Radio_Ch_0_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _Radio_Ch_0.CheckedChanged
        CntTimer_Tick(CntTimer, New System.EventArgs())
    End Sub

    '---------------------------------------------------------
    ' Hex shift precess(Counter value)
    '---------------------------------------------------------
    Private Sub _Radio_Ch_1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _Radio_Ch_1.CheckedChanged
        CntTimer_Tick(CntTimer, New System.EventArgs())
    End Sub

    '---------------------------------------------------------
    ' Dec shift precess(Preset value)
    '---------------------------------------------------------
    Private Sub _Radio_Preset_0_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _Radio_Preset_0.CheckedChanged
        Dim i As Short
        Dim PresetData As Integer

        If _Radio_Preset_0.Checked = True Then
            For i = 0 To 7
                szText = Edit_Preset(i).Text
                PresetData = Val("&H" & szText)
                ' In order to display the preset value to unsigned, convert it once through hexadecimal characters
                Edit_Preset(i).Text = CStr(Convert.ToUInt32(Hex(PresetData), 16))
            Next i
        End If
    End Sub

    '---------------------------------------------------------
    ' Hex shift precess(Preset value)
    '---------------------------------------------------------
    Private Sub _Radio_Preset_1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _Radio_Preset_1.CheckedChanged
        Dim i As Short
        Dim PresetData As Integer

        If _Radio_Preset_1.Checked = True Then
            For i = 0 To 7
                szText = Edit_Preset(i).Text
                ' In order to set the preset value to signed, convert it once through hexadecimal characters
                PresetData = Convert.ToInt32(Hex(Val(szText)), 16)
                Edit_Preset(i).Text = Hex(PresetData)
            Next i
        End If
    End Sub

    '---------------------------------------------------------
    ' Subroutine: HIWORD/LOWORD
    '---------------------------------------------------------
    Function HiLoWord(ByRef Number As Integer, ByRef HiLo As Short) As Integer

        Dim Hi As Integer
        Dim Lo As Integer
        Dim StrData As String
        Dim i As Short

        StrData = Hex(Number)
        If Len(StrData) < 8 Then
            For i = 1 To 8 - Len(StrData)
                StrData = "0" & StrData
            Next i
        End If

        Lo = Val("&h" & VB.Right(StrData, 4))
        Hi = Val("&h" & VB.Left(StrData, 4))
        If HiLo = 0 Then
            HiLoWord = Lo
        ElseIf HiLo = 1 Then
            HiLoWord = Hi
        End If

        Return HiLoWord
    End Function

    '---------------------------------------------------------
    ' Timer process
    '---------------------------------------------------------
    Private Sub CntTimer_Tick(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles CntTimer.Tick

        Dim ChNo(8) As Short
        Dim CountData(8) As Integer
        Dim Sts(8) As Short
        Dim i As Short
        Dim bIs10 As Boolean

        ' Set parameter
        For i = 0 To ChannelNum - 1
            ChNo(i) = i
            CountData(i) = 0
        Next i

        ' Get counter value
        Ret = CntReadCount(Id, ChNo, ChannelNum, CountData)

        ' Error process
        If (Ret <> CNT_ERR_SUCCESS) Then
            Call CntGetErrorString(Ret, szError)
            Edit_ReturnCode.Text = "CntReadCount Ret = " & Ret & ":" & szError.ToString()
            CntTimer.Enabled = False
            Exit Sub
        End If

        ' Get status
        For i = 0 To ChannelNum - 1
            Ret = CntReadStatus(Id, i, Sts(i))

            ' Error process
            If (Ret <> CNT_ERR_SUCCESS) Then
                Call CntGetErrorString(Ret, szError)
                Edit_ReturnCode.Text = "CntReadStatus Ret = " & Ret & ":" & szError.ToString()
                CntTimer.Enabled = False
                Exit Sub
            End If
        Next i

        ' Count data�AStatus data express
        bIs10 = _Radio_Ch_0.Checked
        For i = 0 To ChannelNum - 1
            If bIs10 Then
                ' In order to display the count data to unsigned, convert it once through hexadecimal characters
                szText = Str(Convert.ToUInt32(Hex(CountData(i)), 16))
            Else
                szText = Hex(CountData(i))
            End If

            Edit_Ch(i).Text = szText
            szText = Hex(Sts(i))
            Edit_Status(i).Text = szText
        Next i


    End Sub

    '---------------------------------------------------------
    ' Subroutine: Message process
    '---------------------------------------------------------
    Private Sub CountMatch(ByVal ChNo As Short, ByVal UpDown As Short, ByVal dwCount As Integer)

        Dim szUpDown As String

        ' Create screen data
        szUpDown = ""

        If (UpDown = CNTM_DIR_UP) Then
            szUpDown = "Up"
        End If

        If (UpDown = CNTM_DIR_DOWN) Then
            szUpDown = "Down"
        End If

        ' Expression
        ' In order to display the count data to unsigned, convert it once through hexadecimal characters
        Edit_CompResult.Text = "Ch : " & ChNo & " Count= " & Str(Convert.ToUInt32(Hex(dwCount), 16)) & ":" & szUpDown

    End Sub

    '---------------------------------------------------------
    ' Message Process
    '---------------------------------------------------------
    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        ' LOWORD(wParam)=ID Getted from CntInit function
        ' HIWORD(wParam)=Direction(UP=1, DOWN=2)
        ' lParam=Compare Data
        Dim ChNo As Short
        Dim UpDown As Integer
        Dim dwCount As Integer

        ' Id = HiLoWord(m.WParam.ToInt32, 0)

        Select Case m.Msg
            Case CNTM_COUNTUP_CH0
                ChNo = 0
                UpDown = HiLoWord(m.WParam.ToInt32, 1)
                ' In order to set the comparison value to signed, convert it once through hexadecimal characters
                dwCount = Convert.ToInt32(Hex(Val(m.LParam.ToString)), 16)
                Call CountMatch(ChNo, UpDown, dwCount)
            Case CNTM_COUNTUP_CH1
                ChNo = 1
                UpDown = HiLoWord(m.WParam.ToInt32, 1)
                ' In order to set the comparison value to signed, convert it once through hexadecimal characters
                dwCount = Convert.ToInt32(Hex(Val(m.LParam.ToString)), 16)
                Call CountMatch(ChNo, UpDown, dwCount)
            Case CNTM_COUNTUP_CH2
                ChNo = 2
                UpDown = HiLoWord(m.WParam.ToInt32, 1)
                ' In order to set the comparison value to signed, convert it once through hexadecimal characters
                dwCount = Convert.ToInt32(Hex(Val(m.LParam.ToString)), 16)
                Call CountMatch(ChNo, UpDown, dwCount)
            Case CNTM_COUNTUP_CH3
                ChNo = 3
                UpDown = HiLoWord(m.WParam.ToInt32, 1)
                ' In order to set the comparison value to signed, convert it once through hexadecimal characters
                dwCount = Convert.ToInt32(Hex(Val(m.LParam.ToString)), 16)
                Call CountMatch(ChNo, UpDown, dwCount)
            Case CNTM_COUNTUP_CH4
                ChNo = 4
                UpDown = HiLoWord(m.WParam.ToInt32, 1)
                ' In order to set the comparison value to signed, convert it once through hexadecimal characters
                dwCount = Convert.ToInt32(Hex(Val(m.LParam.ToString)), 16)
                Call CountMatch(ChNo, UpDown, dwCount)
            Case CNTM_COUNTUP_CH5
                ChNo = 5
                UpDown = HiLoWord(m.WParam.ToInt32, 1)
                ' In order to set the comparison value to signed, convert it once through hexadecimal characters
                dwCount = Convert.ToInt32(Hex(Val(m.LParam.ToString)), 16)
                Call CountMatch(ChNo, UpDown, dwCount)
            Case CNTM_COUNTUP_CH6
                ChNo = 6
                UpDown = HiLoWord(m.WParam.ToInt32, 1)
                ' In order to set the comparison value to signed, convert it once through hexadecimal characters
                dwCount = Convert.ToInt32(Hex(Val(m.LParam.ToString)), 16)
                Call CountMatch(ChNo, UpDown, dwCount)
            Case CNTM_COUNTUP_CH7
                ChNo = 7
                UpDown = HiLoWord(m.WParam.ToInt32, 1)
                ' In order to set the comparison value to signed, convert it once through hexadecimal characters
                dwCount = Convert.ToInt32(Hex(Val(m.LParam.ToString)), 16)
                Call CountMatch(ChNo, UpDown, dwCount)
        End Select

        MyBase.WndProc(m)
    End Sub
End Class
