﻿Option Strict Off
Option Explicit On 
Imports System.Text

Public Class Event_Renamed
	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 LDeviceName As System.Windows.Forms.Label
	Friend WithEvents txtDeviceName As System.Windows.Forms.TextBox
	Friend WithEvents Label2 As System.Windows.Forms.Label
	Friend WithEvents cboTargetAxis As System.Windows.Forms.ComboBox
	Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
	Friend WithEvents chkStop As System.Windows.Forms.CheckBox
	Friend WithEvents chkBank As System.Windows.Forms.CheckBox
	Friend WithEvents chkOutPulse As System.Windows.Forms.CheckBox
	Friend WithEvents txtBankNo As System.Windows.Forms.TextBox
	Friend WithEvents txtOutPulse As System.Windows.Forms.TextBox
	Friend WithEvents txtCountPulse As System.Windows.Forms.TextBox
	Friend WithEvents GroupBox2 As System.Windows.Forms.GroupBox
	Friend WithEvents lblComment As System.Windows.Forms.Label
    Friend WithEvents btnComEnd As System.Windows.Forms.Button
    Friend WithEvents chkCountPulse As System.Windows.Forms.CheckBox
	<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.LDeviceName = New System.Windows.Forms.Label()
        Me.txtDeviceName = New System.Windows.Forms.TextBox()
        Me.Label2 = New System.Windows.Forms.Label()
        Me.cboTargetAxis = New System.Windows.Forms.ComboBox()
        Me.GroupBox1 = New System.Windows.Forms.GroupBox()
        Me.chkCountPulse = New System.Windows.Forms.CheckBox()
        Me.txtCountPulse = New System.Windows.Forms.TextBox()
        Me.txtOutPulse = New System.Windows.Forms.TextBox()
        Me.txtBankNo = New System.Windows.Forms.TextBox()
        Me.chkOutPulse = New System.Windows.Forms.CheckBox()
        Me.chkBank = New System.Windows.Forms.CheckBox()
        Me.chkStop = New System.Windows.Forms.CheckBox()
        Me.GroupBox2 = New System.Windows.Forms.GroupBox()
        Me.lblComment = New System.Windows.Forms.Label()
        Me.btnComEnd = New System.Windows.Forms.Button()
        Me.GroupBox1.SuspendLayout()
        Me.GroupBox2.SuspendLayout()
        Me.SuspendLayout()
        '
        'LDeviceName
        '
        Me.LDeviceName.AutoSize = True
        Me.LDeviceName.Location = New System.Drawing.Point(16, 18)
        Me.LDeviceName.Name = "LDeviceName"
        Me.LDeviceName.Size = New System.Drawing.Size(79, 14)
        Me.LDeviceName.TabIndex = 0
        Me.LDeviceName.Text = "Device Name"
        '
        'txtDeviceName
        '
        Me.txtDeviceName.Location = New System.Drawing.Point(112, 16)
        Me.txtDeviceName.Name = "txtDeviceName"
        Me.txtDeviceName.Size = New System.Drawing.Size(80, 21)
        Me.txtDeviceName.TabIndex = 1
        Me.txtDeviceName.Text = "SMC000"
        '
        'Label2
        '
        Me.Label2.AutoSize = True
        Me.Label2.Location = New System.Drawing.Point(224, 18)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(44, 14)
        Me.Label2.TabIndex = 2
        Me.Label2.Text = "AxisNo"
        '
        'cboTargetAxis
        '
        Me.cboTargetAxis.Location = New System.Drawing.Point(288, 16)
        Me.cboTargetAxis.Name = "cboTargetAxis"
        Me.cboTargetAxis.Size = New System.Drawing.Size(72, 23)
        Me.cboTargetAxis.TabIndex = 3
        '
        'GroupBox1
        '
        Me.GroupBox1.Controls.AddRange(New System.Windows.Forms.Control() {Me.chkCountPulse, Me.txtCountPulse, Me.txtOutPulse, Me.txtBankNo, Me.chkOutPulse, Me.chkBank, Me.chkStop})
        Me.GroupBox1.Location = New System.Drawing.Point(16, 48)
        Me.GroupBox1.Name = "GroupBox1"
        Me.GroupBox1.Size = New System.Drawing.Size(400, 160)
        Me.GroupBox1.TabIndex = 4
        Me.GroupBox1.TabStop = False
        Me.GroupBox1.Text = "EVENT"
        '
        'chkCountPulse
        '
        Me.chkCountPulse.Location = New System.Drawing.Point(16, 122)
        Me.chkCountPulse.Name = "chkCountPulse"
        Me.chkCountPulse.Size = New System.Drawing.Size(200, 16)
        Me.chkCountPulse.TabIndex = 6
        Me.chkCountPulse.Text = "ENCODER COUNT EVENT"
        '
        'txtCountPulse
        '
        Me.txtCountPulse.Location = New System.Drawing.Point(224, 120)
        Me.txtCountPulse.Name = "txtCountPulse"
        Me.txtCountPulse.Size = New System.Drawing.Size(160, 21)
        Me.txtCountPulse.TabIndex = 5
        Me.txtCountPulse.Text = ""
        Me.txtCountPulse.TextAlign = System.Windows.Forms.HorizontalAlignment.Right
        '
        'txtOutPulse
        '
        Me.txtOutPulse.Location = New System.Drawing.Point(224, 88)
        Me.txtOutPulse.Name = "txtOutPulse"
        Me.txtOutPulse.Size = New System.Drawing.Size(160, 21)
        Me.txtOutPulse.TabIndex = 4
        Me.txtOutPulse.Text = ""
        Me.txtOutPulse.TextAlign = System.Windows.Forms.HorizontalAlignment.Right
        '
        'txtBankNo
        '
        Me.txtBankNo.Location = New System.Drawing.Point(224, 56)
        Me.txtBankNo.Name = "txtBankNo"
        Me.txtBankNo.Size = New System.Drawing.Size(160, 21)
        Me.txtBankNo.TabIndex = 3
        Me.txtBankNo.Text = ""
        Me.txtBankNo.TextAlign = System.Windows.Forms.HorizontalAlignment.Right
        '
        'chkOutPulse
        '
        Me.chkOutPulse.Location = New System.Drawing.Point(16, 90)
        Me.chkOutPulse.Name = "chkOutPulse"
        Me.chkOutPulse.Size = New System.Drawing.Size(200, 16)
        Me.chkOutPulse.TabIndex = 2
        Me.chkOutPulse.Text = "OUTPULSE COUNT EVENT"
        '
        'chkBank
        '
        Me.chkBank.Location = New System.Drawing.Point(16, 58)
        Me.chkBank.Name = "chkBank"
        Me.chkBank.Size = New System.Drawing.Size(176, 16)
        Me.chkBank.TabIndex = 1
        Me.chkBank.Text = "BANK EVENT"
        '
        'chkStop
        '
        Me.chkStop.Location = New System.Drawing.Point(16, 24)
        Me.chkStop.Name = "chkStop"
        Me.chkStop.Size = New System.Drawing.Size(176, 16)
        Me.chkStop.TabIndex = 0
        Me.chkStop.Text = "STOP EVENT"
        '
        'GroupBox2
        '
        Me.GroupBox2.Controls.AddRange(New System.Windows.Forms.Control() {Me.lblComment})
        Me.GroupBox2.Location = New System.Drawing.Point(16, 216)
        Me.GroupBox2.Name = "GroupBox2"
        Me.GroupBox2.Size = New System.Drawing.Size(400, 136)
        Me.GroupBox2.TabIndex = 5
        Me.GroupBox2.TabStop = False
        Me.GroupBox2.Text = "COMMENT"
        '
        'lblComment
        '
        Me.lblComment.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
        Me.lblComment.Location = New System.Drawing.Point(16, 24)
        Me.lblComment.Name = "lblComment"
        Me.lblComment.Size = New System.Drawing.Size(368, 96)
        Me.lblComment.TabIndex = 0
        '
        'btnComEnd
        '
        Me.btnComEnd.Location = New System.Drawing.Point(232, 368)
        Me.btnComEnd.Name = "btnComEnd"
        Me.btnComEnd.Size = New System.Drawing.Size(104, 24)
        Me.btnComEnd.TabIndex = 7
        Me.btnComEnd.Text = "E&XIT"
        '
        'Event_Renamed
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
        Me.ClientSize = New System.Drawing.Size(432, 406)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnComEnd, Me.GroupBox2, Me.GroupBox1, Me.cboTargetAxis, Me.Label2, Me.LDeviceName, Me.txtDeviceName})
        Me.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog
        Me.MaximizeBox = False
        Me.MinimizeBox = False
        Me.Name = "Event_Renamed"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = "EVENT"
        Me.GroupBox1.ResumeLayout(False)
        Me.GroupBox2.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

#End Region

	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
