Example Source Code:
Visual Basic Test Program
For the WheelCommander WC-132
Description
This example uses NdiCmdC.dll, a Windows 2000/XP dynamic link library designed to provide a simple "flat" interface to NdiCmd.dll, a C++ DLL used by WC Wizard. NdiCmdC.dll provides access to all of the useful commands within a WheelCommander, in a way that is compatible with C and Visual Basic programs on a Windows machine.
Wc116.bas is a module used to declare all of the entry points into NdiCmdC.dll, and provides an initialization function which will either locate the WheelCommander (by scanning all enabled serial ports), or, if not found, will pop up the WC Wizard so that you can make changes to the communication settings and attempt to connect manually.
Module1.bas contains Sub main(), the main entry point to the program. This calls Init_WheelCommander, and if successful, queries the firmware version and passes it to frmMain.frm, the main form.
FrmMain.frm contains 5 controls: a text box that displays the WheelCommander version and connection status; a text box for entering a velocity value; a scroll bar for changing the velocity value; a button for issuing a Go! command; and a button for issuing a Coast command. The code for frmMain.frm contains the NdiCmdC.dll calls to affect this behavior.
Download
wc116.bas - Visual Basic source
Module1.bas - Visual Basic source
frmMain.frm - Visual Basic form
vbcommander.zip - All project files
Source Code
wc116.bas
Attribute VB_Name = "wc116"
' declare entry points to WheelCommander DLL
Declare Sub NdiCmd_Init Lib "NdiCmdC.dll" Alias "_NdiCmd_Init@0" ()
Declare Function NdiCmd_Search Lib "NdiCmdC.dll" Alias "_NdiCmd_Search@0" () As Long
Declare Function NdiCmd_CheckPort Lib "NdiCmdC.dll" Alias "_NdiCmd_CheckPort@4" (ByVal port As Long) As Long
Declare Function NdiCmd_GetFirstPort Lib "NdiCmdC.dll" Alias "_NdiCmd_GetFirstPort@0" () As Long
Declare Function NdiCmd_GetNextPort Lib "NdiCmdC.dll" Alias "_NdiCmd_GetNextPort@4" (ByVal prev As Long) As Long
Declare Sub NdiCmd_RunWizard Lib "NdiCmdC.dll" Alias "_NdiCmd_RunWizard@4" (ByVal wizMode As Long)
Declare Function NdiCmd_GetWheelCommander Lib "NdiCmdC.dll" Alias "_NdiCmd_GetWheelCommander@0" () As Long
Declare Function NdiCmd_Sync Lib "NdiCmdC.dll" Alias "_NdiCmd_Sync@4" (ByVal handle As Long) As Long
Declare Function NdiCmd_GetName Lib "NdiCmdC.dll" Alias "_NdiCmd_GetName@12" (ByVal handle As Long, ByVal name As String, version As Long) As Long
Declare Function NdiCmd_Coast Lib "NdiCmdC.dll" Alias "_NdiCmd_Coast@4" (ByVal handle As Long) As Long
Declare Function NdiCmd_Brake Lib "NdiCmdC.dll" Alias "_NdiCmd_Brake@4" (ByVal handle As Long) As Long
Declare Function NdiCmd_Reset Lib "NdiCmdC.dll" Alias "_NdiCmd_Reset@4" (ByVal handle As Long) As Long
Declare Function NdiCmd_Go Lib "NdiCmdC.dll" Alias "_NdiCmd_Go@4" (ByVal handle As Long) As Long
Declare Function NdiCmd_SetVelocity Lib "NdiCmdC.dll" Alias "_NdiCmd_SetVelocity@8" (ByVal handle As Long, ByVal vel As Long) As Long
Declare Function NdiCmd_GetVelocity Lib "NdiCmdC.dll" Alias "_NdiCmd_GetVelocity@8" (ByVal handle As Long, vel As Long) As Long
Declare Function NdiCmd_SetAccel Lib "NdiCmdC.dll" Alias "_NdiCmd_SetAcceleration@8" (ByVal handle As Long, ByVal vel As Long) As Long
Declare Function NdiCmd_GetAccel Lib "NdiCmdC.dll" Alias "_NdiCmd_GetAcceleration@8" (ByVal handle As Long, vel As Long) As Long
Declare Function NdiCmd_SetRotation Lib "NdiCmdC.dll" Alias "_NdiCmd_SetRotation@8" (ByVal handle As Long, ByVal vel As Long) As Long
Declare Function NdiCmd_GetRotation Lib "NdiCmdC.dll" Alias "_NdiCmd_GetRotation@8" (ByVal handle As Long, vel As Long) As Long
Declare Function NdiCmd_SetAngle Lib "NdiCmdC.dll" Alias "_NdiCmd_SetAngle@8" (ByVal handle As Long, ByVal vel As Long) As Long
Declare Function NdiCmd_GetAngle Lib "NdiCmdC.dll" Alias "_NdiCmd_GetAngle@8" (ByVal handle As Long, vel As Long) As Long
Declare Function NdiCmd_SetXAngle Lib "NdiCmdC.dll" Alias "_NdiCmd_SetXAngle@8" (ByVal handle As Long, ByVal vel As Long) As Long
Declare Function NdiCmd_GetXAngle Lib "NdiCmdC.dll" Alias "_NdiCmd_GetXAngle@8" (ByVal handle As Long, vel As Long) As Long
Declare Function NdiCmd_SetDist Lib "NdiCmdC.dll" Alias "_NdiCmd_SetDistance@8" (ByVal handle As Long, ByVal vel As Long) As Long
Declare Function NdiCmd_GetDist Lib "NdiCmdC.dll" Alias "_NdiCmd_GetDistance@8" (ByVal handle As Long, vel As Long) As Long
Declare Function NdiCmd_SetXDist Lib "NdiCmdC.dll" Alias "_NdiCmd_SetXDistance@8" (ByVal handle As Long, ByVal vel As Long) As Long
Declare Function NdiCmd_GetXDist Lib "NdiCmdC.dll" Alias "_NdiCmd_GetXDistance@8" (ByVal handle As Long, vel As Long) As Long
Declare Sub NdiCmd_CloseAllPorts Lib "NdiCmdC.dll" Alias "_NdiCmd_CloseAllPorts@0" ()
Public handle As Long ' handle to current WheelCommander
Public Function Init_WheelCommander() As Long
handle = -1
NdiCmd_Init ' init library
If NdiCmd_Search <> 1 Then ' try finding WheelCommander
NdiCmd_RunWizard 1 ' if it failed, run the wizard
If NdiCmd_Search = 1 Then ' try finding it again
handle = NdiCmd_GetWheelCommander ' if found, get a handle to the WheelCommander
End If
Else
handle = NdiCmd_GetWheelCommander ' we found it straight away, so get a handle to it
End If
Init_WheelCommander = handle
End FunctionModule1.bas
Attribute VB_Name = "Module1"
Public fMainForm As frmMain
Sub main()
Dim name As String
Dim version As Long
frmSplash.Show
frmSplash.Refresh
Init_WheelCommander ' find wc116 if possible; if not, launch wizard
Set fMainForm = New frmMain
Load fMainForm
Unload frmSplash
NdiCmd_Sync handle
If handle <> -1 Then
If NdiCmd_GetName(handle, name, version) = 1 Then
fMainForm.WCStatus.Text = "Connected: Ver " + Str(version) + " Type " + name
Else
fMainForm.WCStatus.Text = "Connected"
End If
Else
fMainForm.WCStatus.Text = "Not Connected"
End If
fMainForm.Show
End Sub
frmMain.frm
VERSION 5.00
Object = "{831FDD16-0C5C-11D2-A9FC-0000F8754DA1}#2.0#0"; "MSCOMCTL.OCX"
Begin VB.Form frmMain
Caption = "VBCommander"
ClientHeight = 5610
ClientLeft = 165
ClientTop = 735
ClientWidth = 6945
LinkTopic = "Form1"
ScaleHeight = 5610
ScaleWidth = 6945
StartUpPosition = 3 'Windows Default
Begin VB.VScrollBar VelScroll
Height = 375
LargeChange = 10
Left = 1440
Max = 80
TabIndex = 7
Top = 2040
Value = 30
Width = 375
End
Begin VB.TextBox WCStatus
Height = 375
Left = 480
TabIndex = 0
Text = "Not Connected"
Top = 960
Width = 3975
End
Begin VB.CommandButton Coast
Caption = "Coast"
Height = 285
Left = 3600
TabIndex = 4
Top = 2040
Width = 855
End
Begin VB.CommandButton Go
Caption = "Go!"
Height = 285
Left = 2640
TabIndex = 3
Top = 2040
Width = 735
End
Begin VB.TextBox Velocity
Height = 375
Left = 480
TabIndex = 1
Text = "30"
Top = 2040
Width = 975
End
Begin MSComctlLib.StatusBar sbStatusBar
Align = 2 'Align Bottom
Height = 405
Left = 0
TabIndex = 2
Top = 5205
Width = 6945
_ExtentX = 12250
_ExtentY = 714
_Version = 393216
BeginProperty Panels {8E3867A5-8586-11D1-B16A-00C0F0283628}
NumPanels = 1
BeginProperty Panel1 {8E3867AB-8586-11D1-B16A-00C0F0283628}
EndProperty
EndProperty
End
Begin VB.Label Label2
Caption = "Velocity:"
Height = 375
Left = 480
TabIndex = 6
Top = 1680
Width = 1095
End
Begin VB.Label Label1
Caption = "WheelCommander Status:"
Height = 375
Left = 480
TabIndex = 5
Top = 600
Width = 2295
End
Begin VB.Menu mnuFile
Caption = "&File"
Begin VB.Menu mnuFileNew
Caption = "&New"
Shortcut = ^N
End
Begin VB.Menu mnuFileOpen
Caption = "&Open..."
End
Begin VB.Menu mnuFileClose
Caption = "&Close"
End
Begin VB.Menu mnuFileBar0
Caption = "-"
End
Begin VB.Menu mnuFileSave
Caption = "&Save"
End
Begin VB.Menu mnuFileSaveAs
Caption = "Save &As..."
End
Begin VB.Menu mnuFileSaveAll
Caption = "Save A&ll"
End
Begin VB.Menu mnuFileBar1
Caption = "-"
End
Begin VB.Menu mnuFileProperties
Caption = "Propert&ies"
End
Begin VB.Menu mnuFileBar2
Caption = "-"
End
Begin VB.Menu mnuFilePageSetup
Caption = "Page Set&up..."
End
Begin VB.Menu mnuFilePrintPreview
Caption = "Print Pre&view"
End
Begin VB.Menu mnuFilePrint
Caption = "&Print..."
End
Begin VB.Menu mnuFileBar3
Caption = "-"
End
Begin VB.Menu mnuFileSend
Caption = "Sen&d..."
End
Begin VB.Menu mnuFileBar4
Caption = "-"
End
Begin VB.Menu mnuFileMRU
Caption = ""
Index = 1
Visible = 0 'False
End
Begin VB.Menu mnuFileMRU
Caption = ""
Index = 2
Visible = 0 'False
End
Begin VB.Menu mnuFileMRU
Caption = ""
Index = 3
Visible = 0 'False
End
Begin VB.Menu mnuFileBar5
Caption = "-"
Visible = 0 'False
End
Begin VB.Menu mnuFileExit
Caption = "E&xit"
End
End
Begin VB.Menu mnuEdit
Caption = "&Edit"
Begin VB.Menu mnuEditUndo
Caption = "&Undo"
End
Begin VB.Menu mnuEditBar0
Caption = "-"
End
Begin VB.Menu mnuEditCut
Caption = "Cu&t"
Shortcut = ^X
End
Begin VB.Menu mnuEditCopy
Caption = "&Copy"
Shortcut = ^C
End
Begin VB.Menu mnuEditPaste
Caption = "&Paste"
Shortcut = ^V
End
Begin VB.Menu mnuEditPasteSpecial
Caption = "Paste &Special..."
End
End
Begin VB.Menu mnuView
Caption = "&View"
Begin VB.Menu mnuViewToolbar
Caption = "&Toolbar"
Checked = -1 'True
End
Begin VB.Menu mnuViewStatusBar
Caption = "Status &Bar"
Checked = -1 'True
End
Begin VB.Menu mnuViewBar0
Caption = "-"
End
Begin VB.Menu mnuViewRefresh
Caption = "&Refresh"
End
Begin VB.Menu mnuViewOptions
Caption = "&Options..."
End
Begin VB.Menu mnuViewWebBrowser
Caption = "&Web Browser"
End
End
Begin VB.Menu mnuTools
Caption = "&Tools"
Begin VB.Menu mnuToolsOptions
Caption = "&Options..."
End
End
Begin VB.Menu mnuHelp
Caption = "&Help"
Begin VB.Menu mnuHelpContents
Caption = "&Contents"
End
Begin VB.Menu mnuHelpSearchForHelpOn
Caption = "&Search For Help On..."
End
Begin VB.Menu mnuHelpBar0
Caption = "-"
End
Begin VB.Menu mnuHelpAbout
Caption = "&About "
End
End
End
Attribute VB_Name = "frmMain"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Declare Function OSWinHelp% Lib "user32" Alias "WinHelpA" (ByVal hwnd&, ByVal HelpFile$, ByVal wCommand%, dwData As Any)
Private velTextBoxChanging As Boolean
Private Sub Coast_Click()
NdiCmd_Coast handle ' tell WheelCommander to stop moving
End Sub
Private Sub Go_Click()
NdiCmd_Go handle ' tell WheelCommander to start using current velocity setting
End Sub
Private Sub Velocity_Change()
Dim vel As Integer
vel = Velocity.Text
velTextBoxChanging = True
VelScroll.Value = vel
velTextBoxChanging = False
NdiCmd_SetVelocity handle, vel ' pass velocity value from form to WheelCommander
End Sub
Private Sub VelScroll_Change()
If velTextBoxChanging = False Then
Velocity.Text = VelScroll.Value
End If
End Sub
Private Sub Form_Load()
Me.Left = GetSetting(App.Title, "Settings", "MainLeft", 1000)
Me.Top = GetSetting(App.Title, "Settings", "MainTop", 1000)
Me.Width = GetSetting(App.Title, "Settings", "MainWidth", 6500)
Me.Height = GetSetting(App.Title, "Settings", "MainHeight", 6500)
End Sub
Private Sub Form_Unload(Cancel As Integer)
Dim i As Integer
'close all sub forms
For i = Forms.Count - 1 To 1 Step -1
Unload Forms(i)
Next
If Me.WindowState <> vbMinimized Then
SaveSetting App.Title, "Settings", "MainLeft", Me.Left
SaveSetting App.Title, "Settings", "MainTop", Me.Top
SaveSetting App.Title, "Settings", "MainWidth", Me.Width
SaveSetting App.Title, "Settings", "MainHeight", Me.Height
End If
End Sub
Private Sub mnuHelpAbout_Click()
frmAbout.Show vbModal, Me
End Sub
Private Sub mnuHelpSearchForHelpOn_Click()
Dim nRet As Integer
'if there is no helpfile for this project display a message to the user
'you can set the HelpFile for your application in the
'Project Properties dialog
If Len(App.HelpFile) = 0 Then
MsgBox "Unable to display Help Contents. There is no Help associated with this project.", vbInformation, Me.Caption
Else
On Error Resume Next
nRet = OSWinHelp(Me.hwnd, App.HelpFile, 261, 0)
If Err Then
MsgBox Err.Description
End If
End If
End Sub
Private Sub mnuHelpContents_Click()
Dim nRet As Integer
'if there is no helpfile for this project display a message to the user
'you can set the HelpFile for your application in the
'Project Properties dialog
If Len(App.HelpFile) = 0 Then
MsgBox "Unable to display Help Contents. There is no Help associated with this project.", vbInformation, Me.Caption
Else
On Error Resume Next
nRet = OSWinHelp(Me.hwnd, App.HelpFile, 3, 0)
If Err Then
MsgBox Err.Description
End If
End If
End Sub
Private Sub mnuToolsOptions_Click()
frmOptions.Show vbModal, Me
End Sub
Private Sub mnuViewWebBrowser_Click()
Dim frmB As New frmBrowser
frmB.StartingAddress = "http://www.nubotics.com"
frmB.Show
End Sub
Private Sub mnuViewOptions_Click()
frmOptions.Show vbModal, Me
End Sub
Private Sub mnuViewRefresh_Click()
'ToDo: Add 'mnuViewRefresh_Click' code.
MsgBox "Add 'mnuViewRefresh_Click' code."
End Sub
Private Sub mnuViewStatusBar_Click()
mnuViewStatusBar.Checked = Not mnuViewStatusBar.Checked
sbStatusBar.Visible = mnuViewStatusBar.Checked
End Sub
Private Sub mnuEditPasteSpecial_Click()
'ToDo: Add 'mnuEditPasteSpecial_Click' code.
MsgBox "Add 'mnuEditPasteSpecial_Click' code."
End Sub
Private Sub mnuEditPaste_Click()
'ToDo: Add 'mnuEditPaste_Click' code.
MsgBox "Add 'mnuEditPaste_Click' code."
End Sub
Private Sub mnuEditCopy_Click()
'ToDo: Add 'mnuEditCopy_Click' code.
MsgBox "Add 'mnuEditCopy_Click' code."
End Sub
Private Sub mnuEditCut_Click()
'ToDo: Add 'mnuEditCut_Click' code.
MsgBox "Add 'mnuEditCut_Click' code."
End Sub
Private Sub mnuEditUndo_Click()
'ToDo: Add 'mnuEditUndo_Click' code.
MsgBox "Add 'mnuEditUndo_Click' code."
End Sub
Private Sub mnuFileExit_Click()
'unload the form
Unload Me
End Sub
Private Sub mnuFileSend_Click()
'ToDo: Add 'mnuFileSend_Click' code.
MsgBox "Add 'mnuFileSend_Click' code."
End Sub
Private Sub mnuFilePrint_Click()
'ToDo: Add 'mnuFilePrint_Click' code.
MsgBox "Add 'mnuFilePrint_Click' code."
End Sub
Private Sub mnuFilePrintPreview_Click()
'ToDo: Add 'mnuFilePrintPreview_Click' code.
MsgBox "Add 'mnuFilePrintPreview_Click' code."
End Sub
Private Sub mnuFilePageSetup_Click()
On Error Resume Next
With dlgCommonDialog
.DialogTitle = "Page Setup"
.CancelError = True
.ShowPrinter
End With
End Sub
Private Sub mnuFileProperties_Click()
'ToDo: Add 'mnuFileProperties_Click' code.
MsgBox "Add 'mnuFileProperties_Click' code."
End Sub
Private Sub mnuFileSaveAll_Click()
'ToDo: Add 'mnuFileSaveAll_Click' code.
MsgBox "Add 'mnuFileSaveAll_Click' code."
End Sub
Private Sub mnuFileSaveAs_Click()
'ToDo: Add 'mnuFileSaveAs_Click' code.
MsgBox "Add 'mnuFileSaveAs_Click' code."
End Sub
Private Sub mnuFileSave_Click()
'ToDo: Add 'mnuFileSave_Click' code.
MsgBox "Add 'mnuFileSave_Click' code."
End Sub
Private Sub mnuFileClose_Click()
'ToDo: Add 'mnuFileClose_Click' code.
MsgBox "Add 'mnuFileClose_Click' code."
End Sub
Private Sub mnuFileOpen_Click()
Dim sFile As String
With dlgCommonDialog
.DialogTitle = "Open"
.CancelError = False
'ToDo: set the flags and attributes of the common dialog control
.Filter = "All Files (*.*)|*.*"
.ShowOpen
If Len(.FileName) = 0 Then
Exit Sub
End If
sFile = .FileName
End With
'ToDo: add code to process the opened file
End Sub
Private Sub mnuFileNew_Click()
'ToDo: Add 'mnuFileNew_Click' code.
MsgBox "Add 'mnuFileNew_Click' code."
End Sub
© 2004-2011 Noetic Design, Inc. All Rights Reserved. Nubotics and WheelWatcher are trademarks of Noetic Design, Inc.