﻿Imports System.Text
Public Class Form1

    Dim dwRet As Integer
    Dim wBankNo As Short
    Dim lOutPulse As Integer
    Dim lCountPulse As Integer
    Dim szComment As String
    Dim Id As Short
    Dim AxisNo As Short
    Dim ErrorString As New StringBuilder("", 256)  'Error String

    Private Sub chkBank_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkBank.Click

        Dim bEventMode As Byte

        Try
            wBankNo = Val(txtBankNo.Text)
        Catch ex As Exception
            wBankNo = 0
        End Try
        If chkBank.Checked = True Then
            bEventMode = CSMC_ENABLE
        ElseIf chkBank.Checked = False Then
            bEventMode = CSMC_DISABLE
        End If

        '----------------------------------
        ' Set Event for StopMotion to Driver
        '----------------------------------
        dwRet = SmcWBankEvent(Id, AxisNo, Me.Handle, bEventMode, wBankNo)
        If dwRet <> 0 Then
            SmcWGetErrorString(dwRet, ErrorString)
            lblComment.Text = "SmcWBankEvent = " & dwRet & " : " & ErrorString.ToString
            Exit Sub
        End If

    End Sub

    Private Sub chkCountPulse_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkCountPulse.Click

        Dim bEventMode As Byte

        Try
            lCountPulse = Val(txtCountPulse.Text)
        Catch ex As Exception
            lCountPulse = 0
        End Try

        If chkCountPulse.Checked = True Then
            bEventMode = CSMC_ENABLE
        ElseIf chkCountPulse.Checked = False Then
            bEventMode = CSMC_DISABLE
        End If

        '----------------------------------
        ' Set Event for Encoder to Driver
        '----------------------------------
        dwRet = SmcWCountEvent(Id, AxisNo, Me.Handle, bEventMode, CSMC_ENCODER, lCountPulse)
        If dwRet <> 0 Then
            SmcWGetErrorString(dwRet, ErrorString)
            lblComment.Text = "SmcWCountEvent = " & dwRet & " : " & ErrorString.ToString
            Exit Sub
        End If

    End Sub

    Private Sub chkOutPulse_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkOutPulse.Click

        Dim bEventMode As Byte

        Try
            lOutPulse = Val(txtOutPulse.Text)
        Catch ex As Exception
            lOutPulse = 0
        End Try

        If chkOutPulse.Checked = True Then
            bEventMode = CSMC_ENABLE
        ElseIf chkOutPulse.Checked = False Then
            bEventMode = CSMC_DISABLE
        End If

        '----------------------------------
        ' Set Event for OutPulse to Driver
        '----------------------------------
        dwRet = SmcWCountEvent(Id, AxisNo, Me.Handle, bEventMode, CSMC_OUTPULSE, lOutPulse)
        If dwRet <> 0 Then
            SmcWGetErrorString(dwRet, ErrorString)
            lblComment.Text = "SmcWCountEvent = " & dwRet & " : " & ErrorString.ToString
            Exit Sub
        End If

    End Sub

    Private Sub chkStop_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkStop.Click

        Dim bEventMode As Byte

        If chkStop.Checked = True Then
            bEventMode = CSMC_ENABLE
        ElseIf chkStop.Checked = False Then
            bEventMode = CSMC_DISABLE
        End If

        '----------------------------------
        ' Set Event for StopMotion to Driver
        '----------------------------------
        dwRet = SmcWStopEvent(Id, AxisNo, Me.Handle, bEventMode)
        If dwRet <> 0 Then
            SmcWGetErrorString(dwRet, ErrorString)
            lblComment.Text = "SmcWStopEvent = " & dwRet & " : " & ErrorString.ToString
            Exit Sub
        End If

    End Sub

    Protected Overrides Sub WndProc(ByRef m As Message)

        Dim szMsg As String
        Dim Title As String

        'Bank Message Function
        If m.Msg = CSMC_MESSAGE + 3 Then
            szMsg = "BankNo is " & Val(wBankNo) & "."
            Title = "Bank Event"
            MessageBox.Show(szMsg, Title, MessageBoxButtons.OK)
        End If

        'CountPulse Message Function
        If m.Msg = CSMC_MESSAGE + 2 Then
            szMsg = "Encoder is " & Val(lCountPulse) & "."
            Title = "Encoder Event"
            MessageBox.Show(szMsg, Title, MessageBoxButtons.OK)
        End If

        'OutPulse Message Function
        If m.Msg = CSMC_MESSAGE + 1 Then
            szMsg = "OutPulse is " & Val(lOutPulse) & "."
            Title = "OutPulse Event"
            MessageBox.Show(szMsg, Title, MessageBoxButtons.OK)
        End If

        'Stop Message Function
        If m.Msg = CSMC_MESSAGE Then
            szMsg = "AxisNo" + m.LParam.ToString + " was stop."
            Title = "Stop"
            MessageBox.Show(szMsg, Title, MessageBoxButtons.OK)
        End If

        MyBase.WndProc(m)

    End Sub

    Private Sub cboTargetAxis_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboTargetAxis.SelectedIndexChanged

        AxisNo = cboTargetAxis.SelectedIndex + 1

    End Sub

    Private Sub btnComEnd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnComEnd.Click

        SmcWExit(Id)
        End

    End Sub

    Private Sub Event_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim n As Short

        AxisNo = 1

        ' AxisNo ComboBox
        cboTargetAxis.Items.Clear()
        For n = 1 To 8
            cboTargetAxis.Items.Add(Str(n))
        Next

        ' ComboBox Initialise
        cboTargetAxis.SelectedIndex = AxisNo - 1

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

        '------------------
        ' Set BankNo to 3
        '------------------
        wBankNo = 3
        txtBankNo.Text = Str(wBankNo)

        '----------------------
        ' Set OutPulse to 500
        '----------------------
        lOutPulse = 500
        txtOutPulse.Text = Str(lOutPulse)

        '---------------------------
        ' Set Encoder Pulse to 500
        '---------------------------
        lCountPulse = 500
        txtCountPulse.Text = Str(lCountPulse)

        '-------------
        ' Initialize
        '-------------
        dwRet = SmcWInit(txtDeviceName.Text, Id)
        If dwRet <> 0 Then
            SmcWGetErrorString(dwRet, ErrorString)
            lblComment.Text = "SmcWInit = " & dwRet & " : " & ErrorString.ToString
            Exit Sub
        End If

        lblComment.Text = "OK "

    End Sub

    Private Sub txtDeviceName_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtDeviceName.TextChanged

        dwRet = SmcWInit(txtDeviceName.Text, Id)
        If dwRet <> 0 Then
            SmcWGetErrorString(dwRet, ErrorString)
            lblComment.Text = "SmcWInit = " & dwRet & " : " & ErrorString.ToString
            Exit Sub
        End If

        lblComment.Text = "OK "

    End Sub

    Private Sub txtBankNo_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtBankNo.TextChanged

        chkBank_Click(sender, e)

    End Sub

    Private Sub txtCountPulse_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtCountPulse.TextChanged

        chkCountPulse_Click(sender, e)

    End Sub

    Private Sub txtOutPulse_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtOutPulse.TextChanged

        chkOutPulse_Click(sender, e)

    End Sub
End Class
