﻿Option Strict Off
Option Explicit On 
Imports System.Text
Public Class BankSpd
	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 GroupBox1 As System.Windows.Forms.GroupBox
	Friend WithEvents btnComMove As System.Windows.Forms.Button
	Friend WithEvents btnComStop As System.Windows.Forms.Button
	Friend WithEvents GroupBox2 As System.Windows.Forms.GroupBox
	Friend WithEvents rdoOptionSingle As System.Windows.Forms.RadioButton
	Friend WithEvents rdoOptionLoop As System.Windows.Forms.RadioButton
	Friend WithEvents btnComEnd As System.Windows.Forms.Button
	Friend WithEvents GroupBox3 As System.Windows.Forms.GroupBox
	Friend WithEvents lblComment As System.Windows.Forms.Label
	Friend WithEvents cboTargetAxis As System.Windows.Forms.ComboBox
	Friend WithEvents Label10 As System.Windows.Forms.Label
	Friend WithEvents lblSetList As System.Windows.Forms.Label
	<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
		Me.LDeviceName = New System.Windows.Forms.Label()
		Me.txtDeviceName = New System.Windows.Forms.TextBox()
		Me.Label10 = New System.Windows.Forms.Label()
		Me.GroupBox1 = New System.Windows.Forms.GroupBox()
		Me.btnComStop = New System.Windows.Forms.Button()
		Me.btnComMove = New System.Windows.Forms.Button()
		Me.GroupBox2 = New System.Windows.Forms.GroupBox()
		Me.rdoOptionLoop = New System.Windows.Forms.RadioButton()
		Me.rdoOptionSingle = New System.Windows.Forms.RadioButton()
		Me.btnComEnd = New System.Windows.Forms.Button()
		Me.GroupBox3 = New System.Windows.Forms.GroupBox()
		Me.lblComment = New System.Windows.Forms.Label()
		Me.lblSetList = New System.Windows.Forms.Label()
		Me.cboTargetAxis = New System.Windows.Forms.ComboBox()
		Me.GroupBox1.SuspendLayout()
		Me.GroupBox2.SuspendLayout()
		Me.GroupBox3.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(88, 21)
		Me.txtDeviceName.TabIndex = 0
		Me.txtDeviceName.Text = "SMC000"
		'
		'Label10
		'
		Me.Label10.AutoSize = True
		Me.Label10.Location = New System.Drawing.Point(240, 18)
		Me.Label10.Name = "Label10"
		Me.Label10.Size = New System.Drawing.Size(44, 14)
		Me.Label10.TabIndex = 2
		Me.Label10.Text = "AxisNo"
		'
		'GroupBox1
		'
		Me.GroupBox1.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnComStop, Me.btnComMove})
		Me.GroupBox1.Location = New System.Drawing.Point(16, 48)
		Me.GroupBox1.Name = "GroupBox1"
		Me.GroupBox1.Size = New System.Drawing.Size(120, 96)
		Me.GroupBox1.TabIndex = 2
		Me.GroupBox1.TabStop = False
		Me.GroupBox1.Text = "MOTION"
		'
		'btnComStop
		'
		Me.btnComStop.Location = New System.Drawing.Point(16, 56)
		Me.btnComStop.Name = "btnComStop"
		Me.btnComStop.Size = New System.Drawing.Size(88, 24)
		Me.btnComStop.TabIndex = 1
		Me.btnComStop.Text = "STOP"
		'
		'btnComMove
		'
		Me.btnComMove.Location = New System.Drawing.Point(16, 24)
		Me.btnComMove.Name = "btnComMove"
		Me.btnComMove.Size = New System.Drawing.Size(88, 24)
		Me.btnComMove.TabIndex = 0
		Me.btnComMove.Text = "START"
		'
		'GroupBox2
		'
		Me.GroupBox2.Controls.AddRange(New System.Windows.Forms.Control() {Me.rdoOptionLoop, Me.rdoOptionSingle})
		Me.GroupBox2.Location = New System.Drawing.Point(160, 48)
		Me.GroupBox2.Name = "GroupBox2"
		Me.GroupBox2.Size = New System.Drawing.Size(112, 96)
		Me.GroupBox2.TabIndex = 3
		Me.GroupBox2.TabStop = False
		Me.GroupBox2.Text = "TYPE"
		'
		'rdoOptionLoop
		'
		Me.rdoOptionLoop.Location = New System.Drawing.Point(16, 56)
		Me.rdoOptionLoop.Name = "rdoOptionLoop"
		Me.rdoOptionLoop.Size = New System.Drawing.Size(64, 24)
		Me.rdoOptionLoop.TabIndex = 1
		Me.rdoOptionLoop.Text = "Loop"
		'
		'rdoOptionSingle
		'
		Me.rdoOptionSingle.Location = New System.Drawing.Point(16, 16)
		Me.rdoOptionSingle.Name = "rdoOptionSingle"
		Me.rdoOptionSingle.Size = New System.Drawing.Size(72, 24)
		Me.rdoOptionSingle.TabIndex = 0
		Me.rdoOptionSingle.Text = "Single"
		'
		'btnComEnd
		'
		Me.btnComEnd.Location = New System.Drawing.Point(296, 96)
		Me.btnComEnd.Name = "btnComEnd"
		Me.btnComEnd.Size = New System.Drawing.Size(96, 24)
		Me.btnComEnd.TabIndex = 4
		Me.btnComEnd.Text = "E&XIT"
		'
		'GroupBox3
		'
		Me.GroupBox3.Controls.AddRange(New System.Windows.Forms.Control() {Me.lblComment})
		Me.GroupBox3.Location = New System.Drawing.Point(16, 152)
		Me.GroupBox3.Name = "GroupBox3"
		Me.GroupBox3.Size = New System.Drawing.Size(376, 136)
		Me.GroupBox3.TabIndex = 7
		Me.GroupBox3.TabStop = False
		Me.GroupBox3.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(344, 96)
		Me.lblComment.TabIndex = 0
		'
		'lblSetList
		'
		Me.lblSetList.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
		Me.lblSetList.Location = New System.Drawing.Point(16, 304)
		Me.lblSetList.Name = "lblSetList"
		Me.lblSetList.Size = New System.Drawing.Size(376, 160)
		Me.lblSetList.TabIndex = 8
		'
		'cboTargetAxis
		'
		Me.cboTargetAxis.Location = New System.Drawing.Point(296, 16)
		Me.cboTargetAxis.Name = "cboTargetAxis"
		Me.cboTargetAxis.Size = New System.Drawing.Size(72, 23)
		Me.cboTargetAxis.TabIndex = 1
		'
		'BankSpd
		'
		Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
		Me.ClientSize = New System.Drawing.Size(408, 478)
		Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.cboTargetAxis, Me.lblSetList, Me.GroupBox3, Me.btnComEnd, Me.GroupBox2, Me.GroupBox1, Me.Label10, Me.txtDeviceName, Me.LDeviceName})
		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 = "BankSpd"
		Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
		Me.Text = "BANKSPD"
		Me.GroupBox1.ResumeLayout(False)
		Me.GroupBox2.ResumeLayout(False)
		Me.GroupBox3.ResumeLayout(False)
		Me.ResumeLayout(False)

	End Sub

#End Region

	Dim dwRet As Integer
	Dim MotionType As Short
	Dim szComment As String
	Dim bRet As Boolean
	Dim Id As Short
	Dim AxisNo As Short
    Dim wBankNum As Short
    Dim ErrorString As New StringBuilder("", 256)  'Error String

	Private Sub cboTargetAxis_SelectedIndexChanged(ByVal eventSender As System.Object, ByVal eventArgs 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 btnComMove_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnComMove.Click

		Dim n As Short
		'----------------------------------
		' Set parameters to Driver
		'----------------------------------
		bRet = SetBankParam()
		If bRet = False Then
			Exit Sub
		End If

		'---------------
		' Ready motion
		'---------------
		dwRet = SmcWSetBankReady(Id, AxisNo, MotionType)
        If dwRet <> 0 Then
            SmcWGetErrorString(dwRet, ErrorString)
            lblComment.Text = "SmcWSetBankReady = " & dwRet & " : " & ErrorString.ToString
            Exit Sub
        End If

        lblSetList.Text = ""
        For n = 1 To wBankNum
            bRet = GetMoveParam(n)
            If bRet = False Then
                Exit Sub
            End If
        Next

        '---------------
        ' Start Motion
        '---------------
        dwRet = SmcWMotionStart(Id, AxisNo)
        If dwRet <> 0 Then
            SmcWGetErrorString(dwRet, ErrorString)
            lblComment.Text = "SmcMotionStart = " & dwRet & " : " & ErrorString.ToString
            Exit Sub
        End If

        lblComment.Text = "OK "
        
    End Sub

    Private Sub btnComStop_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnComStop.Click

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

        lblComment.Text = "OK "
        
    End Sub

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

        Dim n As Short
        ' Centering
        Me.Left = (Screen.PrimaryScreen.Bounds.Width - Me.Width) / 2
        Me.Top = (Screen.PrimaryScreen.Bounds.Height - Me.Height) / 2
        '-------------
        ' Initialize
        '-------------
        AxisNo = 1
        wBankNum = 5
        rdoOptionSingle.Checked = True

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

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

        For n = 1 To wBankNum
            bRet = GetMoveParam(n)
            If bRet = False Then
                Exit Sub
            End If
        Next

        lblComment.Text = "OK "
        
    End Sub

    Function SetBankParam() As Boolean

        Dim Reserved As Short
        Dim BankNo As Short
        Dim InterAxis As Short
        Dim InterType As Short
        '----------------------------
        ' Set Bank Number to Driver
        '----------------------------
        dwRet = SmcWSetBankNumber(Id, AxisNo, wBankNum)
        If dwRet <> 0 Then
            SmcWGetErrorString(dwRet, ErrorString)
            lblComment.Text = "SmcWSetBankNumber = " & dwRet & " : " & ErrorString.ToString
            SetBankParam = False
            Exit Function
        End If

        '----------------------------
        ' Set Interpolation to Driver
        '----------------------------
        InterType = 0 'interpolation clear
        InterAxis = (2 ^ (AxisNo - 1))
        For BankNo = 1 To wBankNum
            dwRet = SmcWSetBankInterpolation(Id, AxisNo, BankNo, InterType, InterAxis, Reserved)
            If dwRet <> 0 Then
                SmcWGetErrorString(dwRet, ErrorString)
                lblComment.Text = "SmcWSetBankInterpolation = " & dwRet & " : " & ErrorString.ToString
                SetBankParam = False
                Exit Function
            End If

        Next

        '-------------------------
        ' Set Distance to Driver
        '-------------------------
        dwRet = SmcWSetBankDistance(Id, AxisNo, 1, 500)
        dwRet = SmcWSetBankDistance(Id, AxisNo, 2, 1000)
        dwRet = SmcWSetBankDistance(Id, AxisNo, 3, 1500)
        dwRet = SmcWSetBankDistance(Id, AxisNo, 4, 2000)
        dwRet = SmcWSetBankDistance(Id, AxisNo, 5, -5000)

        '----------------------------
        ' Set StartSpeed to Driver
        '----------------------------
        dwRet = SmcWSetBankStartSpeed(Id, AxisNo, 1, 500.0#)
        dwRet = SmcWSetBankStartSpeed(Id, AxisNo, 2, 1000.0#)
        dwRet = SmcWSetBankStartSpeed(Id, AxisNo, 3, 1500.0#)
        dwRet = SmcWSetBankStartSpeed(Id, AxisNo, 4, 2000.0#)
        dwRet = SmcWSetBankStartSpeed(Id, AxisNo, 5, 2500.0#)

        '----------------------------
        ' Set TargetSpeed to Driver
        '----------------------------
        dwRet = SmcWSetBankTargetSpeed(Id, AxisNo, 1, 1000.0#)
        dwRet = SmcWSetBankTargetSpeed(Id, AxisNo, 2, 2000.0#)
        dwRet = SmcWSetBankTargetSpeed(Id, AxisNo, 3, 3000.0#)
        dwRet = SmcWSetBankTargetSpeed(Id, AxisNo, 4, 4000.0#)
        dwRet = SmcWSetBankTargetSpeed(Id, AxisNo, 5, 5000.0#)

        '----------------------------------
        ' Set AccelTime to Driver
        '----------------------------------
        dwRet = SmcWSetBankAccelTime(Id, AxisNo, 1, 50)
        dwRet = SmcWSetBankAccelTime(Id, AxisNo, 2, 100)
        dwRet = SmcWSetBankAccelTime(Id, AxisNo, 3, 150)
        dwRet = SmcWSetBankAccelTime(Id, AxisNo, 4, 200)
        dwRet = SmcWSetBankAccelTime(Id, AxisNo, 5, 250)

        '----------------------------------
        ' Set DecelTime to Driver
        '----------------------------------
        dwRet = SmcWSetBankDecelTime(Id, AxisNo, 1, 100)
        dwRet = SmcWSetBankDecelTime(Id, AxisNo, 2, 150)
        dwRet = SmcWSetBankDecelTime(Id, AxisNo, 3, 200)
        dwRet = SmcWSetBankDecelTime(Id, AxisNo, 4, 250)
        dwRet = SmcWSetBankDecelTime(Id, AxisNo, 5, 300)

        '-----------------------------
        ' Set SSpeed to Driver
        '-----------------------------
        dwRet = SmcWSetBankSSpeed(Id, AxisNo, 1, 200)
        dwRet = SmcWSetBankSSpeed(Id, AxisNo, 2, 400)
        dwRet = SmcWSetBankSSpeed(Id, AxisNo, 3, 600)
        dwRet = SmcWSetBankSSpeed(Id, AxisNo, 4, 800)
        dwRet = SmcWSetBankSSpeed(Id, AxisNo, 5, 1000)

        '----------------------------------------
        ' Other parameter use default parameter
        '----------------------------------------
        SetBankParam = True

    End Function

    Function GetMoveParam(ByVal i As Integer) As Boolean

        Dim lDistance As Integer
        Dim dblSSpeed As Double
        Dim dblDecelTime As Double
        Dim dblAccelTime As Double
        Dim dblTargetSpeed As Double
        Dim dblStartSpeed As Double
        '----------------------------------
        ' Get default value of StartSpeed
        '----------------------------------
        dwRet = SmcWGetBankStartSpeed(Id, AxisNo, i, dblStartSpeed)
        If dwRet <> 0 Then
            SmcWGetErrorString(dwRet, ErrorString)
            lblComment.Text = "SmcWGetBankStartSpeed = " & dwRet & " : " & ErrorString.ToString
            GetMoveParam = False
            Exit Function
        End If

        lblSetList.Text = lblSetList.Text & "BankNo:" & i & Str(dblStartSpeed)

        '----------------------------------
        ' Get default value of TargetSpeed
        '----------------------------------
        dwRet = SmcWGetBankTargetSpeed(Id, AxisNo, i, dblTargetSpeed)
        If dwRet <> 0 Then
            SmcWGetErrorString(dwRet, ErrorString)
            lblComment.Text = "SmcWGetBankTargetSpeed = " & dwRet & " : " & ErrorString.ToString
            GetMoveParam = False
            Exit Function
        End If

        lblSetList.Text = lblSetList.Text & Str(dblTargetSpeed)

        '----------------------------------
        ' Get default value of AccelRate
        '----------------------------------
        dwRet = SmcWGetBankAccelTime(Id, AxisNo, i, dblAccelTime)
        If dwRet <> 0 Then
            SmcWGetErrorString(dwRet, ErrorString)
            lblComment.Text = "SmcWGetBankAccelTime = " & dwRet & " : " & ErrorString.ToString
            GetMoveParam = False
            Exit Function
        End If

        lblSetList.Text = lblSetList.Text & Str(dblAccelTime)

        '----------------------------------
        ' Get default value of DecelRate
        '----------------------------------
        dwRet = SmcWGetBankDecelTime(Id, AxisNo, i, dblDecelTime)
        If dwRet <> 0 Then
            SmcWGetErrorString(dwRet, ErrorString)
            lblComment.Text = "SmcWGetBankDecelTime = " & dwRet & " : " & ErrorString.ToString
            GetMoveParam = False
            Exit Function
        End If

        lblSetList.Text = lblSetList.Text & Str(dblDecelTime)

        '-----------------------------
        ' Get default value of SSpeed
        '-----------------------------
        dwRet = SmcWGetBankSSpeed(Id, AxisNo, i, dblSSpeed)
        If dwRet <> 0 Then
            SmcWGetErrorString(dwRet, ErrorString)
            lblComment.Text = "SmcWGetBankSSpeed = " & dwRet & " : " & ErrorString.ToString
            GetMoveParam = False
            Exit Function
        End If

        lblSetList.Text = lblSetList.Text & Str(dblSSpeed)

        '----------------------------------
        ' Get default value of lDistance
        '----------------------------------
        dwRet = SmcWGetBankDistance(Id, AxisNo, i, lDistance)
        If dwRet <> 0 Then
            SmcWGetErrorString(dwRet, ErrorString)
            lblComment.Text = "SmcWGetBankDistance = " & dwRet & " : " & ErrorString.ToString
            GetMoveParam = False
            Exit Function
        End If

        lblSetList.Text = lblSetList.Text & Str(lDistance) & vbCrLf

        GetMoveParam = True

    End Function

    Private Sub rdoOptionLoop_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rdoOptionLoop.CheckedChanged

        MotionType = CSMC_LOOP

    End Sub

    Private Sub rdoOptionSingle_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rdoOptionSingle.CheckedChanged

        MotionType = CSMC_SINGLE

    End Sub

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

        Dim n As Short

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

        lblSetList.Text = ""
        For n = 1 To wBankNum
            bRet = GetMoveParam(n)
            If bRet = False Then
                Exit Sub
            End If
        Next

        lblComment.Text = "OK "

    End Sub

End Class
