﻿Imports System.Text
Public Class Form1

    Dim dwRet As Integer
    Dim MotionType As Short
    Dim dblResolveSpeed As Double
    Dim dblStartSpeed As Double
    Dim dblTargetSpeed As Double
    Dim dblAccelTime As Double
    Dim dblDecelTime As Double
    Dim dblSSpeed As Double
    Dim lDistance As Integer
    Dim szComment As String
    Dim StartDir As Short
    Dim bRet As Boolean
    Dim Id As Short
    Dim AxisNo As Short
    Dim bCoordinate As Short
    Dim Change As Short
    Dim ErrorString As New StringBuilder("", 256)  'Error String

    Private Sub cboChange_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboChange.SelectedIndexChanged

        Change = cboChange.SelectedIndex

    End Sub

    Private Sub cboCoordinate_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboCoordinate.SelectedIndexChanged

        If cboCoordinate.SelectedIndex = 0 Then
            bCoordinate = CSMC_ABS
        Else
            bCoordinate = CSMC_INC
        End If

    End Sub

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

        AxisNo = cboTargetAxis.SelectedIndex + 1

        '------------------------
        ' Set lDistance to 1000
        '------------------------
        lDistance = 1000
        dwRet = SmcWSetStopPosition(Id, AxisNo, bCoordinate, lDistance)
        If dwRet <> 0 Then
            SmcWGetErrorString(dwRet, ErrorString)
            lblComment.Text = "SmcWSetStopPosition = " & dwRet & " : " & ErrorString.ToString
            Exit Sub
        End If

        '---------------------------
        ' Get parameters of Driver
        '---------------------------
        bRet = GetMoveParam()
        If bRet = False Then
            Exit Sub
        End If

        lblComment.Text = "OK "

    End Sub

    Private Sub btnComCCW_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnComCCW.Click

        StartDir = CSMC_CCW
        '----------------------------------
        ' Set parameters to Driver
        '----------------------------------
        bRet = SetMoveParam()
        If bRet = False Then
            Exit Sub
        End If

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

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

        '-------------------------------------
        ' Get setting parameters from Driver
        '-------------------------------------
        bRet = GetMoveParam()
        If bRet = False Then
            Exit Sub
        End If

        lblComment.Text = "OK "

    End Sub

    Private Sub btnComCW_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnComCW.Click

        StartDir = CSMC_CW
        '----------------------------------
        ' Set parameters to Driver
        '----------------------------------
        bRet = SetMoveParam()
        If bRet = False Then
            Exit Sub
        End If

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

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

        '-------------------------------------
        ' Get setting parameters from Driver
        '-------------------------------------
        bRet = GetMoveParam()
        If bRet = False Then
            Exit Sub
        End If

        lblComment.Text = "OK "

    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 btnComChange_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnComChange.Click

        If Change = 4 Then
            '----------------------------------
            ' Set TargetSpeed to Driver
            '----------------------------------
            Try
                dblTargetSpeed = Val(txtTargetSpeed.Text)
            Catch ex As Exception
                dblTargetSpeed = 0
            End Try
            dwRet = SmcWSetTargetSpeed(Id, AxisNo, dblTargetSpeed)
            If dwRet <> 0 Then
                SmcWGetErrorString(dwRet, ErrorString)
                lblComment.Text = "SmcWSetTargetSpeed = " & dwRet & " : " & ErrorString.ToString
                Exit Sub
            End If

            '----------------------------------
            ' Set AccelTime to Driver
            '----------------------------------
            Try
                dblAccelTime = Val(txtAccelTime.Text)
            Catch ex As Exception
                dblAccelTime = 0
            End Try
            dwRet = SmcWSetAccelTime(Id, AxisNo, dblAccelTime)
            If dwRet <> 0 Then
                SmcWGetErrorString(dwRet, ErrorString)
                lblComment.Text = "SmcWSetAccelTime = " & dwRet & " : " & ErrorString.ToString
                Exit Sub
            End If

            '----------------------------------
            ' Set DecelTime to Driver
            '----------------------------------
            Try
                dblDecelTime = Val(txtDecelTime.Text)
            Catch ex As Exception
                dblDecelTime = 0
            End Try
            dwRet = SmcWSetDecelTime(Id, AxisNo, dblDecelTime)
            If dwRet <> 0 Then
                SmcWGetErrorString(dwRet, ErrorString)
                lblComment.Text = "SmcWSetDecelTime = " & dwRet & " : " & ErrorString.ToString
                Exit Sub
            End If

        End If

        If Change = 5 Or Change = 6 Then
            '-------------------------
            ' Set Distance to Driver
            '-------------------------
            Try
                lDistance = Val(txtDistance.Text)
            Catch ex As Exception
                lDistance = 0
            End Try
            If MotionType = CSMC_PTP And bCoordinate = CSMC_INC Then
                lDistance = System.Math.Abs(lDistance)
                If StartDir = CSMC_CCW Then
                    lDistance = -(lDistance)
                End If
            End If

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

        End If

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

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

        '-------------------------------------
        ' Get setting parameters from Driver
        '-------------------------------------
        bRet = GetMoveParam()
        If bRet = False Then
            Exit Sub
        End If

        lblComment.Text = "OK "

    End Sub

    Private Sub btnComSDStop_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnComSDStop.Click

        dwRet = SmcWMotionDecStop(Id, AxisNo)
        If dwRet <> 0 Then
            SmcWGetErrorString(dwRet, ErrorString)
            lblComment.Text = "SmcWMotionDecStop = " & 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 = "SmcWMotionStop = " & dwRet & " : " & ErrorString.ToString
            Exit Sub
        End If

        lblComment.Text = "OK "

    End Sub

    Private Sub BaseMove_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

        ' Coordinate ComboBox
        cboCoordinate.Items.Clear()
        cboCoordinate.Items.Add("ABS")
        cboCoordinate.Items.Add("INC")
        cboCoordinate.SelectedIndex = CSMC_INC

        ' Coordinate ComboBox
        cboChange.Items.Clear()
        cboChange.Items.Add("At once FL")
        cboChange.Items.Add("At once FH")
        cboChange.Items.Add("Decel FL")
        cboChange.Items.Add("Accel FH")
        cboChange.Items.Add("Motion Change")
        cboChange.Items.Add("Position Change1")
        cboChange.Items.Add("Position Change2")
        cboChange.SelectedIndex = 4

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

        ' Initialize
        dblResolveSpeed = 0
        dblStartSpeed = 0
        dblTargetSpeed = 0
        dblAccelTime = 0
        dblDecelTime = 0
        dblSSpeed = 0
        lDistance = 0
        rdoOptionPtp.Checked = True

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

        '------------------------
        ' Set lDistance to 1000
        '------------------------
        lDistance = 1000
        dwRet = SmcWSetStopPosition(Id, AxisNo, bCoordinate, lDistance)
        If dwRet <> 0 Then
            SmcWGetErrorString(dwRet, ErrorString)
            lblComment.Text = "SmcWSetStopPosition = " & dwRet & " : " & ErrorString.ToString
            Exit Sub
        End If

        '---------------------------
        ' Get parameters of Driver
        '---------------------------
        bRet = GetMoveParam()
        If bRet = False Then
            Exit Sub
        End If

        lblComment.Text = "OK "

    End Sub

    Private Sub rdoOptionJog_Checkedchanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rdoOptionJog.CheckedChanged

        MotionType = CSMC_JOG

    End Sub

    Private Sub rdoOptionOrg_Checkedchanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rdoOptionOrg.CheckedChanged

        MotionType = CSMC_ORG

    End Sub

    Private Sub rdoOptionPtp_Checkedchanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rdoOptionPtp.CheckedChanged

        MotionType = CSMC_PTP

    End Sub

    Function SetMoveParam() As Boolean

        '----------------------------------
        ' Set Resolution to Driver
        '----------------------------------
        Try
            dblResolveSpeed = Val(txtResolution.Text)
        Catch ex As Exception
            dblResolveSpeed = 0
        End Try
        dwRet = SmcWSetResolveSpeed(Id, AxisNo, dblResolveSpeed)
        If dwRet <> 0 Then
            SmcWGetErrorString(dwRet, ErrorString)
            lblComment.Text = "SmcWSetResolveSpeed = " & dwRet & " : " & ErrorString.ToString
            SetMoveParam = False
            Exit Function
        End If

        '----------------------------------
        ' Set StartSpeed to Driver
        '----------------------------------
        Try
            dblStartSpeed = Val(txtStartSpeed.Text)
        Catch ex As Exception
            dblStartSpeed = 0
        End Try
        dwRet = SmcWSetStartSpeed(Id, AxisNo, dblStartSpeed)
        If dwRet <> 0 Then
            SmcWGetErrorString(dwRet, ErrorString)
            lblComment.Text = "SmcWSetStartSpeed = " & dwRet & " : " & ErrorString.ToString
            SetMoveParam = False
            Exit Function
        End If

        '----------------------------------
        ' Set TargetSpeed to Driver
        '----------------------------------
        Try
            dblTargetSpeed = Val(txtTargetSpeed.Text)
        Catch ex As Exception
            dblTargetSpeed = 0
        End Try
        dwRet = SmcWSetTargetSpeed(Id, AxisNo, dblTargetSpeed)
        If dwRet <> 0 Then
            SmcWGetErrorString(dwRet, ErrorString)
            lblComment.Text = "SmcWSetTargetSpeed = " & dwRet & " : " & ErrorString.ToString
            SetMoveParam = False
            Exit Function
        End If

        '----------------------------------
        ' Set AccelTime to Driver
        '----------------------------------
        Try
            dblAccelTime = Val(txtAccelTime.Text)
        Catch ex As Exception
            dblAccelTime = 0
        End Try
        dwRet = SmcWSetAccelTime(Id, AxisNo, dblAccelTime)
        If dwRet <> 0 Then
            SmcWGetErrorString(dwRet, ErrorString)
            lblComment.Text = "SmcWSetAccelTime = " & dwRet & " : " & ErrorString.ToString
            SetMoveParam = False
            Exit Function
        End If

        '----------------------------------
        ' Set DecelTime to Driver
        '----------------------------------
        Try
            dblDecelTime = Val(txtDecelTime.Text)
        Catch ex As Exception
            dblDecelTime = 0
        End Try
        dwRet = SmcWSetDecelTime(Id, AxisNo, dblDecelTime)
        If dwRet <> 0 Then
            SmcWGetErrorString(dwRet, ErrorString)
            lblComment.Text = "SmcWSetDecelTime = " & dwRet & " : " & ErrorString.ToString
            SetMoveParam = False
            Exit Function
        End If

        '-----------------------------
        ' Set SSpeed to Driver
        '-----------------------------
        Try
            dblSSpeed = Val(txtSSpeed.Text)
        Catch ex As Exception
            dblSSpeed = 0
        End Try
        dwRet = SmcWSetSSpeed(Id, AxisNo, dblSSpeed)
        If dwRet <> 0 Then
            SmcWGetErrorString(dwRet, ErrorString)
            lblComment.Text = "SmcWSetSSpeed = " & dwRet & " : " & ErrorString.ToString
            SetMoveParam = False
            Exit Function
        End If

        '-------------------------
        ' Set Distance to Driver
        '-------------------------
        If MotionType <> CSMC_PTP Then
            SetMoveParam = True
            Exit Function
        End If

        Try
            lDistance = Val(txtDistance.Text)
        Catch ex As Exception
            lDistance = 0
        End Try
        If bCoordinate = CSMC_INC Then
            lDistance = System.Math.Abs(lDistance)
            If StartDir = CSMC_CCW Then
                lDistance = -(lDistance)
            End If
        End If

        dwRet = SmcWSetStopPosition(Id, AxisNo, bCoordinate, lDistance)
        If dwRet <> 0 Then
            SmcWGetErrorString(dwRet, ErrorString)
            lblComment.Text = "SmcWSetStopPosition = " & dwRet & " : " & ErrorString.ToString
            SetMoveParam = False
            Exit Function
        End If

        SetMoveParam = True

    End Function

    Function GetMoveParam() As Boolean

        '----------------------------------
        ' Set default value of Resolution
        '----------------------------------
        dwRet = SmcWGetResolveSpeed(Id, AxisNo, dblResolveSpeed)
        If dwRet <> 0 Then
            SmcWGetErrorString(dwRet, ErrorString)
            lblComment.Text = "SmcWGetResolveSpeed = " & dwRet & " : " & ErrorString.ToString
            GetMoveParam = False
            Exit Function
        End If

        txtResolution.Text = Str(dblResolveSpeed)

        '----------------------------------
        ' Set default value of StartSpeed
        '----------------------------------
        dwRet = SmcWGetStartSpeed(Id, AxisNo, dblStartSpeed)
        If dwRet <> 0 Then
            SmcWGetErrorString(dwRet, ErrorString)
            lblComment.Text = "SmcWGetStartSpeed = " & dwRet & " : " & ErrorString.ToString
            GetMoveParam = False
            Exit Function
        End If

        txtStartSpeed.Text = Str(dblStartSpeed)

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

        txtTargetSpeed.Text = Str(dblTargetSpeed)

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

        txtAccelTime.Text = Str(dblAccelTime)

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

        txtDecelTime.Text = Str(dblDecelTime)

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

        txtSSpeed.Text = Str(dblSSpeed)

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

        txtDistance.Text = Str(lDistance)

        GetMoveParam = True

    End Function

    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

        rdoOptionPtp.Checked = True

        MotionType = CSMC_PTP
        '------------------------
        ' Set lDistance to 1000
        '------------------------
        lDistance = 1000
        dwRet = SmcWSetStopPosition(Id, AxisNo, bCoordinate, lDistance)
        If dwRet <> 0 Then
            SmcWGetErrorString(dwRet, ErrorString)
            lblComment.Text = "SmcWSetStopPosition = " & dwRet & " : " & ErrorString.ToString
            Exit Sub
        End If
        '---------------------------
        ' Get parameters of Driver
        '---------------------------
        bRet = GetMoveParam()
        If bRet = False Then
            Exit Sub
        End If

        lblComment.Text = "OK "

    End Sub
End Class
