Public Class Form1
	Inherits System.Windows.Forms.Form

    ' This subroutine uses another thread to write to the Comm Port. It 
    '// Private members
    Private miComPort As Integer
    Friend WithEvents btnOpenCom As System.Windows.Forms.Button
    Friend WithEvents btnCloseCom As System.Windows.Forms.Button
    Friend WithEvents btnTx As System.Windows.Forms.Button
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents txtTx As System.Windows.Forms.TextBox
    Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
    Friend WithEvents optCom2 As System.Windows.Forms.RadioButton
    Friend WithEvents optCom1 As System.Windows.Forms.RadioButton
    Friend WithEvents txtTimeout As System.Windows.Forms.TextBox
    Friend WithEvents Label4 As System.Windows.Forms.Label
    Friend WithEvents txtBaudrate As System.Windows.Forms.TextBox
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents ToolTip1 As System.Windows.Forms.ToolTip
    Friend WithEvents chkAutorx As System.Windows.Forms.CheckBox
    Friend WithEvents chkRTS As System.Windows.Forms.CheckBox
    Friend WithEvents chkDTR As System.Windows.Forms.CheckBox
    Friend WithEvents btnExit As System.Windows.Forms.Button
    Private WithEvents moRS232 As Rs232
    Friend WithEvents Timer1 As System.Windows.Forms.Timer
    Friend WithEvents interval As System.Windows.Forms.TextBox
    Friend WithEvents Label3 As System.Windows.Forms.Label
    Friend WithEvents chkrandom As System.Windows.Forms.CheckBox
    Friend WithEvents runstep As System.Windows.Forms.TextBox
    Friend WithEvents steps As System.Windows.Forms.Label
    Friend WithEvents GroupBox2 As System.Windows.Forms.GroupBox
    Friend WithEvents GroupBox3 As System.Windows.Forms.GroupBox
    Private mlTicks As Long
    Friend WithEvents manual_deg As System.Windows.Forms.TextBox
    Friend WithEvents auto_deg As System.Windows.Forms.TextBox
    Friend WithEvents Label6 As System.Windows.Forms.Label
    Friend WithEvents Label5 As System.Windows.Forms.Label
    Friend WithEvents Label7 As System.Windows.Forms.Label
    Friend WithEvents repeat As System.Windows.Forms.TextBox
    Friend WithEvents Label9 As System.Windows.Forms.Label
    Private Delegate Sub CommEventUpdate(ByVal source As Rs232, ByVal mask As Rs232.EventMasks)
    Public a As Byte = 0
    Dim Out() As Byte

#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
    Private components As System.ComponentModel.IContainer


    'Required by the Windows Form Designer

    '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 Label8 As System.Windows.Forms.Label
    Friend WithEvents txtPortNum As System.Windows.Forms.TextBox
    Friend WithEvents btnTest As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container
        Me.ToolTip1 = New System.Windows.Forms.ToolTip(Me.components)
        Me.chkRTS = New System.Windows.Forms.CheckBox
        Me.btnTx = New System.Windows.Forms.Button
        Me.txtTimeout = New System.Windows.Forms.TextBox
        Me.txtTx = New System.Windows.Forms.TextBox
        Me.btnOpenCom = New System.Windows.Forms.Button
        Me.txtBaudrate = New System.Windows.Forms.TextBox
        Me.chkDTR = New System.Windows.Forms.CheckBox
        Me.txtPortNum = New System.Windows.Forms.TextBox
        Me.btnTest = New System.Windows.Forms.Button
        Me.interval = New System.Windows.Forms.TextBox
        Me.runstep = New System.Windows.Forms.TextBox
        Me.manual_deg = New System.Windows.Forms.TextBox
        Me.auto_deg = New System.Windows.Forms.TextBox
        Me.repeat = New System.Windows.Forms.TextBox
        Me.chkAutorx = New System.Windows.Forms.CheckBox
        Me.chkrandom = New System.Windows.Forms.CheckBox
        Me.Label1 = New System.Windows.Forms.Label
        Me.Label4 = New System.Windows.Forms.Label
        Me.Label2 = New System.Windows.Forms.Label
        Me.optCom1 = New System.Windows.Forms.RadioButton
        Me.btnCloseCom = New System.Windows.Forms.Button
        Me.optCom2 = New System.Windows.Forms.RadioButton
        Me.GroupBox1 = New System.Windows.Forms.GroupBox
        Me.Label9 = New System.Windows.Forms.Label
        Me.Label8 = New System.Windows.Forms.Label
        Me.btnExit = New System.Windows.Forms.Button
        Me.Timer1 = New System.Windows.Forms.Timer(Me.components)
        Me.Label3 = New System.Windows.Forms.Label
        Me.steps = New System.Windows.Forms.Label
        Me.GroupBox2 = New System.Windows.Forms.GroupBox
        Me.Label7 = New System.Windows.Forms.Label
        Me.Label6 = New System.Windows.Forms.Label
        Me.GroupBox3 = New System.Windows.Forms.GroupBox
        Me.Label5 = New System.Windows.Forms.Label
        Me.GroupBox1.SuspendLayout()
        Me.GroupBox2.SuspendLayout()
        Me.GroupBox3.SuspendLayout()
        Me.SuspendLayout()
        '
        'ToolTip1
        '
        '
        'chkRTS
        '
        Me.chkRTS.Checked = True
        Me.chkRTS.CheckState = System.Windows.Forms.CheckState.Checked
        Me.chkRTS.FlatStyle = System.Windows.Forms.FlatStyle.System
        Me.chkRTS.Location = New System.Drawing.Point(212, 90)
        Me.chkRTS.Name = "chkRTS"
        Me.chkRTS.Size = New System.Drawing.Size(63, 19)
        Me.chkRTS.TabIndex = 3
        Me.chkRTS.Text = "RTS"
        Me.ToolTip1.SetToolTip(Me.chkRTS, "Set state of RTS Line")
        '
        'btnTx
        '
        Me.btnTx.Enabled = False
        Me.btnTx.Location = New System.Drawing.Point(204, 27)
        Me.btnTx.Name = "btnTx"
        Me.btnTx.Size = New System.Drawing.Size(88, 21)
        Me.btnTx.TabIndex = 7
        Me.btnTx.Text = "Tx"
        Me.ToolTip1.SetToolTip(Me.btnTx, "Sends data to COM Port")
        '
        'txtTimeout
        '
        Me.txtTimeout.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.txtTimeout.Location = New System.Drawing.Point(69, 24)
        Me.txtTimeout.Name = "txtTimeout"
        Me.txtTimeout.Size = New System.Drawing.Size(49, 21)
        Me.txtTimeout.TabIndex = 3
        Me.txtTimeout.Text = "1500"
        Me.ToolTip1.SetToolTip(Me.txtTimeout, "COM Port timeout in ms")
        '
        'txtTx
        '
        Me.txtTx.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.txtTx.Location = New System.Drawing.Point(69, 27)
        Me.txtTx.Name = "txtTx"
        Me.txtTx.Size = New System.Drawing.Size(48, 21)
        Me.txtTx.TabIndex = 6
        Me.txtTx.Text = "100"
        Me.ToolTip1.SetToolTip(Me.txtTx, "Type data you want to send")
        '
        'btnOpenCom
        '
        Me.btnOpenCom.Location = New System.Drawing.Point(210, 19)
        Me.btnOpenCom.Name = "btnOpenCom"
        Me.btnOpenCom.Size = New System.Drawing.Size(95, 27)
        Me.btnOpenCom.TabIndex = 1
        Me.btnOpenCom.Text = "Open COM Port"
        Me.ToolTip1.SetToolTip(Me.btnOpenCom, "Initializes and Open COM port")
        '
        'txtBaudrate
        '
        Me.txtBaudrate.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.txtBaudrate.Location = New System.Drawing.Point(69, 60)
        Me.txtBaudrate.Name = "txtBaudrate"
        Me.txtBaudrate.Size = New System.Drawing.Size(49, 21)
        Me.txtBaudrate.TabIndex = 5
        Me.txtBaudrate.Text = "2000"
        Me.ToolTip1.SetToolTip(Me.txtBaudrate, "COM Port Baudrate")
        '
        'chkDTR
        '
        Me.chkDTR.Checked = True
        Me.chkDTR.CheckState = System.Windows.Forms.CheckState.Checked
        Me.chkDTR.FlatStyle = System.Windows.Forms.FlatStyle.System
        Me.chkDTR.Location = New System.Drawing.Point(260, 90)
        Me.chkDTR.Name = "chkDTR"
        Me.chkDTR.Size = New System.Drawing.Size(63, 19)
        Me.chkDTR.TabIndex = 4
        Me.chkDTR.Text = "DTR"
        Me.ToolTip1.SetToolTip(Me.chkDTR, "Set state of DTR Line")
        '
        'txtPortNum
        '
        Me.txtPortNum.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.txtPortNum.Location = New System.Drawing.Point(132, 60)
        Me.txtPortNum.Name = "txtPortNum"
        Me.txtPortNum.Size = New System.Drawing.Size(49, 21)
        Me.txtPortNum.TabIndex = 6
        Me.txtPortNum.Text = "1"
        Me.ToolTip1.SetToolTip(Me.txtPortNum, "Enter port number")
        '
        'btnTest
        '
        Me.btnTest.Location = New System.Drawing.Point(132, 98)
        Me.btnTest.Name = "btnTest"
        Me.btnTest.Size = New System.Drawing.Size(49, 21)
        Me.btnTest.TabIndex = 7
        Me.btnTest.Text = "Test"
        Me.ToolTip1.SetToolTip(Me.btnTest, "Test port availability")
        '
        'interval
        '
        Me.interval.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.interval.Location = New System.Drawing.Point(30, 34)
        Me.interval.Name = "interval"
        Me.interval.Size = New System.Drawing.Size(59, 21)
        Me.interval.TabIndex = 34
        Me.interval.Text = "5000"
        Me.ToolTip1.SetToolTip(Me.interval, "Interval between send data")
        '
        'runstep
        '
        Me.runstep.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.runstep.Location = New System.Drawing.Point(17, 86)
        Me.runstep.Name = "runstep"
        Me.runstep.Size = New System.Drawing.Size(46, 21)
        Me.runstep.TabIndex = 37
        Me.runstep.Text = "50"
        Me.ToolTip1.SetToolTip(Me.runstep, "Type data you want to send")
        '
        'manual_deg
        '
        Me.manual_deg.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.manual_deg.Location = New System.Drawing.Point(132, 27)
        Me.manual_deg.Name = "manual_deg"
        Me.manual_deg.Size = New System.Drawing.Size(49, 21)
        Me.manual_deg.TabIndex = 8
        Me.manual_deg.Text = "180"
        Me.ToolTip1.SetToolTip(Me.manual_deg, "Type data you want to send")
        '
        'auto_deg
        '
        Me.auto_deg.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.auto_deg.Location = New System.Drawing.Point(72, 86)
        Me.auto_deg.Name = "auto_deg"
        Me.auto_deg.Size = New System.Drawing.Size(48, 21)
        Me.auto_deg.TabIndex = 39
        Me.auto_deg.Text = "100"
        Me.ToolTip1.SetToolTip(Me.auto_deg, "Type data you want to send")
        '
        'repeat
        '
        Me.repeat.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.repeat.Location = New System.Drawing.Point(70, 98)
        Me.repeat.Name = "repeat"
        Me.repeat.Size = New System.Drawing.Size(48, 21)
        Me.repeat.TabIndex = 40
        Me.repeat.Text = "3"
        Me.ToolTip1.SetToolTip(Me.repeat, "Will send burs of bytes")
        '
        'chkAutorx
        '
        Me.chkAutorx.Enabled = False
        Me.chkAutorx.Location = New System.Drawing.Point(151, 28)
        Me.chkAutorx.Name = "chkAutorx"
        Me.chkAutorx.Size = New System.Drawing.Size(141, 30)
        Me.chkAutorx.TabIndex = 13
        Me.chkAutorx.Text = "Auto transmit steps"
        '
        'chkrandom
        '
        Me.chkrandom.Enabled = False
        Me.chkrandom.Location = New System.Drawing.Point(151, 80)
        Me.chkrandom.Name = "chkrandom"
        Me.chkrandom.Size = New System.Drawing.Size(121, 30)
        Me.chkrandom.TabIndex = 36
        Me.chkrandom.Text = "Random 0 to 199"
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(69, 10)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(82, 14)
        Me.Label1.TabIndex = 2
        Me.Label1.Text = "Timeout (ms)"
        '
        'Label4
        '
        Me.Label4.Location = New System.Drawing.Point(69, 46)
        Me.Label4.Name = "Label4"
        Me.Label4.Size = New System.Drawing.Size(82, 14)
        Me.Label4.TabIndex = 4
        Me.Label4.Text = "BaudRate"
        '
        'Label2
        '
        Me.Label2.Location = New System.Drawing.Point(7, 29)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(82, 14)
        Me.Label2.TabIndex = 5
        Me.Label2.Text = "Data to Tx"
        '
        'optCom1
        '
        Me.optCom1.Checked = True
        Me.optCom1.Location = New System.Drawing.Point(10, 18)
        Me.optCom1.Name = "optCom1"
        Me.optCom1.Size = New System.Drawing.Size(64, 26)
        Me.optCom1.TabIndex = 0
        Me.optCom1.TabStop = True
        Me.optCom1.Text = "COM &1"
        '
        'btnCloseCom
        '
        Me.btnCloseCom.Enabled = False
        Me.btnCloseCom.Location = New System.Drawing.Point(211, 51)
        Me.btnCloseCom.Name = "btnCloseCom"
        Me.btnCloseCom.Size = New System.Drawing.Size(95, 27)
        Me.btnCloseCom.TabIndex = 2
        Me.btnCloseCom.Text = "Close COM Port"
        '
        'optCom2
        '
        Me.optCom2.Location = New System.Drawing.Point(10, 38)
        Me.optCom2.Name = "optCom2"
        Me.optCom2.Size = New System.Drawing.Size(66, 26)
        Me.optCom2.TabIndex = 1
        Me.optCom2.Text = "COM &2"
        '
        'GroupBox1
        '
        Me.GroupBox1.Controls.Add(Me.Label9)
        Me.GroupBox1.Controls.Add(Me.repeat)
        Me.GroupBox1.Controls.Add(Me.Label8)
        Me.GroupBox1.Controls.Add(Me.btnTest)
        Me.GroupBox1.Controls.Add(Me.txtPortNum)
        Me.GroupBox1.Controls.Add(Me.txtTimeout)
        Me.GroupBox1.Controls.Add(Me.Label4)
        Me.GroupBox1.Controls.Add(Me.txtBaudrate)
        Me.GroupBox1.Controls.Add(Me.Label1)
        Me.GroupBox1.Controls.Add(Me.optCom2)
        Me.GroupBox1.Controls.Add(Me.optCom1)
        Me.GroupBox1.Location = New System.Drawing.Point(7, 19)
        Me.GroupBox1.Name = "GroupBox1"
        Me.GroupBox1.Size = New System.Drawing.Size(198, 132)
        Me.GroupBox1.TabIndex = 0
        Me.GroupBox1.TabStop = False
        Me.GroupBox1.Text = "COM Setup"
        '
        'Label9
        '
        Me.Label9.Location = New System.Drawing.Point(70, 83)
        Me.Label9.Name = "Label9"
        Me.Label9.Size = New System.Drawing.Size(46, 14)
        Me.Label9.TabIndex = 41
        Me.Label9.Text = "Repeat"
        '
        'Label8
        '
        Me.Label8.Location = New System.Drawing.Point(132, 45)
        Me.Label8.Name = "Label8"
        Me.Label8.Size = New System.Drawing.Size(58, 14)
        Me.Label8.TabIndex = 8
        Me.Label8.Text = "Port check"
        '
        'btnExit
        '
        Me.btnExit.Anchor = CType(((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.btnExit.Location = New System.Drawing.Point(211, 363)
        Me.btnExit.Name = "btnExit"
        Me.btnExit.Size = New System.Drawing.Size(93, 25)
        Me.btnExit.TabIndex = 14
        Me.btnExit.Text = "&Close"
        '
        'Timer1
        '
        '
        'Label3
        '
        Me.Label3.Location = New System.Drawing.Point(19, 17)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(98, 14)
        Me.Label3.TabIndex = 35
        Me.Label3.Text = "Send interval (ms)"
        '
        'steps
        '
        Me.steps.Location = New System.Drawing.Point(18, 69)
        Me.steps.Name = "steps"
        Me.steps.Size = New System.Drawing.Size(35, 14)
        Me.steps.TabIndex = 38
        Me.steps.Text = "Steps"
        '
        'GroupBox2
        '
        Me.GroupBox2.Controls.Add(Me.Label7)
        Me.GroupBox2.Controls.Add(Me.Label6)
        Me.GroupBox2.Controls.Add(Me.manual_deg)
        Me.GroupBox2.Controls.Add(Me.txtTx)
        Me.GroupBox2.Controls.Add(Me.btnTx)
        Me.GroupBox2.Controls.Add(Me.Label2)
        Me.GroupBox2.Location = New System.Drawing.Point(7, 157)
        Me.GroupBox2.Name = "GroupBox2"
        Me.GroupBox2.Size = New System.Drawing.Size(298, 61)
        Me.GroupBox2.TabIndex = 39
        Me.GroupBox2.TabStop = False
        Me.GroupBox2.Text = "Tx manual"
        '
        'Label7
        '
        Me.Label7.Location = New System.Drawing.Point(132, 10)
        Me.Label7.Name = "Label7"
        Me.Label7.Size = New System.Drawing.Size(35, 14)
        Me.Label7.TabIndex = 42
        Me.Label7.Text = "Deg"
        '
        'Label6
        '
        Me.Label6.Location = New System.Drawing.Point(69, 10)
        Me.Label6.Name = "Label6"
        Me.Label6.Size = New System.Drawing.Size(35, 14)
        Me.Label6.TabIndex = 41
        Me.Label6.Text = "Steps"
        '
        'GroupBox3
        '
        Me.GroupBox3.Controls.Add(Me.Label5)
        Me.GroupBox3.Controls.Add(Me.auto_deg)
        Me.GroupBox3.Controls.Add(Me.steps)
        Me.GroupBox3.Controls.Add(Me.runstep)
        Me.GroupBox3.Controls.Add(Me.Label3)
        Me.GroupBox3.Controls.Add(Me.chkrandom)
        Me.GroupBox3.Controls.Add(Me.interval)
        Me.GroupBox3.Controls.Add(Me.chkAutorx)
        Me.GroupBox3.Location = New System.Drawing.Point(7, 224)
        Me.GroupBox3.Name = "GroupBox3"
        Me.GroupBox3.Size = New System.Drawing.Size(298, 133)
        Me.GroupBox3.TabIndex = 40
        Me.GroupBox3.TabStop = False
        Me.GroupBox3.Text = "Tx auto"
        '
        'Label5
        '
        Me.Label5.Location = New System.Drawing.Point(74, 69)
        Me.Label5.Name = "Label5"
        Me.Label5.Size = New System.Drawing.Size(35, 14)
        Me.Label5.TabIndex = 40
        Me.Label5.Text = "Deg"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 14)
        Me.ClientSize = New System.Drawing.Size(315, 400)
        Me.Controls.Add(Me.GroupBox3)
        Me.Controls.Add(Me.GroupBox2)
        Me.Controls.Add(Me.btnExit)
        Me.Controls.Add(Me.chkDTR)
        Me.Controls.Add(Me.chkRTS)
        Me.Controls.Add(Me.GroupBox1)
        Me.Controls.Add(Me.btnCloseCom)
        Me.Controls.Add(Me.btnOpenCom)
        Me.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog
        Me.MaximizeBox = False
        Me.Name = "Form1"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = "Antena rotator control"
        Me.GroupBox1.ResumeLayout(False)
        Me.GroupBox1.PerformLayout()
        Me.GroupBox2.ResumeLayout(False)
        Me.GroupBox2.PerformLayout()
        Me.GroupBox3.ResumeLayout(False)
        Me.GroupBox3.PerformLayout()
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub btnOpenCom_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpenCom.Click
        moRS232 = New Rs232()
        Try
            '// Setup parameters
            With moRS232
                .Port = miComPort
                .BaudRate = Int32.Parse(txtBaudrate.Text)
                .DataBit = 8
                .StopBit = Rs232.DataStopBit.StopBit_1
                .Parity = Rs232.DataParity.Parity_None
                .Timeout = Int32.Parse(txtTimeout.Text)
            End With
            '// Initializes port
            moRS232.Open()
            '// Set state of RTS / DTS
            moRS232.Dtr = (chkDTR.CheckState = CheckState.Checked)
            moRS232.Rts = (chkRTS.CheckState = CheckState.Checked)
        Catch Ex As Exception
            MessageBox.Show(Ex.Message, "Connection Error", MessageBoxButtons.OK)
        Finally
            chkAutorx.Enabled = True
            chkrandom.Enabled = True
            btnCloseCom.Enabled = moRS232.IsOpen
            btnOpenCom.Enabled = Not moRS232.IsOpen
            btnTx.Enabled = moRS232.IsOpen
        End Try
    End Sub

    Private Sub btnCloseCom_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCloseCom.Click
        moRS232.Close()
        btnCloseCom.Enabled = moRS232.IsOpen
        btnOpenCom.Enabled = Not moRS232.IsOpen
        btnTx.Enabled = moRS232.IsOpen
        chkAutorx.Enabled = False
        chkrandom.Enabled = False
    End Sub

    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTx.Click
        Dim i, index As Integer
        Dim a As Byte
        index = CInt(Val(repeat.Text)) * 4 - 1
        ReDim Out(index)

        '// Clear Tx/Rx Buffers
        moRS232.PurgeBuffer(Rs232.PurgeBuffers.TxClear Or Rs232.PurgeBuffers.RXClear)

        a = CByte(Val(txtTx.Text))
        'vytvori jeden povel
        out(0) = 171
        out(1) = a
        Out(2) = CByte(255 - a)
        Out(3) = 171
        'posle nekolikrat za sebou
        While i < index + 1
            Out(i) = Out(0)
            Out(i + 1) = Out(1)
            Out(i + 2) = Out(2)
            Out(i + 3) = Out(3)
            i = i + 4
        End While

        moRS232.Write(out)

    End Sub

    Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        If Not moRS232 Is Nothing Then
            '// Disables Events if active
            moRS232.DisableEvents()
            If moRS232.IsOpen Then moRS232.Close()
        End If
    End Sub

    Private Sub chkDTR_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkDTR.Click
        If Not (moRS232 Is Nothing) Then
            moRS232.Dtr = chkDTR.CheckState = CheckState.Checked
        End If
    End Sub

    Private Sub chkRTS_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkRTS.Click
        If Not (moRS232 Is Nothing) Then
            moRS232.Rts = chkRTS.CheckState = CheckState.Checked
        End If

    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub


    Private Sub optCom1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles optCom1.CheckedChanged, optCom2.CheckedChanged
        If (sender Is optCom1) Then
            miComPort = 1
        Else
            miComPort = 2
        End If
    End Sub

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

    End Sub




    Private Sub btnTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTest.Click
        Try
            If Rs232.IsPortAvailable(Int32.Parse(txtPortNum.Text)) Then
                MessageBox.Show("Port available", "Port test", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Else
                MessageBox.Show("Port NOT available", "Port test", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        Catch ex As Exception
            MessageBox.Show("Port test failed", "Port test", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub chkAutorx_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkAutorx.CheckedChanged
        If chkAutorx.Checked Then
            a = 0
            Timer1.Interval = CInt(Val(interval.Text))
            Timer1.Enabled = True
        Else
            Timer1.Enabled = False
        End If

    End Sub



    Public Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick, chkAutorx.CheckedChanged
        Dim i, index As Integer
        index = CInt(Val(repeat.Text)) * 4 - 1
        ReDim Out(index)
        '// Clear Tx/Rx Buffers
        moRS232.PurgeBuffer(Rs232.PurgeBuffers.TxClear Or Rs232.PurgeBuffers.RXClear)

        If chkrandom.Checked Then
            a = CByte((199 + 1) * Rnd())
            runstep.Text = Str(a)
        Else
            a = a + CByte(Val(runstep.Text))
            If a > 198 Then
                a = 0
            End If
        End If

        'vytvori jeden povel
        out(0) = 171
        out(1) = CByte(a)
        Out(2) = CByte(255 - a)
        Out(3) = 171
        'posle nekolikrat za sebou
        While i < index + 1
            Out(i) = Out(0)
            Out(i + 1) = Out(1)
            Out(i + 2) = Out(2)
            Out(i + 3) = Out(3)
            i = i + 4
        End While
        moRS232.Write(Out)
    End Sub

    Private Sub Label3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label3.Click

    End Sub

    Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label2.Click

    End Sub

    Private Sub manual_deg_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles manual_deg.LostFocus
        txtTx.Text = Str(Int(Val(manual_deg.Text) / 1.8))
    End Sub

    Private Sub tx_deg_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles manual_deg.TextChanged

    End Sub

    Private Sub runstep_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles runstep.TextChanged
        auto_deg.Text = Str(Val(runstep.Text) * 1.8)

    End Sub

    Private Sub txtTx_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtTx.TextChanged
        manual_deg.Text = Str(Val(txtTx.Text) * 1.8)
    End Sub

    Private Sub auto_deg_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles auto_deg.LostFocus
        runstep.Text = Str(Int(Val(auto_deg.Text) / 1.8))
    End Sub


    Private Sub auto_deg_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles auto_deg.MouseLeave

    End Sub

    Private Sub Label5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label5.Click

    End Sub

    Private Sub steps_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles steps.Click

    End Sub

    Private Sub ToolTip1_Popup(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PopupEventArgs) Handles ToolTip1.Popup

    End Sub

    Private Sub Label9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label9.Click

    End Sub

    Private Sub chkrandom_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkrandom.CheckedChanged

    End Sub
End Class
