Working with the Veyron in VB.NET

In this guide I am going to show you how to use the Veyron from VB.NET.

I will be using Visual Studio 2015 Community Edition. You can download this for free from MIcrosoft here is a link.

So lets begin by creating a new project.

1) Open up Visual Studio if you have not done so.

2) Select New Project

3) Select Windows Form Application

4) Give it a name and select a place to save it.

5) By default Visual Studio will create a new blank form for you to work from.

6) We are going to need to add the following controls to the form.

2-ComboBoxes, 2-Sliders (Horizontal or Vertical), 4-Labels.

7) Here is what my form looks like.

8) Before we can begin doing any programming we need to name our controls. Here are the names I used.

cmbPort for Combo1, cmbChannel for Combo2, scrSpeed for Slider 1, scrPosition for Slider 2.

9) Once you have your controls and labels positioned we will start programming.

10) Right click on you form and choose view code.

11) What you will see is the begining to the forms class, we are going to add several items. This is what you should see.

Public Class Form1

 

End Class

12) So lets add the following lines.

Private WithEvents sp As System.IO.Ports.SerialPort
    Dim Position As Long
    Dim Channel As Integer
    Dim Speed As Long

13) What you should have now is the following.

Public Class Form1

Private WithEvents sp As System.IO.Ports.SerialPort
    Dim Position As Long
    Dim Channel As Integer
    Dim Speed As Long

 

End Class

 

14) Lets go back to our form and double click on a empty area with no controls. This will create the Forms Load event.

15) Within the forms load event lets add the following.

For Each y As String In System.IO.Ports.SerialPort.GetPortNames
            Me.cmbPort.Items.Add(y)
        Next

        For chnl = 0 To 23
            cmbChannel.Items.Add(chnl)
        Next

        sp = New IO.Ports.SerialPort("COM4")
        sp.StopBits = IO.Ports.StopBits.One
        sp.BaudRate = 9600
        sp.DataBits = 8
        sp.Parity = IO.Ports.Parity.None
        sp.Handshake = IO.Ports.Handshake.None

        sp.Open()

16) So now your code should look like this

Public Class Form1
    Private WithEvents sp As System.IO.Ports.SerialPort
    Dim Position As Long
    Dim Channel As Integer
    Dim Speed As Long
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        For Each y As String In System.IO.Ports.SerialPort.GetPortNames
            Me.cmbPort.Items.Add(y)
        Next

        For chnl = 0 To 23
            cmbChannel.Items.Add(chnl)
        Next

        sp = New IO.Ports.SerialPort("COM4")
        sp.StopBits = IO.Ports.StopBits.One
        sp.BaudRate = 9600
        sp.DataBits = 8
        sp.Parity = IO.Ports.Parity.None
        sp.Handshake = IO.Ports.Handshake.None

        sp.Open()
    End Sub

End Class

17) Now we need to make those controls do something.

18) Go to the form view and double click on the combobox you named cmbChannel. This will take you into the controls event for selectedindexchanged.

19) Lets add the following code to this area.

Channel = cmbChannel.Text

20) Now click on the scrSpeed (ScrollBar) and lets change its maximum value to 5000 in the property window.

21) Now click on the scrPosition (Scrollbar) and lets change its maximum value to 2500 in the property window.

22) Double click on the scrSpeed (ScrollBar) and you will be in the scrSpeed_Scroll event. Lets add the following code.

Speed = scrSpeed.Value
        lblSpeed.Text = "Servo Speed " & Speed

23) The label that you positioned over this control change its name in the property window to lblSpeed.

24) Double click on the scrPosition (Scrollbar) and you will be in the scrPosition_Scroll event. Lets add the following code. The label that you positioned over this control change its name to lblPosition.

Position = scrPosition.Value
        lblPosition.Text = "Servo Position " & Position
        Servo()

25) Now we need to add a function that will tell the Veyron what to do. Add the following lines.

Private Function Servo()
        sp.Write("#" & Channel)
        sp.Write("P" & Position)
        sp.Write("S" & Speed & vbCr)
    End Function

26) Here is what your code should look like now.

Public Class Form1
    Private WithEvents sp As System.IO.Ports.SerialPort
    Dim Position As Long
    Dim Channel As Integer
    Dim Speed As Long
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        For Each y As String In System.IO.Ports.SerialPort.GetPortNames
            Me.cmbPort.Items.Add(y)
        Next

        For chnl = 0 To 23
            cmbChannel.Items.Add(chnl)
        Next

        sp = New IO.Ports.SerialPort("COM4")
        sp.StopBits = IO.Ports.StopBits.One
        sp.BaudRate = 9600
        sp.DataBits = 8
        sp.Parity = IO.Ports.Parity.None
        sp.Handshake = IO.Ports.Handshake.None

        sp.Open()
    End Sub

    Private Sub cmbChannel_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbChannel.SelectedIndexChanged
        Channel = cmbChannel.Text
    End Sub

    Private Sub scrSpeed_Scroll(sender As Object, e As ScrollEventArgs) Handles scrSpeed.Scroll
        Speed = scrSpeed.Value
        lblSpeed.Text = "Servo Speed " & Speed
    End Sub

   

    Private Sub scrPosition_Scroll(sender As Object, e As ScrollEventArgs) Handles scrPosition.Scroll
        Position = scrPosition.Value
        lblPosition.Text = "Servo Position " & Position
        Servo()
    End Sub

    Private Function Servo()
        sp.Write("#" & Channel)
        sp.Write("P" & Position)
        sp.Write("S" & Speed & vbCr)
    End Function


End Class

27) Now attach your Veyron to the computer with the USB cable and run the program. It will detect the Com Port that has been assigned to it and you can select it. You can now select the servo channel and adjust the speed then then once you adjust the position the servo will move.