﻿Imports System.Text
Public Class Form1

    Dim dwRet As Integer
    Dim szComment As String
    Dim Id As Short
    Dim AxisNo As Short
    Dim SyncAxis As Short
    Dim SyncChip As Short
    Dim SyncBoard As Short
    Dim GetSyncAxis As Short
    Dim GetSyncChip As Short
    Dim GetSyncBoard As Short
    Dim ErrorString As New StringBuilder("", 256)  'Error String

    Private Sub chkAxis1_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkAxis1.CheckedChanged

        If chkAxis1.Checked = True Then
            SyncAxis = SyncAxis Or &H1
        ElseIf chkAxis1.Checked = False Then
            SyncAxis = SyncAxis Xor &H1
        End If

        txtSetSync.Text = Hex(SyncAxis) + "H" & " " & Hex(SyncChip) + "H" & " " & Hex(SyncBoard) + "H"

    End Sub

    Private Sub chkAxis2_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkAxis2.CheckedChanged

        If chkAxis2.Checked = True Then
            SyncAxis = SyncAxis Or &H2
        ElseIf chkAxis2.Checked = False Then
            SyncAxis = SyncAxis Xor &H2
        End If

        txtSetSync.Text = Hex(SyncAxis) + "H" & " " & Hex(SyncChip) + "H" & " " & Hex(SyncBoard) + "H"

    End Sub

    Private Sub chkAxis3_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkAxis3.CheckedChanged

        If chkAxis3.Checked = True Then
            SyncAxis = SyncAxis Or &H4
        ElseIf chkAxis3.Checked = False Then
            SyncAxis = SyncAxis Xor &H4
        End If

        txtSetSync.Text = Hex(SyncAxis) + "H" & " " & Hex(SyncChip) + "H" & " " & Hex(SyncBoard) + "H"

    End Sub

    Private Sub chkAxis4_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkAxis4.CheckedChanged

        If chkAxis4.Checked = True Then
            SyncAxis = SyncAxis Or &H8
        ElseIf chkAxis4.Checked = False Then
            SyncAxis = SyncAxis Xor &H8
        End If

        txtSetSync.Text = Hex(SyncAxis) + "H" & " " & Hex(SyncChip) + "H" & " " & Hex(SyncBoard) + "H"

    End Sub

    Private Sub chkAxis5_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkAxis5.CheckedChanged

        If chkAxis5.Checked = True Then
            SyncAxis = SyncAxis Or &H10
        ElseIf chkAxis5.Checked = False Then
            SyncAxis = SyncAxis Xor &H10
        End If

        txtSetSync.Text = Hex(SyncAxis) + "H" & " " & Hex(SyncChip) + "H" & " " & Hex(SyncBoard) + "H"

    End Sub

    Private Sub chkAxis6_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkAxis6.CheckedChanged

        If chkAxis6.Checked = True Then
            SyncAxis = SyncAxis Or &H20
        ElseIf chkAxis6.Checked = False Then
            SyncAxis = SyncAxis Xor &H20
        End If

        txtSetSync.Text = Hex(SyncAxis) + "H" & " " & Hex(SyncChip) + "H" & " " & Hex(SyncBoard) + "H"

    End Sub

    Private Sub chkAxis7_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkAxis7.CheckedChanged

        If chkAxis7.Checked = True Then
            SyncAxis = SyncAxis Or &H40
        ElseIf chkAxis7.Checked = False Then
            SyncAxis = SyncAxis Xor &H40
        End If

        txtSetSync.Text = Hex(SyncAxis) + "H" & " " & Hex(SyncChip) + "H" & " " & Hex(SyncBoard) + "H"

    End Sub

    Private Sub chkAxis8_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkAxis8.CheckedChanged

        If chkAxis8.Checked = True Then
            SyncAxis = SyncAxis Or &H80
        ElseIf chkAxis8.Checked = False Then
            SyncAxis = SyncAxis Xor &H80
        End If

        txtSetSync.Text = Hex(SyncAxis) + "H" & " " & Hex(SyncChip) + "H" & " " & Hex(SyncBoard) + "H"

    End Sub

    Private Sub chkChip_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkChip.CheckedChanged

        If chkChip.Checked = True Then
            SyncChip = 1
        ElseIf chkChip.Checked = False Then
            SyncChip = 0
        End If

        txtSetSync.Text = Hex(SyncAxis) + "H" & " " & Hex(SyncChip) + "H" & " " & Hex(SyncBoard) + "H"

    End Sub

    Private Sub chkBoardCN2_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkBoardCN2.CheckedChanged

        If chkBoardCN2.Checked = True Then
            SyncBoard = SyncBoard Or &H1
        ElseIf chkBoardCN2.Checked = False Then
            SyncBoard = SyncBoard Xor &H1
        End If
        txtSetSync.Text = Hex(SyncAxis) + "H" & " " & Hex(SyncChip) + "H" & " " & Hex(SyncBoard) + "H"

    End Sub

    Private Sub chkBoardCN3_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkBoardCN3.CheckedChanged

        If chkBoardCN3.Checked = True Then
            SyncBoard = SyncBoard Or &H2
        ElseIf chkBoardCN3.Checked = False Then
            SyncBoard = SyncBoard Xor &H2
        End If
        txtSetSync.Text = Hex(SyncAxis) + "H" & " " & Hex(SyncChip) + "H" & " " & Hex(SyncBoard) + "H"

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

        dwRet = SmcWGetSync(Id, GetSyncAxis, GetSyncChip, GetSyncBoard)
        If dwRet <> 0 Then
            SmcWGetErrorString(dwRet, ErrorString)
            lblComment.Text = "SmcWSetSync = " & dwRet & " : " & ErrorString.ToString
            Exit Sub
        End If

        txtGetSync.Text = Hex(GetSyncAxis) + "H" & " " & Hex(GetSyncChip) + "H" & " " & Hex(GetSyncBoard) + "H"

        lblComment.Text = "OK "

    End Sub

    Private Sub btnComSetSync_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnComSetSync.Click

        dwRet = SmcWSetSync(Id, SyncAxis, SyncChip, SyncBoard)
        If dwRet <> 0 Then
            SmcWGetErrorString(dwRet, ErrorString)
            lblComment.Text = "SmcWSetSync = " & dwRet & " : " & ErrorString.ToString
            Exit Sub
        End If

        lblComment.Text = "OK "

    End Sub

    Private Sub btnComStartSync_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnComStartSync.Click

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

        lblComment.Text = "OK "

    End Sub

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

        Dim BoardCN As Short
        Dim Axis As Short
        Dim n As Short
        ' Centering
        Me.Left = (Screen.PrimaryScreen.Bounds.Width - Me.Width) / 2
        Me.Top = (Screen.PrimaryScreen.Bounds.Height - Me.Height) / 2

        AxisNo = 1

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

        ' ComboBox Initialise
        cboTargetAxis.SelectedIndex = AxisNo - 1

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

        SyncAxis = 0
        txtSetSync.Text = Hex(SyncAxis)

        '------------------------
        ' Get synchronous setting
        '------------------------
        dwRet = SmcWGetSync(Id, SyncAxis, SyncChip, SyncBoard)
        If dwRet <> 0 Then
            SmcWGetErrorString(dwRet, ErrorString)
            lblComment.Text = "SmcWSetSync = " & dwRet & " : " & ErrorString.ToString
            Exit Sub
        End If

        For n = 0 To 7
            Axis = (2 ^ n) And &HFF
            If (SyncAxis And Axis) = Axis Then
                Select Case n
                    Case 0
                        chkAxis1.Checked = True
                    Case 1
                        chkAxis2.Checked = True
                    Case 2
                        chkAxis3.Checked = True
                    Case 3
                        chkAxis4.Checked = True
                    Case 4
                        chkAxis5.Checked = True
                    Case 5
                        chkAxis6.Checked = True
                    Case 6
                        chkAxis7.Checked = True
                    Case 7
                        chkAxis8.Checked = True
                End Select
            End If
        Next

        If SyncChip = 1 Then
            chkChip.Checked = True
        End If

        For n = 0 To 2
            BoardCN = (2 ^ n) And &HFF
            If (SyncBoard And BoardCN) = BoardCN Then
                Select Case n
                    Case 0
                        chkBoardCN2.Checked = True
                    Case 1
                        chkBoardCN3.Checked = True
                End Select
            End If
        Next

        lblComment.Text = "OK "

    End Sub

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

        Dim BoardCN As Short
        Dim Axis As Short
        Dim n As Short

        If Id <> -1 Then
            SmcWExit(Id)
        End If

        chkAxis1.Checked = False
        chkAxis2.Checked = False
        chkAxis3.Checked = False
        chkAxis4.Checked = False
        chkAxis5.Checked = False
        chkAxis6.Checked = False
        chkAxis7.Checked = False
        chkAxis8.Checked = False
        chkChip.Checked = False
        chkBoardCN2.Checked = False
        chkBoardCN3.Checked = False

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

        '------------------------
        ' Get synchronous setting
        '------------------------
        dwRet = SmcWGetSync(Id, SyncAxis, SyncChip, SyncBoard)
        If dwRet <> 0 Then
            SmcWGetErrorString(dwRet, ErrorString)
            lblComment.Text = "SmcWSetSync = " & dwRet & " : " & ErrorString.ToString
            Exit Sub
        End If

        For n = 0 To 7
            Axis = (2 ^ n) And &HFF
            If (SyncAxis And Axis) = Axis Then
                Select Case n
                    Case 0
                        chkAxis1.Checked = True
                    Case 1
                        chkAxis2.Checked = True
                    Case 2
                        chkAxis3.Checked = True
                    Case 3
                        chkAxis4.Checked = True
                    Case 4
                        chkAxis5.Checked = True
                    Case 5
                        chkAxis6.Checked = True
                    Case 6
                        chkAxis7.Checked = True
                    Case 7
                        chkAxis8.Checked = True
                End Select
            End If
        Next

        If SyncChip = 1 Then
            chkChip.Checked = True
        End If

        For n = 0 To 2
            BoardCN = (2 ^ n) And &HFF
            If (SyncBoard And BoardCN) = BoardCN Then
                Select Case n
                    Case 0
                        chkBoardCN2.Checked = True
                    Case 1
                        chkBoardCN3.Checked = True
                End Select
            End If
        Next

        lblComment.Text = "OK "

    End Sub
End Class
