AduHid Click-by-Click with Visual Basic
These instructions show how to build a minimal application using Visual Basic 6.0. This
page is a click-by-click introduction for people unfamiliar with Visual Basic.
Briefly the steps are:
- Create a new project
- Add buttons to the form
- Add fields to the form
- Define Open click behavior
- Define Write click behavior
- Define Read click behavior
- Define Close click behavior
- Declare global variables
- Declare AduHid.dll entry points
- Save the project
- Copy the AduHid.dll
- Run the code
Creating a New Project
Start Visual Basic
Left-click File on the menu bar.
Left-click New Project on the File drop-down list.
On the New Project window left-click Standard exe to select it.
Left-click the OK button.
Adding Buttons to the Form
Drag a button onto the form
Change the button (Name) to "Open"
Change the button Caption to "Open
Add 3 more buttons named and captioned Write, Read and Close
Add Fields to the Form
Add 2 TextBoxes to the form
Name the first one txtCommand
Name the second one txtResponse
Defining the Open_Click Behavior
Right-click the Open button on Form1
Left-click the View Code item on the popup menu
Left-click the expansion (down) arrow of the Object list on the code window
Select Open from the Object list
Type in the code to open the ADU Device (see listing)
aduHandle = OpenAduDevice(0)
Defining the Write_Click Behavior
Right-click the Write button on Form1
Left-click the View Code item on the popup menu
Left-click the expansion (down) arrow of the Object list on the code window
Select Write from the Object list
Type in the code that writes to the ADU Device (see listing)
Dim iRC As Long
iRC = WriteAduDevice(aduHandle, txtCommand.Text, _
Len(txtCommand.Text), 0, 0)
Defining the Read_Click Behavior
Right-click the Read button on Form1
Left-click the View Code item on the popup menu
Left-click the expansion (down) arrow of the Object list on the code window
Select Read from the Object list
Type in the code that read from the ADU Device (see listing)
Dim iRC As Long
Dim iBytesRead As Long
Dim sResponse As String * 8
iRC = ReadAduDevice(aduHandle, sResponse, 7, _
iBytesRead, 0)
Let txtResponse.Text = sResponse
Defining the Close_Click Behavior
Right-click the Close button on Form1
Left-click the View Code item on the popup menu
Left-click the expansion (down) arrow of the Object list on the code window
Select Close from the Object list
Type in the code that closes the ADU Device (see listing)
CloseAduDevice (aduHandle)
Declare Global Variables
Left-click to select (General) in the Object box of the code view window
Left-click to select (Declarations) in the Procedures box of the code view
window
Type in the declaration for the aduHandle
Dim aduHandle As Long
AduHid.dll Entry Points
Left-click Project on the menu bar
Left-click Add Module on the Project drop-down list
Left-click to select Module on the Add Module window
Left-click Open
Make sure that (General) and (Declarations) are selected in the Project 1
- Module 1 (Code) window
Type in the declarations for the AduHid.dll entry points (see listing)
Declare Function OpenAduDevice Lib "AduHid.DLL" _
(ByVal iTimeout As Long) As Long
Declare Function WriteAduDevice Lib "AduHid.DLL" _
(ByVal aduHandle As Long, ByVal lpBuffer As String, _
ByVal lNumberOfBytesToWrite As Long, _
ByRef lBytesWritten As Long, ByVal iTimeout As Long) As Long
Declare Function ReadAduDevice Lib "AduHid.DLL" _
(ByVal aduHandle As Long, ByVal lpBuffer As String, _
ByVal lNumberOfBytesToRead As Long, _
ByRef lBytesRead As Long, ByVal iTimeout As Long) As Long
Declare Function CloseAduDevice Lib "AduHid.DLL" _
(ByVal iHandle As Long) As Long
Save The Project
Use the File menu and Save drop-down button to save the project before
continuing.
Copy AduHid.dll
The Visual Basic program must be able to find the AduHid.dll file. Copy the AduHid.dll
file into a system directory such as:
- C:\WINNT
- C:\WINNT\SYSTEM
- C:\WINDOWS
- C:\WINDOWS\SYSTEM
Run The Program
Left-click Run on the menu bar
Left-click Start on the drop-down list
Left-click Open to open a connection to the ADU device
In the txtCommand field type in a command to send to the ADU device
Left-click Write to send the command to the ADU device
Left-click Read to read the response from the ADU device
Left-click Close to close the connection to the ADU device
If a response is requested when none is forth-coming from the ADU device then the
ReadAduDevice function waits forever for the response. This sample program is so
simple-minded that it locks up in this case. To avoid this see the advanced programming
documentation for information on Timeouts.
Source Code Listings
Visual Basic is normally not read as ASCII source code. However the following listings may
assist programmers. Lines can be copied from the browser window and pasted into the Visual
Basic program.
Form1.frm
VERSION 5.00
Begin VB.Form Form1
Caption = "Form1"
ClientHeight = 3135
ClientLeft = 90
ClientTop = 375
ClientWidth = 4620
LinkTopic = "Form1"
ScaleHeight = 3135
ScaleWidth = 4620
StartUpPosition = 3 'Windows Default
Begin VB.TextBox txtResponse
Height = 495
Left = 240
TabIndex = 5
Text = "Text2"
Top = 1440
Width = 2655
End
Begin VB.TextBox txtCommand
Height = 495
Left = 240
TabIndex = 4
Text = "Text1"
Top = 840
Width = 2655
End
Begin VB.CommandButton Close
Caption = "Close"
Height = 495
Left = 3240
TabIndex = 3
Top = 2040
Width = 1215
End
Begin VB.CommandButton Read
Caption = "Read"
Height = 495
Left = 3240
TabIndex = 2
Top = 1440
Width = 1215
End
Begin VB.CommandButton Write
Caption = "Write"
Height = 495
Left = 3240
TabIndex = 1
Top = 840
Width = 1215
End
Begin VB.CommandButton Open
Caption = "Open"
Height = 495
Left = 3240
TabIndex = 0
Top = 240
Width = 1215
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Dim aduHandle As Long
Private Sub Close_Click()
CloseAduDevice (aduHandle)
End Sub
Private Sub Open_Click()
aduHandle = OpenAduDevice(0)
End Sub
Private Sub Read_Click()
Dim iRC As Long
Dim iBytesRead As Long
Dim sResponse As String * 8
iRC = ReadAduDevice(aduHandle, sResponse, 7, _
iBytesRead, 0)
Let txtResponse.Text = sResponse
End Sub
Private Sub Write_Click()
Dim iRC As Long
iRC = WriteAduDevice(aduHandle, txtCommand.Text, _
Len(txtCommand.Text), 0, 0)
End Sub
Module1.bas
Attribute VB_Name = "Module1"
Declare Function OpenAduDevice Lib "AduHid.DLL" _
(ByVal iTimeout As Long) As Long
Declare Function WriteAduDevice Lib "AduHid.DLL" _
(ByVal aduHandle As Long, ByVal lpBuffer As String, _
ByVal lNumberOfBytesToWrite As Long, _
ByRef lBytesWritten As Long, ByVal iTimeout As Long) As Long
Declare Function ReadAduDevice Lib "AduHid.DLL" _
(ByVal aduHandle As Long, ByVal lpBuffer As String, _
ByVal lNumberOfBytesToRead As Long, _
ByRef lBytesRead As Long, ByVal iTimeout As Long) As Long
Declare Function CloseAduDevice Lib "AduHid.DLL" _
(ByVal iHandle As Long) As Long
|