Working with the Veyron Servo Controller VB6 Part 1

In this guide I am going to walk you through creating a program in VB4, 5 or 6 that will allow you to control a single channel on the Veyron Servo Controller.

If you have not already setup the Veyron to work with you computer then click on this link to set it up.

Lets Begin.

1) Open up VB6 and create a new standard exe project.

2) By default a new form is created for you.

3) We now need to add some components to make this work.

4) Click on Project in the main menu and then click on Components.

We need to add the following components:

 

 

 

 

 

 

 

 

5) At this point the components shown should look the same as this.

6) The component that looks like a telephone we will need first.

7) Select it and then place one on your form. Do not change the name or any other values in the property window.

8) Ok now we are going to need to add the following to the form.

2 Combo Boxes, 3 labels, 1 Vertical or Horizontal Slider (Your Choice)

Here is what my form looks like:

9) Size your form and then position the controls to the way you like it.

10) Lets give the controls some names so we can use them.

Combo1 change to cmbPort, Combo2 change to cmbChannel, Vscroll1 just leave. The label that you place over the Scroll Bar change to lblAmt.

11) By default the combo boxes will display Combo in them, lets get rid of that by removing it it the properties window of the control. We also need to change the text being displayed for the labels. You can name yours whatever you want. The label above the scroll bar remove any text being displayed and leave the field blank.

12) Now we are going to start doing some of the actual programming to make this work. I am going to use a simple a straight forward method of doing the programming. I have seen many tutorials on creating software and in most cases they are too advanced for those wanting to learn.

13) Double click on a blank area of your form. This will take you into the Forms load event.

14) The first thing we need to do is add some values to those combo boxes.

Type or copy the following into the forms load event and then save the project.

 

cmbPort.Clear
cmbPort.AddItem "1"
cmbPort.AddItem "2"
cmbPort.AddItem "3"
cmbPort.AddItem "4"
cmbPort.AddItem "5"
cmbPort.AddItem "6"
cmbPort.AddItem "7"
cmbPort.AddItem "8"

cmbChannel.Clear
cmbChannel.AddItem "0"
cmbChannel.AddItem "1"
cmbChannel.AddItem "2"
cmbChannel.AddItem "3"
cmbChannel.AddItem "4"
cmbChannel.AddItem "5"
cmbChannel.AddItem "6"
cmbChannel.AddItem "7"
cmbChannel.AddItem "8"
cmbChannel.AddItem "9"
cmbChannel.AddItem "10"
cmbChannel.AddItem "11"
cmbChannel.AddItem "12"
cmbChannel.AddItem "13"
cmbChannel.AddItem "14"
cmbChannel.AddItem "15"
cmbChannel.AddItem "16"
cmbChannel.AddItem "17"
cmbChannel.AddItem "18"
cmbChannel.AddItem "19"
cmbChannel.AddItem "20"
cmbChannel.AddItem "21"
cmbChannel.AddItem "22"
cmbChannel.AddItem "23"

Now when you run the program the combo boxes will have values populated into them so they can be used.

15) We now need to make a change to the slider. Click on it and in the properties window change the max value to 2500.

16) Double click once again on a empty area of the form so that you are in the load event of the form.

17) Position your curso just above the area that says: Private Sub Form_Load(), and either type or copy the following line in.

Private Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long)
Dim A As Integer

You code should now look like this. Save the project.
 

Private Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long)
Dim A As Integer


Private Sub Form_Load()

18) Now Double click on the scroll bar and you will be taken into the controls change event.

Private Sub VScroll1_Change()

19) We need to add the following lines into the Scroll bars Change event.

lblAmt.Caption = VScroll1.Value
A = Cint(VScroll1.Value)
Servo

20) We also need to add another event for the Scroll bar called Scroll.

Private Sub VScroll1_Scroll()

End Sub

21) The same lines that we had to add to the Scroll Change event we also need to add to its Scroll event. So you should end up with the following. Save the project.

Private Sub VScroll1_Scroll()
lblAmt.Caption = VScroll1.Value
A =Cint( VScroll1.Value)
Servo
End Sub

22) We now need to add an event for the cmbPort Combo Box. Double click on it and you will be taken into its event for Change.

23) type or copy the following line in: Startup, it should look like this.

Private Sub cmbPort_Change()
startup
End Sub

24) Now lets add an event for the cmbPort for the Click event. Type or copy the following then save the project.

Private Sub cmbPort_Click()
startup
End Sub

25) Now we are going to create two functions. The first one is to initiate communication with the comm port and the second is to tell the Veyron what to do when you move the scroll bar.

26) Type or copy the following lines into your code then save the project.

Private Function startup()

    On Error GoTo ClearError
    If MSComm1.PortOpen = True Then
        MSComm1.PortOpen = False
        MSComm1.Settings = "9600,N,8,1"
        MSComm1.CommPort = CLng(cmbPort.Text)
        MSComm1.OutBufferCount = 0
        MSComm1.PortOpen = True
    Else
        MSComm1.Settings = "9600,N,8,1"
        MSComm1.CommPort = CLng(cmbPort.Text)
        MSComm1.OutBufferCount = 0
        MSComm1.PortOpen = True
    End If
    
ClearError:

    Err.Clear
    
    End Function

 

27) Now lets create the function that will tell the Veyron what to do when you move the scroll bar then Save the project.

Private Function Servo()
    On Error GoTo Errorhandler
    If MSComm1.PortOpen = False Then
MSComm1.PortOpen = True
End If
    
   
        MSComm1.Output = "#" & CLng(cmbChannel.Text)
        MSComm1.Output = "P" & A
        MSComm1.Output = "S" & 5000 & vbCr 
        MSComm1.Output = Chr$(85) 'Update
        MSComm1.OutBufferCount = 0 'Clear Buffer
      
        Sleep 60
        MSComm1.RThreshold = 0
        If Mmscomm1.OutBufferCount = 0 Then
        MSComm1.PortOpen = False
        MSComm1.RThreshold = 0
        End If
        DoEvents
    
Errorhandler:
        Err.Clear
        
    End Function

28) Save the project and now run it. You can select the Comm port that you Veyron is attached to and then Select the Servo Channel and when you move the Scroll Bar you will see your servo move to that position.

Below is a full listing of the code behind the form in the event that you missed something.

Private Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long)
Dim A As Integer


Private Sub cmbPort_Change()
startup
End Sub

Private Sub cmbPort_Click()
startup
End Sub

Private Sub Form_Load()
cmbPort.Clear
cmbPort.AddItem "1"
cmbPort.AddItem "2"
cmbPort.AddItem "3"
cmbPort.AddItem "4"
cmbPort.AddItem "5"
cmbPort.AddItem "6"
cmbPort.AddItem "7"
cmbPort.AddItem "8"

cmbChannel.Clear
cmbChannel.AddItem "0"
cmbChannel.AddItem "1"
cmbChannel.AddItem "2"
cmbChannel.AddItem "3"
cmbChannel.AddItem "4"
cmbChannel.AddItem "5"
cmbChannel.AddItem "6"
cmbChannel.AddItem "7"
cmbChannel.AddItem "8"
cmbChannel.AddItem "9"
cmbChannel.AddItem "10"
cmbChannel.AddItem "11"
cmbChannel.AddItem "12"
cmbChannel.AddItem "13"
cmbChannel.AddItem "14"
cmbChannel.AddItem "15"
cmbChannel.AddItem "16"
cmbChannel.AddItem "17"
cmbChannel.AddItem "18"
cmbChannel.AddItem "19"
cmbChannel.AddItem "20"
cmbChannel.AddItem "21"
cmbChannel.AddItem "22"
cmbChannel.AddItem "23"


End Sub

Private Sub VScroll1_Change()
lblAmt.Caption = VScroll1.Value
A = Cint(VScroll1.Value)
Servo
End Sub

Private Sub VScroll1_Scroll()
lblAmt.Caption = VScroll1.Value
A = Cint(VScroll1.Value)
Servo
End Sub

Private Function startup()

    On Error GoTo ClearError
    If MSComm1.PortOpen = True Then
        MSComm1.PortOpen = False
        MSComm1.Settings = "9600,N,8,1"
        MSComm1.CommPort = CLng(cmbPort.Text)
        MSComm1.OutBufferCount = 0
        MSComm1.PortOpen = True
    Else
        MSComm1.Settings = "9600,N,8,1"
        MSComm1.CommPort = CLng(cmbPort.Text)
        MSComm1.OutBufferCount = 0
        MSComm1.PortOpen = True
    End If
    
ClearError:

    Err.Clear
    
    End Function
    
    Private Function Servo()
    On Error GoTo Errorhandler
    If MSComm1.PortOpen = False Then
MSComm1.PortOpen = True
End If
      ' mmscomm1.Output = Chr$(85) 'Update
   
        MSComm1.Output = "#" & CLng(cmbChannel.Text)
        MSComm1.Output = "P" & A
        MSComm1.Output = "S" & 5000 & vbCr  'was 5000
        MSComm1.Output = Chr$(85) 'Update
        MSComm1.OutBufferCount = 0 'Clear Buffer
        'mmscomm1.PortOpen = False
        Sleep 60
        MSComm1.RThreshold = 0
        If Mmscomm1.OutBufferCount = 0 Then
        MSComm1.PortOpen = False
        MSComm1.RThreshold = 0
        End If
        DoEvents
    
Errorhandler:
        Err.Clear
        
    End Function