﻿Imports System.Text
Public Class Form1

    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
