Imports System.Threading

Public Class Form_FocusML

    Dim tickBegin As Long
    Dim tickEnd As Long
    Dim Cmd As String = ""
    Dim Rsp As String = "No responses received"
    Dim WithEvents KeyControl As New KeyButton()
    Dim InOutCount1 As Integer = 0
    Dim InOutCount2 As Integer = 0
    Dim IOrealCount, IOrealCount2 As Integer
    Dim FocusTime, FocusTime2 As Integer
    Dim NormColor, ActiveColor As Color
    Dim Ding As Boolean
    Dim TimeStartF, TimeEndF, TimeStartF2, TimeEndF2 As Single
    Dim ProgI, ProgO, ProgI2, ProgO2 As Single
    Dim ReadDelay As Integer = 200
    Dim PrevSerialPort As String = ""
    Dim M1out As String = "M1--> "
    Dim M2out As String = "M2--> "
    Dim M1in As String = "M1<-- "
    Dim M2in As String = "M2<-- "
    Dim Inc1 As Integer = 10
    Dim Inc2 As Integer = 10
    Dim KeyHeld As Boolean = False
    Dim IgnoreKey As Boolean = False
    Dim M1save As String
    Dim M2save As String

    Protected Overrides ReadOnly Property CreateParams() As CreateParams
        Get
            Dim cp As CreateParams = MyBase.CreateParams
            Const CS_DBLCLKS As Int32 = &H8
            Const CS_NOCLOSE As Int32 = &H200
            cp.ClassStyle = CS_DBLCLKS Or CS_NOCLOSE
            Return cp
        End Get
    End Property


    Private Sub Form_FocusML_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Dim Kx, Ky, Kw, Kh As Integer
        Dim Kt As String

        ' Make sure form is on the screen
        If Me.Location.X < 0 Or Me.Location.Y < 0 Then
            Me.Location = New Point(0, 0)
        End If

        ' Create button for keyboard control using arrows
        Kx = ButtonKeyboardDummy.Location.X
        Ky = ButtonKeyboardDummy.Location.Y
        Kw = ButtonKeyboardDummy.Size.Width
        Kh = ButtonKeyboardDummy.Size.Height
        Kt = ButtonKeyboardDummy.Text

        KeyControl.Size = New Size(Kw, Kh)
        KeyControl.Location = New Point(Kx, Ky)
        KeyControl.Text = New String(Kt)
        Me.Controls.Add(KeyControl)
        ToolTip1.SetToolTip(KeyControl, "Click to activate keyboard control")
        KeyControl.BringToFront()

        NormColor = Form1.NormColor
        ActiveColor = Form1.ActiveColor
        TabPage1.Enabled = False
        TabPage2.Enabled = False

    End Sub

    Private Sub Form_FocusML_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Form1.FocusMlW = Me.Width
        Form1.FocusMlH = Me.Height

    End Sub

    Private Sub ButtonClose_Click(sender As System.Object, e As System.EventArgs) Handles ButtonClose.Click
        Form1.FocusMlW = Me.Width
        Form1.FocusMlH = Me.Height
        Me.Hide()

    End Sub

    Private Sub ServoControllerVersionToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ServoControllerVersionToolStripMenuItem.Click
        SendCmdResp(":GV#")
        TextBox1.SelectedText = "Firmware verison " & Rsp & vbCrLf

    End Sub

    Private Sub GetTemperatureToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles GetTemperatureToolStripMenuItem.Click
        Dim DegC As Single
        Dim DegF As Single

        SendCmdResp(":GT#")
        DegC = (Format(CInt("&H" & Rsp), "0")) * 0.5
        DegF = ((DegC * 9) / 5) + 32
        TextBox1.SelectedText = "Current temperature " & Format(DegC, "0.0") & "°C (" & Format(DegF, "0.0") & "°F)" & vbCrLf

    End Sub

    Private Sub CheckMotor1RunningToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles CheckMotor1RunningToolStripMenuItem.Click
        Dim MR As String = "Motor 1 Moving unknown"

        SendCmdResp(":GI#")
        If Rsp = "01" Then
            MR = "Motor 1 Moving"
        End If
        If Rsp = "00" Then
            MR = "Motor 1 Not Moving"
        End If
        TextBox1.SelectedText = MR & vbCrLf

    End Sub

    Private Sub CheckRunningForMotor2ToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles CheckRunningForMotor2ToolStripMenuItem.Click
        Dim MR As String = "Motor 2 Moving unknown"

        SendCmdResp(":2GI#")
        If Rsp = "01" Then
            MR = "Motor 2 Moving"
        End If
        If Rsp = "00" Then
            MR = "Motor 2 Not Moving"
        End If
        TextBox1.SelectedText = MR & vbCrLf

    End Sub

    Private Sub GetRedLedBacklight_Click(sender As System.Object, e As System.EventArgs) Handles GetRedLedBacklight.Click
        Dim RedLed As Integer

        SendCmdResp(":GB#")
        RedLed = Format(CInt("&H" & Rsp), "0")
        TextBox1.SelectedText = "Current RED Led Backlight value " & RedLed & vbCrLf

    End Sub

    Private Sub SetRedLedBacklight_Click(sender As System.Object, e As System.EventArgs) Handles SetRedLedBacklight.Click
        Dim RedLed As String
        Dim H As String
        H = Hex(ComboBoxRedBacklight.SelectedItem).PadLeft(2, "0"c)
        RedLed = ":SB" & H & "#"
        SendCmdNoResp(RedLed, M1out)

    End Sub

    Private Sub ShowCommandsSent_Click(sender As System.Object, e As System.EventArgs) Handles ShowCommandsSent.Click
        If ShowCommandsSent.Checked = True Then
            ShowCommandsSent.Checked = False
        Else
            ShowCommandsSent.Checked = True
        End If

    End Sub

    Private Sub ClearLoggingToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ClearLoggingToolStripMenuItem.Click
        TextBox1.Clear()

    End Sub

    Private Sub ButtonConnect_Click(sender As System.Object, e As System.EventArgs) Handles ButtonConnect.Click
        Dim Purge As String
        Dim ComPort As String
        Dim Motor As Integer = TabControl1.SelectedIndex

        ComPort = Trim(Microsoft.VisualBasic.Left(ToolStripComboBoxCom.Items(ToolStripComboBoxCom.SelectedIndex), 6))
        If SerialPort1.IsOpen = False Then
            SerialPort1.PortName = ComPort
            TextBox1.SelectedText = "Opening " & SerialPort1.PortName & vbCrLf
            Application.DoEvents()
            On Error GoTo Open_Error
            SerialPort1.Open()
            Purge = SerialPort1.ReadExisting()
            ButtonConnect.Text = "Disconnect"
            TextBox1.SelectedText = "Connected " & SerialPort1.PortName & vbCrLf
            TabPage1.Enabled = True
            If DualMotorController.Checked = True Then
                TabPage2.Enabled = True
            End If
            If SerialPort1.PortName <> PrevSerialPort Then
                ZeroCounterFocus1()
                If DualMotorController.Checked = True Then
                    ZeroCounterFocus2()
                End If
            End If
            TabControl1.SelectedIndex = 0
            ButtonGetSpeed1.PerformClick()
            If Rsp <> "" Then
                ButtonGH1.PerformClick()
                ButtonGetCurPos1.PerformClick()
                TabControl1.SelectedIndex = 1
            End If
            If DualMotorController.Checked = True Then
                TabControl1.SelectedIndex = 1
                ButtonGetSpeed2.PerformClick()
                If Rsp <> "" Then
                    ButtonGH2.PerformClick()
                    ButtonGetCurPos2.PerformClick()
                End If
            End If
            TabControl1.SelectedIndex = Motor

        Else
            StopAny()
            PrevSerialPort = SerialPort1.PortName
            SerialPort1.Close()
            ButtonConnect.Text = "Connect"
            TextBox1.SelectedText = "Disconnected " & SerialPort1.PortName & vbCrLf
            TabPage1.Enabled = False
            TabPage2.Enabled = False
        End If
        Exit Sub

Open_Error:
        TextBox1.SelectedText = Err.Description & vbCrLf

    End Sub

    Private Sub SendCmdResp(ByVal Cmd As String)
        Dim Status As String = ""
        Dim Motor As String

        If Mid(Cmd, 2, 1) = "2" Then
            Motor = "M2"
        Else
            Motor = "M1"
        End If

        If SerialPort1.IsOpen = True Then
            Status = ""
            Rsp = ""
            SerialPort1.Write(Cmd)
            If ShowCommandsSent.Checked = True Then
                TextBox1.SelectedText = Motor & "--> " & Cmd & vbCrLf
            End If
            tickBegin = Int(Date.Now.Ticks / 10000000)

            Select Case Cmd
                Case ":GV#"
                    ' Get firmware verison does not return #
                    Do Until Rsp.Length = 2
                        Rsp = Rsp & SerialPort1.ReadExisting()
                        tickEnd = Int(Date.Now.Ticks / 10000000)
                        If tickEnd - tickBegin > Form1.ReadTimeout Then
                            SerialPort1.DiscardInBuffer()
                            TextBox1.SelectedText = "Timeout reading response" & vbCrLf
                            Exit Do
                        End If
                    Loop
                Case Else
                    Do Until Microsoft.VisualBasic.Right(Rsp, 1) = "#"
                        Rsp = Rsp & SerialPort1.ReadExisting()
                        tickEnd = Int(Date.Now.Ticks / 10000000)
                        If tickEnd - tickBegin > Form1.ReadTimeout Then
                            SerialPort1.DiscardInBuffer()
                            TextBox1.SelectedText = "Timeout reading response" & vbCrLf
                            Exit Do
                        End If
                    Loop
            End Select

            If Len(Rsp) > 0 Then
                If ShowCommandsSent.Checked = True Then
                    TextBox1.SelectedText = Motor & "<-- " & Rsp & vbCrLf
                End If
                If Microsoft.VisualBasic.Right(Rsp, 1) = "#" Then
                    Rsp = Mid(Rsp, 1, Rsp.Length - 1)
                End If
            End If
        Else
            TextBox1.SelectedText = "Not connected" & vbCrLf
        End If

    End Sub

    Private Sub SendCmdNoResp(ByVal Cmd As String, ByVal Motor As String)
        If SerialPort1.IsOpen = True Then
            SerialPort1.Write(Cmd)
            If ShowCommandsSent.Checked = True Then
                TextBox1.SelectedText = Motor & Cmd & vbCrLf
            End If
        End If

    End Sub

    Private Sub StopAny()
        If TimerM1.Enabled = True Then
            TimerM1.Enabled = False
            ButtonFocusStop1.PerformClick()
        End If
        If TimerM2.Enabled = True Then
            TimerM2.Enabled = False
            ButtonFocusStop2.PerformClick()
        End If

    End Sub

    Private Sub ZeroCounterFocus1()
        InOutCount1 = 0
        TextBoxIOcount1.Text = InOutCount1

    End Sub

    Private Sub UpdateFocuscount1()
        If InOutCount1 < 0 Then
            TextBoxIOcount1.Text = InOutCount1 & " In"
        Else
            TextBoxIOcount1.Text = InOutCount1 & " Out"
        End If
    End Sub

    Private Sub ZeroCounterFocus2()
        InOutCount2 = 0
        TextBoxIOcount2.Text = InOutCount2

    End Sub

    Private Sub UpdateFocuscount2()
        If InOutCount2 < 0 Then
            TextBoxIOcount2.Text = InOutCount2 & " In"
        Else
            TextBoxIOcount2.Text = InOutCount2 & " Out"
        End If

    End Sub


    REM Focus Motor 1

    Private Sub ButtonGH1_Click(sender As System.Object, e As System.EventArgs) Handles ButtonGH1.Click
        SendCmdResp(":GH#")
        If Rsp = "FF" Then
            TextBoxStep1.Text = "Half"
        End If
        If Rsp = "00" Then
            TextBoxStep1.Text = "Full"
        End If

    End Sub

    Private Sub ButtonGetSpeed1_Click(sender As System.Object, e As System.EventArgs) Handles ButtonGetSpeed1.Click
        Dim I As Integer = -1

        SendCmdResp(":GD#")
        Select Case Rsp
            Case "20"
                TextBoxGetSpeed1.Text = "15 PPS"
            Case "10"
                TextBoxGetSpeed1.Text = "30 PPS"
            Case "08"
                TextBoxGetSpeed1.Text = "60 PPS"
            Case "04"
                TextBoxGetSpeed1.Text = "125 PPS"
            Case "02"
                TextBoxGetSpeed1.Text = "250 PPS"
        End Select

    End Sub

    Private Sub ButtonGetCurPos1_Click(sender As System.Object, e As System.EventArgs) Handles ButtonGetCurPos1.Click
        SendCmdResp(":GP#")
        If Rsp.Length = 4 Then
            TextBoxCurPos1.Text = Format(CInt("&H" & Rsp), "00000")
        End If

    End Sub

    Private Sub ButtonSetCurPos1_Click(sender As System.Object, e As System.EventArgs) Handles ButtonSetCurPos1.Click
        Dim P As String
        Dim H As String
        If TextBoxCurPos1.Text > 65535 Then
            TextBoxCurPos1.Text = "65535"
        End If
        H = Hex(TextBoxCurPos1.Text).PadLeft(4, "0"c)
        P = ":SP" & H & "#"
        SendCmdNoResp(P, M1out)

    End Sub

    Private Sub ButtonGetNewPos1_Click(sender As System.Object, e As System.EventArgs) Handles ButtonGetNewPos1.Click
        SendCmdResp(":GN#")
        If Rsp.Length = 4 Then
            TextBoxNewPos1.Text = Format(CInt("&H" & Rsp), "00000")
        End If

    End Sub

    Private Sub ButtonSetNewPos1_Click(sender As System.Object, e As System.EventArgs) Handles ButtonSetNewPos1.Click
        Dim P As String
        Dim H As String
        If TextBoxNewPos1.Text > 65535 Then
            TextBoxNewPos1.Text = "65535"
        End If
        H = Hex(TextBoxNewPos1.Text).PadLeft(4, "0"c)
        P = ":SN" & H & "#"
        SendCmdNoResp(P, M1out)

    End Sub

    Private Sub ButtonMoveNewPos1_Click(sender As System.Object, e As System.EventArgs) Handles ButtonMoveNewPos1.Click
        SendCmdNoResp(":FG#", M1out)
        TimerM1.Enabled = True
        LabelMovingM1.Visible = True

    End Sub

    Private Sub ButtonSetSpeed1_Click(sender As System.Object, e As System.EventArgs) Handles ButtonSetSpeed1.Click
        Dim I As Integer = ComboBoxMotorSpeed1.SelectedIndex

        Select Case I
            Case 4
                SendCmdNoResp(":SD02#", M1out)
            Case 3
                SendCmdNoResp(":SD04#", M1out)
            Case 2
                SendCmdNoResp(":SD08#", M1out)
            Case 1
                SendCmdNoResp(":SD10#", M1out)
            Case 0
                SendCmdNoResp(":SD20#", M1out)
        End Select
        ButtonGetSpeed1.PerformClick()

    End Sub

    Private Sub ButtonSetStepFull1_Click(sender As System.Object, e As System.EventArgs) Handles ButtonSetStepFull1.Click
        SendCmdNoResp(":SF#", M1out)
        ButtonGH1.PerformClick()

    End Sub

    Private Sub ButtonSetStepHalf1_Click(sender As System.Object, e As System.EventArgs) Handles ButtonSetStepHalf1.Click
        SendCmdNoResp(":SH#", M1out)
        ButtonGH1.PerformClick()

    End Sub

    Private Sub LabelZeroInOut1_Click(sender As System.Object, e As System.EventArgs) Handles LabelZeroInOut1.Click
        ZeroCounterFocus1()
        ButtonGH1.PerformClick()

    End Sub

    Private Sub ButtonIn1_Click(sender As System.Object, e As System.EventArgs) Handles ButtonIn1.Click
        Dim Pos As Integer

        SendCmdResp(":GP#")
        If Rsp.Length = 4 Then
            Pos = CInt("&H" & Rsp)
            Pos = Pos - Inc1
            If Pos < 0 Then
                Pos = 0
            End If
            TextBoxNewPos1.Text = Format(Pos, "00000")
            ButtonSetNewPos1.PerformClick()
            ButtonMoveNewPos1.PerformClick()
            TimerM1.Enabled = True
            InOutCount1 = InOutCount1 - Inc1
            UpdateFocuscount1()
        End If

    End Sub

    Private Sub ButtonOut1_Click(sender As System.Object, e As System.EventArgs) Handles ButtonOut1.Click
        Dim Pos As Integer

        SendCmdResp(":GP#")
        If Rsp.Length = 4 Then
            Pos = CInt("&H" & Rsp)
            Pos = Pos + Inc1
            If Pos > 65535 Then
                Pos = 65535
            End If
            TextBoxNewPos1.Text = Format(Pos, "00000")
            ButtonSetNewPos1.PerformClick()
            ButtonMoveNewPos1.PerformClick()
            TimerM1.Enabled = True
            InOutCount1 = InOutCount1 + Inc1
            UpdateFocuscount1()
        End If

    End Sub

    Private Sub ComboBoxInc1_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ComboBoxInc1.SelectedIndexChanged
        Inc1 = ComboBoxInc1.SelectedItem

    End Sub

    Private Sub ComboBoxInc1_TextChanged(sender As Object, e As System.EventArgs) Handles ComboBoxInc1.TextChanged
        Inc1 = ComboBoxInc1.Text

    End Sub

    Private Sub ButtonFocusStop1_Click(sender As System.Object, e As System.EventArgs) Handles ButtonFocusStop1.Click
        SendCmdNoResp(":FQ#", M1out)
        LabelMovingM1.Visible = False

    End Sub

    Private Sub TimerM1_Tick(sender As System.Object, e As System.EventArgs) Handles TimerM1.Tick
        SendCmdResp(":GI#")
        If Rsp = "01" Then
            ButtonGetCurPos1.PerformClick()
        End If
        If Rsp = "00" Then
            LabelMovingM1.Visible = False
            TimerM1.Enabled = False
            ButtonGetCurPos1.PerformClick()
        End If

    End Sub


    REM Focus Motor 2

    Private Sub ButtonGH2_Click(sender As System.Object, e As System.EventArgs) Handles ButtonGH2.Click
        SendCmdResp(":2GH#")
        If Rsp = "FF" Then
            TextBoxStep2.Text = "Half"
        End If
        If Rsp = "00" Then
            TextBoxStep2.Text = "Full"
        End If

    End Sub

    Private Sub ButtonGetSpeed2_Click(sender As System.Object, e As System.EventArgs) Handles ButtonGetSpeed2.Click
        Dim I As Integer = -1

        SendCmdResp(":2GD#")
        Select Case Rsp
            Case "20"
                TextBoxGetSpeed2.Text = "15 PPS"
            Case "10"
                TextBoxGetSpeed2.Text = "30 PPS"
            Case "08"
                TextBoxGetSpeed2.Text = "60 PPS"
            Case "04"
                TextBoxGetSpeed2.Text = "125 PPS"
            Case "02"
                TextBoxGetSpeed2.Text = "250 PPS"
        End Select

    End Sub

    Private Sub ButtonGetCurPos2_Click(sender As System.Object, e As System.EventArgs) Handles ButtonGetCurPos2.Click
        SendCmdResp(":2GP#")
        If Rsp.Length = 4 Then
            TextBoxCurPos2.Text = Format(CInt("&H" & Rsp), "00000")
        End If

    End Sub

    Private Sub ButtonSetCurPos2_Click(sender As System.Object, e As System.EventArgs) Handles ButtonSetCurPos2.Click
        Dim P As String
        Dim H As String
        If TextBoxCurPos2.Text > 65535 Then
            TextBoxCurPos2.Text = "65535"
        End If
        H = Hex(TextBoxCurPos2.Text).PadLeft(4, "0"c)
        P = ":2SP" & H & "#"
        SendCmdNoResp(P, M2out)

    End Sub

    Private Sub ButtonGetNewPos2_Click(sender As System.Object, e As System.EventArgs) Handles ButtonGetNewPos2.Click
        SendCmdResp(":2GN#")
        If Rsp.Length = 4 Then
            TextBoxNewPos2.Text = Format(CInt("&H" & Rsp), "00000")
        End If

    End Sub

    Private Sub ButtonSetNewPos2_Click(sender As System.Object, e As System.EventArgs) Handles ButtonSetNewPos2.Click
        Dim P As String
        Dim H As String
        If TextBoxNewPos2.Text > 65535 Then
            TextBoxNewPos2.Text = "65535"
        End If
        H = Hex(TextBoxNewPos2.Text).PadLeft(4, "0"c)
        P = ":2SN" & H & "#"
        SendCmdNoResp(P, M2out)

    End Sub

    Private Sub ButtonMoveNewPos2_Click(sender As System.Object, e As System.EventArgs) Handles ButtonMoveNewPos2.Click
        SendCmdNoResp(":2FG#", M2out)
        LabelMovingM2.Visible = True
        TimerM2.Enabled = True

    End Sub

    Private Sub ButtonSetSpeed2_Click(sender As System.Object, e As System.EventArgs) Handles ButtonSetSpeed2.Click
        Dim I As Integer = ComboBoxMotorSpeed2.SelectedIndex

        Select Case I
            Case 4
                SendCmdNoResp(":2SD02#", M2out)
            Case 3
                SendCmdNoResp(":2SD04#", M2out)
            Case 2
                SendCmdNoResp(":2SD08#", M2out)
            Case 1
                SendCmdNoResp(":2SD10#", M2out)
            Case 0
                SendCmdNoResp(":2SD20#", M2out)
        End Select
        ButtonGetSpeed2.PerformClick()

    End Sub

    Private Sub ButtonSetStepFull2_Click(sender As System.Object, e As System.EventArgs) Handles ButtonSetStepFull2.Click
        SendCmdNoResp(":2SF#", M2out)
        ButtonGH2.PerformClick()

    End Sub

    Private Sub ButtonSetStepHalf2_Click(sender As System.Object, e As System.EventArgs) Handles ButtonSetStepHalf2.Click
        SendCmdNoResp(":2SH#", M2out)
        ButtonGH2.PerformClick()

    End Sub

    Private Sub LabelZeroInOut2_Click(sender As System.Object, e As System.EventArgs) Handles LabelZeroInOut2.Click
        ZeroCounterFocus2()

    End Sub

    Private Sub Button1n2_Click(sender As System.Object, e As System.EventArgs) Handles ButtonIn2.Click
        Dim Pos As Integer

        SendCmdResp(":2GP#")
        If Rsp.Length = 4 Then
            Pos = CInt("&H" & Rsp)
            Pos = Pos - Inc2
            If Pos < 0 Then
                Pos = 0
            End If
            TextBoxNewPos2.Text = Format(Pos, "00000")
            ButtonSetNewPos2.PerformClick()
            ButtonMoveNewPos2.PerformClick()
            TimerM2.Enabled = True
            InOutCount2 = InOutCount2 - Inc2
            UpdateFocuscount2()
        End If

    End Sub

    Private Sub ButtonOut2_Click(sender As System.Object, e As System.EventArgs) Handles ButtonOut2.Click
        Dim Pos As Integer

        SendCmdResp(":2GP#")
        If Rsp.Length = 4 Then
            Pos = CInt("&H" & Rsp)
            Pos = Pos + Inc2
            If Pos > 65535 Then
                Pos = 65535
            End If
            TextBoxNewPos2.Text = Format(Pos, "00000")
            ButtonSetNewPos2.PerformClick()
            ButtonMoveNewPos2.PerformClick()
            TimerM2.Enabled = True
            InOutCount2 = InOutCount2 + Inc2
            UpdateFocuscount2()
        End If

    End Sub

    Private Sub ComboBoxInc2_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ComboBoxInc2.SelectedIndexChanged
        Inc2 = ComboBoxInc2.SelectedItem

    End Sub

    Private Sub ComboBoxInc2_TextChanged(sender As Object, e As System.EventArgs) Handles ComboBoxInc2.TextChanged
        Inc2 = ComboBoxInc2.Text

    End Sub

    Private Sub ButtonFocusStop2_Click(sender As System.Object, e As System.EventArgs) Handles ButtonFocusStop2.Click
        LabelMovingM2.Visible = False
        SendCmdNoResp(":2FQ#", M2out)

    End Sub


    Private Sub TimerM2_Tick(sender As System.Object, e As System.EventArgs) Handles TimerM2.Tick
        SendCmdResp(":2GI#")
        If Rsp = "01" Then
            ButtonGetCurPos2.PerformClick()
        End If
        If Rsp = "00" Then
            LabelMovingM2.Visible = False
            TimerM2.Enabled = False
            ButtonGetCurPos2.PerformClick()
        End If

    End Sub

    Private Sub KeyControl_GotFocus(sender As Object, e As System.EventArgs) Handles KeyControl.GotFocus
        KeyControl.BackColor = ActiveColor
        LabelKey.Visible = True

    End Sub

    Private Sub KeyControl_LostFocus(sender As Object, e As System.EventArgs) Handles KeyControl.LostFocus
        KeyControl.BackColor = Color.Transparent
        LabelKey.Visible = False

    End Sub

    Private Sub KeyControl_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles KeyControl.KeyDown
        Dim K As String

        If IgnoreKey = False Then
            If KeyHeld = False Then
                K = e.KeyCode
                If K = Keys.Menu Then
                    IgnoreKey = True
                Else

                    Select Case TabControl1.SelectedIndex
                        Case 0
                            ' Motor 1
                            Select Case K
                                Case Keys.PageUp, Keys.OemOpenBrackets, Keys.NumPad4
                                    ButtonIn1.PerformClick()
                                Case Keys.PageDown, Keys.OemCloseBrackets, Keys.NumPad6
                                    ButtonOut1.PerformClick()
                            End Select

                        Case 1
                            ' Motor 2
                            Select Case K
                                Case Keys.PageUp, Keys.OemOpenBrackets, Keys.NumPad4
                                    ButtonIn2.PerformClick()
                                Case Keys.PageDown, Keys.OemCloseBrackets, Keys.NumPad6
                                    ButtonOut2.PerformClick()
                            End Select

                    End Select

                    If Form1.GroupBox1.Enabled = True Then
                        ' Mount movement
                        If Form1.CheckBoxSlewTime.Checked = False Then
                            ' Mount movement
                            Select Case K
                                Case Keys.Up, Keys.NumPad5
                                    Form1.ProgNstart()
                                Case Keys.Down, Keys.NumPad2
                                    Form1.ProgSstart()
                                Case Keys.Right, Keys.NumPad3
                                    Form1.ProgEstart()
                                Case Keys.Left, Keys.NumPad1
                                    Form1.ProgWstart()
                            End Select
                        End If
                    End If

                    KeyHeld = True
                End If
            End If
        End If


    End Sub

    Private Sub KeyControl_KeyUp(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles KeyControl.KeyUp
        Dim K As String = ""

        K = e.KeyCode
        If IgnoreKey = False Then


            If Form1.GroupBox1.Enabled = True Then
                ' Mount movement
                If Form1.CheckBoxSlewTime.Checked = False Then
                    ' Mount movement
                    Select Case K
                        Case Keys.Up, Keys.NumPad5
                            Form1.ProgNend()
                        Case Keys.Down, Keys.NumPad2
                            Form1.progSend()
                        Case Keys.Right, Keys.NumPad3
                            Form1.progEend()
                        Case Keys.Left, Keys.NumPad1
                            Form1.progWend()
                    End Select
                Else
                    Select Case K
                        Case Keys.Up, Keys.NumPad5
                            Form1.ButtonN.PerformClick()
                        Case Keys.Down, Keys.NumPad2
                            Form1.ButtonS.PerformClick()
                        Case Keys.Right, Keys.NumPad3
                            Form1.ButtonE.PerformClick()
                        Case Keys.Left, Keys.NumPad1
                            Form1.ButtonW.PerformClick()
                    End Select
                End If
            End If


            KeyHeld = False
        Else
            If K = Keys.Menu Then
                IgnoreKey = False
            End If
        End If

    End Sub

    Private Sub DualMotorController_Click(sender As System.Object, e As System.EventArgs) Handles DualMotorController.Click
        If DualMotorController.Checked = True Then
            DualMotorController.Checked = False
            TabPage2.Enabled = False
        Else
            DualMotorController.Checked = True
            TabPage2.Enabled = True
        End If
    End Sub

    Private Sub LabelKeyHelp_Click(sender As System.Object, e As System.EventArgs) Handles LabelKeyHelp.Click
        If Form_FocusHelp.Visible = True Then
            Form_FocusHelp.Close()
        Else
            Form_FocusHelp.Show()
        End If

    End Sub

    Private Sub ButtonSavePos1_Click(sender As System.Object, e As System.EventArgs) Handles ButtonSavePos1.Click
        M1save = TextBoxCurPos1.Text
        ButtonRestorePos1.Enabled = True
        TextBox1.SelectedText = "Saved Position " & M1save & vbCrLf

    End Sub

    Private Sub ButtonRestorePos1_Click(sender As System.Object, e As System.EventArgs) Handles ButtonRestorePos1.Click
        TextBoxNewPos1.Text = M1save
        TextBox1.SelectedText = "Restore Position " & M1save & vbCrLf
        ButtonSetNewPos1.PerformClick()
        ButtonMoveNewPos1.PerformClick()

    End Sub

    Private Sub ButtonSavePos2_Click(sender As System.Object, e As System.EventArgs) Handles ButtonSavePos2.Click
        M2save = TextBoxCurPos2.Text
        ButtonRestorePos2.Enabled = True
        TextBox1.SelectedText = "Saved Position " & M2save & vbCrLf

    End Sub

    Private Sub ButtonRestorePos2_Click(sender As System.Object, e As System.EventArgs) Handles ButtonRestorePos2.Click
        TextBoxNewPos2.Text = M2save
        TextBox1.SelectedText = "Restore Position " & M2save & vbCrLf
        ButtonSetNewPos2.PerformClick()
        ButtonMoveNewPos2.PerformClick()

    End Sub
End Class