Imports System Imports System.IO.Ports Imports System.Threading Imports System.Text Imports Microsoft.VisualBasic Public Class Form1 Dim portName As String Dim baudRate As Integer Dim mySerial As SerialPort Dim byteArray() As Byte Dim connBytes() As Byte 'Dim vScrolls As ControlCollection 'no real use found for these... 'Dim textBoxes As ControlCollection Dim textBoxArray() As TextBox Dim vScrollArray() As VScrollBar Dim byteBatch() As Byte 'All servo values to MiniSSC should be in the range of 0 to 254 Dim steerServoMax As Integer Dim steerServoMin As Integer Dim potServoMax As Integer Dim potServoMin As Integer Dim dirServoPosition1 As Integer Dim dirServoPosition2 As Integer Dim servoCenterValue As Integer Dim steerServoCenterValue As Integer Dim isSerialConn As Boolean Dim bytesToReadOld As Integer 'Method is run when main (and only) window of program opens. Initializes various values. ' and automatically calls the serial connection method. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Start timers Timer1.Interval = 1000 'checks every second Timer1.Enabled = True AutoByteTimer.Interval = 800 AutoByteTimer2.Interval = 1000 'Set textbox defaults speedServo.Text = speedServo.Items.Item(7) ' Default: servo 7 steerServo.Text = steerServo.Items.Item(0) ' Default: servo 0 dirServo.Text = dirServo.Items.Item(4) ' Default: servo 4 ComboBox4.Text = ComboBox4.Items.Item(9) ' Starts at 0, Default: 9 => COM10 'Set callibrated constants; many of these are empirically determined/callibrated values. Change as needed. steerServoMax = 182 '177 steerServoMin = 77 potServoMax = 252 potServoMin = 100 dirServoPosition1 = 158 dirServoPosition2 = 93 servoCenterValue = 127 steerServoCenterValue = HScrollBar2.Value connBytes = New Byte(2) {255, 8, 127} bytesToReadOld = 0 'These two arrays would ideally be used, but didn't quite work... ' see ReadScrollBarToTextBox() method vScrollArray = New VScrollBar(7) {VScrollBar0, VScrollBar1, VScrollBar2, _ VScrollBar3, VScrollBar4, VScrollBar5, VScrollBar6, VScrollBar7} textBoxArray = New TextBox(7) {TextBox0, TextBox1, TextBox2, TextBox3, _ TextBox4, TextBox5, TextBox6, TextBox7} vScrollArray(steerServo.Text).Value = steerServoCenterValue Call ReadScrollBarToTextBox(9) 'Sets display of the current values of horizontal scrollbars. TextBox9.Text = HScrollBar2.Value TextBox8.Text = HScrollBar1.Value Call OpenNewSerialConnection() End Sub 'Servos are moved when keys are held down... Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles StatusBox.KeyDown, Me.KeyDown e.Handled = True Select Case e.KeyCode Case Keys.Up Call MoveServo("Forward") Case Keys.Down Call MoveServo("Backward") Case Keys.Left Call MoveServo("Left") Case Keys.Right Call MoveServo("Right") 'Case Keys.BrowserBack ' Call OpenNewSerialConnection() 'Case Keys.RWin 'Trying to limit what happens with keys ' Call MoveServo("Stop") ' on numpads, but doesn't seem to work 'Case Keys.Enter ' Call MoveServo("Stop") 'Case Keys.BrowserForward ' Call CloseSerial() End Select End Sub 'Servos go back to center position when keys are released... allows for ' steering and accelerating at same time, then just steering or accelerating Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles StatusBox.KeyUp, Me.KeyUp e.Handled = True Select Case e.KeyCode Case Keys.Up Call MoveServo("Stop") Case Keys.Down Call MoveServo("Stop") Case Keys.Left Call MoveServo("Center") Case Keys.Right Call MoveServo("Center") Case Keys.RWin Call MoveServo("Stop") Case Keys.BrowserBack Call OpenNewSerialConnection() 'Case Keys.RWin 'Trying to limit what happens with keys ' Call MoveServo("Stop") ' on numpads, but doesn't seem to work Case Keys.Enter Call MoveServo("Stop") 'Case Keys.VolumeUp 'BrowserForward 'Call Disconnect_Click(sender, e) End Select End Sub ' Method receives a string and uses a bunch of If structures Private Sub MoveServo(ByVal purpose As String) 'purpose values: ' Stop, Forward, Backward, Left, Right If purpose.Equals("Stop") Then 'byteBatch = New Byte(2) {255, Microsoft.VisualBasic.Val(speedServo.Text(0)), servoCenterValue} 'mySerial.Write(byteBatch, 0, 3) byteBatch = New Byte(2) {255, Microsoft.VisualBasic.Val(dirServo.Text(0)), servoCenterValue} mySerial.Write(byteBatch, 0, 3) 'vScrollArray(Microsoft.VisualBasic.Val(speedServo.Text(0))).Value = servoCenterValue vScrollArray(Microsoft.VisualBasic.Val(dirServo.Text(0))).Value = servoCenterValue ElseIf purpose.Equals("Center") Then byteBatch = New Byte(2) {255, Microsoft.VisualBasic.Val(steerServo.Text(0)), steerServoCenterValue} mySerial.Write(byteBatch, 0, 3) vScrollArray(Microsoft.VisualBasic.Val(steerServo.Text(0))).Value = steerServoCenterValue ElseIf purpose.Equals("Forward") Then byteBatch = New Byte(2) {255, Microsoft.VisualBasic.Val(dirServo.Text(0)), dirServoPosition1} mySerial.Write(byteBatch, 0, 3) 'byteBatch = New Byte(2) {255, Microsoft.VisualBasic.Val(speedServo.Text(0)), 255 - potServoMax} 'mySerial.Write(byteBatch, 0, 3) vScrollArray(Microsoft.VisualBasic.Val(dirServo.Text(0))).Value = dirServoPosition1 'vScrollArray(Microsoft.VisualBasic.Val(speedServo.Text(0))).Value = potServoMax ElseIf purpose.Equals("Backward") Then byteBatch = New Byte(2) {255, Microsoft.VisualBasic.Val(dirServo.Text(0)), dirServoPosition2} mySerial.Write(byteBatch, 0, 3) 'byteBatch = New Byte(2) {255, Microsoft.VisualBasic.Val(speedServo.Text(0)), 255 - potServoMax} 'mySerial.Write(byteBatch, 0, 3) vScrollArray(Microsoft.VisualBasic.Val(dirServo.Text(0))).Value = dirServoPosition2 'vScrollArray(Microsoft.VisualBasic.Val(speedServo.Text(0))).Value = potServoMax ElseIf purpose.Equals("Left") Then byteBatch = New Byte(2) {255, Microsoft.VisualBasic.Val(steerServo.Text(0)), steerServoMax} mySerial.Write(byteBatch, 0, 3) vScrollArray(Microsoft.VisualBasic.Val(steerServo.Text(0))).Value = steerServoMax ElseIf purpose.Equals("Right") Then byteBatch = New Byte(2) {255, Microsoft.VisualBasic.Val(steerServo.Text(0)), steerServoMin} mySerial.Write(byteBatch, 0, 3) vScrollArray(Microsoft.VisualBasic.Val(steerServo.Text(0))).Value = steerServoMin 'If changing pot, make sure 3rd byte values of next two ElseIf's are accurate ElseIf purpose.Equals("SetSpeed") Then byteBatch = New Byte(2) {255, Microsoft.VisualBasic.Val(speedServo.Text(0)), 255 - potServoMax} mySerial.Write(byteBatch, 0, 3) vScrollArray(Microsoft.VisualBasic.Val(speedServo.Text(0))).Value = potServoMax ElseIf purpose.Equals("ResetSpeed") Then byteBatch = New Byte(2) {255, Microsoft.VisualBasic.Val(speedServo.Text(0)), servoCenterValue} mySerial.Write(byteBatch, 0, 3) vScrollArray(Microsoft.VisualBasic.Val(speedServo.Text(0))).Value = servoCenterValue Else End If End Sub 'Method recreates mySerial object, opens the connection, and does other "maintenance" 'Uses error handling in case problems arise with the serial connection Private Sub OpenNewSerialConnection() baudRate = 9600 bytesToReadOld = 0 Try If mySerial.IsOpen Then mySerial.Close() End If Catch ex As Exception End Try mySerial = New SerialPort(ComboBox4.Text, baudRate) Try mySerial.Open() isSerialConn = True Call ToggleControls() AutoByteTimer.Enabled = True AutoByteTimer2.Enabled = True Call MoveServo("SetSpeed") Catch ex As Exception End Try End Sub 'Before window closes, method closes mySerial's connection if there's an open connection Private Sub Form1_Unload() If isSerialConn Then mySerial.Close() End If End Sub 'Method for reconnect button simply calls OpenNewSerialConnection method Private Sub ReconnectBTN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ReconnectBTN.Click Call OpenNewSerialConnection() End Sub 'Method copies values of scrollbars to textboxes Private Sub ReadScrollBarToTextBox(ByVal index As Integer) If index = 9 Then TextBox0.Text = VScrollBar0.Value TextBox1.Text = VScrollBar1.Value TextBox2.Text = VScrollBar2.Value TextBox3.Text = VScrollBar3.Value TextBox4.Text = VScrollBar4.Value TextBox5.Text = VScrollBar5.Value TextBox6.Text = VScrollBar6.Value TextBox7.Text = VScrollBar7.Value Else 'probably will crash if it doesn't receive a 9... I am lazy textBoxArray(index).Text = vScrollArray(index).Value End If End Sub 'method called if any of the vscrollbars change their value. Private Sub VScroll_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) _ Handles VScrollBar0.ValueChanged, VScrollBar1.ValueChanged, VScrollBar2.ValueChanged, VScrollBar3.ValueChanged, _ VScrollBar4.ValueChanged, VScrollBar5.ValueChanged, VScrollBar6.ValueChanged, VScrollBar7.ValueChanged Call ReadScrollBarToTextBox(9) 'Don't know/lack an easy way to determine which scrollbar changed End Sub 'I don't currently need this one... 'though I can't figure out how to keep the user from moving the scrollbars manually... Private Sub scrollBars_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) _ Handles VScrollBar0.Scroll, VScrollBar1.Scroll, VScrollBar2.Scroll, VScrollBar3.Scroll, VScrollBar4.Scroll, _ VScrollBar5.Scroll, VScrollBar6.Scroll, VScrollBar7.Scroll Call ReadScrollBarToTextBox(9) End Sub 'Not currently used 'Same purpose achieved in another method. Private Sub ScrollBarChange(ByVal sender As System.Object) If sender.Equals(VScrollBar0) Then TextBox0.Text = VScrollBar0.Value byteBatch = New Byte(2) {255, 0, DirectCast(VScrollBar0.Value, Integer)} ElseIf sender.Equals(VScrollBar1) Then TextBox1.Text = VScrollBar1.Value byteBatch = New Byte(2) {255, 1, DirectCast(VScrollBar1.Value, Integer)} ElseIf sender.Equals(VScrollBar2) Then TextBox2.Text = VScrollBar2.Value byteBatch = New Byte(2) {255, 2, DirectCast(VScrollBar2.Value, Integer)} ElseIf sender.Equals(VScrollBar3) Then TextBox3.Text = VScrollBar3.Value byteBatch = New Byte(2) {255, 3, DirectCast(VScrollBar3.Value, Integer)} ElseIf sender.Equals(VScrollBar4) Then TextBox4.Text = VScrollBar4.Value byteBatch = New Byte(2) {255, 4, DirectCast(VScrollBar4.Value, Integer)} ElseIf sender.Equals(VScrollBar5) Then TextBox5.Text = VScrollBar5.Value byteBatch = New Byte(2) {255, 5, DirectCast(VScrollBar5.Value, Integer)} ElseIf sender.Equals(VScrollBar6) Then TextBox6.Text = VScrollBar6.Value byteBatch = New Byte(2) {255, 6, DirectCast(VScrollBar6.Value, Integer)} ElseIf sender.Equals(VScrollBar7) Then TextBox7.Text = VScrollBar7.Value byteBatch = New Byte(2) {255, 7, DirectCast(VScrollBar7.Value, Integer)} End If 'Call ReadScrollBarToTextBox(9) End Sub 'Timer1 checks whether serial connection is open and sets "StatusBox" to red, if not Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick If mySerial.IsOpen() Then Label4.Visible = True Label6.Visible = False 'StatusBox.BackColor = Color.LimeGreen 'Handled by AutoByteTimer_Tick Else Label4.Visible = False Label6.Visible = True StatusBox.BackColor = Color.Red End If End Sub 'When clicked, close serial connection and disable scrollbars Private Sub Disconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DisconnButton.Click Call CloseSerial() Call ToggleControls() End Sub 'Method uses error handling to safely close the serial connection. Private Sub CloseSerial() If isSerialConn Then Try Call MoveServo("ResetSpeed") mySerial.Close() Catch ex As Exception End Try AutoByteTimer.Enabled = False AutoByteTimer2.Enabled = False End If End Sub Private Sub HScrollBar1_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles HScrollBar1.Scroll potServoMax = HScrollBar1.Value TextBox8.Text = HScrollBar1.Value Call MoveServo("SetSpeed") End Sub Private Sub HScrollBar2_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles HScrollBar2.Scroll steerServoCenterValue = HScrollBar2.Value TextBox9.Text = HScrollBar2.Value End Sub Private Sub AutoByteTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AutoByteTimer.Tick If mySerial.IsOpen() Then mySerial.Write(connBytes, 0, 3) End If TextBox10.BackColor = Color.Aquamarine TextBox10.Text = mySerial.BytesToRead 'bytesToReadOld = mySerial.BytesToRead End Sub Private Sub AutoByteTimer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AutoByteTimer2.Tick 'TextBox10.BackColor = Color.BlanchedAlmond If mySerial.BytesToRead > bytesToReadOld Then StatusBox.BackColor = Color.LimeGreen Else StatusBox.BackColor = Color.Orange 'Red End If bytesToReadOld = mySerial.BytesToRead AutoByteTimer2.Enabled = False End Sub 'Method is called to toggle all scrollbars at once. Private Sub ToggleControls() If mySerial.IsOpen Then VScrollBar0.Enabled = True VScrollBar1.Enabled = True VScrollBar2.Enabled = True VScrollBar3.Enabled = True VScrollBar4.Enabled = True VScrollBar5.Enabled = True VScrollBar6.Enabled = True VScrollBar7.Enabled = True HScrollBar1.Enabled = True HScrollBar2.Enabled = True Else VScrollBar0.Enabled = False VScrollBar1.Enabled = False VScrollBar2.Enabled = False VScrollBar3.Enabled = False VScrollBar4.Enabled = False VScrollBar5.Enabled = False VScrollBar6.Enabled = False VScrollBar7.Enabled = False HScrollBar1.Enabled = False HScrollBar2.Enabled = False End If End Sub End Class