Online Development Notebook > Index > Programming Overview > General VB Raw Example

General VB Example


This example uses VB to initialize the U4x1. The very minimum application would be to open the USB device and transmit a single command. This example shows the code that it takes to open the U4x1 device and initialize the ports. The VBLIB files need to be included as part of this project.

The communication with the U4xx is done by transferring a group of eight bytes via the WriteUSBdevice call of the U4x1 VB library. Eight bytes are returned from the device and read via the ReadUSBdevice U4x1 VB call. These bytes are located in two arrays that are defined below.

Option Explicit
Dim OutBuffer(10) As Byte
Dim InBuffer(10) As Byte

When the form for this project is loaded, a call to OpenUSBdevice with the appropriate parameters returns true if the device has been detected.

The OpenUSBdevice parameters are used to find the device. A single parameter (in this example the string "U401") will open the first device located that matches that string. Multiple parameters allow devices to be opened via manufacturer name, VID, etc.

When a single parameter is used, the other parameters should be either 0, or a null string, as in the example.

Multiple parameters are used when device selection refinement is necessary, such as when multiple U4x1 devices are used on a single machine. In that case, the serial number string should be used to refine the selection.

' Form load
' When the form is loaded at startup, find the hardware.
' Indicate status in "DeviceStatus" box.

Private Sub Form_Load()

    If OpenUSBdevice("U401", "", 0, 0, 0, "") Then
        DeviceStatus.Caption = "USB Device Found"
    Else
        DeviceStatus.Caption = "USB Device Not Found"
    End If

End Sub

Calling the function WriteReadUSB moves the eight command bytes of the array "OutBuffer" to the device and fills "InBuffer" with the 8 bytes sent from the U4xx.

' USB Transfer

Public Sub WriteReadUSB()
   
    Call WriteUSBdevice(AddressFor(OutBuffer(0)), 8)
    DoEvents
    Call ReadUSBdevice(AddressFor(InBuffer(0)), 8)

End Sub

The single button on the form of this application example transmits the InitPorts Command string of 00-00-00-00-00-00-00-00 to the U4xx.

' Button: Cmd

' Send a command to the device

Private Sub Cmd_Click()

    OutBuffer(0) = &H0
    OutBuffer(1) = &H0
    OutBuffer(2) = &H0
    OutBuffer(3) = &H0
    OutBuffer(4) = &H0
    OutBuffer(5) = &H0
    OutBuffer(6) = &H0
    OutBuffer(7) = &H0
   
    Call WriteReadUSB
       
End Sub


Hardware:  U401 USB Interface    U421 USB Interface   U451 USB Interface

Programming:  USBm DLL Programming    Download Files

Application Notes:  U4x1 Application Notes    Misc Applications and Information    FAQ

While every effort has been made to make sure that the information posted on this site is correct, the author can not be held liable for any damages whatsoever for losses as a result of the application of this information. Use this information at your own risk.

USBmicro can design your custom and semi-custom USB product. Email about USB design can be directed to " Robert " at usbmicro.com.

Copyright © USBmicro, L.L.C., 2002-2010

Previous  |  Next ]     [ Up  |  First  |  Last ]     (Article 28 of 126)

This page is created with TreePad